diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..82a5721d31b6363579299fa20441591876e4fe2a --- /dev/null +++ b/pom.xml @@ -0,0 +1,154 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.tarento</groupId> + <artifactId>analytics</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <name>Analytic Service</name> + <description>Project for handling the data for analytics visualization</description> + <url>http://maven.apache.org</url> + <parent> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-parent</artifactId> + <version>1.5.3.RELEASE</version> + <relativePath></relativePath> + </parent> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <java.version>1.8</java.version> + <cxf.version>3.0.1</cxf.version> + <log4j2.version>2.16.0</log4j2.version> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.kafka</groupId> + <artifactId>spring-kafka</artifactId> + <version>1.1.2.RELEASE</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-jdbc</artifactId> + <version>2.0.0.RELEASE</version> + </dependency> + <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.0</version> + </dependency> + <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jersey --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-jersey</artifactId> + <version>1.2.0.RELEASE</version> + </dependency> + + <dependency> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-xml</artifactId> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + </dependency> + <dependency> + <groupId>org.apache.poi</groupId> + <artifactId>poi-ooxml</artifactId> + <version>3.10-FINAL</version> + </dependency> + + <dependency> + <groupId>javax.ws.rs</groupId> + <artifactId>jsr311-api</artifactId> + <version>1.1.1</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-frontend-jaxws</artifactId> + <version>${cxf.version}</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-transports-http</artifactId> + <version>${cxf.version}</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-frontend-jaxrs</artifactId> + <version>${cxf.version}</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-tools-java2ws</artifactId> + <version>${cxf.version}</version> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.4</version> + </dependency> + <dependency> + <groupId>org.elasticsearch.client</groupId> + <artifactId>elasticsearch-rest-high-level-client</artifactId> + <version>6.2.4</version> + </dependency> + <dependency> + <groupId>org.elasticsearch</groupId> + <artifactId>elasticsearch</artifactId> + <version>6.2.4</version> + </dependency> + <dependency> + <groupId>com.github.fge</groupId> + <artifactId>json-schema-validator</artifactId> + <version>2.2.6</version> + </dependency> + <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.6</version> + </dependency> + <!-- https://mvnrepository.com/artifact/org.json/json --> + <dependency> + <groupId>org.json</groupId> + <artifactId>json</artifactId> + <version>20180813</version> + </dependency> + <dependency> + <groupId>com.googlecode.json-simple</groupId> + <artifactId>json-simple</artifactId> + <version>1.1</version> + </dependency> + + <dependency> + <groupId>com.amazonaws</groupId> + <artifactId>aws-java-sdk</artifactId> + <version>1.11.550</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin><!-- Include if you want to make an executable jar[FAT JAR which + includes all dependencies along with sprinboot loader] that you can run on + commandline using java -jar NAME --> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> diff --git a/src/main/java/com/tarento/analytics/AnalyticApp.java b/src/main/java/com/tarento/analytics/AnalyticApp.java new file mode 100644 index 0000000000000000000000000000000000000000..883def0f5ab8eca7bb6fc86036cc47ab86d213bc --- /dev/null +++ b/src/main/java/com/tarento/analytics/AnalyticApp.java @@ -0,0 +1,35 @@ +package com.tarento.analytics; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +import com.tarento.analytics.constant.Constants; + + +@SpringBootApplication +public class AnalyticApp { + public static void main( String[] args ) { + SpringApplication.run(AnalyticApp.class, args); + } + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurerAdapter() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**").allowedMethods(Constants.GET, Constants.POST,Constants.PUT, Constants.DELETE, Constants.OPTIONS).allowedOrigins("*") + .allowedHeaders("*"); + } + }; + } +} diff --git a/src/main/java/com/tarento/analytics/ConfigurationLoader.java b/src/main/java/com/tarento/analytics/ConfigurationLoader.java new file mode 100644 index 0000000000000000000000000000000000000000..caf06df022922b2d558bb76e60528e0a263bcec9 --- /dev/null +++ b/src/main/java/com/tarento/analytics/ConfigurationLoader.java @@ -0,0 +1,100 @@ +package com.tarento.analytics; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.PostConstruct; + +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.support.ResourcePatternUtils; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +@Component("configurationLoader") +public class ConfigurationLoader { + + private static Logger logger = LoggerFactory.getLogger(ConfigurationLoader.class); + private Map<String, ObjectNode> nameContentMap = new HashMap<>(); + @Autowired + private ResourceLoader resourceLoader; + @Autowired + private ObjectMapper objectMapper; + + private static final String RESOURCE_LOCATION = "classpath*:schema/*.json"; + + public static final String ROLE_DASHBOARD_CONFIG = "RoleDashboardMappingsConf.json"; + public static final String MASTER_DASHBOARD_CONFIG = "MasterDashboardConfig.json"; + + /** + * Loads config resources + * + * @throws IOException + * + * @throws Exception + */ + @PostConstruct + public void loadResources() throws IOException { + Resource[] resources = getResources(RESOURCE_LOCATION); + + for (Resource resource : resources) { + String jsonContent = getContent(resource); + ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(jsonContent); + nameContentMap.put(resource.getFilename(), jsonNode); + } + logger.info("Number of resources loaded {} ", nameContentMap.size()); + + } + + /** + * Obtains a ObjectNode w.r.t given resource/file name in classpath*:schema + * + * @param name + * @return + */ + public ObjectNode getConfigForProfile(String profileName, String name) { + return nameContentMap.get(profileName + "_" + name); + } + + /** + * Loads all the resources/files with a given pattern *.json + * + * @param pattern + * path with *json + * @return + * @throws IOException + */ + private Resource[] getResources(String pattern) throws IOException { + return ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern); + } + + /** + * Returns a content of resource + * + * @param resource + * @return + */ + private String getContent(Resource resource) { + String content = null; + try { + InputStream is = resource.getInputStream(); + byte[] encoded = IOUtils.toByteArray(is); + content = new String(encoded, StandardCharsets.UTF_8); + + } catch (IOException e) { + logger.error("Cannot load resource {} ", resource.getFilename()); + + } + return content; + } + +} diff --git a/src/main/java/com/tarento/analytics/constant/Constants.java b/src/main/java/com/tarento/analytics/constant/Constants.java new file mode 100644 index 0000000000000000000000000000000000000000..917d63a423bb801df09a6c29e5ce14ffba780037 --- /dev/null +++ b/src/main/java/com/tarento/analytics/constant/Constants.java @@ -0,0 +1,175 @@ +package com.tarento.analytics.constant; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Constants { + + public static final long ACCESS_TOKEN_VALIDITY_SECONDS = 30l * 24l * 60l * 60l; + public static final String SIGNING_KEY = "devglan123r"; + public static final String JWT_ISSUER = "http://devglan.com"; + public static final String JWT_GRANTED_AUTHORITY = "ROLE_ADMIN"; + + /** + * Allowed Origins for CORS Bean + */ + public static final String GET = "GET"; + public static final String POST = "POST"; + public static final String PUT = "PUT"; + public static final String DELETE = "DELETE"; + public static final String OPTIONS = "OPTIONS"; + + public static final int UNAUTHORIZED_ID = 401; + public static final int SUCCESS_ID = 200; + public static final int FAILURE_ID = 320; + public static final String UNAUTHORIZED = "Invalid credentials. Please try again."; + public static final String PROCESS_FAIL = "Process failed, Please try again."; + public static final String SUCCESS = "success"; + + public static final String PRIMARY = "PRIMARY"; + public static final String SECONDARY = "SECONDARY"; + public static final String TERNARY = "TERNARY"; + public static final String QUADNARY = "QUADNARY"; + + // chart format + + public static final String D3 = "d3"; + public static final String CHARTJS = "chartjs"; + + // chart type + public static final String BAR = "bar"; + public static final String PIE = "pie"; + public static final String STACKEDBAR = "stackedbar"; + public static final String LINE = "line"; + public static final String HORIZONTAL_BAR = "horizontalBar"; + public static final String DOUGHNUT = "doughnut"; + public static final String HEAT = "heat"; + public static final String RADAR = "radar"; + + public static final Long FEEDBACK_MESSAGE_TIMEOUT = 2000l; + + public static final String STORE_ID = "storeId"; + + public static final String PLACEMENTS_DASHBOARD = "DASHBOARD"; + public static final String PLACEMENTS_HOME = "HOME"; + + protected static final List<Long> RATING_LIST = new ArrayList<>(Arrays.asList(1l, 2l, 3l, 4l, 5l)); + protected static final List<String> RATING_LIST_STRING = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5")); + protected static final List<String> RATING_LIST_STRING_STAR = new ArrayList<>( + Arrays.asList("1 Star", "2 Star", "3 Star", "4 Star", "5 Star")); + + public final class Modules { + private Modules() { + } + + public static final String HOME_REVENUE = "HOME_REVENUE"; + public static final String HOME_SERVICES = "HOME_SERVICES"; + public static final String COMMON = "COMMON"; + public static final String PT = "PT"; + public static final String TL = "TL"; + } + + public final class KafkaTopics { + private KafkaTopics() { + } + + public static final String NEW_CONTENT_MESSAGE = "SaveContent"; + public static final String SIMULATOR_TRANSACTION = "SaveTransaction"; + } + + public final class ConfigurationFiles { + private ConfigurationFiles() { + } + + public static final String CHART_API_CONFIG = "ChartApiConfig.json"; + public static final String REPORT_API_CONFIG = "ReportApiConfig.json"; + } + + public final class JsonPaths { + private JsonPaths() { + } + + public static final String CHART_TYPE = "chartType"; + public static final String QUERIES = "queries"; + public static final String AGGREGATION_QUERY = "aggrQuery"; + public static final String INDEX_NAME = "indexName"; + public static final String REQUEST_QUERY_MAP = "requestQueryMap"; + public static final String DATE_REF_FIELD = "dateRefField"; + public static final String AGGS = "aggs"; + public static final String AGGREGATIONS = "aggregations"; + public static final String MODULE = "module"; + public static final String INTERVAL_VAL = "intervalvalue"; + public static final String INTERVAL = "interval"; + public static final String IS_MDMS_ENABALED = "isMdmsEnabled"; + public static final String INSIGHT = "insight"; + public static final String ES_INSTANCE = "esInstance"; + public static final String ALWAYS_VIEW = "alwaysView"; + + } + + public final class Filters { + private Filters() { + } + + public static final String MODULE = "module"; + public static final String FILTER_ALL = "*"; + } + + public final class Catagory { + private Catagory() { + } + + public static final String SEVICE = "service"; + public static final String REVENUE = "revenue"; + } + + public final class DashBoardConfig { + private DashBoardConfig() { + } + + public static final String ROLES = "roles"; + public static final String ROLE_ID = "roleId"; + public static final String DASHBOARDS = "dashboards"; + public static final String VISUALISATIONS = "visualizations"; + public static final String NAME = "name"; + public static final String ID = "id"; + public static final String TITLE = "title"; + public static final String WIDGET_CHARTS = "widgetCharts"; + public static final String FILTERS = "filters"; + public static final String VISIBILITY = "visibility"; + public static final String KEY = "key"; + public static final String VALUE = "value"; + public static final String VALUES = "values"; + } + + public final class MDMSKeys { + private MDMSKeys() { + } + + public static final String CODE = "code"; + public static final String DISTRICT_CODE = "districtCode"; + public static final String DDR_NAME = "ddrName"; + public static final String TENANTS = "tenants"; + public static final String KEY = "key"; + } + + public enum Interval { + week, month, year, day, minute, date + } + + public final class PostAggregationTheories { + private PostAggregationTheories() { + } + + public static final String RESPONSE_DIFF_DATES = "repsonseToDifferenceOfDates"; + } + + public enum ClienServiceType { + DEFAULT_CLIENT, MDMS_CLIENT + } + + public final SimpleDateFormat dashboardDateFormat = new SimpleDateFormat("MMM dd, yyyy"); + +} diff --git a/src/main/java/com/tarento/analytics/constant/ElasticSearchConstants.java b/src/main/java/com/tarento/analytics/constant/ElasticSearchConstants.java new file mode 100644 index 0000000000000000000000000000000000000000..b4cf140ceea245794cfdae6881c3d4eb2cc476af --- /dev/null +++ b/src/main/java/com/tarento/analytics/constant/ElasticSearchConstants.java @@ -0,0 +1,25 @@ +package com.tarento.analytics.constant; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ElasticSearchConstants { + + private ElasticSearchConstants() { + } + + public static final String LTE = "<="; + public static final String LT = "<"; + public static final String GTE = ">="; + public static final String GT = ">"; + public static final String ASC_ORDER = "ASC"; + public static final String STARTS_WITH = "startsWith"; + public static final String ENDS_WITH = "endsWith"; + protected static final List<String> upsertResults = new ArrayList<>(Arrays.asList("CREATED", "UPDATED", "NOOP")); + public static final String SOFT_MODE = "soft"; + public static final String RAW_APPEND = ".raw"; + public static final String DAY_OF_WEEK = "dayOfWeek"; + public static final String DAY = "day"; + public static final String HOUR = "hour"; +} diff --git a/src/main/java/com/tarento/analytics/constant/ErrorCode.java b/src/main/java/com/tarento/analytics/constant/ErrorCode.java new file mode 100644 index 0000000000000000000000000000000000000000..01db6093c77d23bc3d30f24feb9d0e7420d46091 --- /dev/null +++ b/src/main/java/com/tarento/analytics/constant/ErrorCode.java @@ -0,0 +1,10 @@ +package com.tarento.analytics.constant; + +public class ErrorCode { + + private ErrorCode() { + } + + public static final String ERR320 = "320"; + +} diff --git a/src/main/java/com/tarento/analytics/controllers/DashboardController.java b/src/main/java/com/tarento/analytics/controllers/DashboardController.java new file mode 100644 index 0000000000000000000000000000000000000000..6c19f2beb1393ec46f47871b294665a350921550 --- /dev/null +++ b/src/main/java/com/tarento/analytics/controllers/DashboardController.java @@ -0,0 +1,258 @@ +package com.tarento.analytics.controllers; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.request.ServletWebRequest; +import org.springframework.web.multipart.MultipartFile; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.constant.ErrorCode; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.RequestDto; +import com.tarento.analytics.dto.RoleDto; +import com.tarento.analytics.dto.UserDto; +import com.tarento.analytics.exception.AINException; +import com.tarento.analytics.org.service.ClientServiceFactory; +import com.tarento.analytics.producer.AnalyticsProducer; +import com.tarento.analytics.service.AmazonS3ClientService; +import com.tarento.analytics.service.MetadataService; +import com.tarento.analytics.utils.PathRoutes; +import com.tarento.analytics.utils.ResponseGenerator; + +@RestController +@RequestMapping(PathRoutes.DashboardApi.DASHBOARD_ROOT_PATH) +public class DashboardController { + + public static final Logger logger = LoggerFactory.getLogger(DashboardController.class); + + @Autowired + private MetadataService metadataService; + + @Autowired + private ClientServiceFactory clientServiceFactory; + + @Autowired + private AmazonS3ClientService amazonS3ClientService; + + @Autowired + private AnalyticsProducer analyticsProducer; + + private static final String MESSAGE = "message"; + + @GetMapping(value = PathRoutes.DashboardApi.TEST_PATH, produces = MediaType.APPLICATION_JSON_VALUE) + public String getTest() throws JsonProcessingException { + return ResponseGenerator.successResponse("success"); + + } + + @PostMapping(value = "/files") + public Map<String, String> uploadFile(@RequestPart(value = "file") MultipartFile file) { + Map<String, String> response = new HashMap<>(); + try { + String imgUrl = this.amazonS3ClientService.uploadFileToS3Bucket(file, true); + response.put(MESSAGE, + "file [" + file.getOriginalFilename() + "] uploading request submitted successfully."); + response.put("url", imgUrl); + } catch (Exception e) { + logger.error("S3 file upload : {} ", e.getMessage()); + response.put(MESSAGE, e.getMessage()); + response.put("url", ""); + } + + return response; + } + + @DeleteMapping("/files") + public Map<String, String> deleteFile(@RequestParam("file_name") String fileName) { + Map<String, String> response = new HashMap<>(); + try { + this.amazonS3ClientService.deleteFileFromS3Bucket(fileName); + response.put(MESSAGE, "file [" + fileName + "] removing request submitted successfully."); + } catch (Exception e) { + logger.error("S3 file upload : {} ", e.getMessage()); + response.put(MESSAGE, e.getMessage()); + + } + return response; + + } + + @GetMapping(value = PathRoutes.DashboardApi.GET_DASHBOARD_CONFIG + "/{profileName}" + "/{dashboardId}") + public String getDashboardConfiguration(@PathVariable String profileName, @PathVariable String dashboardId, + @RequestParam(value = "catagory", required = false) String catagory, + @RequestHeader(value = "x-user-info", required = false) String xUserInfo) throws AINException, IOException { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + UserDto user = gson.fromJson(xUserInfo, UserDto.class); + /* + * user = new UserDto(); RoleDto role = new RoleDto(); role.setId(2068l); + * List<RoleDto> roles = new ArrayList<>(); roles.add(role); + * user.setRoles(roles); + */ + logger.info("user {} ", xUserInfo); + return ResponseGenerator.successResponse( + metadataService.getDashboardConfiguration(profileName, dashboardId, catagory, user.getRoles())); + } + + @GetMapping(value = PathRoutes.DashboardApi.GET_DASHBOARDS_FOR_PROFILE + "/{profileName}") + public String getDashboardsForProfile(@PathVariable String profileName, + @RequestHeader(value = "x-user-info", required = false) String xUserInfo) throws AINException, IOException { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + UserDto user = gson.fromJson(xUserInfo, UserDto.class); + return ResponseGenerator.successResponse(metadataService.getDashboardsForProfile(profileName, user.getRoles())); + } + + @PostMapping(value = PathRoutes.DashboardApi.GET_CHART_V2 + "/{profileName}") + public String getVisualizationChartV2(@PathVariable String profileName, @RequestBody RequestDto requestDto, + @RequestHeader(value = "x-user-info", required = false) String xUserInfo, + @RequestHeader(value = "Authorization", required = false) String authorization, ServletWebRequest request) + throws IOException { + Long requestTime = new Date().getTime(); + logger.info("Request Detail: {} ", requestDto); +// Gson gson = new GsonBuilder().setPrettyPrinting().create(); +// UserDto user = gson.fromJson(xUserInfo, UserDto.class); + + Gson gson = new GsonBuilder().setPrettyPrinting().create(); UserDto user = + new UserDto(); RoleDto role = new RoleDto(); role.setId(2068l); List<RoleDto> + roles = new ArrayList<>(); roles.add(role); user.setRoles(roles); + logger.info("user"+xUserInfo); + + + // Getting the request information only from the Full Request + AggregateRequestDto requestInfo = requestDto.getAggregationRequestDto(); + Map<String, Object> headers = requestDto.getHeaders(); + // requestInfo.getFilters().putAll(headers); + String response = ""; + try { + if (headers.isEmpty()) { + logger.error("Please provide header details"); + throw new AINException(ErrorCode.ERR320, "header is missing"); + } + if (headers.get("tenantId") == null) { + logger.error("Please provide tenant ID details"); + throw new AINException(ErrorCode.ERR320, "tenant is missing"); + + } + if (requestDto.getAggregationRequestDto() == null) { + logger.error("Please provide requested Visualization Details"); + throw new AINException(ErrorCode.ERR320, "Visualization Request is missing"); + } + /* + * if(requestDto.getAggregationRequestDto().getRequestId() == null) { + * logger.error("Please provide Request ID"); throw new + * AINException(ErrorCode.ERR320, + * "Request ID is missing. Insights will not work"); } + */ + + // To be removed once the development is complete + if (StringUtils.isBlank(requestInfo.getModuleLevel())) { + requestInfo.setModuleLevel(Constants.Modules.HOME_REVENUE); + } + Object responseData = clientServiceFactory.get(profileName, requestInfo.getVisualizationCode()) + .getAggregatedData(profileName, requestInfo, user.getRoles()); + // clientService.getAggregatedData(requestInfo, user.getRoles()); + response = ResponseGenerator.successResponse(responseData); + Long responseTime = new Date().getTime(); + pushRequestsToLoggers(requestDto, user, requestTime, responseTime); + } catch (AINException e) { + logger.error("error while executing api getVisualizationChart"); + response = ResponseGenerator.failureResponse(e.getErrorCode(), e.getMessage()); + } catch (Exception e) { + logger.error("error while executing api getVisualizationChart {} ", e.getMessage()); + // could be bad request or internal server error + // response = + // ResponseGenerator.failureResponse(HttpStatus.BAD_REQUEST.toString(),"Bad + // request"); + } + return response; + } + + @PostMapping(value = PathRoutes.DashboardApi.GET_REPORT) + public String getReport(@PathVariable String profileName, @RequestBody RequestDto requestDto, + @RequestHeader(value = "x-user-info", required = false) String xUserInfo, + @RequestHeader(value = "Authorization", required = false) String authorization, ServletWebRequest request) + throws IOException { + + /* + * logger.info("Request Detail:" + requestDto); Gson gson = new + * GsonBuilder().setPrettyPrinting().create(); UserDto user = + * gson.fromJson(xUserInfo, UserDto.class); + */ + UserDto user = new UserDto(); + logger.info("user {} ", xUserInfo); + + // Getting the request information only from the Full Request + AggregateRequestDto requestInfo = requestDto.getAggregationRequestDto(); + Map<String, Object> headers = requestDto.getHeaders(); + // requestInfo.getFilters().putAll(headers); + String response = ""; + try { + if (headers.isEmpty()) { + logger.error("Please provide header details"); + throw new AINException(ErrorCode.ERR320, "header is missing"); + } + if (headers.get("tenantId") == null) { + logger.error("Please provide tenant ID details"); + throw new AINException(ErrorCode.ERR320, "tenant is missing"); + + } + if (requestDto.getAggregationRequestDto() == null) { + logger.error("Please provide requested Visualization Details"); + throw new AINException(ErrorCode.ERR320, "Visualization Request is missing"); + } + + // To be removed once the development is complete + if (StringUtils.isBlank(requestInfo.getModuleLevel())) { + requestInfo.setModuleLevel(Constants.Modules.HOME_REVENUE); + } + Object responseData = clientServiceFactory.get(profileName, requestInfo.getVisualizationCode()) + .getAggregatedData(profileName, requestInfo, user.getRoles()); + // clientService.getAggregatedData(requestInfo, user.getRoles()); + response = ResponseGenerator.successResponse(responseData); + } catch (AINException e) { + logger.error("error while executing api getVisualizationChart"); + response = ResponseGenerator.failureResponse(e.getErrorCode(), e.getErrorMessage()); + } catch (Exception e) { + logger.error("error while executing api getVisualizationChart {} ", e.getMessage()); + // could be bad request or internal server error + // response = + // ResponseGenerator.failureResponse(HttpStatus.BAD_REQUEST.toString(),"Bad + // request"); + } + return response; + } + + private void pushRequestsToLoggers(RequestDto requestDto, UserDto user, Long requestTime, Long responseTime) { + new Thread(() -> { + Map<String, Object> requestMap = new HashMap<>(); + requestMap.put("request", requestDto); + requestMap.put("user", user); + requestMap.put("timeOfRequest", requestTime); + requestMap.put("timeOfResponse", responseTime); + analyticsProducer.pushToPipeline(requestMap, "userrequestdata", "userrequestdata"); + }).start(); + } + +} diff --git a/src/main/java/com/tarento/analytics/dao/ElasticSearchDao.java b/src/main/java/com/tarento/analytics/dao/ElasticSearchDao.java new file mode 100644 index 0000000000000000000000000000000000000000..6f9f06349af5acad9076b81968a032e1d1fadb24 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dao/ElasticSearchDao.java @@ -0,0 +1,44 @@ +package com.tarento.analytics.dao; + +import java.util.List; +import java.util.Map; + +import org.elasticsearch.action.search.MultiSearchResponse; +import org.elasticsearch.action.search.SearchRequest; + +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.SearchDto; +import com.tarento.analytics.model.ElasticSearchDictator; + +public interface ElasticSearchDao { + + public Map<String, Object> getDataByIdentifier(String index, String type, String identifier); + + public Map<String, Object> searchData(String index, String type, Map<String, Object> searchData); + + public Map<String, Object> complexSearch(SearchDto searchDTO, String index, String... type); + + public boolean healthCheck(); + + /* + * public ElasticSearchDictator createSearchDictator(AggregateRequestDto dto, + * String tenant) throws Exception ; + * + * public ElasticSearchDictator createSearchDictator(AggregateRequestDto dto, + * String indexName, String documentType, String filterDateField) throws + * Exception ; + */ + public ElasticSearchDictator createSearchDictatorV2(AggregateRequestDto dto, String indexName, String documentType, + String filterDateField); + + public SearchRequest buildElasticSearchQuery(ElasticSearchDictator dictator); + + public MultiSearchResponse executeMultiSearchRequest(List<SearchRequest> searchRequestList, String tenant); + + // public ElasticSearchDictator createSearchDictator(String indexName, String + // documentType, CummulativeDataRequestDto dto, String tenant) throws Exception + // ; + + MultiSearchResponse executeMultiSearchRequest(List<SearchRequest> searchRequestList, Boolean primaryOrNot); + +} diff --git a/src/main/java/com/tarento/analytics/dao/impl/ElasticSearchDaoImpl.java b/src/main/java/com/tarento/analytics/dao/impl/ElasticSearchDaoImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..031981965dd6fd60ed3d1d198fef803351eb50cd --- /dev/null +++ b/src/main/java/com/tarento/analytics/dao/impl/ElasticSearchDaoImpl.java @@ -0,0 +1,1228 @@ +package com.tarento.analytics.dao.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHost; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.search.MultiSearchRequest; +import org.elasticsearch.action.search.MultiSearchResponse; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.ExistsQueryBuilder; +import org.elasticsearch.index.query.MatchQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.RangeQueryBuilder; +import org.elasticsearch.index.query.TermQueryBuilder; +import org.elasticsearch.index.query.TermsQueryBuilder; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.BucketOrder; +import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; +import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds; +import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; +import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder; +import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder; +import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder; +import org.elasticsearch.search.aggregations.pipeline.cumulativesum.CumulativeSumPipelineAggregationBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.search.sort.SortBuilders; +import org.elasticsearch.search.sort.SortOrder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.tarento.analytics.constant.ElasticSearchConstants; +import com.tarento.analytics.dao.ElasticSearchDao; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.SearchDto; +import com.tarento.analytics.model.ElasticSearchDictator; +import com.tarento.analytics.utils.ElasticProperties; +import com.tarento.analytics.utils.ElasticSearchClient; + +@Component +public class ElasticSearchDaoImpl implements ElasticSearchDao { + + @Autowired + private ElasticSearchClient elasticSearchClient; + + @Autowired + public ObjectMapper mapper; + + private static String elasticHost; + private static int elasticPort; + private static final String REST_SCHEME = "http"; + private static final String REST_SCHEME2 = "https"; + private static RestHighLevelClient client; + private static RestHighLevelClient alternateClient; + private String indexName; + private String docType; + + public String getIndexName() { + return indexName; + } + + public void setIndexName(String indexName) { + this.indexName = indexName; + } + + public String getDocType() { + return docType; + } + + public void setDocType(String docType) { + this.docType = docType; + } + + public static final Logger logger = LoggerFactory.getLogger(ElasticSearchDaoImpl.class); + + public ElasticSearchDaoImpl(@Value("${services.esindexer.primary.host.name}") String elasticsearchHost, + @Value("${services.esindexer.primary.host.port}") int elasticsearchPort) { + elasticHost = elasticsearchHost; + elasticPort = elasticsearchPort; + client = getClientForElastic(); + + } + + @Override + public Map<String, Object> getDataByIdentifier(String index, String type, String identifier) { + RestHighLevelClient restHighLevelClient = elasticSearchClient.getClient(); + long startTime = System.currentTimeMillis(); + logger.info("ElasticSearchUtil getDataByIdentifier method started at == {} for Type {} ", startTime, type); + GetResponse response = null; + + try { + if (StringUtils.isBlank(index) || StringUtils.isBlank(identifier)) { + logger.error("Invalid request is coming."); + return new HashMap<>(); + } else if (StringUtils.isBlank(type)) { + response = restHighLevelClient.get(new GetRequest(index).type(type)); + + } else { + response = restHighLevelClient.get(new GetRequest(index, type, identifier)); + } + if (response == null || null == response.getSource()) { + return new HashMap<>(); + } + long stopTime = System.currentTimeMillis(); + long elapsedTime = stopTime - startTime; + logger.info( + "ElasticSearchUtil getDataByIdentifier method end at == {} for Type {} , Total time elapsed = {} ", + stopTime, type, elapsedTime); + return response.getSource(); + } catch (IOException ex) { + logger.error("Exception in getDataByIdentifier {} ", ex.getMessage()); + } finally { + try { + restHighLevelClient.close(); + } catch (IOException e) { + logger.error("Exception in getDataByIdentifier {} ", e.getMessage()); + } + } + return null; + } + + @Override + public Map<String, Object> searchData(String index, String type, Map<String, Object> searchData) { + RestHighLevelClient client = elasticSearchClient.getClient(); + + long startTime = System.currentTimeMillis(); + logger.info("ElasticSearchUtil searchData method started at == {} for Type {} ", startTime, type); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); + Iterator<Entry<String, Object>> itr = searchData.entrySet().iterator(); + while (itr.hasNext()) { + Entry<String, Object> entry = itr.next(); + sourceBuilder.query(QueryBuilders.commonTermsQuery(entry.getKey(), entry.getValue())); + } + + SearchResponse sr = null; + try { + sr = client.search(new SearchRequest(index).types(type).source(sourceBuilder)); + + } catch (IOException ex) { + logger.error("Error while execution in Elasticsearch", ex); + } + + if (sr != null && (sr.getHits() == null || sr.getHits().getTotalHits() == 0)) { + return new HashMap<>(); + } + sr.getHits().getAt(0).getSourceAsMap(); + long stopTime = System.currentTimeMillis(); + long elapsedTime = stopTime - startTime; + + logger.info("ElasticSearchUtil searchData method end at == {} for Type {} ,Total time elapsed = {} ", + stopTime, type, elapsedTime); + try { + client.close(); + } catch (IOException e) { + logger.error("Error while closing the client"); + } + return sr.getAggregations().asList().get(0).getMetaData(); + } + + @SuppressWarnings("rawtypes") + @Override + public Map<String, Object> complexSearch(SearchDto searchDTO, String index, String... type) { + RestHighLevelClient restHighLevelClient = elasticSearchClient.getClient(); + + long startTime = System.currentTimeMillis(); + logger.info("ElasticSearchUtil complexSearch method started at == {} ", startTime); + + // Map<String, Float> constraintsMap = getConstraints(searchDTO); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0); + BoolQueryBuilder query = new BoolQueryBuilder(); + + if (!StringUtils.isBlank(searchDTO.getQuery())) { + query.must(QueryBuilders.simpleQueryStringQuery(searchDTO.getQuery()).field("all_fields")); + } + + // apply sorting + if (searchDTO.getSortBy() != null && searchDTO.getSortBy().size() > 0) { + + for (Map.Entry<String, String> entry : searchDTO.getSortBy().entrySet()) { + SortBuilder sortB = SortBuilders.fieldSort(entry.getKey()).order(getSortOrder(entry.getValue())); + searchSourceBuilder.sort(sortB); + + } + } + + // apply the fields filter + searchSourceBuilder.fetchSource( + searchDTO.getFields() != null ? searchDTO.getFields().stream().toArray(String[]::new) : null, + searchDTO.getExcludedFields() != null ? searchDTO.getExcludedFields().stream().toArray(String[]::new) + : null); + + // setting the offset + if (searchDTO.getOffset() != null) { + searchSourceBuilder.from(searchDTO.getOffset()); + } + + // setting the limit + if (searchDTO.getLimit() != null) { + searchSourceBuilder.size(searchDTO.getLimit()); + } + + // apply additional properties + if (searchDTO.getAdditionalProperties() != null && searchDTO.getAdditionalProperties().size() > 0) { + for (Map.Entry<String, Object> entry : searchDTO.getAdditionalProperties().entrySet()) { + addAdditionalProperties(query, entry); + } + } + + searchSourceBuilder.query(query); + + if (null != searchDTO.getFacets() && !searchDTO.getFacets().isEmpty()) { + // addAggregations(searchSourceBuilder, searchDTO.getFacets()); + } + logger.info("calling search builder====== {} ", searchSourceBuilder); + SearchResponse response = null; + SearchRequest searchReq = new SearchRequest(index).types(type).source(searchSourceBuilder); + List<Map<String, Object>> esSource = new ArrayList<>(); + + try { + response = restHighLevelClient.search(searchReq); + + if (response != null) { + SearchHits hits = response.getHits(); + for (SearchHit hit : hits) { + esSource.add(hit.getSourceAsMap()); + } + } + } catch (IOException e) { + logger.error("Exception in complexSearch {} ", e.getMessage()); + } + return null; + + } + + @SuppressWarnings("unchecked") + private void addAggregations(SearchSourceBuilder searchSourceBuilder, Map<String, Object> aggregations) { + long startTime = System.currentTimeMillis(); + logger.info("ElasticSearchUtil addAggregations method started at == {} ", startTime); + for (Map.Entry<String, Object> entry : aggregations.entrySet()) { + + String key = entry.getKey(); + String interval = "interval"; + for (Map.Entry<String, Object> en : aggregations.entrySet()) { + if ("DATE_HISTOGRAM".equalsIgnoreCase(en.getKey())) { + Map<String, String> aggsVal = (Map<String, String>) en.getValue(); + DateHistogramInterval dateHistogramInterval = null; + if (aggsVal.get(interval).equals("day")) { + dateHistogramInterval = DateHistogramInterval.DAY; + } else if (aggsVal.get(interval).equals("hour")) { + dateHistogramInterval = DateHistogramInterval.HOUR; + + } else if (aggsVal.get(interval).equals("month")) { + dateHistogramInterval = DateHistogramInterval.MONTH; + + } else if (aggsVal.get(interval).equals("year")) { + dateHistogramInterval = DateHistogramInterval.YEAR; + + } + searchSourceBuilder.aggregation(AggregationBuilders.dateHistogram(key).field(aggsVal.get("field")) + .dateHistogramInterval(dateHistogramInterval)); + + } + /* + * else if ("TERMS".equalsIgnoreCase(en.getKey())) { Map<String, String> aggVal + * = (Map<String, String>) en.getValue(); for (Map.Entry<String, String> entryS + * : aggVal.entrySet()) { searchSourceBuilder.aggregation(AggregationBuilders. + * dateHistogram(key).field(aggsVal.get("field")) .dateHistogramInterval(Date)); + * } } + */ + } + + } + long stopTime = System.currentTimeMillis(); + long elapsedTime = stopTime - startTime; + logger.info("ElasticSearchUtil addAggregations method end at == {} ,Total time elapsed = {} ", stopTime, + elapsedTime); + } + + @SuppressWarnings("unchecked") + private void addAdditionalProperties(BoolQueryBuilder query, Entry<String, Object> entry) { + long startTime = System.currentTimeMillis(); + logger.info("ElasticSearchUtil addAdditionalProperties method started at == {} ", startTime); + String key = entry.getKey(); + + if (key.equalsIgnoreCase("FILTERS")) { + + Map<String, Object> filters = (Map<String, Object>) entry.getValue(); + for (Map.Entry<String, Object> en : filters.entrySet()) { + createFilterESOpperation(en, query); + } + } else if (key.equalsIgnoreCase("EXISTS") || key.equalsIgnoreCase("NOT_EXISTS")) { + createESOpperation(entry, query); + } + long stopTime = System.currentTimeMillis(); + long elapsedTime = stopTime - startTime; + logger.info("ElasticSearchUtil addAdditionalProperties method end at == {} ,Total time elapsed = {}", stopTime, + elapsedTime); + } + + /** Method to create EXISTS and NOT EXIST FILTER QUERY . */ + @SuppressWarnings("unchecked") + private void createESOpperation(Entry<String, Object> entry, BoolQueryBuilder query) { + + String operation = entry.getKey(); + List<String> existsList = (List<String>) entry.getValue(); + + if (operation.equalsIgnoreCase("EXISTS")) { + for (String name : existsList) { + query.must(createExistQuery(name)); + } + } else if (operation.equalsIgnoreCase("NOT_EXISTS")) { + for (String name : existsList) { + query.mustNot(createExistQuery(name)); + } + } + } + + /** Method to create CommonTermQuery , multimatch and Range Query. */ + @SuppressWarnings("unchecked") + private void createFilterESOpperation(Entry<String, Object> entry, BoolQueryBuilder query) { + + String key = entry.getKey(); + Object val = entry.getValue(); + if (val instanceof List) { + if (!((List) val).isEmpty()) { + if (((List) val).get(0) instanceof String) { + ((List<String>) val).replaceAll(String::toLowerCase); + query.must(createTermsQuery(key, (List<String>) val)); + } else { + query.must(createTermsQuery(key, (List) val)); + } + } + } else if (val instanceof Map) { + Map<String, Object> value = (Map<String, Object>) val; + Map<String, Object> rangeOperation = new HashMap<>(); + Map<String, Object> lexicalOperation = new HashMap<>(); + for (Map.Entry<String, Object> it : value.entrySet()) { + String operation = it.getKey(); + if (operation.startsWith(ElasticSearchConstants.LT) + || operation.startsWith(ElasticSearchConstants.GT)) { + rangeOperation.put(operation, it.getValue()); + } else if (operation.startsWith(ElasticSearchConstants.STARTS_WITH) + || operation.startsWith(ElasticSearchConstants.ENDS_WITH)) { + lexicalOperation.put(operation, it.getValue()); + } + } + if (!(rangeOperation.isEmpty())) { + query.must(createRangeQuery(key, rangeOperation)); + } + if (!(lexicalOperation.isEmpty())) { + query.must(createLexicalQuery(key, lexicalOperation)); + } + + } else if (val instanceof String) { + query.must(createTermQuery(key, ((String) val).toLowerCase())); + } else { + query.must(createTermQuery(key, val)); + } + } + + private static TermQueryBuilder createTermQuery(String name, Object text) { + + return QueryBuilders.termQuery(name, text); + + } + + @SuppressWarnings("unchecked") + private static TermsQueryBuilder createTermsQuery(String key, List values) { + + return QueryBuilders.termsQuery(key, (values).stream().toArray(Object[]::new)); + + } + + private static Map<String, Float> getConstraints(SearchDto searchDTO) { + if (null != searchDTO.getSoftConstraints() && !searchDTO.getSoftConstraints().isEmpty()) { + return searchDTO.getSoftConstraints().entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().floatValue())); + } + return Collections.emptyMap(); + } + + @Override + public boolean healthCheck() { + return false; + } + + /// New Methods for Elastic Search Query Builders + + private RangeQueryBuilder createRangeQuery(String name, Map<String, Object> rangeOperation) { + + RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(name); + for (Map.Entry<String, Object> it : rangeOperation.entrySet()) { + if (it.getKey().equalsIgnoreCase(ElasticSearchConstants.LTE)) { + rangeQueryBuilder.lte(it.getValue()); + } else if (it.getKey().equalsIgnoreCase(ElasticSearchConstants.LT)) { + rangeQueryBuilder.lt(it.getValue()); + } else if (it.getKey().equalsIgnoreCase(ElasticSearchConstants.GTE)) { + rangeQueryBuilder.gte(it.getValue()); + } else if (it.getKey().equalsIgnoreCase(ElasticSearchConstants.GT)) { + rangeQueryBuilder.gt(it.getValue()); + } + } + + return rangeQueryBuilder; + } + + private SortOrder getSortOrder(String value) { + return value.equalsIgnoreCase(ElasticSearchConstants.ASC_ORDER) ? SortOrder.ASC : SortOrder.DESC; + } + + private QueryBuilder createLexicalQuery(String key, Map<String, Object> rangeOperation) { + QueryBuilder queryBuilder = null; + for (Map.Entry<String, Object> it : rangeOperation.entrySet()) { + if (it.getKey().equalsIgnoreCase(ElasticSearchConstants.STARTS_WITH)) { + + queryBuilder = QueryBuilders.prefixQuery(key, (String) it.getValue()); + } else if (it.getKey().equalsIgnoreCase(ElasticSearchConstants.ENDS_WITH)) { + String endsWithRegex = "~" + it.getValue(); + queryBuilder = QueryBuilders.regexpQuery(key, endsWithRegex); + } + } + return queryBuilder; + } + + private ExistsQueryBuilder createExistQuery(String name) { + return QueryBuilders.existsQuery(name); + } + /* + * + * @SuppressWarnings("unchecked") + * + * @Override public ElasticSearchDictator + * createSearchDictator(AggregateRequestDto dto, String tenant) throws Exception + * { ElasticSearchDictator dictator = new ElasticSearchDictator(); if + * (dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_WRT_TIME) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_DAY_WISE) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_REASON) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_GENDER) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_AGEGROUP) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_TIME) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_TOP_PERFORMING) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_LEAST_PERFORMING)) { + * dictator.setIndexName(tenant + ratingElasticIndexName); + * dictator.setDocumentType(ratingElasticDocType); } else { + * dictator.setIndexName(tenant + transactionElasticIndexName); + * dictator.setDocumentType(transactionElasticDocType); } if + * (StringUtils.isNotBlank(dto.getServiceApi())) { + * dictator.setVisualisationName(dto.getServiceApi()); } Map<String, Map<String, + * List<Object>>> queryMap = new HashMap<>(); if (dto.getCustomData() != null) { + * for (Map.Entry<String, Object> entry : dto.getCustomData().entrySet()) { if + * (StringUtils.isNotBlank(entry.getKey()) && entry.getValue() != null) { + * List<Object> valueList = new ArrayList<>(); + * + * if (entry.getValue() instanceof ArrayList) { + * + * List<Object> valueArray = (ArrayList<Object>) entry.getValue(); + * + * for (Object value : valueArray) { valueList.add(value); } } else { + * valueList.add(entry.getValue()); } if (!valueList.isEmpty()) { String + * entryKey = entry.getKey(); if + * (dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_WRT_TIME) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_DAY_WISE) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_REASON) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_GENDER) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_AGEGROUP) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_TIME) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_TOP_PERFORMING) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_LEAST_PERFORMING)) { + * + * entryKey = "storeId"; } if + * (queryMap.containsKey(ElasticProperties.Query.MATCH_CONDITION)) { Map<String, + * List<Object>> queryInnerMap = queryMap + * .get(ElasticProperties.Query.MATCH_CONDITION); queryInnerMap.put(entryKey, + * valueList); } else { Map<String, List<Object>> queryInnerMap = new + * HashMap<>(); queryInnerMap.put(entryKey, valueList); + * queryMap.put(ElasticProperties.Query.MATCH_CONDITION, queryInnerMap); } } } } + * } + * + * if (dto.getDates() != null) { if + * (StringUtils.isNotBlank(dto.getDates().getStartDate()) && + * StringUtils.isNotBlank(dto.getDates().getEndDate())) { List<Object> valueList + * = new ArrayList<>(); + * + * valueList.add(dto.getDates().getStartDate()); + * valueList.add(dto.getDates().getEndDate()); Map<String, List<Object>> + * queryInnerMap = new HashMap<>(); if + * (dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_WRT_TIME) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_REASON) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_GENDER) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_AGEGROUP) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_STORE_TIME) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_TOP_PERFORMING) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_LEAST_PERFORMING) || + * dto.getServiceApi().equals(ServiceApiConstants.CSAT_DAY_WISE)) { + * queryInnerMap.put(ElasticProperties.Query.FEEDBACK_DATE_TIME, valueList); } + * else { queryInnerMap.put(ElasticProperties.Query.TRANSACTION_DATE_FIELD, + * valueList); } + * + * queryMap.put(ElasticProperties.Query.RANGE_CONDITION, queryInnerMap); } } + * dictator.setQueryMap(queryMap); + * + */ + /* + * Map<String,Map<String,Map<String,Object>>> aggregationMap = new HashMap<>(); + * Map<String,Map<String, String>> innerMap = new HashMap<>(); Map<String, + * String> deepInnerMap = new HashMap<>(); + *//* + * + * + * return dictator; + * + * } + */ + + @SuppressWarnings("unchecked") + @Override + public SearchRequest buildElasticSearchQuery(ElasticSearchDictator dictator) { + SearchSourceBuilder searchBuilder = buildSearchSourceBuilder(); + BoolQueryBuilder boolQuery = buildBoolQuery(); + String localDateStartRange = null; + String localDateEndRange = null; + if (dictator.getQueryMap().containsKey(ElasticProperties.Query.RANGE_CONDITION)) { + + String searchParamRange = null; + Map<String, List<Object>> innerMap = dictator.getQueryMap().get(ElasticProperties.Query.RANGE_CONDITION); + for (Entry<String, List<Object>> en : innerMap.entrySet()) { + searchParamRange = en.getKey(); + localDateStartRange = (String) en.getValue().get(0); + localDateEndRange = (String) en.getValue().get(1); + } + + addMustOnBoolQuery(boolQuery, buildRangeQuery(searchParamRange, localDateStartRange, localDateEndRange)); + + } + + if (dictator.getQueryMap().containsKey(ElasticProperties.Query.MATCH_CONDITION)) { + String searchTermField = null; + Map<String, List<Object>> innerMap = dictator.getQueryMap().get(ElasticProperties.Query.MATCH_CONDITION); + for (Entry<String, List<Object>> en : innerMap.entrySet()) { + searchTermField = en.getKey(); + addFilterTermsOnBoolQuery(boolQuery, buildTermsQuery(searchTermField, en.getValue())); + } + } + + addQueryToSearchBuilder(searchBuilder, boolQuery); + + DateHistogramAggregationBuilder dateAggBuilder = null; + AvgAggregationBuilder avgAggBuilder = null; + SumAggregationBuilder sumAggBuilder = null; + TermsAggregationBuilder termsAggBuilder = null; + NestedAggregationBuilder nestedAggBuilder = null; + ValueCountAggregationBuilder valueCountAggBuilder = null; + if (dictator.getQueryAggregationMap() == null) { + return new SearchRequest(dictator.getIndexName()).types(dictator.getDocumentType()).source(searchBuilder); + } + + for (Map.Entry<String, Object> itr : dictator.getQueryAggregationMap().entrySet()) { + + String aggregationType = itr.getKey(); + Object value = itr.getValue(); + if (ElasticProperties.Query.NESTED.equalsIgnoreCase(aggregationType)) { + Map<String, Object> nestedMap = null; + if (value instanceof HashMap) { + nestedMap = (HashMap<String, Object>) value; + } else if (value instanceof LinkedHashMap) { + nestedMap = (LinkedHashMap<String, Object>) value; + } + if (nestedMap != null) { + String aggregationName = String.valueOf(nestedMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + nestedAggBuilder = buildNestedAggregationBuilder(aggregationName, nestedMap); + } + + } else if (ElasticProperties.Query.SUM.equalsIgnoreCase(aggregationType)) { + Map<String, Object> sumMap = null; + if (value instanceof HashMap) { + sumMap = (HashMap<String, Object>) value; + } else if (value instanceof LinkedHashMap) { + sumMap = (LinkedHashMap<String, Object>) value; + } + if (sumMap != null) { + String aggregationName = String.valueOf(sumMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + sumAggBuilder = buildSumAggregation(aggregationName, + String.valueOf(sumMap.get(ElasticProperties.Query.FIELD.toLowerCase()))); + } + } else if (ElasticProperties.Query.TERM.equalsIgnoreCase(aggregationType)) { + Map<String, Object> termMap = null; + if (value instanceof HashMap) { + termMap = (HashMap<String, Object>) value; + } else if (value instanceof LinkedHashMap) { + termMap = (LinkedHashMap<String, Object>) value; + + } + if (termMap != null) { + String aggregationName = String.valueOf(termMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + termsAggBuilder = buildTermAggregation(aggregationName, termMap); + } + + } else if (ElasticProperties.Query.DATE_HISTOGRAM.equalsIgnoreCase(aggregationType)) { + Map<String, Object> histogramMap = null; + + if (value instanceof HashMap) { + histogramMap = (HashMap<String, Object>) value; + } else if (value instanceof LinkedHashMap) { + histogramMap = (LinkedHashMap<String, Object>) value; + + } + if (histogramMap != null) { + String aggregationName = String + .valueOf(histogramMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + dateAggBuilder = buildDateHistogramAggregation(aggregationName, + histogramMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString(), + histogramMap.get(ElasticProperties.Query.INTERVAL.toLowerCase()).toString().toUpperCase(), + Long.parseLong(localDateStartRange), Long.parseLong(localDateEndRange)); + } + + } else if (ElasticProperties.Query.COUNT.equalsIgnoreCase(aggregationType)) { + Map<String, Object> countAggMap = null; + + if (value instanceof HashMap) { + countAggMap = (HashMap<String, Object>) value; + } else if (value instanceof LinkedHashMap) { + countAggMap = (LinkedHashMap<String, Object>) value; + + } + if (countAggMap != null) { + String countField = countAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString(); + String countAggName = String.valueOf(countAggMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + valueCountAggBuilder = buildCountsAggregationQuery(countAggName, countField); + } + } else if (ElasticProperties.Query.AGGREGATION_CONDITION.equalsIgnoreCase(aggregationType)) { + + Map<String, Object> firstLevelAggMap = (LinkedHashMap<String, Object>) value; + for (Map.Entry<String, Object> firstLevelItrEntry : firstLevelAggMap.entrySet()) { + + String firstLevelEntryKey = firstLevelItrEntry.getKey(); + Object firstLevelEntryValue = firstLevelItrEntry.getValue(); + if (ElasticProperties.Query.SUM.equalsIgnoreCase(firstLevelEntryKey)) { + Map<String, Object> sumAggMap = (LinkedHashMap<String, Object>) firstLevelEntryValue; + String sumField = sumAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString(); + String sumAggName = String.valueOf(sumAggMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + if (termsAggBuilder != null) { + termsAggBuilder = buildSubSumAggreationForTerms(termsAggBuilder, sumAggName, sumField); + } else if (dateAggBuilder != null) { + dateAggBuilder = buildSubSumAggreationForDateHistogram(dateAggBuilder, sumAggName, + sumField); + } + } else if (ElasticProperties.Query.AVG.equalsIgnoreCase(firstLevelEntryKey)) { + Map<String, Object> avgAggMap = (LinkedHashMap<String, Object>) firstLevelEntryValue; + String avgField = avgAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString(); + String avgAggName = String.valueOf(avgAggMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + if (termsAggBuilder != null) { + termsAggBuilder = buildSubAvgAggreationForTerms(termsAggBuilder, avgAggName, avgField); + } else if (dateAggBuilder != null) { + dateAggBuilder = buildSubSumAggreationForDateHistogram(dateAggBuilder, avgAggName, + avgField); + } + } else if (ElasticProperties.Query.COUNT.equalsIgnoreCase(firstLevelEntryKey)) { + Map<String, Object> countAggMap = (LinkedHashMap<String, Object>) firstLevelEntryValue; + String countField = countAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString(); + String countAggName = String + .valueOf(countAggMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + if (termsAggBuilder != null) { + termsAggBuilder = buildSubCountAggregationForTerms(termsAggBuilder, countAggName, + countField); + } else if (dateAggBuilder != null) { + dateAggBuilder = buildSubCountAggregationForDateHistogram(dateAggBuilder, countAggName, + countField); + } + } else if (ElasticProperties.Query.TERM.equalsIgnoreCase(firstLevelEntryKey)) { + Map<String, Object> subTermAggMap = (LinkedHashMap<String, Object>) firstLevelEntryValue; + String subTermAggName = String + .valueOf(subTermAggMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + if (nestedAggBuilder != null) { + buildSubTermsAggregationForNested(nestedAggBuilder, subTermAggName, + subTermAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString()); + } else if (dateAggBuilder != null) { + buildSubTermsAggregationForHistogram(dateAggBuilder, subTermAggName, + subTermAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString()); + } else if (termsAggBuilder != null) { + buildSubTermsAggregationForTerms(termsAggBuilder, subTermAggName, + subTermAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()).toString()); + } + + } else if (ElasticProperties.Query.AGGREGATION_CONDITION.equals(firstLevelEntryKey.toUpperCase())) { + + Map<String, Object> secondLevelAggMap = (LinkedHashMap<String, Object>) firstLevelEntryValue; + for (Map.Entry<String, Object> secondLevelItrEntry : secondLevelAggMap.entrySet()) { + String secondLevelItrEntryKey = secondLevelItrEntry.getKey(); + Object secondLevelItrEntryValue = secondLevelItrEntry.getValue(); + if (ElasticProperties.Query.SUM.equalsIgnoreCase(secondLevelItrEntryKey)) { + Map<String, Object> subSumAggMap = (LinkedHashMap<String, Object>) secondLevelItrEntryValue; + + String subSumField = subSumAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()) + .toString(); + String subSumAggName = String + .valueOf(subSumAggMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + + if (nestedAggBuilder != null) { + List<AggregationBuilder> aggBuilders = nestedAggBuilder.getSubAggregations(); + for (AggregationBuilder aggregationBuilder : aggBuilders) { + + if (aggregationBuilder instanceof TermsAggregationBuilder) { + TermsAggregationBuilder subTermAggBuilder = (TermsAggregationBuilder) aggregationBuilder; + subTermAggBuilder = buildSubSumAggreationForTerms(subTermAggBuilder, + subSumAggName, subSumField); + + } + } + } + } else if (ElasticProperties.Query.COUNT.equalsIgnoreCase(secondLevelItrEntryKey)) { + Map<String, Object> subCountAggMap = (LinkedHashMap<String, Object>) secondLevelItrEntryValue; + + String subCountField = subCountAggMap.get(ElasticProperties.Query.FIELD.toLowerCase()) + .toString(); + String subCountAggName = String + .valueOf(subCountAggMap.get(ElasticProperties.Query.LABEL.toLowerCase())); + + if (dateAggBuilder != null) { + List<AggregationBuilder> aggBuilders = dateAggBuilder.getSubAggregations(); + for (AggregationBuilder aggregationBuilder : aggBuilders) { + + if (aggregationBuilder instanceof TermsAggregationBuilder) { + TermsAggregationBuilder subTermAggBuilder = (TermsAggregationBuilder) aggregationBuilder; + subTermAggBuilder = buildSubCountAggregationForTerms(subTermAggBuilder, + subCountAggName, subCountField); + } + } + } + } + } + } + } + } + } + + if (dateAggBuilder != null) { + addAggregationToSearchBuilder(searchBuilder, dateAggBuilder); + } + if (sumAggBuilder != null) { + addSumAggregationToSearchBuilder(searchBuilder, sumAggBuilder); + } + if (avgAggBuilder != null) { + addAvgAggregationToSearchBuilder(searchBuilder, avgAggBuilder); + } + if (valueCountAggBuilder != null) { + addValueCountAggregationToSearchBuilder(searchBuilder, valueCountAggBuilder); + } + if (termsAggBuilder != null) { + addTermsAggregationToSearchBuilder(searchBuilder, termsAggBuilder); + } + if (nestedAggBuilder != null) { + addNestedAggregationToSearchBuilder(searchBuilder, nestedAggBuilder); + } + return new SearchRequest(dictator.getIndexName()).types(dictator.getDocumentType()).source(searchBuilder); + } + + private NestedAggregationBuilder buildSubTermsAggregationForNested(NestedAggregationBuilder nestedAggBuilder, + String subAggregationName, String fieldName) { + return nestedAggBuilder.subAggregation(AggregationBuilders.terms(subAggregationName).field(fieldName)); + + } + + private void addNestedAggregationToSearchBuilder(SearchSourceBuilder searchBuilder, + NestedAggregationBuilder nestedAggBuilder) { + searchBuilder.aggregation(nestedAggBuilder); + } + + private RangeQueryBuilder buildRangeQuery(String fieldName, Object startRange, Object endRange) { + return QueryBuilders.rangeQuery(fieldName).gte(startRange).lte(endRange); + } + + private TermQueryBuilder buildTermQuery(String fieldName, Object fieldValue) { + return QueryBuilders.termQuery(fieldName, fieldValue); + } + + private TermsQueryBuilder buildTermsQuery(String fieldName, List<Object> fieldValue) { + return QueryBuilders.termsQuery(fieldName, fieldValue); + } + + private MatchQueryBuilder buildMatchQuery(String fieldName, Object fieldValue) { + return QueryBuilders.matchQuery(fieldName, fieldValue); + } + + private ValueCountAggregationBuilder buildCountsAggregationQuery(String aggregationName, String fieldName) { + return AggregationBuilders.count(aggregationName).field(fieldName); + } + + private SumAggregationBuilder buildSumAggregation(String aggregationName, String fieldName) { + return AggregationBuilders.sum(aggregationName).field(fieldName); + } + + @SuppressWarnings("unchecked") + private TermsAggregationBuilder buildTermAggregation(String aggregationName, Map<String, Object> paramMap) { + TermsAggregationBuilder aggBuilder = AggregationBuilders.terms(aggregationName); + for (Map.Entry<String, Object> param : paramMap.entrySet()) { + if (param.getKey().equalsIgnoreCase(ElasticProperties.Query.FIELD)) { + aggBuilder = aggBuilder.field((String) param.getValue()); + } else if (param.getKey().equalsIgnoreCase(ElasticProperties.Query.SIZE)) { + aggBuilder = aggBuilder.size((Integer) param.getValue()); + } else if (param.getKey().equalsIgnoreCase(ElasticProperties.Query.ORDER)) { + Map<String, Object> keyMap = (HashMap<String, Object>) param.getValue(); + BucketOrder order = null; + for (Map.Entry<String, Object> valueMap : keyMap.entrySet()) { + Map<String, String> orderMap = (HashMap<String, String>) valueMap.getValue(); + + String key = valueMap.getKey() + "of" + orderMap.get(ElasticProperties.Query.FIELD.toLowerCase()); + + order = BucketOrder.aggregation(key, + orderMap.get("orderBy").equalsIgnoreCase(ElasticProperties.Query.ASC) ? true : false); + } + aggBuilder = aggBuilder.order(order); + } + } + return aggBuilder; + + } + + private AvgAggregationBuilder buildAvgAggregation(String aggregationName, String fieldName) { + return AggregationBuilders.avg(aggregationName).field(fieldName); + } + + private NestedAggregationBuilder buildNestedAggregationBuilder(String aggregationName, + Map<String, Object> paramMap) { + String pathName = paramMap.get(ElasticProperties.Query.PATH.toLowerCase()).toString(); + return AggregationBuilders.nested(aggregationName, pathName); + } + + private AggregationBuilder buildSubTermForNestedAggregation(NestedAggregationBuilder nestedBuilder, + String aggregationName, Map<String, Object> paramMap) { + return nestedBuilder.subAggregation(buildTermAggregation(aggregationName, paramMap)); + + } + + private DateHistogramAggregationBuilder buildDateHistogramAggregation(String aggregationName, String fieldName, + String interval, Long boundMin, Long boundMax) { + DateHistogramInterval dateHistogramInterval = null; + if (ElasticProperties.Query.HOUR.equals(interval)) { + dateHistogramInterval = DateHistogramInterval.HOUR; + } else if (ElasticProperties.Query.DAY.equals(interval)) { + dateHistogramInterval = DateHistogramInterval.DAY; + } else if (ElasticProperties.Query.MINUTE.equals(interval)) { + dateHistogramInterval = DateHistogramInterval.MINUTE; + } else if (ElasticProperties.Query.MONTH.equals(interval)) { + dateHistogramInterval = DateHistogramInterval.MONTH; + + } + return AggregationBuilders.dateHistogram(aggregationName).field(fieldName) + .dateHistogramInterval(dateHistogramInterval).format("epoch_millis") + .extendedBounds(new ExtendedBounds(boundMin, boundMax)); + } + + private DateHistogramAggregationBuilder buildSubSumAggreationForDateHistogram( + DateHistogramAggregationBuilder builder, String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.sum(aggregationName).field(fieldName)); + } + + private DateHistogramAggregationBuilder buildSubCummulativeSumAggreationForDateHistogram( + DateHistogramAggregationBuilder builder, String aggregationName, String fieldName) { + PipelineAggregationBuilder pipeLineAggregation = new CumulativeSumPipelineAggregationBuilder(aggregationName, + fieldName); + return builder.subAggregation(pipeLineAggregation); + } + + private DateHistogramAggregationBuilder buildSubAvgAggreationForDateHistogram( + DateHistogramAggregationBuilder builder, String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.avg(aggregationName).field(fieldName)); + } + + private DateHistogramAggregationBuilder buildSubCountAggregationForDateHistogram( + DateHistogramAggregationBuilder builder, String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.count(aggregationName).field(fieldName)); + } + + private TermsAggregationBuilder buildSubCountAggregationForTerms(TermsAggregationBuilder builder, + String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.count(aggregationName).field(fieldName)); + } + + private TermsAggregationBuilder buildSubTermsAggregationForTerms(TermsAggregationBuilder builder, + String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.terms(aggregationName).field(fieldName)); + } + + private DateHistogramAggregationBuilder buildSubTermsAggregationForHistogram( + DateHistogramAggregationBuilder builder, String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.terms(aggregationName).field(fieldName)); + } + + private TermsAggregationBuilder buildSubSumAggreationForTerms(TermsAggregationBuilder builder, + String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.sum(aggregationName).field(fieldName)); + } + + private TermsAggregationBuilder buildSubAvgAggreationForTerms(TermsAggregationBuilder builder, + String aggregationName, String fieldName) { + return builder.subAggregation(AggregationBuilders.avg(aggregationName).field(fieldName)); + } + + private SumAggregationBuilder buildSubSumAggregation(SumAggregationBuilder builder, String aggregationName, + String fieldName) { + return builder.subAggregation(AggregationBuilders.sum(aggregationName).field(fieldName)); + } + + private AvgAggregationBuilder buildSubAvgAggregation(AvgAggregationBuilder builder, String aggregationName, + String fieldName) { + return builder.subAggregation(AggregationBuilders.avg(aggregationName).field(fieldName)); + } + + private BoolQueryBuilder buildBoolQuery() { + return QueryBuilders.boolQuery(); + } + + private BoolQueryBuilder addMustOnBoolQuery(BoolQueryBuilder builder, RangeQueryBuilder rangeBuilder) { + return builder.must(rangeBuilder); + } + + private BoolQueryBuilder addFilterOnBoolQuery(BoolQueryBuilder builder, TermQueryBuilder termBuilder) { + return builder.filter(termBuilder); + } + + private BoolQueryBuilder addFilterTermsOnBoolQuery(BoolQueryBuilder builder, TermsQueryBuilder termBuilder) { + return builder.filter(termBuilder); + } + + private BoolQueryBuilder addMatchOnBoolQuery(BoolQueryBuilder builder, MatchQueryBuilder matchBuilder) { + return builder.filter(matchBuilder); + } + + private SearchSourceBuilder buildSearchSourceBuilder() { + return new SearchSourceBuilder().size(0); + } + + private SearchSourceBuilder addQueryToSearchBuilder(SearchSourceBuilder builder, BoolQueryBuilder queryBuilder) { + return builder.query(queryBuilder); + } + + private SearchSourceBuilder addAggregationToSearchBuilder(SearchSourceBuilder builder, + DateHistogramAggregationBuilder aggBuilder) { + return builder.aggregation(aggBuilder); + } + + private SearchSourceBuilder addSumAggregationToSearchBuilder(SearchSourceBuilder builder, + SumAggregationBuilder aggBuilder) { + return builder.aggregation(aggBuilder); + } + + private SearchSourceBuilder addAvgAggregationToSearchBuilder(SearchSourceBuilder builder, + AvgAggregationBuilder aggBuilder) { + return builder.aggregation(aggBuilder); + } + + private SearchSourceBuilder addValueCountAggregationToSearchBuilder(SearchSourceBuilder builder, + ValueCountAggregationBuilder aggBuilder) { + return builder.aggregation(aggBuilder); + } + + private SearchSourceBuilder addTermsAggregationToSearchBuilder(SearchSourceBuilder builder, + TermsAggregationBuilder aggBuilder) { + return builder.aggregation(aggBuilder); + } + + @Override + public MultiSearchResponse executeMultiSearchRequest(List<SearchRequest> searchRequestList, String tenant) { + MultiSearchRequest multiRequest = new MultiSearchRequest(); + MultiSearchResponse response = null; + + for (SearchRequest request : searchRequestList) { + logger.info("ES Query is : " + request.source()); + multiRequest.add(request); + } + + try { + response = client.multiSearch(multiRequest); + } catch (IOException e) { + logger.error("Encountered an error while connecting : " + e); + logger.error("Error Message to report : " + e.getMessage()); + } + + return response; + } + + private RestHighLevelClient getClientForElastic() { + return new RestHighLevelClient(RestClient.builder(new HttpHost(elasticHost, elasticPort, REST_SCHEME))); + } + + private RestHighLevelClient getClientForAlternate() throws MalformedURLException { + URL url = new URL("https://egov-micro-dev.egovernments.org/elasticsearch/"); + return new RestHighLevelClient(RestClient.builder(new HttpHost(url.getHost(), url.getPort(), REST_SCHEME2))); + } + + /* + * @SuppressWarnings("unchecked") + * + * @Override public ElasticSearchDictator createSearchDictator(String indexName, + * String documentType, CummulativeDataRequestDto dto, String dateField) throws + * Exception { ElasticSearchDictator dictator = new ElasticSearchDictator(); + * + * dictator.setIndexName(indexName); dictator.setDocumentType(documentType); + * + * Map<String, Map<String, List<Object>>> queryMap = new HashMap<>(); if + * (dto.getCustomData() != null) { for (Map.Entry<String, Object> entry : + * dto.getCustomData().entrySet()) { if (StringUtils.isNotBlank(entry.getKey()) + * && entry.getValue() != null) { List<Object> valueList = new ArrayList<>(); + * + * if (entry.getValue() instanceof ArrayList) { + * + * List<Object> valueArray = (ArrayList<Object>) entry.getValue(); + * + * for (Object value : valueArray) { valueList.add(value); } } else { + * valueList.add(entry.getValue()); } if (!valueList.isEmpty()) { String + * entryKey = entry.getKey(); if + * (queryMap.containsKey(ElasticProperties.Query.MATCH_CONDITION)) { Map<String, + * List<Object>> queryInnerMap = queryMap + * .get(ElasticProperties.Query.MATCH_CONDITION); queryInnerMap.put(entryKey, + * valueList); } else { Map<String, List<Object>> queryInnerMap = new + * HashMap<>(); queryInnerMap.put(entryKey, valueList); + * queryMap.put(ElasticProperties.Query.MATCH_CONDITION, queryInnerMap); } } } } + * } + * + * if (dto.getDates() != null) { if + * (StringUtils.isNotBlank(dto.getDates().getStartDate()) && + * StringUtils.isNotBlank(dto.getDates().getEndDate())) { List<Object> valueList + * = new ArrayList<>(); + * + * valueList.add(dto.getDates().getStartDate()); + * valueList.add(dto.getDates().getEndDate()); Map<String, List<Object>> + * queryInnerMap = new HashMap<>(); queryInnerMap.put(dateField, valueList); + * queryMap.put(ElasticProperties.Query.RANGE_CONDITION, queryInnerMap); } } + * dictator.setQueryMap(queryMap); + * + *//* + * Map<String,Map<String,Map<String,Object>>> aggregationMap = new HashMap<>(); + * Map<String,Map<String, String>> innerMap = new HashMap<>(); Map<String, + * String> deepInnerMap = new HashMap<>(); + *//* + * + * return dictator; } + * + * @Override public ElasticSearchDictator + * createSearchDictator(AggregateRequestDto dto, String indexName, String + * documentType, String filterDateField) throws Exception { + * ElasticSearchDictator dictator = new ElasticSearchDictator(); + * + * dictator.setIndexName(indexName); dictator.setDocumentType(documentType); if + * (StringUtils.isNotBlank(dto.getServiceApi())) { + * dictator.setVisualisationName(dto.getServiceApi()); } + * + * Map<String, Map<String, List<Object>>> queryMap = new HashMap<>(); if + * (dto.getCustomData() != null) { for (Map.Entry<String, Object> entry : + * dto.getCustomData().entrySet()) { if (StringUtils.isNotBlank(entry.getKey()) + * && entry.getValue() != null) { List<Object> valueList = new ArrayList<>(); + * + * if (entry.getValue() instanceof ArrayList) { + * + * List<Object> valueArray = (ArrayList<Object>) entry.getValue(); + * + * for (Object value : valueArray) { valueList.add(value); } } else { + * valueList.add(entry.getValue()); } if (!valueList.isEmpty()) { String + * entryKey = entry.getKey(); if + * (queryMap.containsKey(ElasticProperties.Query.MATCH_CONDITION)) { Map<String, + * List<Object>> queryInnerMap = queryMap + * .get(ElasticProperties.Query.MATCH_CONDITION); queryInnerMap.put(entryKey, + * valueList); } else { Map<String, List<Object>> queryInnerMap = new + * HashMap<>(); queryInnerMap.put(entryKey, valueList); + * queryMap.put(ElasticProperties.Query.MATCH_CONDITION, queryInnerMap); } } } } + * } + * + * if (dto.getDates() != null) { if + * (StringUtils.isNotBlank(dto.getDates().getStartDate()) && + * StringUtils.isNotBlank(dto.getDates().getEndDate())) { List<Object> valueList + * = new ArrayList<>(); + * + * valueList.add(dto.getDates().getStartDate()); + * valueList.add(dto.getDates().getEndDate()); Map<String, List<Object>> + * queryInnerMap = new HashMap<>(); + * + * queryInnerMap.put(filterDateField, valueList); + * + * queryMap.put(ElasticProperties.Query.RANGE_CONDITION, queryInnerMap); } } + * dictator.setQueryMap(queryMap); return dictator; } + */ + + @Override + public ElasticSearchDictator createSearchDictatorV2(AggregateRequestDto dto, String indexName, String documentType, + String filterDateField) { + ElasticSearchDictator dictator = new ElasticSearchDictator(); + try { + + dictator.setIndexName(indexName); + dictator.setDocumentType(documentType); + + Map<String, Map<String, List<Object>>> queryMap = new HashMap<>(); + if (dto.getEsFilters() != null && !dto.getEsFilters().isEmpty()) { + for (Map.Entry<String, Object> entry : dto.getEsFilters().entrySet()) { + if (StringUtils.isNotBlank(entry.getKey()) && entry.getValue() != null) { + List<Object> valueList = new ArrayList<>(); + + if (entry.getValue() instanceof ArrayList) { + + List<Object> valueArray = (ArrayList<Object>) entry.getValue(); + + for (Object value : valueArray) { + String valueString = ""; + if (entry.getKey().equals("dataObject.tenantId")) { + valueString = String.valueOf(value); + valueString = valueString.replace("uat: ", ""); + } + if (StringUtils.isBlank(valueString)) + valueList.add(value); + else + valueList.add(valueString); + } + } else { + String valueString = ""; + if (entry.getKey().equals("dataObject.tenantId")) { + valueString = String.valueOf(entry.getValue()); + valueString = valueString.replace("uat: ", ""); + } + if (StringUtils.isBlank(valueString)) + valueList.add(entry.getValue()); + else + valueList.add(valueString); + } + if (!valueList.isEmpty()) { + String entryKey = entry.getKey(); + if (queryMap.containsKey(ElasticProperties.Query.MATCH_CONDITION)) { + Map<String, List<Object>> queryInnerMap = queryMap + .get(ElasticProperties.Query.MATCH_CONDITION); + queryInnerMap.put(entryKey, valueList); + } else { + Map<String, List<Object>> queryInnerMap = new HashMap<>(); + queryInnerMap.put(entryKey, valueList); + queryMap.put(ElasticProperties.Query.MATCH_CONDITION, queryInnerMap); + } + } + } + } + } + + Map<String, Object> queryAggregationMap = new HashMap<>(); + if (dto.getAggregationFactors() != null && !dto.getAggregationFactors().isEmpty()) { + for (Map.Entry<String, Object> entry : dto.getAggregationFactors().entrySet()) { + if (StringUtils.isNotBlank(entry.getKey()) && entry.getValue() != null) { + Map<String, Object> innerAggregationMap = new HashMap<>(); + innerAggregationMap.put("field", entry.getValue()); + queryAggregationMap.put(entry.getKey(), innerAggregationMap); + } + } + } + dictator.setQueryAggregationMap(queryAggregationMap); + if (dto.getRequestDate() != null) { + if (StringUtils.isNotBlank(dto.getRequestDate().getStartDate()) + && StringUtils.isNotBlank(dto.getRequestDate().getEndDate())) { + List<Object> valueList = new ArrayList<>(); + + valueList.add(dto.getRequestDate().getStartDate()); + valueList.add(dto.getRequestDate().getEndDate()); + Map<String, List<Object>> queryInnerMap = new HashMap<>(); + + if (StringUtils.isNotBlank(filterDateField)) { + queryInnerMap.put(filterDateField, valueList); + queryMap.put(ElasticProperties.Query.RANGE_CONDITION, queryInnerMap); + } + } + } + dictator.setQueryMap(queryMap); + return dictator; + } catch (Exception e) { + logger.info("Exception in createSearchDictatorV2 : {} ", e.getMessage()); + return null; + } + } + + @Override + public MultiSearchResponse executeMultiSearchRequest(List<SearchRequest> searchRequestList, Boolean primaryOrNot) { + MultiSearchRequest multiRequest = new MultiSearchRequest(); + MultiSearchResponse response = null; + for (SearchRequest request : searchRequestList) { + logger.info("ES Query is : " + request.source()); + multiRequest.add(request); + } + + try { + if (primaryOrNot) { + response = client.multiSearch(multiRequest); + } else { + logger.info("Alternate Client URL : " + alternateClient.toString()); + response = alternateClient.multiSearch(multiRequest); + } + + } catch (IOException e) { + logger.error("Encountered an error while connecting : " + e); + logger.error("Error Message to report : " + e.getMessage()); + } + return response; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/AggregateDto.java b/src/main/java/com/tarento/analytics/dto/AggregateDto.java new file mode 100644 index 0000000000000000000000000000000000000000..2c6f6ddc9748e21989a9860078442da287215755 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/AggregateDto.java @@ -0,0 +1,105 @@ +package com.tarento.analytics.dto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.tarento.analytics.enums.ChartType; + +/** + * @author Darshan Nagesh + * + */ +public class AggregateDto { + + private ChartType chartType; + + private String visualizationCode; + + private String chartFormat; + + private String drillDownChartId; + + private Object filterKeys; + + public String getVisualizationCode() { + return visualizationCode; + } + + public Object getFilterKeys() { + return filterKeys; + } + + public void setFilterKeys(Object filterKeys) { + this.filterKeys = filterKeys; + } + + public void setVisualizationCode(String visualizationCode) { + this.visualizationCode = visualizationCode; + } + + public String getDrillDownChartId() { + return drillDownChartId; + } + + public void setDrillDownChartId(String drillDownChartId) { + this.drillDownChartId = drillDownChartId; + } + + private Map<String, Object> customData; + + private RequestDate dates; + + private Object filter; + + private List<Data> data = new ArrayList<>(); + + public List<Data> getData() { + return data; + } + + public void setData(List<Data> data) { + this.data = data; + } + + public ChartType getChartType() { + return chartType; + } + + public void setChartType(ChartType chartType) { + this.chartType = chartType; + } + + public String getChartFormat() { + return chartFormat; + } + + public void setChartFormat(String chartFormat) { + this.chartFormat = chartFormat; + } + + + public Map<String, Object> getCustomData() { + return customData; + } + + public void setCustomData(Map<String, Object> customData) { + this.customData = customData; + } + + public RequestDate getDates() { + return dates; + } + + public void setDates(RequestDate dates) { + this.dates = dates; + } + + public Object getFilter() { + return filter; + } + + public void setFilter(Object filter) { + this.filter = filter; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/AggregateRequestDto.java b/src/main/java/com/tarento/analytics/dto/AggregateRequestDto.java new file mode 100644 index 0000000000000000000000000000000000000000..a674a390d9c7a5f48ba8c993b7fff4538fa8ce01 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/AggregateRequestDto.java @@ -0,0 +1,114 @@ +package com.tarento.analytics.dto; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +import java.util.Map; + +public class AggregateRequestDto { + + private String requestId; + private String dashboardId; + private String visualizationType; + private String visualizationCode; + private String moduleLevel; + private String queryType; + private Map<String, Object> filters; + private Map<String, Object> esFilters; + private Map<String, Object> aggregationFactors; + private RequestDate requestDate; + private String interval; + private ObjectNode chartNode; + + public AggregateRequestDto() {} + public AggregateRequestDto(AggregateRequestDtoV3 requestDtoV3, String visualizationType, String visualizationCode) { + this.visualizationCode = visualizationCode; + this.visualizationType = visualizationType; + this.moduleLevel = requestDtoV3.getModuleLevel(); + this.queryType = requestDtoV3.getQueryType(); + this.filters = requestDtoV3.getFilters(); + this.esFilters = requestDtoV3.getEsFilters(); + this.aggregationFactors = requestDtoV3.getAggregationFactors(); + this.requestDate = requestDtoV3.getRequestDate(); + this.interval = requestDtoV3.getInterval(); + this.chartNode = requestDtoV3.getChartNode(); + this.requestId= requestDtoV3.getRequestId(); + } + + public String getDashboardId() { + return dashboardId; + } + public void setDashboardId(String dashboardId) { + this.dashboardId = dashboardId; + } + public String getRequestId() { + return requestId; + } + public void setRequestId(String requestId) { + this.requestId = requestId; + } + public ObjectNode getChartNode() { + return chartNode; + } + + public void setChartNode(ObjectNode chartNode) { + this.chartNode = chartNode; + } + + public String getModuleLevel() { + return moduleLevel; + } + public void setModuleLevel(String moduleLevel) { + this.moduleLevel = moduleLevel; + } + public Map<String, Object> getEsFilters() { + return esFilters; + } + public void setEsFilters(Map<String, Object> esFilters) { + this.esFilters = esFilters; + } + public String getVisualizationCode() { + return visualizationCode; + } + public void setVisualizationCode(String visualizationCode) { + this.visualizationCode = visualizationCode; + } + public String getVisualizationType() { + return visualizationType; + } + public void setVisualizationType(String visualizationType) { + this.visualizationType = visualizationType; + } + public String getQueryType() { + return queryType; + } + public void setQueryType(String queryType) { + this.queryType = queryType; + } + public Map<String, Object> getFilters() { + return filters; + } + public void setFilters(Map<String, Object> filters) { + this.filters = filters; + } + public Map<String, Object> getAggregationFactors() { + return aggregationFactors; + } + public void setAggregationFactors(Map<String, Object> aggregationFactors) { + this.aggregationFactors = aggregationFactors; + } + public RequestDate getRequestDate() { + return requestDate; + } + public void setRequestDate(RequestDate requestDate) { + this.requestDate = requestDate; + } + public String getInterval() { + return interval; + } + public void setInterval(String interval) { + this.interval = interval; + } + + + +} diff --git a/src/main/java/com/tarento/analytics/dto/AggregateRequestDtoV3.java b/src/main/java/com/tarento/analytics/dto/AggregateRequestDtoV3.java new file mode 100644 index 0000000000000000000000000000000000000000..38c7c73486cd83c88a96105e2656a743b5855d1b --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/AggregateRequestDtoV3.java @@ -0,0 +1,88 @@ +package com.tarento.analytics.dto; + +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class AggregateRequestDtoV3 { + + private String requestId; + private List<Visualization> visualizations; + private String moduleLevel; + private String queryType; + private Map<String, Object> filters; + private Map<String, Object> esFilters; + private Map<String, Object> aggregationFactors; + private RequestDate requestDate; + private String interval; + private ObjectNode chartNode; + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + public ObjectNode getChartNode() { + return chartNode; + } + + public void setChartNode(ObjectNode chartNode) { + this.chartNode = chartNode; + } + + public String getModuleLevel() { + return moduleLevel; + } + public void setModuleLevel(String moduleLevel) { + this.moduleLevel = moduleLevel; + } + public Map<String, Object> getEsFilters() { + return esFilters; + } + public void setEsFilters(Map<String, Object> esFilters) { + this.esFilters = esFilters; + } + public String getQueryType() { + return queryType; + } + public void setQueryType(String queryType) { + this.queryType = queryType; + } + public Map<String, Object> getFilters() { + return filters; + } + public void setFilters(Map<String, Object> filters) { + this.filters = filters; + } + public Map<String, Object> getAggregationFactors() { + return aggregationFactors; + } + public void setAggregationFactors(Map<String, Object> aggregationFactors) { + this.aggregationFactors = aggregationFactors; + } + public RequestDate getRequestDate() { + return requestDate; + } + public void setRequestDate(RequestDate requestDate) { + this.requestDate = requestDate; + } + public String getInterval() { + return interval; + } + public void setInterval(String interval) { + this.interval = interval; + } + + public List<Visualization> getVisualizations() { + return visualizations; + } + + public void setVisualizations(List<Visualization> visualizations) { + this.visualizations = visualizations; + } + +} diff --git a/src/main/java/com/tarento/analytics/dto/City.java b/src/main/java/com/tarento/analytics/dto/City.java new file mode 100644 index 0000000000000000000000000000000000000000..1e964e66c4ad5600cb40e5d48c4a374a32ed0a05 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/City.java @@ -0,0 +1,186 @@ +package com.tarento.analytics.dto; + +import java.util.HashMap; +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ "name", "localName", "districtCode", "districtName", "regionName", "ulbGrade", "longitude", + "latitude", "shapeFileLocation", "captcha", "code", "regionCode", "municipalityName" }) +public class City { + + @JsonProperty("name") + private String name; + @JsonProperty("localName") + private String localName; + @JsonProperty("districtCode") + private String districtCode; + @JsonProperty("districtName") + private String districtName; + @JsonProperty("regionName") + private String regionName; + @JsonProperty("ulbGrade") + private String ulbGrade; + @JsonProperty("longitude") + private Double longitude; + @JsonProperty("latitude") + private Double latitude; + @JsonProperty("shapeFileLocation") + private Object shapeFileLocation; + @JsonProperty("captcha") + private Object captcha; + @JsonProperty("code") + private String code; + @JsonProperty("regionCode") + private String regionCode; + @JsonProperty("municipalityName") + private String municipalityName; + @JsonIgnore + private Map<String, Object> additionalProperties = new HashMap<>(); + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + @JsonProperty("localName") + public String getLocalName() { + return localName; + } + + @JsonProperty("localName") + public void setLocalName(String localName) { + this.localName = localName; + } + + @JsonProperty("districtCode") + public String getDistrictCode() { + return districtCode; + } + + @JsonProperty("districtCode") + public void setDistrictCode(String districtCode) { + this.districtCode = districtCode; + } + + @JsonProperty("districtName") + public String getDistrictName() { + return districtName; + } + + @JsonProperty("districtName") + public void setDistrictName(String districtName) { + this.districtName = districtName; + } + + @JsonProperty("regionName") + public String getRegionName() { + return regionName; + } + + @JsonProperty("regionName") + public void setRegionName(String regionName) { + this.regionName = regionName; + } + + @JsonProperty("ulbGrade") + public String getUlbGrade() { + return ulbGrade; + } + + @JsonProperty("ulbGrade") + public void setUlbGrade(String ulbGrade) { + this.ulbGrade = ulbGrade; + } + + @JsonProperty("longitude") + public Double getLongitude() { + return longitude; + } + + @JsonProperty("longitude") + public void setLongitude(Double longitude) { + this.longitude = longitude; + } + + @JsonProperty("latitude") + public Double getLatitude() { + return latitude; + } + + @JsonProperty("latitude") + public void setLatitude(Double latitude) { + this.latitude = latitude; + } + + @JsonProperty("shapeFileLocation") + public Object getShapeFileLocation() { + return shapeFileLocation; + } + + @JsonProperty("shapeFileLocation") + public void setShapeFileLocation(Object shapeFileLocation) { + this.shapeFileLocation = shapeFileLocation; + } + + @JsonProperty("captcha") + public Object getCaptcha() { + return captcha; + } + + @JsonProperty("captcha") + public void setCaptcha(Object captcha) { + this.captcha = captcha; + } + + @JsonProperty("code") + public String getCode() { + return code; + } + + @JsonProperty("code") + public void setCode(String code) { + this.code = code; + } + + @JsonProperty("regionCode") + public String getRegionCode() { + return regionCode; + } + + @JsonProperty("regionCode") + public void setRegionCode(String regionCode) { + this.regionCode = regionCode; + } + + @JsonProperty("municipalityName") + public String getMunicipalityName() { + return municipalityName; + } + + @JsonProperty("municipalityName") + public void setMunicipalityName(String municipalityName) { + this.municipalityName = municipalityName; + } + + @JsonAnyGetter + public Map<String, Object> getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/dto/CummulativeDataRequestDto.java b/src/main/java/com/tarento/analytics/dto/CummulativeDataRequestDto.java new file mode 100644 index 0000000000000000000000000000000000000000..f4adc1dfc0551358e041a0fd16f5afc5a2d41853 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/CummulativeDataRequestDto.java @@ -0,0 +1,31 @@ +package com.tarento.analytics.dto; + +import java.util.Map; + +public class CummulativeDataRequestDto { + private Map<String, Object> customData; + private RequestDate dates; + private String dashCode; + + + + public String getDashCode() { + return dashCode; + } + public void setDashCode(String dashCode) { + this.dashCode = dashCode; + } + public Map<String, Object> getCustomData() { + return customData; + } + public void setCustomData(Map<String, Object> customData) { + this.customData = customData; + } + public RequestDate getDates() { + return dates; + } + public void setDates(RequestDate dates) { + this.dates = dates; + } + +} diff --git a/src/main/java/com/tarento/analytics/dto/DashboardDto.java b/src/main/java/com/tarento/analytics/dto/DashboardDto.java new file mode 100644 index 0000000000000000000000000000000000000000..1ff82a8da07027249879c913382f5d7ca69d5f7f --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/DashboardDto.java @@ -0,0 +1,44 @@ +package com.tarento.analytics.dto; + +public class DashboardDto { + + private Long id; + private String name; + private String code; + private String description; + private String placement; + + public String getPlacement() { + return placement; + } + public void setPlacement(String placement) { + this.placement = placement; + } + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getCode() { + return code; + } + public void setCode(String code) { + this.code = code; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + + + +} diff --git a/src/main/java/com/tarento/analytics/dto/DashboardHeaderDto.java b/src/main/java/com/tarento/analytics/dto/DashboardHeaderDto.java new file mode 100644 index 0000000000000000000000000000000000000000..33db8b1a2629b93a84856caa4b145148f26d9a6a --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/DashboardHeaderDto.java @@ -0,0 +1,28 @@ +package com.tarento.analytics.dto; + +import com.tarento.analytics.model.LineData; + +public class DashboardHeaderDto { + + private String type; + private Object data; + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public LineData getLineData() { + return lineData; + } + public void setLineData(LineData lineData) { + this.lineData = lineData; + } + private LineData lineData; +} diff --git a/src/main/java/com/tarento/analytics/dto/Data.java b/src/main/java/com/tarento/analytics/dto/Data.java new file mode 100644 index 0000000000000000000000000000000000000000..22ccf2df775bcb711715dcba063cfed6bae4d43f --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/Data.java @@ -0,0 +1,101 @@ +package com.tarento.analytics.dto; + +import java.util.ArrayList; +import java.util.List; + +public class Data { + + private String headerName; + private Object headerValue; + private String headerSymbol; + private String colorPaletteCode; + private Long colorPaletteId; + + public Data() {} + + public Data(String name, Object value, String symbol) { + this.headerName = name; + this.headerValue = value; + this.headerSymbol = symbol; + } + + public Data(String name, Object value, String symbol, List<Plot> plots) { + this.headerName = name; + this.headerValue = value; + this.headerSymbol = symbol; + this.plots = plots; + } + + public String getColorPaletteCode() { + return colorPaletteCode; + } + + public void setColorPaletteCode(String colorPaletteCode) { + this.colorPaletteCode = colorPaletteCode; + } + + public Long getColorPaletteId() { + return colorPaletteId; + } + + public void setColorPaletteId(Long colorPaletteId) { + this.colorPaletteId = colorPaletteId; + } + + + + private List<Plot> plots = new ArrayList<>(); + private InsightsWidget insight; + private Boolean isDecimal; + + public Boolean getIsDecimal() { + return isDecimal; + } + + public void setIsDecimal(Boolean isDecimal) { + this.isDecimal = isDecimal; + } + + public InsightsWidget getInsight() { + return insight; + } + + public void setInsight(InsightsWidget insight) { + this.insight = insight; + } + + public void setHeaderName(String headerName) { + this.headerName = headerName; + } + + public void setHeaderSymbol(String headerSymbol) { + this.headerSymbol = headerSymbol; + } + + public List<Plot> getPlots() { + return plots; + } + + public void setPlots(List<Plot> plots) { + this.plots = plots; + } + + public void setHeaderValue(Object headerValue) { + this.headerValue = headerValue; + } + + public String getHeaderName() { + return headerName; + } + + public Object getHeaderValue() { + return headerValue; + } + + public String getHeaderSymbol() { + return headerSymbol; + } + + + +} diff --git a/src/main/java/com/tarento/analytics/dto/InsightsWidget.java b/src/main/java/com/tarento/analytics/dto/InsightsWidget.java new file mode 100644 index 0000000000000000000000000000000000000000..0eed70f97faf87f9da4ec638028f3c0e70cd55f3 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/InsightsWidget.java @@ -0,0 +1,44 @@ +package com.tarento.analytics.dto; + +public class InsightsWidget { + + private String name; + private Object value; + private String indicator; + private String colorCode; + + public InsightsWidget() {} + public InsightsWidget(String name, Object value, String indicator, String colorCode) { + this.name = name; + this.value = value; + this.indicator = indicator; + this.colorCode = colorCode; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public Object getValue() { + return value; + } + public void setValue(Object value) { + this.value = value; + } + public String getIndicator() { + return indicator; + } + public void setIndicator(String indicator) { + this.indicator = indicator; + } + public String getColorCode() { + return colorCode; + } + public void setColorCode(String colorCode) { + this.colorCode = colorCode; + } + + + +} diff --git a/src/main/java/com/tarento/analytics/dto/MappingDto.java b/src/main/java/com/tarento/analytics/dto/MappingDto.java new file mode 100644 index 0000000000000000000000000000000000000000..1bb98d2d1e542fe34ae3da13e75d82f0446c406b --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/MappingDto.java @@ -0,0 +1,14 @@ +package com.tarento.analytics.dto; + +public class MappingDto { + + private Object mappings; + + public Object getMappings() { + return mappings; + } + + public void setMappings(Object mappings) { + this.mappings = mappings; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/Plot.java b/src/main/java/com/tarento/analytics/dto/Plot.java new file mode 100644 index 0000000000000000000000000000000000000000..f91610ebccbe789ba36647a4b8e83e6ad98f1678 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/Plot.java @@ -0,0 +1,106 @@ +package com.tarento.analytics.dto; + +public class Plot { + + private Object label; + private String name; + private Object value; + private String valueLabel; + private String symbol; + private String parentName; + private String parentLabel; + + public Plot() {} + + public String getParentLabel() { + return parentLabel; + } + + + + public void setParentLabel(String parentLabel) { + this.parentLabel = parentLabel; + } + + + + public Plot(String name, Object value, String symbol) { + this.name = name; + this.value = value; + this.symbol = symbol; + } + + public Plot(String name, Object value, String symbol, String headerLabel, String valueLabel) { + this.name = name; + this.value = value; + this.symbol = symbol; + this.label = headerLabel; + this.valueLabel = valueLabel; + } + + public Plot(String name, Object value, String symbol, String parentName, String headerLabel, String valueLabel) { + this.name = name; + this.value = value; + this.symbol = symbol; + this.parentName = parentName; + this.label = headerLabel; + this.valueLabel = valueLabel; + } + + public Plot(String name, Object value, String symbol, String parentName, String headerLabel, String valueLabel, String parentLabel) { + this.name = name; + this.value = value; + this.symbol = symbol; + this.parentName = parentName; + this.label = headerLabel; + this.valueLabel = valueLabel; + this.parentLabel = parentLabel; + } + + public String getValueLabel() { + return valueLabel; + } + + public void setValueLabel(String valueLabel) { + this.valueLabel = valueLabel; + } + + public String getParentName() { + return parentName; + } + + public void setParentName(String parentName) { + this.parentName = parentName; + } + + public void setName(String name) { + this.name = name; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getName() { + return name; + } + + public Object getValue() { + return value; + } + + public String getSymbol() { + return symbol; + } + + public Object getLabel() { + return label; + } + + public void setLabel(Object label) { + this.label = label; + } + public void setValue(Object value) { + this.value = value; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/RequestDate.java b/src/main/java/com/tarento/analytics/dto/RequestDate.java new file mode 100644 index 0000000000000000000000000000000000000000..86effd6ca9410835168ccff18401ed15225248ac --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/RequestDate.java @@ -0,0 +1,36 @@ +package com.tarento.analytics.dto; + +public class RequestDate { + private String targetDate; + private String startDate; + private String endDate; + private String interval; + + public String getInterval() { + return interval; + } + + public void setInterval(String interval) { + this.interval = interval; + } + + public String getTargetDate() { + return targetDate; + } + public void setTargetDate(String targetDate) { + this.targetDate = targetDate; + } + public String getStartDate() { + return startDate; + } + public void setStartDate(String startDate) { + this.startDate = startDate; + } + public String getEndDate() { + return endDate; + } + public void setEndDate(String endDate) { + this.endDate = endDate; + } + +} diff --git a/src/main/java/com/tarento/analytics/dto/RequestDto.java b/src/main/java/com/tarento/analytics/dto/RequestDto.java new file mode 100644 index 0000000000000000000000000000000000000000..02e4b86f2a63563ebea8a6a0a5d9b49f3457507e --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/RequestDto.java @@ -0,0 +1,22 @@ +package com.tarento.analytics.dto; + +import java.util.Map; + +public class RequestDto { + + private Map<String, Object> headers; + private AggregateRequestDto aggregationRequestDto; + + public Map<String, Object> getHeaders() { + return headers; + } + public void setHeaders(Map<String, Object> headers) { + this.headers = headers; + } + public AggregateRequestDto getAggregationRequestDto() { + return aggregationRequestDto; + } + public void setAggregationRequestDto(AggregateRequestDto aggregationRequestDto) { + this.aggregationRequestDto = aggregationRequestDto; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/RequestDtoV3.java b/src/main/java/com/tarento/analytics/dto/RequestDtoV3.java new file mode 100644 index 0000000000000000000000000000000000000000..f152384027ac192b13e1187d681fe5182c7fe622 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/RequestDtoV3.java @@ -0,0 +1,22 @@ +package com.tarento.analytics.dto; + +import java.util.Map; + +public class RequestDtoV3 { + + private Map<String, Object> headers; + private AggregateRequestDtoV3 aggregationRequestDto; + + public Map<String, Object> getHeaders() { + return headers; + } + public void setHeaders(Map<String, Object> headers) { + this.headers = headers; + } + public AggregateRequestDtoV3 getAggregationRequestDto() { + return aggregationRequestDto; + } + public void setAggregationRequestDto(AggregateRequestDtoV3 aggregationRequestDto) { + this.aggregationRequestDto = aggregationRequestDto; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/ResponseDto.java b/src/main/java/com/tarento/analytics/dto/ResponseDto.java new file mode 100644 index 0000000000000000000000000000000000000000..e52d4732cb45065eb0add38fbbf749f351cae05d --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/ResponseDto.java @@ -0,0 +1,38 @@ +package com.tarento.analytics.dto; + +public class ResponseDto { + + private int statusCode; + private String statusMessage; + private Object response; + + public ResponseDto(int statusCode, String statusMessage, Object response) { + this.statusCode = statusCode; + this.statusMessage = statusMessage; + this.response = response; + } + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + public String getStatusMessage() { + return statusMessage; + } + + public void setStatusMessage(String statusMessage) { + this.statusMessage = statusMessage; + } + + public Object getResponse() { + return response; + } + + public void setResponse(Object response) { + this.response = response; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/RichPlot.java b/src/main/java/com/tarento/analytics/dto/RichPlot.java new file mode 100644 index 0000000000000000000000000000000000000000..a244f940097e09691c1e86bec44a8b541630a949 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/RichPlot.java @@ -0,0 +1,62 @@ +package com.tarento.analytics.dto; + +public class RichPlot extends Plot { + + public RichPlot(String name, Double value, String symbol) { + super(name, value, symbol); + } + + public RichPlot() {} + private Object xAxis; + private String xAxisLabel; + private Object yAxis; + private String yAxisLabel; + private Object zAxis; + private String zAxisLabel; + + public Object getzAxis() { + return zAxis; + } + + public void setzAxis(Object zAxis) { + this.zAxis = zAxis; + } + + public String getzAxisLabel() { + return zAxisLabel; + } + + public void setzAxisLabel(String zAxisLabel) { + this.zAxisLabel = zAxisLabel; + } + + public String getxAxisLabel() { + return xAxisLabel; + } + + public void setxAxisLabel(String xAxisLabel) { + this.xAxisLabel = xAxisLabel; + } + + public String getyAxisLabel() { + return yAxisLabel; + } + + public void setyAxisLabel(String yAxisLabel) { + this.yAxisLabel = yAxisLabel; + } + + public Object getxAxis() { + return xAxis; + } + public void setxAxis(Object xAxis) { + this.xAxis = xAxis; + } + public Object getyAxis() { + return yAxis; + } + public void setyAxis(Object yAxis) { + this.yAxis = yAxis; + } +} + diff --git a/src/main/java/com/tarento/analytics/dto/RoleDto.java b/src/main/java/com/tarento/analytics/dto/RoleDto.java new file mode 100644 index 0000000000000000000000000000000000000000..dedbdc9d21df8a9a9879941bf6331425485ecadf --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/RoleDto.java @@ -0,0 +1,46 @@ +package com.tarento.analytics.dto; + +public class RoleDto { + + private Long id; + + private String description; + + private String name; + + private String orgId; + + private boolean isSuperAdmin; + + public String getOrgId() { + return orgId; + } + + public void setOrgId(String orgId) { + this.orgId = orgId; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/com/tarento/analytics/dto/SearchDto.java b/src/main/java/com/tarento/analytics/dto/SearchDto.java new file mode 100644 index 0000000000000000000000000000000000000000..14687f398b70863910160c1d5ac50af20d310d1a --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/SearchDto.java @@ -0,0 +1,165 @@ +package com.tarento.analytics.dto; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This class will take input for elastic search query + * + * @author + */ +public class SearchDto { + + @SuppressWarnings("rawtypes") + private List<Map> properties; + + private List<Map<String, String>> facets = new ArrayList<>(); + private List<String> fields; + private List<String> excludedFields; + private Map<String, String> sortBy = new HashMap<>(); + private String operation; + private String query; + private Integer limit = 250; + private Integer offset = 0; + private boolean fuzzySearch = false; + // additional properties will hold , filters, exist , not exist + private Map<String, Object> additionalProperties = new HashMap<>(); + private Map<String, Integer> softConstraints = new HashMap<>(); + private List<Map<String, Object>> groupQuery = new ArrayList<>(); + private List<String> mode = new ArrayList<>(); + + public List<Map<String, Object>> getGroupQuery() { + return groupQuery; + } + + public void setGroupQuery(List<Map<String, Object>> groupQuery) { + this.groupQuery = groupQuery; + } + + public SearchDto() { + super(); + } + + @SuppressWarnings("rawtypes") + public SearchDto(List<Map> properties, String operation, int limit) { + super(); + this.properties = properties; + this.operation = operation; + this.limit = limit; + } + + @SuppressWarnings("rawtypes") + public List<Map> getProperties() { + return properties; + } + + @SuppressWarnings("rawtypes") + public void setProperties(List<Map> properties) { + this.properties = properties; + } + + public String getOperation() { + return operation; + } + + public void setOperation(String operation) { + this.operation = operation; + } + + public Integer getLimit() { + return limit; + } + + public void setLimit(Integer limit) { + this.limit = limit; + } + + public List<Map<String, String>> getFacets() { + return facets; + } + + public void setFacets(List<Map<String, String>> facets) { + this.facets = facets; + } + + public Map<String, String> getSortBy() { + return sortBy; + } + + public void setSortBy(Map<String, String> sortBy) { + this.sortBy = sortBy; + } + + public boolean isFuzzySearch() { + return fuzzySearch; + } + + public void setFuzzySearch(boolean fuzzySearch) { + this.fuzzySearch = fuzzySearch; + } + + public Map<String, Object> getAdditionalProperties() { + return additionalProperties; + } + + public void setAdditionalProperties(Map<String, Object> additionalProperties) { + this.additionalProperties = additionalProperties; + } + + public Object getAdditionalProperty(String key) { + return additionalProperties.get(key); + } + + public void addAdditionalProperty(String key, Object value) { + this.additionalProperties.put(key, value); + } + + public List<String> getFields() { + return fields; + } + + public void setFields(List<String> fields) { + this.fields = fields; + } + + public Integer getOffset() { + return offset; + } + + public void setOffset(Integer offset) { + this.offset = offset; + } + + public Map<String, Integer> getSoftConstraints() { + return softConstraints; + } + + public void setSoftConstraints(Map<String, Integer> softConstraints) { + this.softConstraints = softConstraints; + } + + public String getQuery() { + return query; + } + + public void setQuery(String query) { + this.query = query; + } + + public List<String> getMode() { + return mode; + } + + public void setMode(List<String> mode) { + this.mode = mode; + } + + public List<String> getExcludedFields() { + return excludedFields; + } + + public void setExcludedFields(List<String> excludedFields) { + this.excludedFields = excludedFields; + } +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/dto/Tenant.java b/src/main/java/com/tarento/analytics/dto/Tenant.java new file mode 100644 index 0000000000000000000000000000000000000000..c50aa7e1c5a214e34fd22bfa1d3e35631efadb0f --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/Tenant.java @@ -0,0 +1,206 @@ +package com.tarento.analytics.dto; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ "code", "name", "description", "logoId", "imageId", "domainUrl", "type", "twitterUrl", + "facebookUrl", "emailId", "OfficeTimings", "city", "address", "contactNumber" }) +public class Tenant { + + @JsonProperty("code") + private String code; + @JsonProperty("name") + private String name; + @JsonProperty("description") + private String description; + @JsonProperty("logoId") + private String logoId; + @JsonProperty("imageId") + private Object imageId; + @JsonProperty("domainUrl") + private String domainUrl; + @JsonProperty("type") + private String type; + @JsonProperty("twitterUrl") + private Object twitterUrl; + @JsonProperty("facebookUrl") + private Object facebookUrl; + @JsonProperty("emailId") + private String emailId; + @JsonProperty("city") + private City city; + @JsonProperty("address") + private String address; + @JsonProperty("contactNumber") + private String contactNumber; + @JsonIgnore + private Map<String, Object> additionalProperties = new HashMap<>(); + + @JsonProperty("code") + public String getCode() { + return code; + } + + @JsonProperty("code") + public void setCode(String code) { + this.code = code; + } + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + @JsonProperty("description") + public String getDescription() { + return description; + } + + @JsonProperty("description") + public void setDescription(String description) { + this.description = description; + } + + @JsonProperty("logoId") + public String getLogoId() { + return logoId; + } + + @JsonProperty("logoId") + public void setLogoId(String logoId) { + this.logoId = logoId; + } + + @JsonProperty("imageId") + public Object getImageId() { + return imageId; + } + + @JsonProperty("imageId") + public void setImageId(Object imageId) { + this.imageId = imageId; + } + + @JsonProperty("domainUrl") + public String getDomainUrl() { + return domainUrl; + } + + @JsonProperty("domainUrl") + public void setDomainUrl(String domainUrl) { + this.domainUrl = domainUrl; + } + + @JsonProperty("type") + public String getType() { + return type; + } + + @JsonProperty("type") + public void setType(String type) { + this.type = type; + } + + @JsonProperty("twitterUrl") + public Object getTwitterUrl() { + return twitterUrl; + } + + @JsonProperty("twitterUrl") + public void setTwitterUrl(Object twitterUrl) { + this.twitterUrl = twitterUrl; + } + + @JsonProperty("facebookUrl") + public Object getFacebookUrl() { + return facebookUrl; + } + + @JsonProperty("facebookUrl") + public void setFacebookUrl(Object facebookUrl) { + this.facebookUrl = facebookUrl; + } + + @JsonProperty("emailId") + public String getEmailId() { + return emailId; + } + + @JsonProperty("emailId") + public void setEmailId(String emailId) { + this.emailId = emailId; + } + + @JsonProperty("city") + public City getCity() { + return city; + } + + @JsonProperty("city") + public void setCity(City city) { + this.city = city; + } + + @JsonProperty("address") + public String getAddress() { + return address; + } + + @JsonProperty("address") + public void setAddress(String address) { + this.address = address; + } + + @JsonProperty("contactNumber") + public String getContactNumber() { + return contactNumber; + } + + @JsonProperty("contactNumber") + public void setContactNumber(String contactNumber) { + this.contactNumber = contactNumber; + } + + @JsonAnyGetter + public Map<String, Object> getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + return "Tenant [code=" + code + ", name=" + name + ", description=" + description + ", logoId=" + logoId + + ", imageId=" + imageId + ", domainUrl=" + domainUrl + ", type=" + type + ", twitterUrl=" + twitterUrl + + ", facebookUrl=" + facebookUrl + ", emailId=" + emailId + + ", city=" + city + ", address=" + address + ", contactNumber=" + contactNumber + + ", additionalProperties=" + additionalProperties + ", getCode()=" + getCode() + ", getName()=" + + getName() + ", getDescription()=" + getDescription() + ", getLogoId()=" + getLogoId() + + ", getImageId()=" + getImageId() + ", getDomainUrl()=" + getDomainUrl() + ", getType()=" + getType() + + ", getTwitterUrl()=" + getTwitterUrl() + ", getFacebookUrl()=" + getFacebookUrl() + ", getEmailId()=" + + getEmailId() + ", getCity()=" + getCity() + + ", getAddress()=" + getAddress() + ", getContactNumber()=" + getContactNumber() + + ", getAdditionalProperties()=" + getAdditionalProperties() + ", getClass()=" + getClass() + + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; + } + + + + +} diff --git a/src/main/java/com/tarento/analytics/dto/Tenants.java b/src/main/java/com/tarento/analytics/dto/Tenants.java new file mode 100644 index 0000000000000000000000000000000000000000..7b2ba3fbe53e76f7daa3d5bef071e5958760f63d --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/Tenants.java @@ -0,0 +1,66 @@ +package com.tarento.analytics.dto; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ "tenantId", "moduleName", "tenants" }) +public class Tenants { + + @JsonProperty("tenantId") + private String tenantId; + @JsonProperty("moduleName") + private String moduleName; + @JsonProperty("tenants") + private List<Tenant> tenants = null; + @JsonIgnore + private Map<String, Object> additionalProperties = new HashMap<>(); + + @JsonProperty("tenantId") + public String getTenantId() { + return tenantId; + } + + @JsonProperty("tenantId") + public void setTenantId(String tenantId) { + this.tenantId = tenantId; + } + + @JsonProperty("moduleName") + public String getModuleName() { + return moduleName; + } + + @JsonProperty("moduleName") + public void setModuleName(String moduleName) { + this.moduleName = moduleName; + } + + @JsonProperty("tenants") + public List<Tenant> getTenants() { + return tenants; + } + + @JsonProperty("tenants") + public void setTenants(List<Tenant> tenants) { + this.tenants = tenants; + } + + @JsonAnyGetter + public Map<String, Object> getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/dto/UserDto.java b/src/main/java/com/tarento/analytics/dto/UserDto.java new file mode 100644 index 0000000000000000000000000000000000000000..e548e8b4caae541ba2451d5998c90c96af45d262 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/UserDto.java @@ -0,0 +1,69 @@ +package com.tarento.analytics.dto; + +import java.util.List; + +public class UserDto { + + private long id; + + private String userName; + + private String emailId; + + private List<RoleDto> roles; + + private String orgId; + + private String countryCode; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getEmailId() { + return emailId; + } + + public void setEmailId(String emailId) { + this.emailId = emailId; + } + + public List<RoleDto> getRoles() { + return roles; + } + + public void setRoles(List<RoleDto> roles) { + this.roles = roles; + } + + public String getOrgId() { + return orgId; + } + + public void setOrgId(String orgId) { + this.orgId = orgId; + } + + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + + + +} diff --git a/src/main/java/com/tarento/analytics/dto/Visualization.java b/src/main/java/com/tarento/analytics/dto/Visualization.java new file mode 100644 index 0000000000000000000000000000000000000000..41a9cd6544a29de67cbd2012aef64f6e219f4328 --- /dev/null +++ b/src/main/java/com/tarento/analytics/dto/Visualization.java @@ -0,0 +1,22 @@ +package com.tarento.analytics.dto; + +public class Visualization { + + private String type; + private String code; + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getCode() { + return code; + } + public void setCode(String code) { + this.code = code; + } + + + +} diff --git a/src/main/java/com/tarento/analytics/enums/AlwaysView.java b/src/main/java/com/tarento/analytics/enums/AlwaysView.java new file mode 100644 index 0000000000000000000000000000000000000000..b84240cfae2272b2fb73799eb84e83384f93e330 --- /dev/null +++ b/src/main/java/com/tarento/analytics/enums/AlwaysView.java @@ -0,0 +1,28 @@ +package com.tarento.analytics.enums; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +public enum AlwaysView { + MONTHWISE("monthwise"); + + private String value; + + AlwaysView(final String value) { + this.value = value; + } + + @JsonValue + @Override + public String toString() { + return value; + } + + @JsonCreator + public static AlwaysView fromValue(final String passedValue) { + for (final AlwaysView obj : AlwaysView.values()) + if (String.valueOf(obj.value).equalsIgnoreCase(passedValue)) + return obj; + return null; + } +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/enums/ChartType.java b/src/main/java/com/tarento/analytics/enums/ChartType.java new file mode 100644 index 0000000000000000000000000000000000000000..3724df80f9711c92cfde704b2d6fecea991e2620 --- /dev/null +++ b/src/main/java/com/tarento/analytics/enums/ChartType.java @@ -0,0 +1,34 @@ +package com.tarento.analytics.enums; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +public enum ChartType { + + XTABLE("xtable"), TABLE("table"), PERFORM("perform"), METRIC("metric"), PIE("pie"), LINE("line"), BAR( + "bar"), MULTIBAR("multibar"), STACKEDBAR("stackedbar"), DONUT("donut"), HORIZONTALBAR( + "horizontalbar"), POLARAREA("polararea"), METRICCOLLECTION("metriccollection"), TREEMAP( + "treemap"), BUBBLECHART("bubblechart"), INDIAMAP("indiamap"), INDIADISTRICTMAP( + "indiadistrictmap"), REPORT("report"), DYNAMICTABLE( + "dynamictable"), LINE_BAR("line_bar"), CALENDARHEATMAP("calendarheatmap"); + + private String value; + + ChartType(final String value) { + this.value = value; + } + + @JsonValue + @Override + public String toString() { + return value; + } + + @JsonCreator + public static ChartType fromValue(final String passedValue) { + for (final ChartType obj : ChartType.values()) + if (String.valueOf(obj.value).equalsIgnoreCase(passedValue)) + return obj; + return null; + } +} diff --git a/src/main/java/com/tarento/analytics/exception/AINException.java b/src/main/java/com/tarento/analytics/exception/AINException.java new file mode 100644 index 0000000000000000000000000000000000000000..3aaf26a16b6282aae919a0df45c9a4d68e6ec405 --- /dev/null +++ b/src/main/java/com/tarento/analytics/exception/AINException.java @@ -0,0 +1,24 @@ +package com.tarento.analytics.exception; + +public class AINException extends Exception { + + private static final long serialVersionUID = 1L; + + String errorCode; + + String errorMessage; + + public AINException(String errorCode, String errorMessage) { + this.errorCode = errorCode; + this.errorMessage = errorMessage; + } + + public String getErrorCode() { + return errorCode; + } + + public String getErrorMessage() { + return errorMessage; + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/AdvanceTableChartResponseHandler.java b/src/main/java/com/tarento/analytics/handler/AdvanceTableChartResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..1112fed47d2c1e9ae3a36a0eaf6181fe60874988 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/AdvanceTableChartResponseHandler.java @@ -0,0 +1,227 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; +import com.tarento.analytics.helper.ComputedFieldFactory; +import com.tarento.analytics.helper.IComputedField; +import com.tarento.analytics.model.ComputedFields; + +/** + * This handles ES response for single index, multiple index to represent data + * as pie figure Creates plots by merging/computing(by summation) index values + * for same key AGGS_PATH : this defines the path/key to be used to search the + * tree VALUE_TYPE : defines the data type for the value formed, this could be + * amount, percentage, number + * + */ +@Component +public class AdvanceTableChartResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(AdvanceTableChartResponseHandler.class); + + @Autowired + private ObjectMapper mapper; + + @Autowired + private ComputedFieldFactory computedFieldFactory; + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) + throws IOException { + + // String input = "{\"aggregations\":{\"AGGR\":{\"doc_count\":33,\"ULBs + // \":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"pb.zirakpur\",\"doc_count\":14,\"Transactions\":{\"value\":14},\"Total + // Licence Issued\":{\"value\":0},\"Total + // Collection\":{\"value\":14110}},{\"key\":\"pb.derabassi\",\"doc_count\":9,\"Transactions\":{\"value\":9},\"Total + // Licence Issued\":{\"value\":0},\"Total + // Collection\":{\"value\":4000}},{\"key\":\"pb.mohali\",\"doc_count\":6,\"Transactions\":{\"value\":6},\"Total + // Licence Issued\":{\"value\":0},\"Total + // Collection\":{\"value\":6400}},{\"key\":\"pb.barnala\",\"doc_count\":1,\"Transactions\":{\"value\":1},\"Total + // Licence Issued\":{\"value\":0},\"Total + // Collection\":{\"value\":600}},{\"key\":\"pb.dharamkot\",\"doc_count\":1,\"Transactions\":{\"value\":1},\"Total + // Licence Issued\":{\"value\":0},\"Total + // Collection\":{\"value\":200}},{\"key\":\"pb.jalandhar\",\"doc_count\":1,\"Transactions\":{\"value\":1},\"Total + // Licence Issued\":{\"value\":0},\"Total + // Collection\":{\"value\":650}},{\"key\":\"pb.ludhiana\",\"doc_count\":1,\"Transactions\":{\"value\":1},\"Total + // Licence Issued\":{\"value\":0},\"Total Collection\":{\"value\":500}}]}}}}"; + // aggregations = (ObjectNode) new ObjectMapper().readTree(input); + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + logger.info("aggregationNode --------------- {} ", aggregationNode); + JsonNode chartNode = requestDto.getChartNode(); + String plotLabel = chartNode.get(PLOT_LABEL).asText(); + JsonNode computedFields = chartNode.get("computedFields"); + JsonNode excludedFields = chartNode.get("excludedColumns"); + + boolean executeComputedFields = computedFields != null && computedFields.isArray(); + List<JsonNode> aggrNodes = aggregationNode.findValues(BUCKETS); + + int[] idx = { 1 }; + List<Data> dataList = new ArrayList<>(); + Map<String, Map<String, Plot>> mappings = new HashMap<>(); + + aggrNodes.stream().forEach(node -> { + logger.info("mdms node -> {} ", node); + + ArrayNode buckets = (ArrayNode) node; + buckets.forEach(bucket -> { + + logger.info("mdms bucket -> {} ", bucket); + Map<String, Plot> plotMap = new LinkedHashMap<>(); + String key = bucket.get(IResponseHandler.KEY).asText(); + logger.info("key===========: {} bucket.findValues(BUCKETS)========= {} ", key, + bucket.findValues(BUCKETS)); + + processNestedObjects(bucket, mappings, key, plotMap); + // removed boilerplated code + + if (!plotMap.isEmpty()) { + Map<String, Plot> plots = new LinkedHashMap<>(); + Plot sno = new Plot(SERIAL_NUMBER, null, TABLE_TEXT); + sno.setLabel(idx[0]++); + Plot plotkey = new Plot(plotLabel.isEmpty() ? TABLE_KEY : plotLabel, null, TABLE_TEXT); + plotkey.setLabel(key); + + plots.put(SERIAL_NUMBER, sno); + plots.put(plotLabel.isEmpty() ? TABLE_KEY : plotLabel, plotkey); + plots.putAll(plotMap); + mappings.put(key, plots); + + } + + }); + + }); + mappings.entrySet().stream().parallel().forEach(plotMap -> { + List<Plot> plotList = plotMap.getValue().values().stream().parallel().collect(Collectors.toList()); + // filter out data object with all zero data. + List<Plot> filterPlot = plotList.stream() + .filter(c -> (!c.getName().equalsIgnoreCase(SERIAL_NUMBER) + && !c.getName().equalsIgnoreCase(plotLabel) && (double) c.getValue() != 0.0)) + .collect(Collectors.toList()); + + if (!filterPlot.isEmpty()) { + Data data = new Data(plotMap.getKey(), + Integer.parseInt(String.valueOf(plotMap.getValue().get(SERIAL_NUMBER).getLabel())), null); + data.setPlots(plotList); + // + if (executeComputedFields) { + try { + List<String> list = mapper.readValue(excludedFields.toString(), + new TypeReference<List<String>>() { + }); + logger.info("excluded list {} ", list); + + List<ComputedFields> computedFieldsList = mapper.readValue(computedFields.toString(), + new TypeReference<List<ComputedFields>>() { + }); + computedFieldsList.forEach(cfs -> { + IComputedField computedFieldObject = computedFieldFactory.getInstance(cfs.getActionName()); + computedFieldObject.set(requestDto, cfs.getPostAggregationTheory()); + computedFieldObject.add(data, cfs.getFields(), cfs.getNewField()); + + }); + + List<Plot> removeplots = data.getPlots().stream().filter(c -> list.contains(c.getName())) + .collect(Collectors.toList()); + data.getPlots().removeAll(removeplots); + } catch (Exception e) { + logger.error("execution of computed field : {} ", e.getMessage()); + } + } + // exclude the fields no to be displayed + + dataList.add(data); + } + + }); + // dataList.sort((o1, o2) -> ((Integer) o1.getHeaderValue()).compareTo((Integer) + // o2.getHeaderValue())); + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + + } + + /** + * Preparing the plots + * + * @param bucketNode + * @param mappings + * @param key + * @param headerName + * @param plotMap + */ + private void process(JsonNode bucketNode, Map<String, Map<String, Plot>> mappings, String key, String headerName, + Map<String, Plot> plotMap) { + JsonNode valNode = bucketNode.findValue(VALUE) != null ? bucketNode.findValue(VALUE) + : bucketNode.findValue(DOC_COUNT); + Double value = valNode.isDouble() ? valNode.asDouble() : valNode.asInt(); + String dataType = valNode.isDouble() ? "amount" : "number"; // to move to config or constants + // String headerName = bucketNode.findValue(KEY).asText(); + Plot plot = new Plot(headerName, value, dataType); + + if (mappings.containsKey(key)) { + double newval = mappings.get(key).get(headerName) == null ? value + : ((double) mappings.get(key).get(headerName).getValue() + value); + plot.setValue(newval); + mappings.get(key).put(headerName, plot); + } else { + plotMap.put(headerName, plot); + } + } + + /** + * Recursively processing the nodes + * + * @param node + * @param mappings + * @param key + * @param plotMap + */ + private void processNestedObjects(JsonNode node, Map<String, Map<String, Plot>> mappings, String key, + Map<String, Plot> plotMap) { + + Iterator<String> fieldNames = node.fieldNames(); + while (fieldNames.hasNext()) { + String fieldName = fieldNames.next(); + if (node.get(fieldName).isArray()) { + logger.info("process array ===========: {} ", node.get(fieldName)); + ArrayNode bucketNodes = (ArrayNode) node.get(fieldName); + bucketNodes.forEach(bucketNode -> { + logger.info("2nd level bucket node = {} ", bucketNode); + process(bucketNode, mappings, key, bucketNode.findValue(KEY).asText(), plotMap); + }); + + } else if (node.get(fieldName).isObject() && node.get(fieldName).get(VALUE) != null) { + logger.info("process lowest object ===========: {} ", node.get(fieldName)); + logger.info("SEARCH - key value pairs: {} , {} ", fieldName, node.get(fieldName).get(VALUE)); + process(node.get(fieldName), mappings, key, fieldName, plotMap); + + } else { + processNestedObjects(node.get(fieldName), mappings, key, plotMap); + } + + } + + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/AdvancedMapResponseHandler.java b/src/main/java/com/tarento/analytics/handler/AdvancedMapResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..49d01aec239527f3ce6a6d49edab5c9467d2850c --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/AdvancedMapResponseHandler.java @@ -0,0 +1,79 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; + +/** + * This handles ES response for single index, multiple index to represent data as line chart + * Creates plots by merging/computing(by summation) index values for same key + * AGGS_PATH : this defines the path/key to be used to search the tree + * VALUE_TYPE : defines the data type for the value formed, this could be amount, percentage, number + * + */ +@Component +public class AdvancedMapResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(AdvancedMapResponseHandler.class); + + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) throws IOException { + + List<Data> dataList = new LinkedList<>(); + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + ArrayNode depthSteps = (ArrayNode) chartNode.get(IResponseHandler.DEPTH_STEPS); + + for(JsonNode headerPath : aggrsPaths) { + List<JsonNode> aggrNodes = aggregationNode.findValues(headerPath.asText()); + Map<String, Double> plotMap = new LinkedHashMap<>(); + List<Double> totalValues = new ArrayList<>(); + for(JsonNode aggrNode : aggrNodes) { + if (!aggrNode.findValues(IResponseHandler.BUCKETS).isEmpty()) { + ArrayNode buckets = (ArrayNode) aggrNode.findValues(IResponseHandler.BUCKETS).get(0); + String headerLabel = chartNode.get(HEADER_LABEL) != null ? chartNode.get(HEADER_LABEL).asText() : HEADER_LABEL_DEFAULT; + String valueLabel = chartNode.get(VALUE_LABEL) != null ? chartNode.get(VALUE_LABEL).asText() : VALUE_LABEL_DEFAULT; + for(JsonNode bucket : buckets) { + String bkey = bucket.findValue(IResponseHandler.KEY).asText(); + for(JsonNode eachDepthStep : depthSteps) { + JsonNode innerNode = bucket.findValue(eachDepthStep.asText()); + ArrayNode innerBuckets = (ArrayNode) innerNode.findValues(IResponseHandler.BUCKETS).get(0); + for(JsonNode innerBucket : innerBuckets) { + String innerBucketKey = innerBucket.findValue(IResponseHandler.KEY).asText(); + double value = (innerBucket.findValue(IResponseHandler.VALUE) != null) ? innerBucket.findValue(IResponseHandler.VALUE).asDouble():innerBucket.findValue(IResponseHandler.DOC_COUNT).asDouble(); + plotMap.put(innerBucketKey, value); + totalValues.add(value); + } + } + List<Plot> plots = plotMap.entrySet().stream().map(e -> new Plot(e.getKey(), e.getValue(), symbol, bkey, headerLabel, valueLabel)).collect(Collectors.toList()); + plotMap.clear(); + Data data = new Data(bkey, (totalValues==null || totalValues.isEmpty()) ? 0.0 : totalValues.stream().reduce(0.0, Double::sum), symbol); + data.setPlots(plots); + dataList.add(data); + } + } + } + } + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/DynamicTableResponseHandler.java b/src/main/java/com/tarento/analytics/handler/DynamicTableResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..2410ef70b992be24dd81d19538371cd68e89f04e --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/DynamicTableResponseHandler.java @@ -0,0 +1,137 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; +import com.tarento.analytics.helper.ComputedFieldHelper; +import com.tarento.analytics.utils.MapConstants; + +/** + * This handles ES response for single index, multiple index to compute + * performance Creates plots by performing ordered (ex: top n performance or + * last n performance) AGGS_PATH : configurable to this defines the path/key to + * be used to search the tree VALUE_TYPE : configurable to define the data type + * for the value formed, this could be amount, percentage, number PLOT_LABEL : + * configurable to define the label for the plot TYPE_MAPPING : defines for a + * plot data type + */ +@Component +public class DynamicTableResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(TableChartResponseHandler.class); + + @Autowired + ComputedFieldHelper computedFieldHelper; + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) + throws IOException { + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + + String plotLabel = chartNode.get(PLOT_LABEL).asText(); + ArrayNode pathDataTypeMap = (ArrayNode) chartNode.get(TYPE_MAPPING); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + Map<String, Map<String, Plot>> mappings = new HashMap<>(); + List<JsonNode> aggrNodes = aggregationNode.findValues(BUCKETS); + List<String> professionNames = new ArrayList<>(); + + aggrNodes.stream().forEach(node -> { + ArrayNode buckets = (ArrayNode) node; + buckets.forEach(bucket -> { + + String distName = bucket.findValue(IResponseHandler.KEY).asText(); + if (StringUtils.isNotBlank(distName)) { + aggrsPaths.forEach(aggrPath -> { + JsonNode datatype = pathDataTypeMap.findValue(aggrPath.asText()); + + ArrayNode innerBuckets = (ArrayNode) bucket.findValues(BUCKETS).get(0); + for (JsonNode eachInnerBucket : innerBuckets) { + String profName = eachInnerBucket.findValue(IResponseHandler.KEY).asText(); + if (!professionNames.contains(profName)) + professionNames.add(profName); + JsonNode valueNode = eachInnerBucket.findValue(aggrPath.asText()); + Double docValue = 0.0; + if (valueNode != null) + docValue = (null == valueNode.findValue(DOC_COUNT)) ? 0.0 + : valueNode.findValue(DOC_COUNT).asDouble(); + Double value = (null == valueNode || null == valueNode.findValue(VALUE)) ? docValue + : valueNode.findValue(VALUE).asDouble(); + Plot plot = new Plot(profName, value, datatype.asText()); + if (!mappings.containsKey(distName)) { + Map<String, Plot> plotMap = new LinkedHashMap<>(); + plotMap.put(profName, plot); + mappings.put(distName, plotMap); + } else { + mappings.get(distName).put(profName, plot); + } + } + }); + } + }); + }); + + professionNames.sort(Comparator.naturalOrder()); + List<Data> dataList = new ArrayList<>(); + int[] idx = { 1 }; + + Iterator<Entry<String, Map<String, Plot>>> mappingItr = mappings.entrySet().iterator(); + while (mappingItr.hasNext()) { + int slNumber = idx[0]++; + Entry<String, Map<String, Plot>> mappingEntry = mappingItr.next(); + String districtName = mappingEntry.getKey(); + Data data = new Data(mappingEntry.getKey(), slNumber, null); + List<Plot> plotList = new LinkedList<>(); + + Plot serialNumberPlot = new Plot(SERIAL_NUMBER, null, TABLE_TEXT); + serialNumberPlot.setLabel(slNumber); + plotList.add(serialNumberPlot); + + Plot keyPlot = new Plot(plotLabel.isEmpty() ? TABLE_KEY : plotLabel, null, TABLE_TEXT); + keyPlot.setLabel(districtName); + plotList.add(keyPlot); + + Plot statePlot = new Plot("State Name", null, TABLE_TEXT); + statePlot.setLabel(MapConstants.DISTRICT_STATE_MAP.get(districtName)); + statePlot.setValue(MapConstants.DISTRICT_STATE_MAP.get(districtName)); + statePlot.setValueLabel("State Name"); + plotList.add(statePlot); + + Map<String, Plot> innerPlotMap = mappingEntry.getValue(); + for (String eachProfessionName : professionNames) { + if (!innerPlotMap.containsKey(eachProfessionName)) { + Plot plot = new Plot(eachProfessionName, 0.0, "number"); + plotList.add(plot); + } else { + Plot eachPlotForProfession = innerPlotMap.get(eachProfessionName); + plotList.add(eachPlotForProfession); + } + } + data.setPlots(plotList); + dataList.add(data); + } + + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + } +} diff --git a/src/main/java/com/tarento/analytics/handler/IPostResponseHandler.java b/src/main/java/com/tarento/analytics/handler/IPostResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..57ceb2bf4cb30adc75f059627a63236b831b4363 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/IPostResponseHandler.java @@ -0,0 +1,15 @@ +package com.tarento.analytics.handler; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +public interface IPostResponseHandler { + + + /** + * Intecepts the response tree + * @param responseNode + */ + public void postResponse(ObjectNode responseNode); + + +} diff --git a/src/main/java/com/tarento/analytics/handler/IResponseHandler.java b/src/main/java/com/tarento/analytics/handler/IResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..d299aaa54afbaa7ade812ab68f1ab322226f4e71 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/IResponseHandler.java @@ -0,0 +1,224 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; +import com.tarento.analytics.enums.ChartType; + +/** + * Handles Elastic search consolidate responses + */ +public interface IResponseHandler { + + public static final Logger logger = LoggerFactory.getLogger(IResponseHandler.class); + + public static final String API_CONFIG_JSON = "ChartApiConfig.json"; + public static final String AGGS_PATH = "aggregationPaths"; + public static final String DEPTH_STEPS = "depthSteps"; + public static final String NEGATIVE_COUNT="Negative Count"; + public static final String CUMULATIVE_VALUE="Cumulative Value"; + + public static final String CHART_NAME = "chartName"; + public static final String CHART_TYPE = "chartType"; + public static final String DRILL_CHART = "drillChart"; + public static final String VALUE_TYPE = "valueType"; + public static final String FILTER_KEYS = "filterKeys"; + public static final String LABEL = "label"; + public static final String FILTER = "filter"; + + public static final String COLOR_PALETTE_CODE = "colorPaletteCode"; + public static final String COLOR_PALETTE_ID = "colorPaletteId"; + + public static final String XPATH = "xPath"; + public static final String YPATH = "yPath"; + public static final String ZPATH = "zPath"; + public static final String CURRENT_PATH = "CurrentPath"; + + // Table Chart Keys + public static final String SERIAL_NUMBER = "S.N."; + public static final String TABLE_TEXT = "text" ; + public static final String TABLE_KEY = "Key"; + + public static final String TOTAL_COLLECTION = "Total Collection"; + public static final String TARGET_COLLECTION = "Target Collection"; + public static final String TARGET_ACHIEVED = "Target Achievement"; + + public static final String PT_DDR_BOUNDARY = "demandCollectionIndexDDRRevenue"; + public static final String PT_BOUNDARY = "demandCollectionIndexBoundaryRevenue"; + public static final String PT_BOUNDARY_DRILL = "boundaryDrillDown"; + public static final String TL_DDR_BOUNDARY = "licenseIssuedDDRRevenue"; + public static final String TL_BOUNDARY = "licenseIssuedBoundaryRevenue"; + public static final String TL_BOUNDARY_DRILL = "licenseIssuedBoundaryDrillDown"; + + + public final String ASC = "asc"; + public final String DESC = "desc"; + public final String RANK = "Rank"; + public final String AGGREGATIONS = "aggregations"; + public final String PLOT_LABEL = "plotLabel"; + public final String LIMIT = "limit"; + public final String ORDER = "order"; + public final String ACTION = "action"; + public final String TYPE_MAPPING = "pathDataTypeMapping"; + public final String RESULT_TYPE = "resultType"; + + public static String BUCKETS = "buckets"; + public static String KEY = "key"; + public static String VALUE = "value"; + + public static String HEADER_LABEL = "headerLabel"; + public static String VALUE_LABEL = "valueLabel"; + public static String PARENT_LABEL = "parentLabel"; + public static String HEADER_LABEL_DEFAULT = "Header"; + public static String VALUE_LABEL_DEFAULT = "Value"; + public static String PARENT_LABEL_DEFAULT = "Parent"; + + + public final String PERCENTAGE = "percentage"; + public final String METRIC_GROUP = "metricgroup"; + public final String DOC_COUNT = "doc_count"; + + public final String IS_DECIMAL = "isDecimal"; + + public static final String POST_AGGREGATION_THEORY = "postAggregationTheory"; + + public static Double BOUNDARY_VALUE = 50.0; + + /** + * Translate the consolidated/aggregated response + * + * @param requestDto + * @param aggregations + * @return + * @throws IOException + */ + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) throws IOException; + + /** + * Prepare aggregated dato for a chart node + * + * @param chartNode + * @param dataList - data plots object + * @return + */ + default AggregateDto getAggregatedDto(JsonNode chartNode, List<Data> dataList, String visualizationCode) { + AggregateDto aggregateDto = new AggregateDto(); + if(chartNode.get(FILTER) != null && StringUtils.isNotBlank(chartNode.get(FILTER).asText())) { + aggregateDto.setFilter(chartNode.get(FILTER).asText()); + } + aggregateDto.setVisualizationCode(visualizationCode); + aggregateDto.setDrillDownChartId(chartNode.get(DRILL_CHART).asText()); + + ChartType chartType = ChartType.fromValue(chartNode.get(CHART_TYPE).asText()); + aggregateDto.setChartType(chartType); + aggregateDto.setData(dataList); + if(null!=chartNode.get(FILTER_KEYS)) + aggregateDto.setFilterKeys((ArrayNode) chartNode.get(FILTER_KEYS)); + return aggregateDto; + } + + /** + * Append computed field for a given Data, for its existing fields + * computes as partfield/wholefield * 100 + * + * @param data + * @param newfield + * @param partField + * @param wholeField + */ + default void addComputedField(Data data, String newfield, String partField, String wholeField) { + try { + Map<String, Plot> plotMap = data.getPlots().stream().parallel().collect(Collectors.toMap(Plot::getName, Function.identity())); + + if ((double)plotMap.get(partField).getValue() == 0.0 || (double)plotMap.get(wholeField).getValue() == 0.0) { + data.getPlots().add(new Plot(newfield, 0.0, "percentage")); + } else { + double fieldValue = (double)plotMap.get(partField).getValue() / (double)plotMap.get(wholeField).getValue() * 100; + data.getPlots().add(new Plot(newfield, fieldValue, "percentage")); + + } + } catch (Exception e) { + data.getPlots().add(new Plot(newfield, 0.0, "percentage")); + } + + } + + /** + * Computes the percentage from 0th and 1st index of list + * Ex: 0th element/1st element * 100 + * @param values + * @return + */ + default Double percentageValue(List<Double> values) { + double val = (values.get(0)/values.get(1) * 100); + return (values.size() > 1 && values.get(0) != 0.0 && values.get(1) != 0.0) ? val : 0.0; + } + + + /** + * Computes the percentage from 1st & 2nd element of collection + * Ex: first element/second element * 100 + * @param values + * @return + */ + default Double getPercentage(Map<String, Double> values, String partField, String wholeField) { + + double val = (values.get(partField)/ values.get(wholeField) * 100); + return (values.size() > 1 && values.get(partField) != 0.0 && values.get(wholeField) != 0.0) ? val: 0.0; + } + + /** + * Adding missing plot elements with cumulative data + * @param plotKeys - all required plot key + * @param data + * @param symbol + */ + default void appendMissingPlot(Set<String> plotKeys, Data data, String symbol, boolean isCumulative) { + + //To maintain the sorted plots list order + Map<String, Plot> sortedMap = data.getPlots().stream() + .collect(Collectors.toMap( + Plot::getName, + plot -> plot, + (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); }, + LinkedHashMap::new + )); + + logger.info(data.getHeaderName() + " existing keys: "+sortedMap.keySet()+ "& size:"+sortedMap.keySet().size()); + + Collection<String> allKeysMinusDataKeys = CollectionUtils.subtract(plotKeys, sortedMap.keySet()); + logger.info(data.getHeaderName() +" missing keys: "+allKeysMinusDataKeys); + + + for(String plKey:allKeysMinusDataKeys){ + sortedMap.put(plKey, new Plot(plKey, new Double("0"), symbol)); + if(isCumulative){ + List<String> keys = sortedMap.keySet().stream().collect(Collectors.toList()); + int index = keys.indexOf(plKey); + double value = index>0 ? (double) sortedMap.get(keys.get(index-1)).getValue():0.0; + sortedMap.get(plKey).setValue(value); + } + } + logger.info("after appending missing plots : "+ sortedMap); + data.setPlots(sortedMap.values().stream().collect(Collectors.toList())); + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/InsightsHandler.java b/src/main/java/com/tarento/analytics/handler/InsightsHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..dee91a02c1481d63ef914b582772257c5100ef80 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/InsightsHandler.java @@ -0,0 +1,41 @@ +package com.tarento.analytics.handler; + +import java.util.List; + +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.enums.ChartType; +import com.tarento.analytics.model.InsightsConfiguration; + +public interface InsightsHandler { + + public static final String CHART_NAME = "chartName"; + public static final String CHART_TYPE = "chartType"; + public static final String DRILL_CHART = "drillChart"; + public static final String VALUE_TYPE = "valueType"; + public static final String FILTER_KEYS = "filterKeys"; + public static final String INSIGHT_WIDGET_NAME = "INSIGHTS"; + public static final String INDICATOR_PLACEHOLDER = "$indicator"; + public static final String VALUE_PLACEHOLDER = "$value"; + public static final String INSIGHT_INTERVAL_PLACEHOLDER = "$insightInterval"; + public static final String INSIGHT_INDICATOR_POSITIVE = "upper_green"; + public static final String INSIGHT_INDICATOR_NEGATIVE = "lower_red"; + public static final String POSITIVE = "+"; + public static final String NEGATIVE = "-"; + + public static final String INSIGHT_NUMBER_DIFFERENCE = "differenceOfNumbers" ; + public static final String INSIGHT_PERCENTAGE_DIFFERENCE = "differenceOfPercentage" ; + + AggregateDto getInsights(AggregateDto aggregateDto, String visualizationCode, String moduleLevel, InsightsConfiguration insightsConfig); + + default AggregateDto getAggregatedDto(List<Data> dataList, String visualizationCode) { + AggregateDto aggregateDto = new AggregateDto(); + aggregateDto.setVisualizationCode(visualizationCode); + aggregateDto.setDrillDownChartId("none"); + ChartType chartType = ChartType.fromValue("metric"); + aggregateDto.setChartType(chartType); + aggregateDto.setData(dataList); + return aggregateDto; + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/InsightsHandlerFactory.java b/src/main/java/com/tarento/analytics/handler/InsightsHandlerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..7167cfcc7f7c09b050792b182cb608f1ca7d7523 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/InsightsHandlerFactory.java @@ -0,0 +1,19 @@ +package com.tarento.analytics.handler; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.tarento.analytics.enums.ChartType; + +@Component +public class InsightsHandlerFactory { + + @Autowired + private MetricsInsightsHandler metricInsightsHandler; + + public InsightsHandler getInstance(ChartType chartType) { + if(chartType == ChartType.METRIC) + return metricInsightsHandler; + return metricInsightsHandler; + } +} diff --git a/src/main/java/com/tarento/analytics/handler/LineBarResponseHandler.java b/src/main/java/com/tarento/analytics/handler/LineBarResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..75ea54220cac4886ff9cb6f9b328e9c3a1ecc556 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/LineBarResponseHandler.java @@ -0,0 +1,175 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; + +@Component +public class LineBarResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(LineBarResponseHandler.class); + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) + throws IOException { + + List<Data> dataList = new LinkedList<>(); + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + boolean isRequestInterval = null == requestDto.getRequestDate() ? false + : requestDto.getRequestDate().getInterval() != null + && !requestDto.getRequestDate().getInterval().isEmpty(); + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + String interval = isRequestInterval ? requestDto.getRequestDate().getInterval() + : chartNode.get(Constants.JsonPaths.INTERVAL).asText(); + if (interval == null || interval.isEmpty()) { + throw new RuntimeException("Interval must have value from config or request"); + } + aggrsPaths.forEach(headerPath -> { + List<JsonNode> aggrNodes = aggregationNode.findValues(headerPath.asText()); + + Map<String, Double> plotMap = new LinkedHashMap<>(); + List<Double> totalValues = new LinkedList<>(); + String headerLabel = chartNode.get(HEADER_LABEL) != null ? chartNode.get(HEADER_LABEL).asText() + : HEADER_LABEL_DEFAULT; + String valueLabel = chartNode.get(VALUE_LABEL) != null ? chartNode.get(VALUE_LABEL).asText() + : VALUE_LABEL_DEFAULT; + aggrNodes.stream().forEach(aggrNode -> { + if (aggrNode.findValues(IResponseHandler.BUCKETS).size() > 0) { + + ArrayNode buckets = (ArrayNode) aggrNode.findValues(IResponseHandler.BUCKETS).get(0); + buckets.forEach(bucket -> { + String bkey = bucket.findValue(IResponseHandler.KEY).asText(); + String key = ""; + if (interval.equals("15m")) { + key = getIntervalKey15m(bkey, interval); + } else { + key = getIntervalKey(bkey, Constants.Interval.valueOf(interval)); + } + double value = 0; + if(bucket.findValues(IResponseHandler.VALUE) != null && bucket.findValues(IResponseHandler.VALUE).size() > 0) { + value = ((bucket.findValues(IResponseHandler.VALUE).get(bucket.findValues(IResponseHandler.VALUE).size() - 1) != null) + ? bucket.findValues(IResponseHandler.VALUE).get(bucket.findValues(IResponseHandler.VALUE).size() - 1).asDouble() + : bucket.findValues(IResponseHandler.DOC_COUNT).get(bucket.findValues(IResponseHandler.DOC_COUNT).size() - 1).asDouble()); + } else { + value = ((bucket.findValue(IResponseHandler.VALUE) != null) + ? bucket.findValue(IResponseHandler.VALUE).asDouble() + : bucket.findValue(IResponseHandler.DOC_COUNT).asDouble()); + } + + plotMap.put(key, plotMap.get(key) == null ? new Double("0") + value : plotMap.get(key) + value); + totalValues.add(value); + }); + } + }); + List<Plot> plots = plotMap.entrySet().stream() + .map(e -> new Plot(e.getKey(), e.getValue(), symbol, headerLabel, valueLabel)) + .collect(Collectors.toList()); + try { + Data data = new Data(headerPath.asText(), (totalValues == null || totalValues.isEmpty()) ? 0.0 + : totalValues.stream().reduce(0.0, Double::sum), symbol); + data.setPlots(plots); + dataList.add(data); + } catch (Exception e) { + logger.error(" Legend/Header " + headerPath.asText() + " exception occurred " + e.getMessage()); + } + }); + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + } + + private String getIntervalKey(String epocString, Constants.Interval interval) { + try { + long epoch = Long.parseLong(epocString); + Date expiry = new Date(epoch); + Calendar cal = Calendar.getInstance(); + cal.setTime(expiry); + String day = String.valueOf(cal.get(Calendar.DATE)); + String month = monthNames(cal.get(Calendar.MONTH) + 1); + String year = "" + cal.get(Calendar.YEAR); + + String intervalKey = ""; + if (interval.equals(Constants.Interval.day)) { + intervalKey = day.concat("-").concat(month); + } else if (interval.equals(Constants.Interval.week)) { + intervalKey = day.concat("-").concat(month); + } else if (interval.equals(Constants.Interval.year)) { + intervalKey = year; + } else if (interval.equals(Constants.Interval.month)) { + intervalKey = month.concat("-").concat(year); + } else { + throw new RuntimeException("Invalid interval"); + } + return intervalKey; + } catch (Exception e) { + return epocString; + } + } + + private String getIntervalKey15m(String epocString, String interval) { + try { + long epoch = Long.parseLong(epocString); + Date expiry = new Date(epoch); + Calendar cal = Calendar.getInstance(); + cal.setTime(expiry); + cal.add(Calendar.HOUR, 5); + cal.add(Calendar.MINUTE, 30); + String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)); + String min = String.valueOf(cal.get(Calendar.MINUTE)); + + String intervalKey = ""; + intervalKey = hour.concat("h:").concat(min).concat("m"); + + return intervalKey; + } catch (Exception e) { + return epocString; + } + } + + private String monthNames(int month) { + if (month == 1) + return "Jan"; + else if (month == 2) + return "Feb"; + else if (month == 3) + return "Mar"; + else if (month == 4) + return "Apr"; + else if (month == 5) + return "May"; + else if (month == 6) + return "Jun"; + else if (month == 7) + return "Jul"; + else if (month == 8) + return "Aug"; + else if (month == 9) + return "Sep"; + else if (month == 10) + return "Oct"; + else if (month == 11) + return "Nov"; + else if (month == 12) + return "Dec"; + else + return "Month"; + } +} diff --git a/src/main/java/com/tarento/analytics/handler/LineChartResponseHandler.java b/src/main/java/com/tarento/analytics/handler/LineChartResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..f4b05bc2446e4e642d3d8fb15c967ad79b90252f --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/LineChartResponseHandler.java @@ -0,0 +1,183 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; + +/** + * This handles ES response for single index, multiple index to represent data + * as line chart Creates plots by merging/computing(by summation) index values + * for same key AGGS_PATH : this defines the path/key to be used to search the + * tree VALUE_TYPE : defines the data type for the value formed, this could be + * amount, percentage, number + * + */ +@Component +public class LineChartResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(LineChartResponseHandler.class); + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) + throws IOException { + + List<Data> dataList = new LinkedList<>(); + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + boolean isRequestInterval = null == requestDto.getRequestDate() ? false + : requestDto.getRequestDate().getInterval() != null + && !requestDto.getRequestDate().getInterval().isEmpty(); + String interval = isRequestInterval ? requestDto.getRequestDate().getInterval() + : chartNode.get(Constants.JsonPaths.INTERVAL).asText(); + if (interval == null || interval.isEmpty()) { + throw new RuntimeException("Interval must have value from config or request"); + } + + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + String action = chartNode.get(IResponseHandler.ACTION).asText(); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + Set<String> plotKeys = new LinkedHashSet<>(); + boolean isCumulative = chartNode.get("isCumulative").asBoolean(); + + aggrsPaths.forEach(headerPath -> { + List<JsonNode> aggrNodes = aggregationNode.findValues(headerPath.asText()); + + Map<String, Double> plotMap = new LinkedHashMap<>(); + List<Double> totalValues = new ArrayList<>(); + String headerLabel = chartNode.get(HEADER_LABEL) != null ? chartNode.get(HEADER_LABEL).asText() + : HEADER_LABEL_DEFAULT; + String valueLabel = chartNode.get(VALUE_LABEL) != null ? chartNode.get(VALUE_LABEL).asText() + : VALUE_LABEL_DEFAULT; + aggrNodes.stream().forEach(aggrNode -> { + if (aggrNode.findValues(IResponseHandler.BUCKETS).size() > 0) { + + ArrayNode buckets = (ArrayNode) aggrNode.findValues(IResponseHandler.BUCKETS).get(0); + buckets.forEach(bucket -> { + String bkey = bucket.findValue(IResponseHandler.KEY).asText(); + String key = ""; + if (interval.equals("15m")) { + key = getIntervalKey15m(bkey, interval); + } else { + key = getIntervalKey(bkey, Constants.Interval.valueOf(interval)); + } + // String key = getIntervalKey(bkey, Constants.Interval.valueOf(interval)); + + plotKeys.add(key); + double previousVal = !isCumulative ? 0.0 + : (totalValues.size() > 0 ? totalValues.get(totalValues.size() - 1) : 0.0); + + double value; + if (action.equals("Negative Count") && action != null + && bucket.findValue(IResponseHandler.NEGATIVE_COUNT) != null) { + value = previousVal + ((bucket.findValue(IResponseHandler.VALUE) != null) + ? bucket.findValue(IResponseHandler.VALUE).asDouble() * -1 + : bucket.findValue(IResponseHandler.DOC_COUNT).asDouble()); + } else { + if (bucket.findValue(IResponseHandler.CUMULATIVE_VALUE) != null) { + value = previousVal + ((bucket.findValue(IResponseHandler.VALUE) != null) + ? bucket.findValue(IResponseHandler.VALUE).asDouble() + : bucket.findValue(IResponseHandler.DOC_COUNT).asDouble()); + } else { + previousVal = 0.0; + value = previousVal + ((bucket.findValue(IResponseHandler.VALUE) != null) + ? bucket.findValue(IResponseHandler.VALUE).asDouble() + : bucket.findValue(IResponseHandler.DOC_COUNT).asDouble()); + } + } + plotMap.put(key, plotMap.get(key) == null ? new Double("0") + value : plotMap.get(key) + value); + totalValues.add(value); + }); + } + }); + List<Plot> plots = plotMap.entrySet().stream() + .map(e -> new Plot(e.getKey(), e.getValue(), symbol, headerLabel, valueLabel)) + .collect(Collectors.toList()); + try { + Data data = new Data(headerPath.asText(), (totalValues == null || totalValues.isEmpty()) ? 0.0 + : totalValues.stream().reduce(0.0, Double::sum), symbol); + /* + * Data data; if(!isCumulative) { data = new Data(headerPath.asText(), + * (totalValues==null || totalValues.isEmpty()) ? 0.0 : + * totalValues.stream().reduce(0.0, Double::sum), symbol); } else { data = new + * Data(headerPath.asText(), (totalValues==null || totalValues.isEmpty()) ? 0.0 + * : plots.get(plots.size()-1), symbol); } + */ + data.setPlots(plots); + dataList.add(data); + } catch (Exception e) { + logger.error(" Legend/Header " + headerPath.asText() + " exception occurred " + e.getMessage()); + } + }); + + dataList.forEach(data -> { + appendMissingPlot(plotKeys, data, symbol, isCumulative); + }); + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + } + + private String getIntervalKey(String epocString, Constants.Interval interval) { + try { + long epoch = Long.parseLong(epocString); + Date expiry = new Date(epoch); + + String intervalKey = ""; + if (interval.equals(Constants.Interval.day) || interval.equals(Constants.Interval.week)) { + intervalKey = new SimpleDateFormat("dd-MMM").format(expiry); + } else if (interval.equals(Constants.Interval.year)) { + intervalKey = new SimpleDateFormat("yyyy").format(expiry); + } else if (interval.equals(Constants.Interval.month)) { + intervalKey = new SimpleDateFormat("MMM-yyyy").format(expiry); + } else if (interval.equals(Constants.Interval.date)) { + intervalKey = new SimpleDateFormat("yyyy-MM-dd").format(expiry); + } else { + throw new RuntimeException("Invalid interval"); + } + + return intervalKey; + } catch (Exception e) { + return epocString; + } + } + + private String getIntervalKey15m(String epocString, String interval) { + try { + long epoch = Long.parseLong(epocString); + Date expiry = new Date(epoch); + Calendar cal = Calendar.getInstance(); + cal.setTime(expiry); + cal.add(Calendar.HOUR, 5); + cal.add(Calendar.MINUTE, 30); + String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)); + String min = String.valueOf(cal.get(Calendar.MINUTE)); + + String intervalKey = ""; + intervalKey = hour.concat("h:").concat(min).concat("m"); + + return intervalKey; + } catch (Exception e) { + return epocString; + } + } +} diff --git a/src/main/java/com/tarento/analytics/handler/MapResponseHandler.java b/src/main/java/com/tarento/analytics/handler/MapResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..0e8249e2fef37ff96081f84fa69ba0623816281f --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/MapResponseHandler.java @@ -0,0 +1,90 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; + +/** + * This handles ES response for single index, multiple index to represent data as line chart + * Creates plots by merging/computing(by summation) index values for same key + * AGGS_PATH : this defines the path/key to be used to search the tree + * VALUE_TYPE : defines the data type for the value formed, this could be amount, percentage, number + * + */ +@Component +public class MapResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(MapResponseHandler.class); + + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) throws IOException { + + List<Data> dataList = new LinkedList<>(); + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + ArrayNode depthSteps = (ArrayNode) chartNode.get(IResponseHandler.DEPTH_STEPS); + + for(JsonNode headerPath : aggrsPaths) { + List<JsonNode> aggrNodes = aggregationNode.findValues(headerPath.asText()); + + List<Double> totalValues = new ArrayList<>(); + List<Plot> finalPlots = new ArrayList<>(); + String bkey = ""; + for(JsonNode aggrNode : aggrNodes) { + if (aggrNode.findValues(IResponseHandler.BUCKETS).size() > 0) { + ArrayNode buckets = (ArrayNode) aggrNode.findValues(IResponseHandler.BUCKETS).get(0); + String headerLabel = chartNode.get(HEADER_LABEL) != null ? chartNode.get(HEADER_LABEL).asText() : HEADER_LABEL_DEFAULT; + String valueLabel = chartNode.get(VALUE_LABEL) != null ? chartNode.get(VALUE_LABEL).asText() : VALUE_LABEL_DEFAULT; + String parentLabel = chartNode.get(PARENT_LABEL) != null ? chartNode.get(PARENT_LABEL).asText() : PARENT_LABEL_DEFAULT; + for(JsonNode bucket : buckets) { + List<Plot> plots = new ArrayList<>(); + Map<String, Plot> plotMap = new LinkedHashMap<>(); + bkey = bucket.findValue(IResponseHandler.KEY).asText(); + String innerBucketKey = null; + for(JsonNode eachDepthStep : depthSteps) { + JsonNode innerNode = bucket.findValue(eachDepthStep.asText()); + ArrayNode innerBuckets = (ArrayNode) innerNode.findValues(IResponseHandler.BUCKETS).get(0); + for(JsonNode innerBucket : innerBuckets) { + innerBucketKey = innerBucket.findValue(IResponseHandler.KEY).asText(); + double value = (innerBucket.findValue(IResponseHandler.VALUE) != null) ? innerBucket.findValue(IResponseHandler.VALUE).asDouble():innerBucket.findValue(IResponseHandler.DOC_COUNT).asDouble(); + Plot plot = new Plot(innerBucketKey, value, symbol, bkey, headerLabel, valueLabel, parentLabel); + plotMap.put(innerBucketKey, plot); + totalValues.add(value); + } + } + plots = plotMap.entrySet().stream().map(e -> e.getValue()).collect(Collectors.toList()); + finalPlots.addAll(plots); + } + } + } + Data data = new Data(headerPath.asText(), (totalValues==null || totalValues.isEmpty()) ? 0.0 : totalValues.stream().reduce(0.0, Double::sum), symbol); + data.setPlots(finalPlots); + if(chartNode.get(COLOR_PALETTE_CODE) != null && chartNode.get(COLOR_PALETTE_ID) != null) { + data.setColorPaletteCode(chartNode.get(COLOR_PALETTE_CODE).asText()); + data.setColorPaletteId(chartNode.get(COLOR_PALETTE_ID).asLong()); + } + dataList.add(data); + } + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/MetricChartResponseHandler.java b/src/main/java/com/tarento/analytics/handler/MetricChartResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..f849c5d5d1653b073615d170868377513c97f068 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/MetricChartResponseHandler.java @@ -0,0 +1,116 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.ConfigurationLoader; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.enums.ChartType; +import com.tarento.analytics.helper.ComputeHelper; +import com.tarento.analytics.helper.ComputeHelperFactory; +import com.tarento.analytics.utils.ResponseRecorder; + +/** + * This handles ES response for single index, multiple index to represent single data value + * Creates plots by merging/computing(by summation or by percentage) index values for same key + * ACTION: for the chart config defines the type either summation or computing percentage + * AGGS_PATH : this defines the path/key to be used to search the tree + * + */ +@Component +public class MetricChartResponseHandler implements IResponseHandler{ + public static final Logger logger = LoggerFactory.getLogger(MetricChartResponseHandler.class); + char insightPrefix = 'i'; + + @Autowired + ConfigurationLoader configurationLoader; + + @Autowired + ComputeHelperFactory computeHelperFactory; + + @Autowired + ResponseRecorder responseRecorder; + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto request, ObjectNode aggregations) throws IOException { + List<Data> dataList = new ArrayList<>(); + String requestId = request.getRequestId(); + String visualizationCode = request.getVisualizationCode(); + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = null; + + if(request.getVisualizationCode().charAt(0) == insightPrefix) { + String internalChartId = request.getVisualizationCode().substring(1); + chartNode = configurationLoader.getConfigForProfile(profileName, API_CONFIG_JSON).get(internalChartId); + } else { + chartNode = configurationLoader.getConfigForProfile(profileName, API_CONFIG_JSON).get(request.getVisualizationCode()); + } + Boolean isDecimalCheck = chartNode.get(IS_DECIMAL).asBoolean(); + List<Double> totalValues = new LinkedList<>(); + List<String> headerPathList = new LinkedList<>(); + String chartName = chartNode.get(CHART_NAME).asText(); + String action = chartNode.get(ACTION).asText(); + + List<Double> percentageList = new LinkedList<>(); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(AGGS_PATH); + + aggrsPaths.forEach(headerPath -> { + List<JsonNode> values = aggregationNode.findValues(headerPath.asText()); + values.stream().parallel().forEach(value -> { + List<JsonNode> valueNodes = value.findValues(VALUE).isEmpty() ? value.findValues(DOC_COUNT) : value.findValues(VALUE); + Double sum = valueNodes.stream().mapToDouble(o -> o.asDouble()).sum(); + DecimalFormat decimalFormat = null; + decimalFormat = new DecimalFormat("#.##"); + Double formattedSum = Double.parseDouble(decimalFormat.format(sum)); + headerPathList.add(headerPath.asText()); + if(action.equals(PERCENTAGE) && aggrsPaths.size()==2){ + percentageList.add(formattedSum); + } else { + totalValues.add(formattedSum); + } + }); + }); + + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + try{ + ChartType chartType = ChartType.fromValue(chartNode.get(Constants.JsonPaths.CHART_TYPE).asText()); + Data data = null; + if(!chartType.equals(ChartType.METRICCOLLECTION)) { + data = new Data(chartName, action.equals(PERCENTAGE) && aggrsPaths.size()==2 ? percentageValue(percentageList) : (totalValues==null || totalValues.isEmpty())? 0.0 :totalValues.stream().reduce(0.0, Double::sum), symbol); + data.setIsDecimal(isDecimalCheck); + dataList.add(data); + } else { + if(action.equals(METRIC_GROUP)) { + for(int i = 0 ; i < totalValues.size(); i++) { + data = new Data(headerPathList.get(i), totalValues.get(i), symbol); + dataList.add(data); + } + } + } + responseRecorder.put(visualizationCode, request.getModuleLevel(), data); + + if(chartNode.get(POST_AGGREGATION_THEORY) != null) { + ComputeHelper computeHelper = computeHelperFactory.getInstance(chartNode.get(POST_AGGREGATION_THEORY).asText()); + computeHelper.compute(request, dataList); + } + }catch (Exception e){ + logger.info("data chart name = "+chartName +" ex occurred "+e.getMessage()); + } + + return getAggregatedDto(chartNode, dataList, request.getVisualizationCode()); + } +} diff --git a/src/main/java/com/tarento/analytics/handler/MetricsInsightsHandler.java b/src/main/java/com/tarento/analytics/handler/MetricsInsightsHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..64be78fd7a66aff10e72cf3390287badf2ddcabb --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/MetricsInsightsHandler.java @@ -0,0 +1,58 @@ +package com.tarento.analytics.handler; + +import java.text.DecimalFormat; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.InsightsWidget; +import com.tarento.analytics.model.InsightsConfiguration; +import com.tarento.analytics.utils.ResponseRecorder; + +@Component +public class MetricsInsightsHandler implements InsightsHandler { + + @Autowired + private ResponseRecorder responseRecorder; + + @Override + public AggregateDto getInsights(AggregateDto aggregateDto, String visualizationCode, String moduleLevel, InsightsConfiguration insightsConfig) { + String internalVisualizationCode = visualizationCode.substring(1); + Data currentData = (Data) responseRecorder.get(internalVisualizationCode, moduleLevel); + Data pastData = (Data) responseRecorder.get(visualizationCode, moduleLevel); + String textToDisplay = insightsConfig.getTextMessage(); + String insightIndicator = ""; + if(INSIGHT_NUMBER_DIFFERENCE.equals(insightsConfig.getAction())) { + Double difference = (Double)currentData.getHeaderValue() - (Double) pastData.getHeaderValue(); + if(difference > 0) { + Double insightValue = (difference / (Double)pastData.getHeaderValue()) * 100; + if(insightValue.isInfinite()) + return aggregateDto; + textToDisplay = textToDisplay.replace(INDICATOR_PLACEHOLDER, POSITIVE); + textToDisplay = textToDisplay.replace(VALUE_PLACEHOLDER, String.valueOf(new DecimalFormat("#").format(insightValue))); + insightIndicator = INSIGHT_INDICATOR_POSITIVE; + } else { + difference = (Double) pastData.getHeaderValue() - (Double) currentData.getHeaderValue(); + Double insightValue = (difference / (Double)pastData.getHeaderValue()) * 100; + if(insightValue.isInfinite()) + return aggregateDto; + if(difference.equals(0.0) && insightValue.equals(0.0)) + return aggregateDto; + textToDisplay = textToDisplay.replace(INDICATOR_PLACEHOLDER, NEGATIVE); + textToDisplay = textToDisplay.replace(VALUE_PLACEHOLDER, String.valueOf(new DecimalFormat("#").format(insightValue))); + insightIndicator = INSIGHT_INDICATOR_NEGATIVE; + } + textToDisplay = textToDisplay.replace(INSIGHT_INTERVAL_PLACEHOLDER, insightsConfig.getInsightInterval()); + InsightsWidget insightsWidget = new InsightsWidget(INSIGHT_WIDGET_NAME, textToDisplay, insightIndicator, insightIndicator); + List<Data> dataList = aggregateDto.getData(); + for(Data data : dataList) { + data.setInsight(insightsWidget); + } + } + return aggregateDto; + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/MultiBarChartResponseHandler.java b/src/main/java/com/tarento/analytics/handler/MultiBarChartResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..c6d30d954a53bf261f28ec2a35174133537dbd40 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/MultiBarChartResponseHandler.java @@ -0,0 +1,193 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; + +/** + * This handles ES response for single index, multiple index to represent data + * as line chart Creates plots by merging/computing(by summation) index values + * for same key AGGS_PATH : this defines the path/key to be used to search the + * tree VALUE_TYPE : defines the data type for the value formed, this could be + * amount, percentage, number + * + */ +@Component +public class MultiBarChartResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(MultiBarChartResponseHandler.class); + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) + throws IOException { + + List<Data> dataList = new LinkedList<>(); + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + boolean isRequestInterval = null == requestDto.getRequestDate() ? false + : requestDto.getRequestDate().getInterval() != null + && !requestDto.getRequestDate().getInterval().isEmpty(); + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + String interval = isRequestInterval ? requestDto.getRequestDate().getInterval() + : chartNode.get(Constants.JsonPaths.INTERVAL).asText(); + if (interval == null || interval.isEmpty()) { + throw new RuntimeException("Interval must have value from config or request"); + } + aggrsPaths.forEach(headerPath -> { + List<JsonNode> aggrNodes = aggregationNode.findValues(headerPath.asText()); + + Map<String, Double> plotMap = new LinkedHashMap<>(); + List<Double> totalValues = new LinkedList<>(); + String headerLabel = chartNode.get(HEADER_LABEL) != null ? chartNode.get(HEADER_LABEL).asText() + : HEADER_LABEL_DEFAULT; + String valueLabel = chartNode.get(VALUE_LABEL) != null ? chartNode.get(VALUE_LABEL).asText() + : VALUE_LABEL_DEFAULT; + aggrNodes.stream().forEach(aggrNode -> { + if (aggrNode.findValues(IResponseHandler.BUCKETS).size() > 0) { + + ArrayNode buckets = (ArrayNode) aggrNode.findValues(IResponseHandler.BUCKETS).get(0); + buckets.forEach(bucket -> { + String bkey = bucket.findValue(IResponseHandler.KEY).asText(); + String key = ""; + if (interval.equals("15m")) { + key = getIntervalKey15m(bkey, interval); + } else { + key = getIntervalKey(bkey, Constants.Interval.valueOf(interval)); + } + // String key = bucket.findValue(IResponseHandler.KEY).asText(); + double value = ((bucket.findValue(IResponseHandler.VALUE) != null) + ? bucket.findValue(IResponseHandler.VALUE).asDouble() + : bucket.findValue(IResponseHandler.DOC_COUNT).asDouble()); + plotMap.put(key, plotMap.get(key) == null ? new Double("0") + value : plotMap.get(key) + value); + totalValues.add(value); + }); + } + }); + List<Plot> plots = plotMap.entrySet().stream() + .map(e -> new Plot(e.getKey(), e.getValue(), symbol, headerLabel, valueLabel)) + .collect(Collectors.toList()); + try { + Data data = new Data(headerPath.asText(), (totalValues == null || totalValues.isEmpty()) ? 0.0 + : totalValues.stream().reduce(0.0, Double::sum), symbol); + /* + * Data data; if(!isCumulative) { data = new Data(headerPath.asText(), + * (totalValues==null || totalValues.isEmpty()) ? 0.0 : + * totalValues.stream().reduce(0.0, Double::sum), symbol); } else { data = new + * Data(headerPath.asText(), (totalValues==null || totalValues.isEmpty()) ? 0.0 + * : plots.get(plots.size()-1), symbol); } + */ + data.setPlots(plots); + dataList.add(data); + } catch (Exception e) { + logger.error(" Legend/Header " + headerPath.asText() + " exception occurred " + e.getMessage()); + } + }); + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + } + + private String getIntervalKey(String epocString, Constants.Interval interval) { + try { + long epoch = Long.parseLong(epocString); + Date expiry = new Date(epoch); + Calendar cal = Calendar.getInstance(); + cal.setTime(expiry); + String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)); + String min = String.valueOf(cal.get(Calendar.MINUTE)); + String day = String.valueOf(cal.get(Calendar.DATE)); + String month = monthNames(cal.get(Calendar.MONTH) + 1); + String year = "" + cal.get(Calendar.YEAR); + + String intervalKey = ""; + if (interval.equals(Constants.Interval.day)) { + intervalKey = day.concat("-").concat(month); + } else if (interval.equals(Constants.Interval.week)) { + intervalKey = day.concat("-").concat(month); + } else if (interval.equals(Constants.Interval.year)) { + intervalKey = year; + } else if (interval.equals(Constants.Interval.month)) { + intervalKey = month.concat("-").concat(year); + } else { + throw new RuntimeException("Invalid interval"); + } + + // String weekMonth = "Week " + cal.get(Calendar.WEEK_OF_YEAR) /*+ " : " + + // dayMonth*/;//+" of Month "+ (cal.get(Calendar.MONTH) + 1); + return intervalKey; + } catch (Exception e) { + return epocString; + } + } + + private String getIntervalKey15m(String epocString, String interval) { + try { + long epoch = Long.parseLong(epocString); + Date expiry = new Date(epoch); + Calendar cal = Calendar.getInstance(); + cal.setTime(expiry); + cal.add(Calendar.HOUR, 5); + cal.add(Calendar.MINUTE, 30); + String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)); + String min = String.valueOf(cal.get(Calendar.MINUTE)); + String day = String.valueOf(cal.get(Calendar.DATE)); + String month = monthNames(cal.get(Calendar.MONTH) + 1); + String year = "" + cal.get(Calendar.YEAR); + + String intervalKey = ""; + intervalKey = hour.concat("h:").concat(min).concat("m"); + + // String weekMonth = "Week " + cal.get(Calendar.WEEK_OF_YEAR) /*+ " : " + + // dayMonth*/;//+" of Month "+ (cal.get(Calendar.MONTH) + 1); + return intervalKey; + } catch (Exception e) { + return epocString; + } + } + + private String monthNames(int month) { + if (month == 1) + return "Jan"; + else if (month == 2) + return "Feb"; + else if (month == 3) + return "Mar"; + else if (month == 4) + return "Apr"; + else if (month == 5) + return "May"; + else if (month == 6) + return "Jun"; + else if (month == 7) + return "Jul"; + else if (month == 8) + return "Aug"; + else if (month == 9) + return "Sep"; + else if (month == 10) + return "Oct"; + else if (month == 11) + return "Nov"; + else if (month == 12) + return "Dec"; + else + return "Month"; + } +} diff --git a/src/main/java/com/tarento/analytics/handler/PerformanceChartResponeHandler.java b/src/main/java/com/tarento/analytics/handler/PerformanceChartResponeHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..091478af36ab4f5ef99ba3a191fe82d96ecdc6e4 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/PerformanceChartResponeHandler.java @@ -0,0 +1,113 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; + +@Component +public class PerformanceChartResponeHandler implements IResponseHandler { + + public static final Logger logger = LoggerFactory.getLogger(PerformanceChartResponeHandler.class); + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) throws IOException { + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + String plotLabel = chartNode.get(PLOT_LABEL).asText(); + String order = chartNode.get(ORDER).asText(); + int limit = chartNode.get(LIMIT).asInt(); + + + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + Map<String, Map<String, Double>> mappings = new LinkedHashMap<>();//HashMap<>(); + + aggrsPaths.forEach(headerPath -> { + aggregationNode.findValues(headerPath.asText()).forEach(aggrNode -> { + if (aggrNode.findValues(IResponseHandler.BUCKETS).size() > 0) { + + ArrayNode buckets = (ArrayNode) aggrNode.findValues(IResponseHandler.BUCKETS).get(0); + buckets.forEach(bucket -> { + String key = bucket.findValue(IResponseHandler.KEY).asText(); + Double value = bucket.findValue(IResponseHandler.VALUE).asDouble(); + + if (mappings.containsKey(key)) { + Double sum = (mappings.get(key)).containsKey(headerPath.asText()) ? (mappings.get(key)).get(headerPath.asText()) + value : value; + (mappings.get(key)).put(headerPath.asText(), sum); + + } else { + Map<String, Double> additiveMap = new HashMap<>(); + additiveMap.put(aggrsPaths.get(0).asText(), new Double("0")); + additiveMap.put(aggrsPaths.get(1).asText(), new Double("0")); + + additiveMap.put(headerPath.asText(), value); + mappings.put(key, additiveMap); + } + }); + } + }); + }); + logger.info("performance chart data mappings : "+mappings); + List<Plot> plotList = mappings.entrySet().stream().parallel().map(e -> new Plot(e.getKey(), getPercentage(e.getValue(), aggrsPaths.get(0).asText(),aggrsPaths.get(1).asText()), symbol)).collect(Collectors.toList()); + List<Plot> plots = plotList.stream().filter(plot -> (double)plot.getValue() != 0.0).collect(Collectors.toList()); + + plots.stream().parallel().forEach(item -> item.setLabel(plotLabel)); + /*Comparator<Plot> plotValueComparator = Comparator.thenComparingDouble(Plot::getValue); + plots.sort(plotValueComparator.reversed());*/ + return getAggregatedDto(chartNode, getDataOnPerformingOrder(plots, limit, order, symbol), requestDto.getVisualizationCode()); + } + + /** + * Prepare the plots with it's header Data in performing order + * @param plots + * @param limit n number of plots elements + * @param order top wise or bottom wise performance + * @param symbol + * @return + */ + private List<Data> getDataOnPerformingOrder(List<Plot> plots, int limit, String order, String symbol) { + + List<Data> dataList = new ArrayList<>(); + if (order.equals(ASC)) { + for (int i = (plots.size() - 1); i >= 0; i--) { + dataList.add(getRankedPLot(i, symbol, plots)); + } + } else if (order.equals(DESC)) { + for (int i = 0; i < plots.size(); i++) { + dataList.add(getRankedPLot(i, symbol, plots)); + } + } + // return dataList.subList(Math.max(0, 0), Math.min(dataList.size(), limit)); + return dataList; + + } + + private Data getRankedPLot(int rank, String dataType, List<Plot> plots){ + Data data = new Data(RANK, rank+1, dataType); + List<Plot> p = new ArrayList<Plot>(); + p.add(plots.get(rank)); + data.setPlots(p); + return data; + } + +} + + diff --git a/src/main/java/com/tarento/analytics/handler/PieChartResponseHandler.java b/src/main/java/com/tarento/analytics/handler/PieChartResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..c6dbc4b6a5ea0fabb596f93ef6f8a60493c23cc7 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/PieChartResponseHandler.java @@ -0,0 +1,87 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; +/** + * This handles ES response for single index, multiple index to represent data as pie figure + * Creates plots by merging/computing(by summation) index values for same key + * AGGS_PATH : this defines the path/key to be used to search the tree + * VALUE_TYPE : defines the data type for the value formed, this could be amount, percentage, number + * + */ +@Component +public class PieChartResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(PieChartResponseHandler.class); + + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) throws IOException { + + List<Data> dataList = new ArrayList<>(); + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + String headerKey = chartNode.get(CHART_NAME).asText(); + List<Plot> headerPlotList = new ArrayList<>(); + List<Double> totalValue = new ArrayList<>(); + + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + + aggrsPaths.forEach(headerPath -> { + aggregationNode.findValues(headerPath.asText()).stream().parallel().forEach(valueNode->{ + if(valueNode.has(BUCKETS)){ + JsonNode buckets = valueNode.findValue(BUCKETS); + String headerLabel = chartNode.get(HEADER_LABEL) != null ? chartNode.get(HEADER_LABEL).asText() : HEADER_LABEL_DEFAULT; + String valueLabel = chartNode.get(VALUE_LABEL) != null ? chartNode.get(VALUE_LABEL).asText() : VALUE_LABEL_DEFAULT; + buckets.forEach(bucket -> { + Plot plot = null; + if(bucket.findValue(VALUE) == null) { + if(bucket.findValue(DOC_COUNT) != null) { + totalValue.add(bucket.findValue(DOC_COUNT).asDouble()); + plot = new Plot(bucket.findValue(KEY).asText(), bucket.findValue(DOC_COUNT).asDouble(), symbol, headerLabel, valueLabel); + } + } else { + totalValue.add(bucket.findValue(VALUE).asDouble()); + plot = new Plot(bucket.findValue(KEY).asText(), bucket.findValue(VALUE).asDouble(), symbol, headerLabel, valueLabel); + } + + headerPlotList.add(plot); + }); + + } else { + List<JsonNode> valueNodes = valueNode.findValues(VALUE).isEmpty() ? valueNode.findValues("doc_count") : valueNode.findValues(VALUE); + double sum = valueNodes.stream().mapToLong(o -> o.asLong()).sum(); + totalValue.add(sum); + Plot plot = new Plot(headerPath.asText(), sum, symbol); + + headerPlotList.add(plot); + } + }); + }); + + Data data = new Data(headerKey, totalValue.stream().reduce(0.0, Double::sum), symbol); + data.setPlots(headerPlotList); + if(chartNode.get(COLOR_PALETTE_CODE) != null && chartNode.get(COLOR_PALETTE_ID) != null) { + data.setColorPaletteCode(chartNode.get(COLOR_PALETTE_CODE).asText()); + data.setColorPaletteId(chartNode.get(COLOR_PALETTE_ID).asLong()); + } + dataList.add(data); + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + + } +} diff --git a/src/main/java/com/tarento/analytics/handler/ResponseHandlerFactory.java b/src/main/java/com/tarento/analytics/handler/ResponseHandlerFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..ee901a0149ead9d9db5a832bcde8eb26f85dac19 --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/ResponseHandlerFactory.java @@ -0,0 +1,118 @@ +package com.tarento.analytics.handler; + +import com.tarento.analytics.enums.ChartType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ResponseHandlerFactory { + + @Autowired + private MetricChartResponseHandler metricChartResponseHandler; + @Autowired + private LineChartResponseHandler lineChartResponseHandler; + @Autowired + private PieChartResponseHandler pieChartResponseHandler; + @Autowired + private PerformanceChartResponeHandler performingBarChartResponeHandler; + @Autowired + private TableChartResponseHandler tableChartResponseHandler; + @Autowired + private AdvanceTableChartResponseHandler advanceTableChartResponseHandler; + @Autowired + private WeightChartResponseHandler weightChartResponseHandler; + @Autowired + private AdvancedMapResponseHandler advancedMapResponseHandler; + @Autowired + private MapResponseHandler mapResponseHandler; + @Autowired + private TableChartResponseHandler reportResponseHandler; + @Autowired + private MultiBarChartResponseHandler multiBarChartResponseHandler; + @Autowired + private DynamicTableResponseHandler dynamicTableResponseHandler; + @Autowired + private LineBarResponseHandler lineBarResponseHandler; + + public IResponseHandler getInstance(ChartType chartType) { + + if (chartType == ChartType.METRIC) { + return metricChartResponseHandler; + + } else if (chartType == ChartType.LINE) { + return lineChartResponseHandler; + + } else if (chartType == ChartType.PIE) { + return pieChartResponseHandler; + + } else if (chartType == ChartType.PERFORM) { + return performingBarChartResponeHandler; + + } else if (chartType == ChartType.TABLE) { + return tableChartResponseHandler; + + } else if (chartType == ChartType.XTABLE) { + return advanceTableChartResponseHandler; + + } else if (chartType == ChartType.BAR) { + return pieChartResponseHandler; + + } else if (chartType == ChartType.STACKEDBAR) { + return lineChartResponseHandler; + + } else if (chartType == ChartType.MULTIBAR) { + return multiBarChartResponseHandler; + + } else if (chartType == ChartType.DONUT) { + return pieChartResponseHandler; + + } else if (chartType == ChartType.HORIZONTALBAR) { + return pieChartResponseHandler; + + } else if (chartType == ChartType.POLARAREA) { + return pieChartResponseHandler; + + } else if (chartType == ChartType.METRICCOLLECTION) { + return metricChartResponseHandler; + + } else if (chartType == ChartType.TREEMAP) { + return pieChartResponseHandler; + + } else if (chartType == ChartType.BUBBLECHART) { + return weightChartResponseHandler; + + } else if (chartType == ChartType.INDIAMAP) { + return pieChartResponseHandler; + + } else if (chartType == ChartType.INDIADISTRICTMAP) { + return mapResponseHandler; + + } else if (chartType == ChartType.REPORT) { + return reportResponseHandler; + + } else if (chartType == ChartType.DYNAMICTABLE) { + return dynamicTableResponseHandler; + + } else if (chartType == ChartType.LINE_BAR) { + return lineBarResponseHandler; + + } else if (chartType == ChartType.CALENDARHEATMAP) { + return lineChartResponseHandler; + + } + + return null; + } + + @Autowired + private TablePostResponseHandler tablePostResponseHandler; + + public IPostResponseHandler get(ChartType chartType) { + + if (chartType == ChartType.TABLE) { + return tablePostResponseHandler; + } + return null; + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/TableChartResponseHandler.java b/src/main/java/com/tarento/analytics/handler/TableChartResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..de5757e5b6953828004d9a8e7485bc7434e03abd --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/TableChartResponseHandler.java @@ -0,0 +1,120 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; +import com.tarento.analytics.helper.ComputedFieldHelper; +/** + * This handles ES response for single index, multiple index to compute performance + * Creates plots by performing ordered (ex: top n performance or last n performance) + * AGGS_PATH : configurable to this defines the path/key to be used to search the tree + * VALUE_TYPE : configurable to define the data type for the value formed, this could be amount, percentage, number + * PLOT_LABEL : configurable to define the label for the plot + * TYPE_MAPPING : defines for a plot data type + */ +@Component +public class TableChartResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(TableChartResponseHandler.class); + + + @Autowired + ComputedFieldHelper computedFieldHelper; + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) throws IOException { + + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + String postAggrTheoryName = chartNode.get(POST_AGGREGATION_THEORY) == null ? "" : chartNode.get(POST_AGGREGATION_THEORY).asText(); + String plotLabel = chartNode.get(PLOT_LABEL).asText(); + ArrayNode pathDataTypeMap = (ArrayNode) chartNode.get(TYPE_MAPPING); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + Map<String, Map<String, Plot>> mappings = new HashMap<>(); + List<JsonNode> aggrNodes = aggregationNode.findValues(BUCKETS); + + int[] idx = { 1 }; + + aggrNodes.stream().forEach(node -> { + ArrayNode buckets = (ArrayNode) node; + buckets.forEach(bucket -> { + Map<String, Plot> plotMap = new LinkedHashMap<>(); + String key = bucket.findValue(IResponseHandler.KEY).asText(); + + aggrsPaths.forEach(headerPath -> { + JsonNode datatype = pathDataTypeMap.findValue(headerPath.asText()); + JsonNode valueNode = bucket.findValue(headerPath.asText()); + //Double value = (null == valueNode || null == valueNode.get(VALUE)) ? 0.0 : valueNode.get(VALUE).asDouble(); + Double doc_value = 0.0; + if(valueNode!=null) + doc_value = (null == valueNode.findValue(DOC_COUNT)) ? 0.0 : valueNode.findValue(DOC_COUNT).asDouble(); + Double value = (null == valueNode || null == valueNode.findValue(VALUE)) ? doc_value : valueNode.findValue(VALUE).asDouble(); + Plot plot = new Plot(headerPath.asText(), value, datatype.asText()); + if (mappings.containsKey(key)) { + double newval = mappings.get(key).get(headerPath.asText()) == null ? value : ((double)mappings.get(key).get(headerPath.asText()).getValue() + value); + plot.setValue(newval); + mappings.get(key).put(headerPath.asText(), plot); + } else { + plotMap.put(headerPath.asText(), plot); + } + }); + + if (plotMap.size() > 0) { + Map<String, Plot> plots = new LinkedHashMap<>(); + Plot sno = new Plot(SERIAL_NUMBER, null, TABLE_TEXT); + sno.setLabel(idx[0]++); + Plot plotkey = new Plot(plotLabel.isEmpty() ? TABLE_KEY : plotLabel, null, TABLE_TEXT); + plotkey.setLabel(key); + + plots.put(SERIAL_NUMBER, sno); + plots.put(plotLabel.isEmpty() ? TABLE_KEY : plotLabel, plotkey); + plots.putAll(plotMap); + mappings.put(key, plots); + + } + }); + + }); + + List<Data> dataList = new ArrayList<>(); + mappings.entrySet().stream().parallel().forEach(plotMap -> { + List<Plot> plotList = plotMap.getValue().values().stream().parallel().collect(Collectors.toList()); + //filter out data object with all zero data. + List<Plot> filterPlot = plotList.stream().filter(c -> (!c.getName().equalsIgnoreCase(SERIAL_NUMBER) && !c.getName().equalsIgnoreCase(plotLabel) && (double)c.getValue() != 0.0)).collect(Collectors.toList()); + + if(filterPlot.size()>0){ + Data data = new Data(plotMap.getKey(), Integer.parseInt(String.valueOf(plotMap.getValue().get(SERIAL_NUMBER).getLabel())), null); + data.setPlots(plotList); + if(requestDto.getVisualizationCode().equals(PT_DDR_BOUNDARY) || requestDto.getVisualizationCode().equals(PT_BOUNDARY) || requestDto.getVisualizationCode().equals(PT_BOUNDARY_DRILL) + || requestDto.getVisualizationCode().equals(TL_DDR_BOUNDARY) || requestDto.getVisualizationCode().equals(TL_BOUNDARY) || requestDto.getVisualizationCode().equals(TL_BOUNDARY_DRILL)) { + + computedFieldHelper.set(requestDto, postAggrTheoryName); + computedFieldHelper.add(data,TARGET_ACHIEVED, TOTAL_COLLECTION, TARGET_COLLECTION ); + } + + dataList.add(data); + } + + + }); + //dataList.sort((o1, o2) -> ((Integer) o1.getHeaderValue()).compareTo((Integer) o2.getHeaderValue())); + return getAggregatedDto(chartNode, dataList, requestDto.getVisualizationCode()); + } + +} diff --git a/src/main/java/com/tarento/analytics/handler/TablePostResponseHandler.java b/src/main/java/com/tarento/analytics/handler/TablePostResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..3df9700ad7225eefa3eb5e20fe8aff33c83bac6d --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/TablePostResponseHandler.java @@ -0,0 +1,105 @@ +package com.tarento.analytics.handler; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.org.service.MdmsApiMappings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Component +public class TablePostResponseHandler implements IPostResponseHandler { + private static Logger logger = LoggerFactory.getLogger(TablePostResponseHandler.class); + + + @Autowired + MdmsApiMappings mdmsApiMappings; + + + + public void preHandlerBuildQuery(ObjectNode filterNode, JsonNode query) { + + // from mdms ddrmap add the filter [tenant Ids] whe no filter present :: name has to be generate dynamically + + if(filterNode.findValue("tenantId") == null){ //no tenantId filter present + + for (String key : mdmsApiMappings.getAll().keySet()){ + List<String> tenantIds = mdmsApiMappings.getAll().get(key); + + + } + } else { + filterNode.findValue("tenantId"); + } + // if present filter [tenant id ] -> find the ddr name from the ddr map :: name to be gererate dynmically + + } + + public void postHandlerBuildResponse(ObjectNode filterNode, JsonNode query) { + + // from mdms ddrmap add the filter [tenant Ids] whe no filter present :: name has to be generate dynamically + + if(filterNode.findValue("tenantId") == null){ //no tenantId filter present + + for (String key : mdmsApiMappings.getAll().keySet()){ + + } + } else { + filterNode.findValue("tenantId"); + } + // if present filter [tenant id ] -> find the ddr name from the ddr map :: name to be gererate dynmically + + } + + /*String json = "{\"aggregations\":{\"dss-collection_v1\":{\"ULBs \":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"pb.derabassi\",\"doc_count\":448,\"Transactions\":{\"value\":448},\"Assessed Properties\":{\"value\":1251},\"Total Collection\":{\"value\":620938.0}},{\"key\":\"pb.nayagaon\",\"doc_count\":97,\"Transactions\":{\"value\":97},\"Assessed Properties\":{\"value\":235},\"Total Collection\":{\"value\":69108.0}}]}},\"dss-target_v1\":{\"ULBs \":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"pb.derabassi\",\"doc_count\":1,\"Target Collection\":{\"value\":1.5E7}},{\"key\":\"pb.nayagaon\",\"doc_count\":1,\"Target Collection\":{\"value\":3500000.0}}]}}}}"; + String json1 = "{\"aggregations\":{\"DDRCode \":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":277,\"Transactions\":{\"value\":277},\"Assessed Properties\":{\"value\":805},\"Total Collection\":{\"value\":618472.0}},{\"key\":\"9\",\"doc_count\":2719,\"Transactions\":{\"value\":2719},\"Assessed Properties\":{\"value\":8434},\"Total Collection\":{\"value\":5572731.0}}]}}}"; + */ + + /** + * Post response handle for replacing the district names from MDMS service map + * @param responseNode + */ + @Override + public void postResponse(ObjectNode responseNode){ + try { + + List<JsonNode> nodes = responseNode.findValues(Constants.MDMSKeys.KEY); + Set<String> values = new HashSet(); + nodes.forEach(node -> { + if(!values.contains(node.asText())) + replaceField(responseNode, Constants.MDMSKeys.KEY, node.asText(),mdmsApiMappings.getDDRNameByCode(node.asText())); + values.add(node.asText()); + + }); + logger.info("post response handling(DDR values) "+responseNode); + + }catch (Exception e){ + logger.error("post response handling(DDR values) "+e.getMessage()); + } + + } + + + public static void replaceField(ObjectNode parent, String fieldName, String oldValue, String newValue) { + if (parent.has(fieldName) && parent.get(fieldName).asText().equalsIgnoreCase(oldValue)) { + parent.put(fieldName, newValue); + } + parent.fields().forEachRemaining(entry -> { + JsonNode entryValue = entry.getValue(); + if (entryValue.isArray()) { + for (int i = 0; i < entryValue.size(); i++) { + if (entry.getValue().get(i).isObject()) + replaceField((ObjectNode) entry.getValue().get(i), fieldName, oldValue, newValue); + } + } else if (entryValue.isObject()) { + replaceField((ObjectNode) entry.getValue(), fieldName, oldValue, newValue); + } + }); + } +} diff --git a/src/main/java/com/tarento/analytics/handler/WeightChartResponseHandler.java b/src/main/java/com/tarento/analytics/handler/WeightChartResponseHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..03a1dce64053c899e14deba1226210c56839a07c --- /dev/null +++ b/src/main/java/com/tarento/analytics/handler/WeightChartResponseHandler.java @@ -0,0 +1,98 @@ +package com.tarento.analytics.handler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; +import com.tarento.analytics.dto.RichPlot; + +/** + * + * @author darshan + * + */ +@Component +public class WeightChartResponseHandler implements IResponseHandler { + public static final Logger logger = LoggerFactory.getLogger(WeightChartResponseHandler.class); + + + @Override + public AggregateDto translate(String profileName, AggregateRequestDto requestDto, ObjectNode aggregations) throws IOException { + + JsonNode aggregationNode = aggregations.get(AGGREGATIONS); + JsonNode chartNode = requestDto.getChartNode(); + + String symbol = chartNode.get(IResponseHandler.VALUE_TYPE).asText(); + Map<String, String> headerPathMap = new HashMap<>(); + headerPathMap.put(XPATH, chartNode.get(IResponseHandler.XPATH).asText()); + headerPathMap.put(YPATH, chartNode.get(IResponseHandler.YPATH).asText()); + headerPathMap.put(ZPATH, chartNode.get(IResponseHandler.ZPATH).asText()); + ArrayNode aggrsPaths = (ArrayNode) chartNode.get(IResponseHandler.AGGS_PATH); + Map<String, RichPlot> plotMap = new HashMap<>(); + Map<String, Data> dataMap = new HashMap<>(); + + for(JsonNode headerPath : aggrsPaths) { + List<JsonNode> aggrNodes = aggregationNode.findValues(headerPath.asText()); + headerPathMap.put(CURRENT_PATH, headerPath.asText()); + for(JsonNode aggrNode : aggrNodes) { + if (aggrNode.findValues(IResponseHandler.BUCKETS).size() > 0) { + + ArrayNode buckets = (ArrayNode) aggrNode.findValues(IResponseHandler.BUCKETS).get(0); + for(JsonNode bucket : buckets) { + String bkey = bucket.findValue(IResponseHandler.KEY).asText(); + double value = ((bucket.findValue(IResponseHandler.VALUE) != null) ? bucket.findValue(IResponseHandler.VALUE).asDouble():bucket.findValue(IResponseHandler.DOC_COUNT).asDouble()); + if(plotMap.containsKey(bkey)) { + RichPlot existingRichPlot = plotMap.get(bkey); + Data existingData = dataMap.get(bkey); + setPlotAxis(headerPathMap, headerPath, value, existingRichPlot); + existingData.setHeaderName(bkey); + } else { + RichPlot newRichPlot = new RichPlot(); + Data data = new Data(bkey, null, symbol); + setPlotAxis(headerPathMap, headerPath, value, newRichPlot); + dataMap.put(bkey, data); + plotMap.put(bkey, newRichPlot); + } + } + } + } + } + + for(Entry<String, Data> entry : dataMap.entrySet()) { + List<Plot> plots = new ArrayList<>(); + plots.add(plotMap.get(entry.getKey())); + entry.getValue().setPlots(plots); + } + return getAggregatedDto(chartNode, dataMap.values().stream().collect(Collectors.toList()), requestDto.getVisualizationCode()); + } + + private void setPlotAxis(Map<String, String> headerPathMap, JsonNode headerPath, double value, RichPlot richPlot) { + if(headerPathMap.get(CURRENT_PATH).equals(headerPathMap.get(XPATH))) { + richPlot.setxAxis(value); + richPlot.setxAxisLabel(headerPath.asText()); + } + if(headerPathMap.get(CURRENT_PATH).equals(headerPathMap.get(YPATH))) { + richPlot.setyAxis(value); + richPlot.setyAxisLabel(headerPath.asText()); + } + if(headerPathMap.get(CURRENT_PATH).equals(headerPathMap.get(ZPATH))) { + richPlot.setzAxis(value/10.0); + richPlot.setzAxisLabel(headerPath.asText()); + } + } +} diff --git a/src/main/java/com/tarento/analytics/helper/ComputeHelper.java b/src/main/java/com/tarento/analytics/helper/ComputeHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..3c50469fe9469fb84de67187dae425288277843e --- /dev/null +++ b/src/main/java/com/tarento/analytics/helper/ComputeHelper.java @@ -0,0 +1,19 @@ +package com.tarento.analytics.helper; + +import java.util.List; + +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; + +/** + * Compute Helper Inferface which receives the Request and the List of Data + * Implementations will derive as to what has to be the computation based on the Business Logic Specifications + * @author darshan + * + */ +public interface ComputeHelper { + + public List<Data> compute(AggregateRequestDto request, List<Data> data); + public Double compute(AggregateRequestDto request, double value); + +} diff --git a/src/main/java/com/tarento/analytics/helper/ComputeHelperFactory.java b/src/main/java/com/tarento/analytics/helper/ComputeHelperFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..f6f6a555fda7f0e87ba6c3f5d2aeb9ff610701ae --- /dev/null +++ b/src/main/java/com/tarento/analytics/helper/ComputeHelperFactory.java @@ -0,0 +1,28 @@ +package com.tarento.analytics.helper; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.tarento.analytics.constant.Constants; + +/** + * Factory Class to supply the right implementation for the Post Aggregation Computation Business Logic + * which has to be supplied based on the Configuration written in the Chart API Configuration + * @author darshan + * + */ +@Component +public class ComputeHelperFactory { + + @Autowired + private TargetPerDateComputeHelper targetPerDateComputeHelper; + + public ComputeHelper getInstance(String intent) { + + if (intent.equals(Constants.PostAggregationTheories.RESPONSE_DIFF_DATES)) { + return targetPerDateComputeHelper; + } + return null; + } + +} diff --git a/src/main/java/com/tarento/analytics/helper/ComputedFieldFactory.java b/src/main/java/com/tarento/analytics/helper/ComputedFieldFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..2c1f3e04ce305575c1b825fe9a0a5b3b59896fd4 --- /dev/null +++ b/src/main/java/com/tarento/analytics/helper/ComputedFieldFactory.java @@ -0,0 +1,22 @@ +package com.tarento.analytics.helper; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ComputedFieldFactory { + + @Autowired + private PercentageComputedField percentageComputedField; + + public IComputedField getInstance(String className){ + + if(className.equalsIgnoreCase(percentageComputedField.getClass().getSimpleName())){ + return percentageComputedField; + } else { + throw new RuntimeException("Computer field not found for className "+className); + } + + } + +} diff --git a/src/main/java/com/tarento/analytics/helper/ComputedFieldHelper.java b/src/main/java/com/tarento/analytics/helper/ComputedFieldHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..fe04c99a557142b53b7ecc9d1ef83605c95cb37d --- /dev/null +++ b/src/main/java/com/tarento/analytics/helper/ComputedFieldHelper.java @@ -0,0 +1,62 @@ +package com.tarento.analytics.helper; + +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; + +@Component +public class ComputedFieldHelper { + + public static final Logger logger = LoggerFactory.getLogger(ComputedFieldHelper.class); + + @Autowired + private ComputeHelperFactory computeHelperFactory; + + private AggregateRequestDto aggregateRequestDto; + private String postAggrTheoryName; + + public void set(AggregateRequestDto requestDto, String postAggrTheoryName){ + + this.aggregateRequestDto = requestDto; + this.postAggrTheoryName = postAggrTheoryName; + } + + public void add(Data data, String newfield, String partField, String wholeField){ + try { + Map<String, Plot> plotMap = data.getPlots().stream().parallel().collect(Collectors.toMap(Plot::getName, Function.identity())); + + if ((double)plotMap.get(partField).getValue() == 0.0 || (double)plotMap.get(wholeField).getValue() == 0.0) { + + data.getPlots().add(new Plot(newfield, 0.0, "percentage")); + } else { + double wholeValue = (double)plotMap.get(wholeField).getValue(); + double fieldValue = (double)plotMap.get(partField).getValue() / (double)plotMap.get(wholeField).getValue() * 100; + + if(postAggrTheoryName != null && !postAggrTheoryName.isEmpty()) { + //logger.info("Chart name: "+aggregateRequestDto.getVisualizationCode()+" :: postAggrTheoryName : "+postAggrTheoryName); + ComputeHelper computeHelper = computeHelperFactory.getInstance(postAggrTheoryName); + fieldValue = computeHelper.compute(aggregateRequestDto, fieldValue); + wholeValue = computeHelper.compute(aggregateRequestDto, wholeValue); + } + + data.getPlots().stream().filter(plot -> wholeField.equalsIgnoreCase(plot.getName())).findAny().orElse(null).setValue(wholeValue); + data.getPlots().add(new Plot(newfield, fieldValue, "percentage")); + + } + + + } catch (Exception e) { + data.getPlots().add(new Plot(newfield, 0.0, "percentage")); + } + } + +} diff --git a/src/main/java/com/tarento/analytics/helper/IComputedField.java b/src/main/java/com/tarento/analytics/helper/IComputedField.java new file mode 100644 index 0000000000000000000000000000000000000000..3139169f3f9c534bf23e67dfc23d5b465951aa0c --- /dev/null +++ b/src/main/java/com/tarento/analytics/helper/IComputedField.java @@ -0,0 +1,12 @@ +package com.tarento.analytics.helper; + +import com.tarento.analytics.dto.AggregateRequestDto; + +import java.util.List; + +public interface IComputedField { + + public void set(AggregateRequestDto requestDto, String postAggrTheoryName); + public void add(Object data, List<String> fields, String newField); + +} diff --git a/src/main/java/com/tarento/analytics/helper/PercentageComputedField.java b/src/main/java/com/tarento/analytics/helper/PercentageComputedField.java new file mode 100644 index 0000000000000000000000000000000000000000..112e5959e806451541c6f1d091ed7ae812e0f195 --- /dev/null +++ b/src/main/java/com/tarento/analytics/helper/PercentageComputedField.java @@ -0,0 +1,66 @@ +package com.tarento.analytics.helper; + +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; +import com.tarento.analytics.dto.Plot; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +@Component +public class PercentageComputedField implements IComputedField{ + + public static final Logger logger = LoggerFactory.getLogger(PercentageComputedField.class); + + + private String postAggrTheoryName; + private AggregateRequestDto aggregateRequestDto; + @Autowired + private ComputeHelperFactory computeHelperFactory; + + @Override + public void set(AggregateRequestDto requestDto, String postAggrTheoryName){ + this.aggregateRequestDto = requestDto; + this.postAggrTheoryName = postAggrTheoryName; + } + + @Override + public void add(Object data1, List<String> fields, String newField) { + Data data = (Data) data1; + try { + Map<String, Plot> plotMap = data.getPlots().stream().parallel().collect(Collectors.toMap(Plot::getName, Function.identity())); + + if ((double)plotMap.get(fields.get(0)).getValue() == 0.0 || (double)plotMap.get(fields.get(1)).getValue() == 0.0) { + + data.getPlots().add(new Plot(newField, 0.0, "percentage")); + } else { + double wholeValue = (double)plotMap.get(fields.get(1)).getValue(); + double fieldValue = (double)plotMap.get(fields.get(0)).getValue() / (double)plotMap.get(fields.get(1)).getValue() * 100; + + logger.error(newField +" wholeValue "+wholeValue); + logger.error(newField+ " fieldValue "+fieldValue); + + if(postAggrTheoryName != null && !postAggrTheoryName.isEmpty()) { + ComputeHelper computeHelper = computeHelperFactory.getInstance(postAggrTheoryName); + fieldValue = computeHelper.compute(aggregateRequestDto, fieldValue); + wholeValue = computeHelper.compute(aggregateRequestDto, wholeValue); + } + data.getPlots().stream().filter(plot -> fields.get(1).equalsIgnoreCase(plot.getName())).findAny().orElse(null).setValue(wholeValue); + data.getPlots().add(new Plot(newField, fieldValue, "percentage")); + } + + } catch (Exception e) { + // throw new RuntimeException("Computed field configuration not correctly provided"); + logger.error("ex occured "+e.getMessage()); + data.getPlots().add(new Plot(newField, 0.0, "percentage")); + } + + } +} + diff --git a/src/main/java/com/tarento/analytics/helper/TargetPerDateComputeHelper.java b/src/main/java/com/tarento/analytics/helper/TargetPerDateComputeHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..e54987747f97a3f6c5cfbc3ea759439ee74c13f0 --- /dev/null +++ b/src/main/java/com/tarento/analytics/helper/TargetPerDateComputeHelper.java @@ -0,0 +1,89 @@ +package com.tarento.analytics.helper; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.Data; + +/** + * This implementation of Compute Helper is used to compute the difference of dates between the Request Date + * The difference is then multiplied against the Per Day Unit of Target which has been obtained from Elastic Search + * @author darshan + * + */ +@Component +public class TargetPerDateComputeHelper implements ComputeHelper { + private static final Long ROUND_OFF= 19801000l; + private static final Long NUMBER_OF_DAYS = 365l; + private static final Long LAST_HOUR = 23l; + private static final Long LAST_MINUTE = 59l; + public static final Logger logger = LoggerFactory.getLogger(TargetPerDateComputeHelper.class); + @Override + public List<Data> compute(AggregateRequestDto request, List<Data> data) { + if(request.getRequestDate()!= null && request.getRequestDate().getStartDate() != null && request.getRequestDate().getEndDate() !=null) { + try { + Long sDate = Long.parseLong(request.getRequestDate().getStartDate()); + logger.info("Start Date : " + String.valueOf(sDate)); + Long eDate = Long.parseLong(request.getRequestDate().getEndDate()); + logger.info("End Date : " + String.valueOf(eDate)); + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date(eDate)); + if(cal.get(Calendar.HOUR_OF_DAY) == LAST_HOUR && cal.get(Calendar.MINUTE) == LAST_MINUTE) { + eDate = eDate + ROUND_OFF; + } + logger.info("End Date after Round Off: " + String.valueOf(eDate)); + Long dateDifference = TimeUnit.DAYS.convert((eDate - sDate), TimeUnit.MILLISECONDS); + if(dateDifference == 0l) dateDifference = dateDifference + 1l ; + for(Data eachData : data) { + Double value = (Double) eachData.getHeaderValue(); + logger.info("Value is : " + value + " :: Date Difference is : " + dateDifference); + value = (value / NUMBER_OF_DAYS) * dateDifference; + eachData.setHeaderValue(value); + } + } catch (Exception ex) { + logger.error("Encountered an error while computing the logic in Target Date Computer : " + ex.getMessage()); + } + } + return data; + } + + public Double compute(AggregateRequestDto request, double value){ + + if(request.getRequestDate()!= null && request.getRequestDate().getStartDate() != null && request.getRequestDate().getEndDate() !=null) { + try { + Long sDate = Long.parseLong(request.getRequestDate().getStartDate()); + logger.info("Start Date : " + String.valueOf(sDate)); + Long eDate = Long.parseLong(request.getRequestDate().getEndDate()); + logger.info("End Date : " + String.valueOf(eDate)); + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date(eDate)); + if(cal.get(Calendar.HOUR_OF_DAY) == LAST_HOUR && cal.get(Calendar.MINUTE) == LAST_MINUTE) { + eDate = eDate + ROUND_OFF; + } + logger.info("End Date after Round Off: " + String.valueOf(eDate)); + Long dateDifference = TimeUnit.DAYS.convert((eDate - sDate), TimeUnit.MILLISECONDS); + if(dateDifference == 0l) dateDifference = dateDifference + 1l ; + + value = (value / NUMBER_OF_DAYS) * dateDifference; + logger.info("Value is : " + value + " :: Date Difference is : " + dateDifference); + + } catch (Exception ex) { + logger.error("Encountered an error while computing the logic in Target Date Computer : " + ex.getMessage()); + } + } + + return value; + + + + } + + +} diff --git a/src/main/java/com/tarento/analytics/model/AmazonS3Config.java b/src/main/java/com/tarento/analytics/model/AmazonS3Config.java new file mode 100644 index 0000000000000000000000000000000000000000..56d0abb449bc3dc1a2d1e6935611c4f4f947ba9d --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/AmazonS3Config.java @@ -0,0 +1,53 @@ +package com.tarento.analytics.model; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Region; +import com.amazonaws.regions.Regions; + +@Configuration +public class AmazonS3Config +{ + @Value("${aws.access.key.id}") + private String awsKeyId; + + @Value("${aws.access.key.secret}") + private String awsKeySecret; + + @Value("${aws.region}") + private String awsRegion; + + @Value("${aws.s3.audio.bucket}") + private String awsS3AudioBucket; + + @Bean(name = "awsKeyId") + public String getAWSKeyId() { + return awsKeyId; + } + + @Bean(name = "awsKeySecret") + public String getAWSKeySecret() { + return awsKeySecret; + } + + @Bean(name = "awsRegion") + public Region getAWSPollyRegion() { + return Region.getRegion(Regions.fromName(awsRegion)); + } + + @Bean(name = "awsCredentialsProvider") + public AWSCredentialsProvider getAWSCredentials() { + BasicAWSCredentials awsCredentials = new BasicAWSCredentials(this.awsKeyId, this.awsKeySecret); + return new AWSStaticCredentialsProvider(awsCredentials); + } + + @Bean(name = "awsS3AudioBucket") + public String getAWSS3AudioBucket() { + return awsS3AudioBucket; + } +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/model/ComputedFields.java b/src/main/java/com/tarento/analytics/model/ComputedFields.java new file mode 100644 index 0000000000000000000000000000000000000000..51f60546c27d6910941ebef643400a89eb7fca58 --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/ComputedFields.java @@ -0,0 +1,44 @@ +package com.tarento.analytics.model; + +import java.util.ArrayList; +import java.util.List; + +public class ComputedFields { + + private String postAggregationTheory; + private String actionName; + private List<String> fields = new ArrayList<>(); + private String newField; + + public String getPostAggregationTheory() { + return postAggregationTheory; + } + + public void setPostAggregationTheory(String postAggregationTheory) { + this.postAggregationTheory = postAggregationTheory; + } + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public List<String> getFields() { + return fields; + } + + public void setFields(List<String> fields) { + this.fields = fields; + } + + public String getNewField() { + return newField; + } + + public void setNewField(String newField) { + this.newField = newField; + } +} diff --git a/src/main/java/com/tarento/analytics/model/ElasticSearchDictator.java b/src/main/java/com/tarento/analytics/model/ElasticSearchDictator.java new file mode 100644 index 0000000000000000000000000000000000000000..ec51d3054bfb5760cafe2f5b5bdca40d8e4199ba --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/ElasticSearchDictator.java @@ -0,0 +1,69 @@ +package com.tarento.analytics.model; + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * + * @author Darshan + * + */ +public class ElasticSearchDictator { + + private String visualisationName; + private String indexName; + private String documentType; + private Map<String, Map<String, List<Object>>> queryMap; + private Map<String, LinkedList<Map<String, Map<String, Object>>>> aggregationMap; + private Map<String, Object> queryAggregationMap; + + public Map<String, Object> getQueryAggregationMap() { + return queryAggregationMap; + } + + public void setQueryAggregationMap(Map<String, Object> queryAggregationMap) { + this.queryAggregationMap = queryAggregationMap; + } + + public String getVisualisationName() { + return visualisationName; + } + + public void setVisualisationName(String visualisationName) { + this.visualisationName = visualisationName; + } + + public String getIndexName() { + return indexName; + } + + public void setIndexName(String indexName) { + this.indexName = indexName; + } + + public String getDocumentType() { + return documentType; + } + + public void setDocumentType(String documentType) { + this.documentType = documentType; + } + + public Map<String, Map<String, List<Object>>> getQueryMap() { + return queryMap; + } + + public void setQueryMap(Map<String, Map<String, List<Object>>> queryMap) { + this.queryMap = queryMap; + } + + public Map<String, LinkedList<Map<String, Map<String, Object>>>> getAggregationMap() { + return aggregationMap; + } + + public void setAggregationMap(Map<String, LinkedList<Map<String, Map<String, Object>>>> aggregationMap) { + this.aggregationMap = aggregationMap; + } + +} diff --git a/src/main/java/com/tarento/analytics/model/InsightsConfiguration.java b/src/main/java/com/tarento/analytics/model/InsightsConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..feae201b78aa97f29e140bd0e3e7a72679ea9681 --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/InsightsConfiguration.java @@ -0,0 +1,54 @@ +package com.tarento.analytics.model; + +public class InsightsConfiguration { + private String chartResponseMap; + private String action; + private String upwardIndicator; + private String downwardIndicator; + private String textMessage; + private String colorCode; + private String insightInterval; + + public String getInsightInterval() { + return insightInterval; + } + public void setInsightInterval(String insightInterval) { + this.insightInterval = insightInterval; + } + public String getChartResponseMap() { + return chartResponseMap; + } + public void setChartResponseMap(String chartResponseMap) { + this.chartResponseMap = chartResponseMap; + } + public String getAction() { + return action; + } + public void setAction(String action) { + this.action = action; + } + public String getUpwardIndicator() { + return upwardIndicator; + } + public void setUpwardIndicator(String upwardIndicator) { + this.upwardIndicator = upwardIndicator; + } + public String getDownwardIndicator() { + return downwardIndicator; + } + public void setDownwardIndicator(String downwardIndicator) { + this.downwardIndicator = downwardIndicator; + } + public String getTextMessage() { + return textMessage; + } + public void setTextMessage(String textMessage) { + this.textMessage = textMessage; + } + public String getColorCode() { + return colorCode; + } + public void setColorCode(String colorCode) { + this.colorCode = colorCode; + } +} diff --git a/src/main/java/com/tarento/analytics/model/Item.java b/src/main/java/com/tarento/analytics/model/Item.java new file mode 100644 index 0000000000000000000000000000000000000000..b1e63e89eaedb3f54841e2f0c83467270276687b --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/Item.java @@ -0,0 +1,92 @@ +package com.tarento.analytics.model; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class Item { + + @JsonProperty("name") + private String name; + @JsonProperty("code") + private String code; + @JsonProperty("id") + private Long id; + @JsonProperty("description") + private String description; + @JsonProperty("price") + private Double price; + @JsonProperty("categoryId") + private Long categoryId; + @JsonProperty("subCategoryId") + private Long subCategoryId; + @JsonProperty("name") + + + public String getName() { + return name; + } + + public Long getCategoryId() { + return categoryId; + } + + public void setCategoryId(Long categoryId) { + this.categoryId = categoryId; + } + + public Long getSubCategoryId() { + return subCategoryId; + } + + public void setSubCategoryId(Long subCategoryId) { + this.subCategoryId = subCategoryId; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + @JsonProperty("code") + public String getCode() { + return code; + } + + @JsonProperty("code") + public void setCode(String code) { + this.code = code; + } + + @JsonProperty("id") + public Long getId() { + return id; + } + + @JsonProperty("id") + public void setId(Long id) { + this.id = id; + } + + @JsonProperty("description") + public String getDescription() { + return description; + } + + @JsonProperty("description") + public void setDescription(String description) { + this.description = description; + } + + @JsonProperty("price") + public Double getPrice() { + return price; + } + + @JsonProperty("price") + public void setPrice(Double price) { + this.price = price; + } + + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/model/KeyData.java b/src/main/java/com/tarento/analytics/model/KeyData.java new file mode 100644 index 0000000000000000000000000000000000000000..6a49dd3bd547e131276196c9c4fe79a8ac63c94c --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/KeyData.java @@ -0,0 +1,53 @@ + +package com.tarento.analytics.model; + +public class KeyData { + + + private Long id; + private Object key; + private String label; + + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public Object getKey() { + return key; + } + public void setKey(Object key) { + this.key = key; + } + public String getLabel() { + return label; + } + public void setLabel(String label) { + this.label = label; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((key == null) ? 0 : key.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + KeyData other = (KeyData) obj; + if (key == null) { + if (other.key != null) + return false; + } else if (!key.equals(other.key)) + return false; + return true; + } +} diff --git a/src/main/java/com/tarento/analytics/model/Label.java b/src/main/java/com/tarento/analytics/model/Label.java new file mode 100644 index 0000000000000000000000000000000000000000..d0ef0c8e9c139358231fc7d8400b5f0ab5fb2d59 --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/Label.java @@ -0,0 +1,19 @@ +package com.tarento.analytics.model; + +public class Label { + + private String xLabel; + private String yLabel; + public String getxLabel() { + return xLabel; + } + public void setxLabel(String xLabel) { + this.xLabel = xLabel; + } + public String getyLabel() { + return yLabel; + } + public void setyLabel(String yLabel) { + this.yLabel = yLabel; + } +} diff --git a/src/main/java/com/tarento/analytics/model/LineData.java b/src/main/java/com/tarento/analytics/model/LineData.java new file mode 100644 index 0000000000000000000000000000000000000000..532eeb814da67e8104067e6a82db928087d503ad --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/LineData.java @@ -0,0 +1,20 @@ +package com.tarento.analytics.model; + +public class LineData { + + private String type; + private Object value; + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public Object getValue() { + return value; + } + public void setValue(Object value) { + this.value = value; + } + +} diff --git a/src/main/java/com/tarento/analytics/model/Query.java b/src/main/java/com/tarento/analytics/model/Query.java new file mode 100644 index 0000000000000000000000000000000000000000..bf34cdbc0823be50d35cc494b6c72c19f91998af --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/Query.java @@ -0,0 +1,26 @@ +package com.tarento.analytics.model; + +import java.util.Map; + +public class Query { + + private String dateFilterField; + private Map<String, Object> aggregation; + + public String getDateFilterField() { + return dateFilterField; + } + + public void setDateFilterField(String dateFilterField) { + this.dateFilterField = dateFilterField; + } + + public Map<String, Object> getAggregation() { + return aggregation; + } + + public void setAggregation(Map<String, Object> aggregation) { + this.aggregation = aggregation; + } + +} diff --git a/src/main/java/com/tarento/analytics/model/ServiceQuery.java b/src/main/java/com/tarento/analytics/model/ServiceQuery.java new file mode 100644 index 0000000000000000000000000000000000000000..f6fdd64ed61b0e8bb173d80560d17616fa1faadb --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/ServiceQuery.java @@ -0,0 +1,27 @@ +package com.tarento.analytics.model; + +public class ServiceQuery { + + private String indexName; + private String queryJson; + private String documentType; + public String getIndexName() { + return indexName; + } + public void setIndexName(String indexName) { + this.indexName = indexName; + } + public String getQueryJson() { + return queryJson; + } + public void setQueryJson(String queryJson) { + this.queryJson = queryJson; + } + public String getDocumentType() { + return documentType; + } + public void setDocumentType(String documentType) { + this.documentType = documentType; + } + +} diff --git a/src/main/java/com/tarento/analytics/model/Transaction.java b/src/main/java/com/tarento/analytics/model/Transaction.java new file mode 100644 index 0000000000000000000000000000000000000000..5e5b063df996270320420390d7cde0af8fab1785 --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/Transaction.java @@ -0,0 +1,264 @@ +package com.tarento.analytics.model; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Transaction Object which gets posted to Elastic Search + * @author Darshan Nagesh + * + */ +public class Transaction { + + @JsonProperty("transactionId") + private Long transactionId; + @JsonProperty("receiptNo") + private String receiptNo; + @JsonProperty("transType") + private String transType; + @JsonProperty("storeCode") + private Long storeCode; + @JsonProperty("posTerminalNo") + private String posTerminalNo; + @JsonProperty("staffId") + private Long staffId; + @JsonProperty("transDate") + private String transDate; + @JsonProperty("transTime") + private String transTime; + @JsonProperty("customerNo") + private Long consumerNo; + @JsonProperty("salesType") + private String salesType; + @JsonProperty("netAmt") + private Double netAmt; + @JsonProperty("grossAmt") + private Double grossAmt; + @JsonProperty("payment") + private Double payment; + @JsonProperty("discountAmt") + private Double discountAmt; + @JsonProperty("costAmt") + private Double costAmt; + @JsonProperty("managerId") + private Long managerId; + @JsonProperty("isTraining") + private Boolean isTraining; + @JsonProperty("statementNo") + private String statementNo; + @JsonProperty("postingStatus") + private String postingStatus; + @JsonProperty("postStatementNo") + private String postStatementNo; + @JsonProperty("customerAgeGroup") + private String customerAgeGroup; + @JsonProperty("customerGender") + private String customerGender; + @JsonProperty("items") + private List<Item> items; + @JsonProperty("itemDetails") + private List<Item> itemDetails; + @JsonProperty("orgId") + private Long orgId; + + public Long getOrgId() { + return orgId; + } + + public void setOrgId(Long orgId) { + this.orgId = orgId; + } + + public List<Item> getItemDetails() { + return itemDetails; + } + + public void setItemDetails(List<Item> itemDetails) { + this.itemDetails = itemDetails; + } + + public List<Item> getItems() { + return items; + } + + public void setItems(List<Item> items) { + this.items = items; + } + + public String getCustomerAgeGroup() { + return customerAgeGroup; + } + + public void setCustomerAgeGroup(String customerAgeGroup) { + this.customerAgeGroup = customerAgeGroup; + } + + public String getCustomerGender() { + return customerGender; + } + + public void setCustomerGender(String customerGender) { + this.customerGender = customerGender; + } + + public Long getTransactionId() { + return transactionId; + } + + public void setTransactionId(Long transactionId) { + this.transactionId = transactionId; + } + + public String getReceiptNo() { + return receiptNo; + } + + public void setReceiptNo(String receiptNo) { + this.receiptNo = receiptNo; + } + + public String getTransType() { + return transType; + } + + public void setTransType(String transType) { + this.transType = transType; + } + + public Long getStoreCode() { + return storeCode; + } + + public void setStoreCode(Long storeCode) { + this.storeCode = storeCode; + } + + public String getPosTerminalNo() { + return posTerminalNo; + } + + public void setPosTerminalNo(String posTerminalNo) { + this.posTerminalNo = posTerminalNo; + } + + public Long getStaffId() { + return staffId; + } + + public void setStaffId(Long staffId) { + this.staffId = staffId; + } + + public String getTransDate() { + return transDate; + } + + public void setTransDate(String transDate) { + this.transDate = transDate; + } + + public String getTransTime() { + return transTime; + } + + public void setTransTime(String transTime) { + this.transTime = transTime; + } + + public Long getConsumerNo() { + return consumerNo; + } + + public void setConsumerNo(Long consumerNo) { + this.consumerNo = consumerNo; + } + + public String getSalesType() { + return salesType; + } + + public void setSalesType(String salesType) { + this.salesType = salesType; + } + + public Double getNetAmt() { + return netAmt; + } + + public void setNetAmt(Double netAmt) { + this.netAmt = netAmt; + } + + public Double getGrossAmt() { + return grossAmt; + } + + public void setGrossAmt(Double grossAmt) { + this.grossAmt = grossAmt; + } + + public Double getPayment() { + return payment; + } + + public void setPayment(Double payment) { + this.payment = payment; + } + + public Double getDiscountAmt() { + return discountAmt; + } + + public void setDiscountAmt(Double discountAmt) { + this.discountAmt = discountAmt; + } + + public Double getCostAmt() { + return costAmt; + } + + public void setCostAmt(Double costAmt) { + this.costAmt = costAmt; + } + + public Long getManagerId() { + return managerId; + } + + public void setManagerId(Long managerId) { + this.managerId = managerId; + } + + public Boolean getIsTraining() { + return isTraining; + } + + public void setIsTraining(Boolean isTraining) { + this.isTraining = isTraining; + } + + public String getStatementNo() { + return statementNo; + } + + public void setStatementNo(String statementNo) { + this.statementNo = statementNo; + } + + public String getPostingStatus() { + return postingStatus; + } + + public void setPostingStatus(String postingStatus) { + this.postingStatus = postingStatus; + } + + public String getPostStatementNo() { + return postStatementNo; + } + + public void setPostStatementNo(String postStatementNo) { + this.postStatementNo = postStatementNo; + } +} diff --git a/src/main/java/com/tarento/analytics/model/dashboardConfig/Chart.java b/src/main/java/com/tarento/analytics/model/dashboardConfig/Chart.java new file mode 100644 index 0000000000000000000000000000000000000000..9a83dbf1ee0bfa78ae2edeffe56fc4ff02293c83 --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/dashboardConfig/Chart.java @@ -0,0 +1,108 @@ +package com.tarento.analytics.model.dashboardConfig; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class Chart { + +@JsonProperty("id") +private Long id; +@JsonProperty("name") +private String name; +@JsonProperty("code") +private String code; +@JsonProperty("serviceApi") +private String serviceApi; +@JsonProperty("chartType") +private String chartType; +@JsonProperty("headersAvailable") +private Boolean headersAvailable; +@JsonProperty("filter") +private String filter; +@JsonProperty("headers") +private List<Header> headers = null; + + +public String getFilter() { + return filter; +} + +public void setFilter(String filter) { + this.filter = filter; +} + +@JsonProperty("id") +public Long getId() { +return id; +} + +@JsonProperty("id") +public void setId(Long id) { +this.id = id; +} + +@JsonProperty("name") +public String getName() { +return name; +} + +@JsonProperty("name") +public void setName(String name) { +this.name = name; +} + +@JsonProperty("code") +public String getCode() { +return code; +} + +@JsonProperty("code") +public void setCode(String code) { +this.code = code; +} + +@JsonProperty("serviceApi") +public String getServiceApi() { +return serviceApi; +} + +@JsonProperty("serviceApi") +public void setServiceApi(String serviceApi) { +this.serviceApi = serviceApi; +} + +@JsonProperty("chartType") +public String getChartType() { +return chartType; +} + +@JsonProperty("chartType") +public void setChartType(String chartType) { +this.chartType = chartType; +} + +@JsonProperty("headersAvailable") +public Boolean getHeadersAvailable() { +return headersAvailable; +} + +@JsonProperty("headersAvailable") +public void setHeadersAvailable(Boolean headersAvailable) { +this.headersAvailable = headersAvailable; +} + +@JsonProperty("headers") +public List<Header> getHeaders() { +return headers; +} + +@JsonProperty("headers") +public void setHeaders(List<Header> headers) { +this.headers = headers; +} + + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/model/dashboardConfig/Dashboard.java b/src/main/java/com/tarento/analytics/model/dashboardConfig/Dashboard.java new file mode 100644 index 0000000000000000000000000000000000000000..f06271f76719619555a33e97fbaa2161f071c3c4 --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/dashboardConfig/Dashboard.java @@ -0,0 +1,100 @@ +package com.tarento.analytics.model.dashboardConfig; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.tarento.analytics.dto.DashboardDto; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class Dashboard { + +@JsonProperty("id") +private Long id; +@JsonProperty("name") +private String name; +@JsonProperty("code") +private String code; +@JsonProperty("isActive") +private Boolean isActive; +@JsonProperty("description") +private String description; +@JsonProperty("placement") +private String placement; +@JsonProperty("visualizations") +private List<Visualization> visualizations = null; + +public Dashboard() {} +public Dashboard(DashboardDto dto) { + this.id = dto.getId(); + this.name = dto.getName(); + this.code = dto.getCode(); + this.description = dto.getDescription(); + this.placement = dto.getPlacement(); +} + +public String getPlacement() { + return placement; +} +public void setPlacement(String placement) { + this.placement = placement; +} +public Long getId() { + return id; +} + +public void setId(Long id) { + this.id = id; +} + +@JsonProperty("name") +public String getName() { +return name; +} + +@JsonProperty("name") +public void setName(String name) { +this.name = name; +} + +@JsonProperty("code") +public String getCode() { +return code; +} + +@JsonProperty("code") +public void setCode(String code) { +this.code = code; +} + +@JsonProperty("isActive") +public Boolean getIsActive() { +return isActive; +} + +@JsonProperty("isActive") +public void setIsActive(Boolean isActive) { +this.isActive = isActive; +} + +@JsonProperty("description") +public String getDescription() { +return description; +} + +@JsonProperty("description") +public void setDescription(String description) { +this.description = description; +} + +@JsonProperty("visualizations") +public List<Visualization> getVisualizations() { +return visualizations; +} + +@JsonProperty("visualizations") +public void setVisualizations(List<Visualization> visualizations) { +this.visualizations = visualizations; +} + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/model/dashboardConfig/Header.java b/src/main/java/com/tarento/analytics/model/dashboardConfig/Header.java new file mode 100644 index 0000000000000000000000000000000000000000..60572bbf9bac96e652188614a13ad3384e50fdb8 --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/dashboardConfig/Header.java @@ -0,0 +1,55 @@ +package com.tarento.analytics.model.dashboardConfig; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class Header { + +@JsonProperty("id") +private Long id; +@JsonProperty("label") +private String label; +@JsonProperty("data") +private String data; +@JsonProperty("field") +private String field; + + +public Long getId() { + return id; +} + +public void setId(Long id) { + this.id = id; +} + +public String getField() { + return field; +} + +public void setField(String field) { + this.field = field; +} + +@JsonProperty("label") +public String getLabel() { +return label; +} + +@JsonProperty("label") +public void setLabel(String label) { +this.label = label; +} + +@JsonProperty("data") +public String getData() { +return data; +} + +@JsonProperty("data") +public void setData(String data) { +this.data = data; +} + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/model/dashboardConfig/Visualization.java b/src/main/java/com/tarento/analytics/model/dashboardConfig/Visualization.java new file mode 100644 index 0000000000000000000000000000000000000000..f88286ff8d5fd1144444bfbebbdd206eb546e03e --- /dev/null +++ b/src/main/java/com/tarento/analytics/model/dashboardConfig/Visualization.java @@ -0,0 +1,114 @@ +package com.tarento.analytics.model.dashboardConfig; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class Visualization { + +@JsonProperty("id") +private Long id; +@JsonProperty("name") +private String name; +@JsonProperty("description") +private String description; +@JsonProperty("chartType") +private String chartType; +@JsonProperty("charts") +private List<Chart> charts = null; +private List<String> storeId; +@JsonProperty("salesAreaCode") +private String salesAreaCode; +@JsonProperty("countryCode") +private String countryCode; + + +public List<String> getStoreId() { + return storeId; +} + + +@JsonProperty("visualRank") +private Long visualRank; + + +public void setStoreId(List<String> storeId) { + this.storeId = storeId; +} + +public String getSalesAreaCode() { + return salesAreaCode; +} + +public void setSalesAreaCode(String salesAreaCode) { + this.salesAreaCode = salesAreaCode; +} + +public String getCountryCode() { + return countryCode; +} + +public void setCountryCode(String countryCode) { + this.countryCode = countryCode; +} + +public Long getVisualRank() { + return visualRank; +} + +public void setVisualRank(Long visualRank) { + this.visualRank = visualRank; +} + +public String getChartType() { + return chartType; +} + +public void setChartType(String chartType) { + this.chartType = chartType; +} + +@JsonProperty("id") +public Long getId() { +return id; +} + +@JsonProperty("id") +public void setId(Long id) { +this.id = id; +} + +@JsonProperty("name") +public String getName() { +return name; +} + +@JsonProperty("name") +public void setName(String name) { +this.name = name; +} + +@JsonProperty("description") +public String getDescription() { +return description; +} + +@JsonProperty("description") +public void setDescription(String description) { +this.description = description; +} + +@JsonProperty("charts") +public List<Chart> getCharts() { +return charts; +} + +@JsonProperty("charts") +public void setCharts(List<Chart> charts) { +this.charts = charts; +} + + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/org/service/ClientService.java b/src/main/java/com/tarento/analytics/org/service/ClientService.java new file mode 100644 index 0000000000000000000000000000000000000000..877ac55544fe93d0d44c75f67297c6a892a05479 --- /dev/null +++ b/src/main/java/com/tarento/analytics/org/service/ClientService.java @@ -0,0 +1,14 @@ +package com.tarento.analytics.org.service; + +import java.io.IOException; +import java.util.List; + +import com.tarento.analytics.dto.*; +import com.tarento.analytics.exception.AINException; + +public interface ClientService { + + public AggregateDto getAggregatedData(String profileName, AggregateRequestDto req, List<RoleDto> roles) throws AINException, IOException; + public List<DashboardHeaderDto> getHeaderData(CummulativeDataRequestDto requestDto, List<RoleDto> roles) throws AINException; + +} diff --git a/src/main/java/com/tarento/analytics/org/service/ClientServiceFactory.java b/src/main/java/com/tarento/analytics/org/service/ClientServiceFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..70e4e7d575d12f08642eb0252cc1f5110f46f4ce --- /dev/null +++ b/src/main/java/com/tarento/analytics/org/service/ClientServiceFactory.java @@ -0,0 +1,49 @@ +package com.tarento.analytics.org.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.ConfigurationLoader; +import com.tarento.analytics.constant.Constants; + +@Component +public class ClientServiceFactory { + + @Autowired + TarentoServiceImpl tarentoServiceImpl; + + @Autowired + ReportServiceImpl reportServiceImpl; + + @Autowired + MdmsServiceImpl mdmsServiceImpl; + + @Autowired + ConfigurationLoader configurationLoader; + + public ClientService getInstance(Constants.ClienServiceType clientServiceName){ + + if(clientServiceName.equals(Constants.ClienServiceType.DEFAULT_CLIENT)) + return tarentoServiceImpl; + else if(clientServiceName.equals(Constants.ClienServiceType.MDMS_CLIENT)) + return mdmsServiceImpl; + + else + throw new RuntimeException(clientServiceName + "not found"); + + } + + public ClientService get(String profileName, String chartId){ + ObjectNode node = configurationLoader.getConfigForProfile(profileName, Constants.ConfigurationFiles.CHART_API_CONFIG); + ObjectNode chartNode = (ObjectNode) node.get(chartId); + return tarentoServiceImpl; + } + + public ClientService getReport(String profileName, String chartId){ + ObjectNode node = configurationLoader.getConfigForProfile(profileName, Constants.ConfigurationFiles.REPORT_API_CONFIG); + ObjectNode chartNode = (ObjectNode) node.get(chartId); + return reportServiceImpl; + } + +} diff --git a/src/main/java/com/tarento/analytics/org/service/MdmsApiMappings.java b/src/main/java/com/tarento/analytics/org/service/MdmsApiMappings.java new file mode 100644 index 0000000000000000000000000000000000000000..13041a4296a15a1516445e0896b37f048e558c12 --- /dev/null +++ b/src/main/java/com/tarento/analytics/org/service/MdmsApiMappings.java @@ -0,0 +1,159 @@ +package com.tarento.analytics.org.service; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.service.impl.RestService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Component +public class MdmsApiMappings { + + + private String testingTenantId = "pb.testing"; + private static Logger logger = LoggerFactory.getLogger(MdmsApiMappings.class); + + private Map<String, String> ddrTenantMapping = new HashMap<>(); + private Map<String, List<String>> ddrTenantMapping1 = new HashMap<>(); + + @Value("${egov.mdms-service.target.url}") + private String mdmsServiceSearchUri; + + @Autowired + private RestService restService; + + @Autowired + private ObjectMapper mapper; + + @Value("${egov.mdms-service.request}") + private String REQUEST_INFO_STR ;//="{\"RequestInfo\":{\"authToken\":\"\"},\"MdmsCriteria\":{\"tenantId\":\"pb\",\"moduleDetails\":[{\"moduleName\":\"tenant\",\"masterDetails\":[{\"name\":\"tenants\"}]}]}}"; + + + /*@PostConstruct + public void loadMdmsService() throws Exception { + + JsonNode requestInfo = mapper.readTree(REQUEST_INFO_STR); + try { + JsonNode response = restService.post(mdmsServiceSearchUri, "", requestInfo); + ArrayNode tenants = (ArrayNode) response.findValues(Constants.MDMSKeys.TENANTS).get(0); + + + for(JsonNode tenant : tenants) { + JsonNode tenantId = tenant.findValue(Constants.MDMSKeys.CODE); + JsonNode ddrCode = tenant.findValue(Constants.MDMSKeys.DISTRICT_CODE); + JsonNode ddrName = tenant.findValue(Constants.MDMSKeys.DDR_NAME); + + if(!tenantId.asText().equalsIgnoreCase(testingTenantId)){ + if(!ddrTenantMapping1.containsKey(ddrName.asText())){ + List<String> tenantList = new ArrayList<>(); + tenantList.add(tenantId.asText()); + ddrTenantMapping1.put(ddrName.asText(),tenantList); + } else { + ddrTenantMapping1.get(ddrName.asText()).add(tenantId.asText()); + } + + if (!ddrTenantMapping.containsKey(ddrCode.asText())){ + ddrTenantMapping.put(ddrCode.asText(), ddrName.asText()); + } + } + + } + } catch (Exception e){ + getDefaultMapping(); + logger.error("Loading Mdms service error: "+e.getMessage()+" :: loaded default DDRs"); + } + //logger.info("ddrTenantMapping = "+ddrTenantMapping); + logger.info("ddrTenantMapping1 = "+ddrTenantMapping1); + + }*/ + + public String getDDRNameByCode(String ddrCode){ + return ddrTenantMapping.getOrDefault(ddrCode, ""); + } + + public List<String> getTenantIds(String ddrCode){ + return ddrTenantMapping1.getOrDefault(ddrCode, new ArrayList<>()); + } + + public String getDDRName(String tenantId){ + + for(Map.Entry entry :ddrTenantMapping1.entrySet()){ + List<String> values = (List<String>) entry.getValue(); + if(values.contains(tenantId)) return entry.getKey().toString(); + + } + return null; + + } + + public Map<String, List<String>> getGroupedTenants(List<String> tenants){ + + Map<String, List<String>> groupTenantIds = new HashMap<>(); + + if(tenants!=null){ + for(String tenant : tenants) { + + String ddrName = getDDRName(tenant); + if (groupTenantIds.containsKey(ddrName)){ + groupTenantIds.get(ddrName).add(tenant); + + } else { + List<String> tenantList = new ArrayList<>(); + tenantList.add(tenant); + groupTenantIds.put(ddrName,tenantList); + } + + } + } + + return groupTenantIds; + } + + + public Map<String, List<String>> getAll(){ + return ddrTenantMapping1; + } + + private void getDefaultMapping(){ + + ddrTenantMapping.put("1", "Amritsar-DDR"); + ddrTenantMapping.put("2", "Patiala-DDR"); + ddrTenantMapping.put("3", "Bathinda-DDR"); + ddrTenantMapping.put("4", "Ferozepur-DDR"); + ddrTenantMapping.put("5", "Ludhiana-DDR"); + ddrTenantMapping.put("6", "Ferozepur-DDR"); + ddrTenantMapping.put("7", "Ferozepur-DDR"); + ddrTenantMapping.put("8", "Amritsar-DDR"); + ddrTenantMapping.put("9", "Jalandhar-DDR"); + ddrTenantMapping.put("10", "Jalandhar-DDR"); + + ddrTenantMapping.put("11", "Jalandhar-DDR"); + ddrTenantMapping.put("12", "Ludhiana-DDR"); + ddrTenantMapping.put("13", "Bathinda-DDR"); + ddrTenantMapping.put("14", "Ferozepur-DDR"); + ddrTenantMapping.put("15", "Patiala-DDR"); + ddrTenantMapping.put("16", "Bathinda-DDR"); + ddrTenantMapping.put("17", "Jalandhar-DDR"); + ddrTenantMapping.put("18", "Pathankot-MC"); + ddrTenantMapping.put("19", "Patiala-DDR"); + ddrTenantMapping.put("20", "Ludhiana-DDR"); + ddrTenantMapping.put("21", "Patiala-DDR"); + ddrTenantMapping.put("22", "Bathinda-DDR"); + ddrTenantMapping.put("140001", "Ludhiana-DDR"); + + } + + +} + diff --git a/src/main/java/com/tarento/analytics/org/service/MdmsServiceImpl.java b/src/main/java/com/tarento/analytics/org/service/MdmsServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..fca8d46a17108c2294abf93c16b6e5d1d9cd511c --- /dev/null +++ b/src/main/java/com/tarento/analytics/org/service/MdmsServiceImpl.java @@ -0,0 +1,155 @@ +package com.tarento.analytics.org.service; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.ConfigurationLoader; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.*; +import com.tarento.analytics.enums.ChartType; +import com.tarento.analytics.exception.AINException; +import com.tarento.analytics.handler.IResponseHandler; +import com.tarento.analytics.handler.ResponseHandlerFactory; +import com.tarento.analytics.service.QueryService; +import com.tarento.analytics.service.impl.RestService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Component +public class MdmsServiceImpl implements ClientService { + + public static final Logger logger = LoggerFactory.getLogger(MdmsServiceImpl.class); + + @Autowired + private QueryService queryService; + + @Autowired + private RestService restService; + + @Autowired + private ConfigurationLoader configurationLoader; + + @Autowired + private ResponseHandlerFactory responseHandlerFactory; + + @Autowired + private MdmsApiMappings mdmsApiMappings; + + Map<String, List<String>> groupTenantIds; + + @Override + public AggregateDto getAggregatedData(String profileName, AggregateRequestDto request, List<RoleDto> roles) throws AINException, IOException { + // Read visualization Code + String chartId = request.getVisualizationCode(); + + // Load Chart API configuration to Object Node for easy retrieval later + ObjectNode node = configurationLoader.getConfigForProfile(profileName, Constants.ConfigurationFiles.CHART_API_CONFIG); + ObjectNode chartNode = (ObjectNode) node.get(chartId); + ChartType chartType = ChartType.fromValue(chartNode.get(Constants.JsonPaths.CHART_TYPE).asText()); + groupTenantIds = mdmsApiMappings.getGroupedTenants((List) request.getFilters().get("tenantId")); + + + ObjectNode reponseNode = buildResponse(chartNode, request); + logger.info("reponseNode do == "+reponseNode); + + //replacing default values by + request.setChartNode(chartNode); + IResponseHandler responseHandler = responseHandlerFactory.getInstance(chartType); + AggregateDto aggregateDto = new AggregateDto(); + if (reponseNode.fields().hasNext()) { + + aggregateDto = responseHandler.translate(profileName, request, reponseNode); + } + return aggregateDto; + } + + public ObjectNode buildResponse(ObjectNode chartNode, AggregateRequestDto request) { + + String plotName = chartNode.get("plotLabel") == null ? "DDR" : chartNode.get("plotLabel").asText(); + ChartType chartType = ChartType.fromValue(chartNode.get(Constants.JsonPaths.CHART_TYPE).asText()); + boolean isDefaultPresent = chartType.equals(ChartType.LINE) && chartNode.get(Constants.JsonPaths.INTERVAL) != null; + boolean isRequestContainsInterval = null == request.getRequestDate() ? false : (request.getRequestDate().getInterval() != null && !request.getRequestDate().getInterval().isEmpty()); + String interval = isRequestContainsInterval ? request.getRequestDate().getInterval() : (isDefaultPresent ? chartNode.get(Constants.JsonPaths.INTERVAL).asText() : ""); + + ObjectNode aggrObjectNode = JsonNodeFactory.instance.objectNode(); + ObjectNode nodes = JsonNodeFactory.instance.objectNode(); + + ArrayNode queries = (ArrayNode) chartNode.get(Constants.JsonPaths.QUERIES); + queries.forEach(query -> { + AggregateRequestDto requestDto = request; + + String module = query.get(Constants.JsonPaths.MODULE).asText(); + if (request.getModuleLevel().equals(Constants.Modules.HOME_REVENUE) || + request.getModuleLevel().equals(Constants.Modules.HOME_SERVICES) || + query.get(Constants.JsonPaths.MODULE).asText().equals(Constants.Modules.COMMON) || + request.getModuleLevel().equals(module)) { + + String indexName = query.get(Constants.JsonPaths.INDEX_NAME).asText(); + // intercept request + _search operation + ObjectNode aggrResponse = aggrResponseBuilder(plotName, requestDto, query, indexName, interval); + + if(nodes.has(indexName)) { + indexName = indexName + "_1"; + } + logger.info("indexName +"+indexName); + nodes.set(indexName, aggrResponse); + aggrObjectNode.set(Constants.JsonPaths.AGGREGATIONS, nodes); + } + }); + return aggrObjectNode; + } + + + private ObjectNode aggrResponseBuilder(String nodeName, AggregateRequestDto requestDto, JsonNode query, String indexName, String interval) { + + ObjectNode nodes = JsonNodeFactory.instance.objectNode(); + ArrayNode bucket = JsonNodeFactory.instance.arrayNode(); + + if (groupTenantIds.size() == 0) { //for all DDRS, no tenantId filter present + bucket = getBuckets(mdmsApiMappings.getAll(), requestDto, query, indexName); + requestDto.getFilters().clear(); + + } else { //comes from global filter + bucket = getBuckets(groupTenantIds, requestDto, query, indexName); + + } + ObjectNode buckets = JsonNodeFactory.instance.objectNode(); + buckets.set("buckets", bucket); + nodes.put(nodeName, buckets); + return nodes; + + } + + private ArrayNode getBuckets(Map<String, List<String>> map, AggregateRequestDto requestDto, JsonNode query, String indexName) { + ObjectNode nodes = JsonNodeFactory.instance.objectNode(); + ArrayNode bucket = JsonNodeFactory.instance.arrayNode(); + for (String ddrkey : map.keySet()) { + List<String> tenantIds = map.get(ddrkey); + requestDto.getFilters().put("tenantId", tenantIds); + ObjectNode requestNode = queryService.getChartConfigurationQuery(requestDto, query, indexName, null); + logger.info("requestNode " + requestNode); + + try { + ObjectNode aggrNode = (ObjectNode) restService.search(indexName, requestNode.toString(),Constants.PRIMARY); + + if(!ddrkey.equalsIgnoreCase("null")) bucket.add(((ObjectNode) aggrNode.get(Constants.JsonPaths.AGGREGATIONS)).put("key", ddrkey)); + } catch (Exception e) { + logger.error("Encountered an Exception while Executing the Query : " + e.getMessage()); + } + } + return bucket; + } + + @Override + public List<DashboardHeaderDto> getHeaderData(CummulativeDataRequestDto requestDto, List<RoleDto> roles) throws AINException { + return null; + } +} diff --git a/src/main/java/com/tarento/analytics/org/service/ReportServiceImpl.java b/src/main/java/com/tarento/analytics/org/service/ReportServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..f99033678b2704525f5fd4cc633b5c9a76627945 --- /dev/null +++ b/src/main/java/com/tarento/analytics/org/service/ReportServiceImpl.java @@ -0,0 +1,208 @@ +package com.tarento.analytics.org.service; + +import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.ConfigurationLoader; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.CummulativeDataRequestDto; +import com.tarento.analytics.dto.DashboardHeaderDto; +import com.tarento.analytics.dto.RoleDto; +import com.tarento.analytics.enums.AlwaysView; +import com.tarento.analytics.enums.ChartType; +import com.tarento.analytics.exception.AINException; +import com.tarento.analytics.handler.IResponseHandler; +import com.tarento.analytics.handler.InsightsHandler; +import com.tarento.analytics.handler.InsightsHandlerFactory; +import com.tarento.analytics.handler.ResponseHandlerFactory; +import com.tarento.analytics.model.InsightsConfiguration; +import com.tarento.analytics.service.QueryService; +import com.tarento.analytics.service.impl.RestService; + +@Component +public class ReportServiceImpl implements ClientService { + + public static final Logger logger = LoggerFactory.getLogger(TarentoServiceImpl.class); + ObjectMapper mapper = new ObjectMapper(); + char insightPrefix = 'i'; + + @Autowired + private QueryService queryService; + + @Autowired + private RestService restService; + + @Autowired + private ConfigurationLoader configurationLoader; + + @Autowired + private ResponseHandlerFactory responseHandlerFactory; + + @Autowired + private InsightsHandlerFactory insightsHandlerFactory; + + @Override + public AggregateDto getAggregatedData(String profileName, AggregateRequestDto request, List<RoleDto> roles) + throws AINException, IOException { + // Read visualization Code + String internalChartId = request.getVisualizationCode(); + ObjectNode aggrObjectNode = JsonNodeFactory.instance.objectNode(); + ObjectNode insightAggrObjectNode = JsonNodeFactory.instance.objectNode(); + ObjectNode nodes = JsonNodeFactory.instance.objectNode(); + ObjectNode insightNodes = JsonNodeFactory.instance.objectNode(); + Boolean continueWithInsight = Boolean.FALSE; + + // Load Chart API configuration to Object Node for easy retrieval later + ObjectNode node = configurationLoader.getConfigForProfile(profileName, + Constants.ConfigurationFiles.REPORT_API_CONFIG); + ObjectNode chartNode = (ObjectNode) node.get(internalChartId); + InsightsConfiguration insightsConfig = null; + if (chartNode.get(Constants.JsonPaths.INSIGHT) != null) { + insightsConfig = mapper.treeToValue(chartNode.get(Constants.JsonPaths.INSIGHT), + InsightsConfiguration.class); + } + ChartType chartType = ChartType.fromValue(chartNode.get(Constants.JsonPaths.CHART_TYPE).asText()); + boolean isDefaultPresent = chartType.equals(ChartType.LINE) + && chartNode.get(Constants.JsonPaths.INTERVAL) != null; + boolean isRequestContainsInterval = null == request.getRequestDate() ? false + : (request.getRequestDate().getInterval() != null && !request.getRequestDate().getInterval().isEmpty()); + String interval = isRequestContainsInterval ? request.getRequestDate().getInterval() + : (isDefaultPresent ? chartNode.get(Constants.JsonPaths.INTERVAL).asText() : ""); + if (chartNode.get(Constants.JsonPaths.ALWAYS_VIEW) != null) { + if (AlwaysView.MONTHWISE + .equals(AlwaysView.fromValue(chartNode.get(Constants.JsonPaths.ALWAYS_VIEW).asText()))) { + changeDatesToMonthWiseView(request); + } + } + executeConfiguredQueries(chartNode, aggrObjectNode, nodes, request, interval); + request.setChartNode(chartNode); + IResponseHandler responseHandler = responseHandlerFactory.getInstance(chartType); + AggregateDto aggregateDto = new AggregateDto(); + if (aggrObjectNode.fields().hasNext()) { + aggregateDto = responseHandler.translate(profileName, request, aggrObjectNode); + } + + if (insightsConfig != null && StringUtils.isNotBlank(insightsConfig.getInsightInterval())) { + continueWithInsight = getInsightsDate(request, insightsConfig.getInsightInterval()); + if (continueWithInsight) { + String insightVisualizationCode = insightPrefix + request.getVisualizationCode(); + request.setVisualizationCode(insightVisualizationCode); + executeConfiguredQueries(chartNode, insightAggrObjectNode, insightNodes, request, interval); + request.setChartNode(chartNode); + responseHandler = responseHandlerFactory.getInstance(chartType); + if (insightAggrObjectNode.fields().hasNext()) { + responseHandler.translate(profileName, request, insightAggrObjectNode); + } + InsightsHandler insightsHandler = insightsHandlerFactory.getInstance(chartType); + aggregateDto = insightsHandler.getInsights(aggregateDto, request.getVisualizationCode(), + request.getModuleLevel(), insightsConfig); + } + } + + return aggregateDto; + } + + private void executeConfiguredQueries(ObjectNode chartNode, ObjectNode aggrObjectNode, ObjectNode nodes, + AggregateRequestDto request, String interval) { + ArrayNode queries = (ArrayNode) chartNode.get(Constants.JsonPaths.QUERIES); + queries.forEach(query -> { + String module = query.get(Constants.JsonPaths.MODULE).asText(); + if (request.getModuleLevel().equals(Constants.Modules.HOME_REVENUE) + || request.getModuleLevel().equals(Constants.Modules.HOME_SERVICES) + || query.get(Constants.JsonPaths.MODULE).asText().equals(Constants.Modules.COMMON) + || request.getModuleLevel().equals(module)) { + + String indexName = query.get(Constants.JsonPaths.INDEX_NAME).asText(); + ObjectNode objectNode = queryService.getChartConfigurationQuery(request, query, indexName, interval); + String instance = query.get(Constants.JsonPaths.ES_INSTANCE).asText(); + try { + JsonNode aggrNode = restService.search(indexName, objectNode.toString(), instance); + if (nodes.has(indexName)) { + indexName = indexName + "_1"; + } + logger.info("indexName +" + indexName); + nodes.set(indexName, aggrNode.get(Constants.JsonPaths.AGGREGATIONS)); + } catch (Exception e) { + logger.error("Encountered an Exception while Executing the Query : " + e.getMessage()); + } + aggrObjectNode.set(Constants.JsonPaths.AGGREGATIONS, nodes); + + } + }); + } + + private void changeDatesToMonthWiseView(AggregateRequestDto request) { + Long daysBetween = daysBetween(Long.parseLong(request.getRequestDate().getStartDate()), + Long.parseLong(request.getRequestDate().getEndDate())); + if (daysBetween <= 30) { + Calendar startCal = Calendar.getInstance(); + Calendar endCal = Calendar.getInstance(); + startCal.setTime(new Date(Long.parseLong(request.getRequestDate().getStartDate()))); + startCal.set(Calendar.DAY_OF_MONTH, 1); + endCal.setTime(new Date(Long.parseLong(request.getRequestDate().getEndDate()))); + int month = endCal.get(Calendar.MONTH) + 1; + endCal.set(Calendar.MONTH, month + 1); + request.getRequestDate().setStartDate(String.valueOf(startCal.getTimeInMillis())); + request.getRequestDate().setEndDate(String.valueOf(endCal.getTimeInMillis())); + } + } + + private Boolean getInsightsDate(AggregateRequestDto request, String insightInterval) { + Long daysBetween = daysBetween(Long.parseLong(request.getRequestDate().getStartDate()), + Long.parseLong(request.getRequestDate().getEndDate())); + if (insightInterval.equals(Constants.Interval.month.toString()) && daysBetween > 32) { + return Boolean.FALSE; + } + if (insightInterval.equals(Constants.Interval.week.toString()) && daysBetween > 8) { + return Boolean.FALSE; + } + if (insightInterval.equals(Constants.Interval.year.toString()) && daysBetween > 366) { + return Boolean.FALSE; + } + Calendar startCal = Calendar.getInstance(); + Calendar endCal = Calendar.getInstance(); + startCal.setTime(new Date(Long.parseLong(request.getRequestDate().getStartDate()))); + endCal.setTime(new Date(Long.parseLong(request.getRequestDate().getEndDate()))); + if (insightInterval.equals(Constants.Interval.month.toString())) { + startCal.add(Calendar.MONTH, -1); + endCal.add(Calendar.MONTH, -1); + } else if (insightInterval.equals(Constants.Interval.week.toString())) { + startCal.add(Calendar.WEEK_OF_YEAR, -1); + endCal.add(Calendar.WEEK_OF_YEAR, -1); + } else if (StringUtils.isBlank(insightInterval) || insightInterval.equals(Constants.Interval.year.toString())) { + startCal.add(Calendar.YEAR, -1); + endCal.add(Calendar.YEAR, -1); + } + request.getRequestDate().setStartDate(String.valueOf(startCal.getTimeInMillis())); + request.getRequestDate().setEndDate(String.valueOf(endCal.getTimeInMillis())); + return Boolean.TRUE; + } + + public long daysBetween(Long start, Long end) { + return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start)); + } + + @Override + public List<DashboardHeaderDto> getHeaderData(CummulativeDataRequestDto requestDto, List<RoleDto> roles) + throws AINException { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/com/tarento/analytics/org/service/TarentoServiceImpl.java b/src/main/java/com/tarento/analytics/org/service/TarentoServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..8ce55610495bbf8f01ca6473771fa3dae7b4fdca --- /dev/null +++ b/src/main/java/com/tarento/analytics/org/service/TarentoServiceImpl.java @@ -0,0 +1,259 @@ +package com.tarento.analytics.org.service; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.ConfigurationLoader; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.AggregateDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.CummulativeDataRequestDto; +import com.tarento.analytics.dto.DashboardHeaderDto; +import com.tarento.analytics.dto.RoleDto; +import com.tarento.analytics.enums.AlwaysView; +import com.tarento.analytics.enums.ChartType; +import com.tarento.analytics.exception.AINException; +import com.tarento.analytics.handler.IResponseHandler; +import com.tarento.analytics.handler.InsightsHandler; +import com.tarento.analytics.handler.InsightsHandlerFactory; +import com.tarento.analytics.handler.ResponseHandlerFactory; +import com.tarento.analytics.model.InsightsConfiguration; +import com.tarento.analytics.service.QueryService; +import com.tarento.analytics.service.impl.RestService; + +@Component +public class TarentoServiceImpl implements ClientService { + + public static final Logger logger = LoggerFactory.getLogger(TarentoServiceImpl.class); + ObjectMapper mapper = new ObjectMapper(); + char insightPrefix = 'i'; + + @Autowired + private QueryService queryService; + + @Autowired + private RestService restService; + + @Autowired + private ConfigurationLoader configurationLoader; + + @Autowired + private ResponseHandlerFactory responseHandlerFactory; + + @Autowired + private InsightsHandlerFactory insightsHandlerFactory; + + @Override + public AggregateDto getAggregatedData(String profileName, AggregateRequestDto request, List<RoleDto> roles) + throws AINException, IOException { + // Read visualization Code + String internalChartId = request.getVisualizationCode(); + ObjectNode aggrObjectNode = JsonNodeFactory.instance.objectNode(); + ObjectNode insightAggrObjectNode = JsonNodeFactory.instance.objectNode(); + ObjectNode nodes = JsonNodeFactory.instance.objectNode(); + ObjectNode insightNodes = JsonNodeFactory.instance.objectNode(); + Boolean continueWithInsight = Boolean.FALSE; + + // Load Chart API configuration to Object Node for easy retrieval later + ObjectNode node = configurationLoader.getConfigForProfile(profileName, + Constants.ConfigurationFiles.CHART_API_CONFIG); + ObjectNode chartNode = (ObjectNode) node.get(internalChartId); + InsightsConfiguration insightsConfig = null; + if (chartNode.get(Constants.JsonPaths.INSIGHT) != null) { + insightsConfig = mapper.treeToValue(chartNode.get(Constants.JsonPaths.INSIGHT), + InsightsConfiguration.class); + } + ChartType chartType = ChartType.fromValue(chartNode.get(Constants.JsonPaths.CHART_TYPE).asText()); + boolean isDefaultPresent = chartType.equals(ChartType.LINE) + && chartNode.get(Constants.JsonPaths.INTERVAL) != null; + boolean isRequestContainsInterval = null == request.getRequestDate() ? false + : (request.getRequestDate().getInterval() != null && !request.getRequestDate().getInterval().isEmpty()); + String interval = isRequestContainsInterval ? request.getRequestDate().getInterval() + : (isDefaultPresent ? chartNode.get(Constants.JsonPaths.INTERVAL).asText() : ""); + if (chartNode.get(Constants.JsonPaths.ALWAYS_VIEW) != null) { + if (AlwaysView.MONTHWISE + .equals(AlwaysView.fromValue(chartNode.get(Constants.JsonPaths.ALWAYS_VIEW).asText()))) { + changeDatesToMonthWiseView(request); + } + } + applyVisibilityTint(profileName, request, roles); + executeConfiguredQueries(chartNode, aggrObjectNode, nodes, request, interval); + request.setChartNode(chartNode); + ChartType masqueradeChartType = null; + if (request.getVisualizationCode().equals("stateProfessionTable")) { + masqueradeChartType = ChartType.fromValue("dynamictable"); + } else { + masqueradeChartType = chartType; + } + IResponseHandler responseHandler = responseHandlerFactory.getInstance(masqueradeChartType); + AggregateDto aggregateDto = new AggregateDto(); + if (aggrObjectNode.fields().hasNext()) { + aggregateDto = responseHandler.translate(profileName, request, aggrObjectNode); + } + + if (insightsConfig != null && StringUtils.isNotBlank(insightsConfig.getInsightInterval())) { + continueWithInsight = getInsightsDate(request, insightsConfig.getInsightInterval()); + if (continueWithInsight) { + String insightVisualizationCode = insightPrefix + request.getVisualizationCode(); + request.setVisualizationCode(insightVisualizationCode); + executeConfiguredQueries(chartNode, insightAggrObjectNode, insightNodes, request, interval); + request.setChartNode(chartNode); + if (request.getVisualizationCode().equals("stateProfessionTable")) { + masqueradeChartType = ChartType.fromValue("dynamictable"); + } else { + masqueradeChartType = chartType; + } + responseHandler = responseHandlerFactory.getInstance(masqueradeChartType); + if (insightAggrObjectNode.fields().hasNext()) { + responseHandler.translate(profileName, request, insightAggrObjectNode); + } + InsightsHandler insightsHandler = insightsHandlerFactory.getInstance(chartType); + aggregateDto = insightsHandler.getInsights(aggregateDto, request.getVisualizationCode(), + request.getModuleLevel(), insightsConfig); + } + } + + return aggregateDto; + } + + private void applyVisibilityTint(String profileName, AggregateRequestDto request, List<RoleDto> roles) { + ObjectNode roleMappingNode = configurationLoader.getConfigForProfile(profileName, + ConfigurationLoader.ROLE_DASHBOARD_CONFIG); + ArrayNode rolesArray = (ArrayNode) roleMappingNode.findValue(Constants.DashBoardConfig.ROLES); + + rolesArray.forEach(role -> { + Object roleId = roles.stream() + .filter(x -> role.get(Constants.DashBoardConfig.ROLE_ID).asLong() == (x.getId())).findAny() + .orElse(null); + if (null != roleId) { + // checks role has given db id + role.get(Constants.DashBoardConfig.DASHBOARDS).forEach(db -> { + if (db.get(Constants.DashBoardConfig.ID).asText().equalsIgnoreCase(request.getDashboardId())) { + ArrayNode visibilityArray = (ArrayNode) db.get(Constants.DashBoardConfig.VISIBILITY); + if (visibilityArray != null) { + Map<String, Object> filterMap = new HashMap<>(); + visibilityArray.forEach(visibility -> { + String key = visibility.get(Constants.DashBoardConfig.KEY).asText(); + ArrayNode valueArray = (ArrayNode) visibility.get(Constants.DashBoardConfig.VALUE); + List<String> valueList = new ArrayList<>(); + valueArray.forEach(value -> { + valueList.add(value.asText()); + }); + if (!request.getFilters().containsKey(key)) { + filterMap.put(key, valueList); + } + }); + request.getFilters().putAll(filterMap); + } + } + }); + } + }); + } + + private void executeConfiguredQueries(ObjectNode chartNode, ObjectNode aggrObjectNode, ObjectNode nodes, + AggregateRequestDto request, String interval) { + ArrayNode queries = (ArrayNode) chartNode.get(Constants.JsonPaths.QUERIES); + queries.forEach(query -> { + String module = query.get(Constants.JsonPaths.MODULE).asText(); + if (request.getModuleLevel().equals(Constants.Modules.HOME_REVENUE) + || request.getModuleLevel().equals(Constants.Modules.HOME_SERVICES) + || query.get(Constants.JsonPaths.MODULE).asText().equals(Constants.Modules.COMMON) + || request.getModuleLevel().equals(module)) { + + String indexName = query.get(Constants.JsonPaths.INDEX_NAME).asText(); + ObjectNode objectNode = queryService.getChartConfigurationQuery(request, query, indexName, interval); + String instance = query.get(Constants.JsonPaths.ES_INSTANCE).asText(); + try { + JsonNode aggrNode = restService.search(indexName, objectNode.toString(), instance); + if (nodes.has(indexName)) { + indexName = indexName + "_1"; + } + logger.info("indexName +" + indexName); + nodes.set(indexName, aggrNode.get(Constants.JsonPaths.AGGREGATIONS)); + } catch (Exception e) { + logger.error("Encountered an Exception while Executing the Query : " + e.getMessage()); + } + aggrObjectNode.set(Constants.JsonPaths.AGGREGATIONS, nodes); + + } + }); + } + + private void changeDatesToMonthWiseView(AggregateRequestDto request) { + Long daysBetween = daysBetween(Long.parseLong(request.getRequestDate().getStartDate()), + Long.parseLong(request.getRequestDate().getEndDate())); + if (daysBetween <= 30) { + Calendar startCal = Calendar.getInstance(); + Calendar endCal = Calendar.getInstance(); + startCal.setTime(new Date(Long.parseLong(request.getRequestDate().getStartDate()))); + startCal.set(Calendar.DAY_OF_MONTH, 1); + endCal.setTime(new Date(Long.parseLong(request.getRequestDate().getEndDate()))); + int month = endCal.get(Calendar.MONTH) + 1; + endCal.set(Calendar.MONTH, month + 1); + request.getRequestDate().setStartDate(String.valueOf(startCal.getTimeInMillis())); + request.getRequestDate().setEndDate(String.valueOf(endCal.getTimeInMillis())); + } + } + + private Boolean getInsightsDate(AggregateRequestDto request, String insightInterval) { + Long daysBetween = daysBetween(Long.parseLong(request.getRequestDate().getStartDate()), + Long.parseLong(request.getRequestDate().getEndDate())); + if (insightInterval.equalsIgnoreCase(Constants.Interval.month.toString()) && daysBetween > 32) { + return Boolean.FALSE; + } + if (insightInterval.equalsIgnoreCase(Constants.Interval.week.toString()) && daysBetween > 8) { + return Boolean.FALSE; + } + if (insightInterval.equalsIgnoreCase(Constants.Interval.year.toString()) && daysBetween > 366) { + return Boolean.FALSE; + } + Calendar startCal = Calendar.getInstance(); + Calendar endCal = Calendar.getInstance(); + startCal.setTime(new Date(Long.parseLong(request.getRequestDate().getStartDate()))); + endCal.setTime(new Date(Long.parseLong(request.getRequestDate().getEndDate()))); + if (insightInterval.equalsIgnoreCase(Constants.Interval.month.toString())) { + startCal.add(Calendar.MONTH, -1); + endCal.add(Calendar.MONTH, -1); + } else if (insightInterval.equalsIgnoreCase(Constants.Interval.week.toString())) { + startCal.add(Calendar.WEEK_OF_YEAR, -1); + endCal.add(Calendar.WEEK_OF_YEAR, -1); + } else if (StringUtils.isBlank(insightInterval) + || insightInterval.equalsIgnoreCase(Constants.Interval.year.toString())) { + startCal.add(Calendar.YEAR, -1); + endCal.add(Calendar.YEAR, -1); + } + request.getRequestDate().setStartDate(String.valueOf(startCal.getTimeInMillis())); + request.getRequestDate().setEndDate(String.valueOf(endCal.getTimeInMillis())); + return Boolean.TRUE; + } + + public long daysBetween(Long start, Long end) { + return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start)); + } + + @Override + public List<DashboardHeaderDto> getHeaderData(CummulativeDataRequestDto requestDto, List<RoleDto> roles) + throws AINException { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/com/tarento/analytics/producer/AnalyticsProducer.java b/src/main/java/com/tarento/analytics/producer/AnalyticsProducer.java new file mode 100644 index 0000000000000000000000000000000000000000..bbe8c60a07bd937a56c5ea4b78d0d1c40c0fced2 --- /dev/null +++ b/src/main/java/com/tarento/analytics/producer/AnalyticsProducer.java @@ -0,0 +1,22 @@ +package com.tarento.analytics.producer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.stereotype.Service; + +@Service +public class AnalyticsProducer { + + public static final Logger LOGGER = LoggerFactory.getLogger(AnalyticsProducer.class); + + @Autowired + private KafkaTemplate<String, Object> kafkaTemplate; + + public void pushToPipeline(Object object, String topic, String key) { + LOGGER.info("Pushing data to : " + topic); + kafkaTemplate.send(topic, key, object); + } +} + diff --git a/src/main/java/com/tarento/analytics/producer/AnalyticsServiceProducerConfig.java b/src/main/java/com/tarento/analytics/producer/AnalyticsServiceProducerConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..5bafaf6f3d33b47e6f51b289f9f481a3da3f25bb --- /dev/null +++ b/src/main/java/com/tarento/analytics/producer/AnalyticsServiceProducerConfig.java @@ -0,0 +1,49 @@ +package com.tarento.analytics.producer; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.common.serialization.StringSerializer; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.kafka.annotation.EnableKafka; +import org.springframework.kafka.core.DefaultKafkaProducerFactory; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.kafka.core.ProducerFactory; +import org.springframework.kafka.support.serializer.JsonSerializer; + +@Configuration +@EnableKafka +public class AnalyticsServiceProducerConfig { + + @Value("${spring.kafka.bootstrap.servers}") + private String serverConfig; + + @Value("${kafka.producer.config.retries_config}") + private Integer retriesConfig; + + @Value("${kafka.producer.config.batch_size_config}") + private Integer batchSizeConfig; + + @Value("${kafka.producer.config.linger_ms_config}") + private Integer lingerMsConfig; + + @Value("${kafka.producer.config.buffer_memory_config}") + private Integer bufferMemoryConfig; + + @Bean + public ProducerFactory<String, Object> producerFactory() { + Map<String, Object> configProps = new HashMap<>(); + configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, serverConfig); + configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); + configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); + return new DefaultKafkaProducerFactory<>(configProps); + } + + @Bean + public KafkaTemplate<String, Object> kafkaTemplate() { + return new KafkaTemplate<>(producerFactory()); + } +} diff --git a/src/main/java/com/tarento/analytics/producer/HashMapSerializer.java b/src/main/java/com/tarento/analytics/producer/HashMapSerializer.java new file mode 100644 index 0000000000000000000000000000000000000000..b9f3537e6caf9acebeafb81defb24f8b72ee6d91 --- /dev/null +++ b/src/main/java/com/tarento/analytics/producer/HashMapSerializer.java @@ -0,0 +1,37 @@ +package com.tarento.analytics.producer; + +import java.util.Map; + +import org.apache.kafka.common.serialization.Serializer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class HashMapSerializer implements Serializer<Map> { + + private static final Logger logger = LoggerFactory.getLogger(HashMapSerializer.class); + + @Override + public void close() { + // TODO Auto-generated method stub + } + + @Override + public void configure(Map<String, ?> arg0, boolean arg1) { + // TODO Auto-generated method stub + } + + @Override + public byte[] serialize(String topic, Map data) { + byte[] value = null; + ObjectMapper objectMapper = new ObjectMapper(); + try { + value = objectMapper.writeValueAsString(data).getBytes(); + } catch (JsonProcessingException e) { + logger.error("Exception in serialize {} ", e.getMessage()); + } + return value; + } +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/producer/JavaSerializer.java b/src/main/java/com/tarento/analytics/producer/JavaSerializer.java new file mode 100644 index 0000000000000000000000000000000000000000..3c5fe25fca5c1bb3ce06417e13c5655f61e32caa --- /dev/null +++ b/src/main/java/com/tarento/analytics/producer/JavaSerializer.java @@ -0,0 +1,37 @@ +package com.tarento.analytics.producer; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.Map; + +import org.apache.kafka.common.serialization.Serializer; + +public class JavaSerializer implements Serializer<Object> { + + @Override + public byte[] serialize(String topic, Object data) { + try { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + ObjectOutputStream objectStream = new ObjectOutputStream(byteStream); + objectStream.writeObject(data); + objectStream.flush(); + objectStream.close(); + return byteStream.toByteArray(); + } + catch (IOException e) { + throw new IllegalStateException("Can't serialize object: " + data, e); + } + } + + @Override + public void configure(Map<String, ?> configs, boolean isKey) { + + } + + @Override + public void close() { + + } + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/query/model/Aggregation.java b/src/main/java/com/tarento/analytics/query/model/Aggregation.java new file mode 100644 index 0000000000000000000000000000000000000000..b7f4c8a28d8e67c92862ee3ba500e1a5e070bb4d --- /dev/null +++ b/src/main/java/com/tarento/analytics/query/model/Aggregation.java @@ -0,0 +1,5 @@ +package com.tarento.analytics.query.model; + +public class Aggregation { + +} diff --git a/src/main/java/com/tarento/analytics/query/model/Query.java b/src/main/java/com/tarento/analytics/query/model/Query.java new file mode 100644 index 0000000000000000000000000000000000000000..d48b10b25da34e74f3ee0c408335a3bacc0ae6c4 --- /dev/null +++ b/src/main/java/com/tarento/analytics/query/model/Query.java @@ -0,0 +1,28 @@ +package com.tarento.analytics.query.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Query { + + @JsonProperty("x_axis") + public XAxis xAxis; + + @JsonProperty("y_axis") + public YAxis yAxis; + + public XAxis getxAxis() { + return xAxis; + } + + public void setxAxis(XAxis xAxis) { + this.xAxis = xAxis; + } + + public YAxis getyAxis() { + return yAxis; + } + + public void setyAxis(YAxis yAxis) { + this.yAxis = yAxis; + } +} diff --git a/src/main/java/com/tarento/analytics/query/model/XAxis.java b/src/main/java/com/tarento/analytics/query/model/XAxis.java new file mode 100644 index 0000000000000000000000000000000000000000..20e658847cde6dc02ac0002d4065ef27827eb7ee --- /dev/null +++ b/src/main/java/com/tarento/analytics/query/model/XAxis.java @@ -0,0 +1,17 @@ +package com.tarento.analytics.query.model; + +import java.util.Map; + +public class XAxis { + + private Map<String, Object> aggregation; + + public Map<String, Object> getAggregation() { + return aggregation; + } + + public void setAggregation(Map<String, Object> aggregation) { + this.aggregation = aggregation; + } + +} diff --git a/src/main/java/com/tarento/analytics/query/model/YAxis.java b/src/main/java/com/tarento/analytics/query/model/YAxis.java new file mode 100644 index 0000000000000000000000000000000000000000..e44af59a16b1527b6053eb0cc68577828bd413dc --- /dev/null +++ b/src/main/java/com/tarento/analytics/query/model/YAxis.java @@ -0,0 +1,17 @@ +package com.tarento.analytics.query.model; + +import java.util.Map; + +public class YAxis { + + Map<String, Object> aggregation; + + public Map<String, Object> getAggregation() { + return aggregation; + } + + public void setAggregation(Map<String, Object> aggregation) { + this.aggregation = aggregation; + } + +} diff --git a/src/main/java/com/tarento/analytics/repository/ElasticSearchRepository.java b/src/main/java/com/tarento/analytics/repository/ElasticSearchRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..1da7bb415cd288d32e79a6931d368efd160cd0f7 --- /dev/null +++ b/src/main/java/com/tarento/analytics/repository/ElasticSearchRepository.java @@ -0,0 +1,86 @@ +package com.tarento.analytics.repository; + +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.RestTemplate; + +import com.tarento.analytics.dto.MappingDto; +import com.tarento.analytics.model.Transaction; + +/** + * This Repository Class is used to perform the transactions of storing the data + * into the Elastic Search Repository + * + * @author Darshan Nagesh + * + */ +@Service +public class ElasticSearchRepository { + + public static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchRepository.class); + + private final RestTemplate restTemplate; + + public ElasticSearchRepository(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + /** + * Based on the Transaction Index Data Obtained and the URL with Headers, this + * method will put the Data obtained on the Elastic Search Database and returns + * the response in the form of Positive or Negative outcome (True Or False) + * + * @param transactionIndex + * @param url + * @param headers + * @return + */ + public Boolean saveTransaction(Transaction transaction, String url, HttpHeaders headers) { + ResponseEntity<Map> map = null; + try { + map = restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(transaction, headers), Map.class); + } catch (final HttpClientErrorException httpClientErrorException) { + LOGGER.error("Error : " + httpClientErrorException); + } catch (HttpServerErrorException httpServerErrorException) { + LOGGER.error("Error : " + httpServerErrorException); + } catch (Exception e) { + LOGGER.error("Error : " + e); + } + if (map != null && (map.getStatusCode() != null && (map.getStatusCode() == HttpStatus.OK) + || (map.getStatusCode() == HttpStatus.CREATED))) { + return true; + } + return false; + } + + // create index + public Boolean createTransactionIndex(MappingDto dto, String url, HttpHeaders headers) { + ResponseEntity<Map> map = null; + try { + LOGGER.info("Each Channel Content : " + dto.toString()); + map = restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(dto, headers), Map.class); + } catch (final HttpClientErrorException httpClientErrorException) { + LOGGER.error("Error : " + httpClientErrorException); + } catch (HttpServerErrorException httpServerErrorException) { + LOGGER.error("Error : " + httpServerErrorException); + } catch (Exception e) { + LOGGER.error("Error : " + e); + } + if (map != null && (map.getStatusCode() != null && (map.getStatusCode() == HttpStatus.OK) + || (map.getStatusCode() == HttpStatus.CREATED))) { + return true; + } + return false; + } + +} diff --git a/src/main/java/com/tarento/analytics/service/AmazonS3ClientService.java b/src/main/java/com/tarento/analytics/service/AmazonS3ClientService.java new file mode 100644 index 0000000000000000000000000000000000000000..1ebda9f0d973b5fac787468460c2ecd5af461287 --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/AmazonS3ClientService.java @@ -0,0 +1,10 @@ +package com.tarento.analytics.service; + +import org.springframework.web.multipart.MultipartFile; + +public interface AmazonS3ClientService +{ + String uploadFileToS3Bucket(MultipartFile multipartFile, boolean enablePublicReadAccess); + + void deleteFileFromS3Bucket(String fileName); +} diff --git a/src/main/java/com/tarento/analytics/service/MetadataService.java b/src/main/java/com/tarento/analytics/service/MetadataService.java new file mode 100644 index 0000000000000000000000000000000000000000..4f8a4e1489db33dcabba56553f0b7edd3912eaaa --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/MetadataService.java @@ -0,0 +1,14 @@ +package com.tarento.analytics.service; + +import java.io.IOException; +import java.util.List; + +import com.tarento.analytics.dto.RoleDto; +import com.tarento.analytics.exception.AINException; + +public interface MetadataService { + + public Object getDashboardConfiguration(String profileName, String dashboardId, String catagory, List<RoleDto> roleIds) throws AINException, IOException; + public Object getDashboardsForProfile(String profileName, List<RoleDto> roleIds) throws AINException, IOException; + +} diff --git a/src/main/java/com/tarento/analytics/service/QueryService.java b/src/main/java/com/tarento/analytics/service/QueryService.java new file mode 100644 index 0000000000000000000000000000000000000000..bc97d7582c8492ec90e5d97d9159db4eecd6ee4b --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/QueryService.java @@ -0,0 +1,30 @@ +package com.tarento.analytics.service; + +import java.io.IOException; + +import org.elasticsearch.search.aggregations.Aggregations; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.exception.AINException; + +public interface QueryService { +/* + List<Aggregation> getAggregateData(AggregateRequestDto aggregateDto, String orgId) throws AINException; +*/ + public static final String API_CONFIG_JSON = "ChartApiConfig.json"; + public static final String DATE_SOURCE_FIELD = "dateSourceField"; + public static final String AGG_QUERY_JSON = "aggregationQueryJson"; + public static final String INDEX_NAME = "indexName" ; + public static final String DOCUMENT_TYPE = "documentType"; +/* Aggregations getAggregateData(AggregateRequestDto aggregateDto, String orgId) throws AINException; + Aggregations getAggregateDataV2(AggregateRequestDtoV2 aggregateDto, String orgId) throws AINException, JsonParseException, JsonMappingException, IOException; + */ + ObjectNode getChartConfigurationQuery(AggregateRequestDto req, JsonNode query, String indexName, String interval); + + +} diff --git a/src/main/java/com/tarento/analytics/service/impl/AmazonS3ClientServiceImpl.java b/src/main/java/com/tarento/analytics/service/impl/AmazonS3ClientServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..18b0f2805906aa49d8e61957a942470f5ec49826 --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/impl/AmazonS3ClientServiceImpl.java @@ -0,0 +1,81 @@ +package com.tarento.analytics.service.impl; + +import com.amazonaws.services.s3.model.CannedAccessControlList; +import com.amazonaws.services.s3.model.DeleteObjectRequest; +import com.amazonaws.services.s3.model.PutObjectRequest; +import com.tarento.analytics.model.AmazonS3Config; +import com.tarento.analytics.service.AmazonS3ClientService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.regions.Region; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Date; + +@Component +public class AmazonS3ClientServiceImpl implements AmazonS3ClientService { + @Autowired + private AmazonS3Config amazonS3Config; + private String awsS3AudioBucket; + private AmazonS3 amazonS3; + private static final Logger logger = LoggerFactory.getLogger(AmazonS3ClientServiceImpl.class); + + @Autowired + public AmazonS3ClientServiceImpl(Region awsRegion, AWSCredentialsProvider awsCredentialsProvider, + String awsS3AudioBucket) { + this.amazonS3 = AmazonS3ClientBuilder.standard().withCredentials(awsCredentialsProvider) + .withRegion(awsRegion.getName()).build(); + this.awsS3AudioBucket = awsS3AudioBucket; + } + + @Async + public String uploadFileToS3Bucket(MultipartFile multipartFile, boolean enablePublicReadAccess) { + String fileName = multipartFile.getOriginalFilename(); + long time = new Date().getTime(); + + String imageURL = ""; + // creating the file in the server (temporarily) + File file = new File(time + "-" + fileName); + try (FileOutputStream fos = new FileOutputStream(file);) { + fos.write(multipartFile.getBytes()); + fos.close(); + + PutObjectRequest putObjectRequest = new PutObjectRequest(this.awsS3AudioBucket, fileName, file); + + if (enablePublicReadAccess) { + putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); + } + this.amazonS3.putObject(putObjectRequest); + + imageURL = String.valueOf(amazonS3.getUrl(amazonS3Config.getAWSS3AudioBucket(), // The S3 Bucket To Upload + // To + file.getName())); + // removing the file created in the server + file.delete(); + } catch (IOException | AmazonServiceException ex) { + logger.error("error [" + ex.getMessage() + "] occurred while uploading [" + fileName + "] "); + } + return imageURL; + + } + + @Async + public void deleteFileFromS3Bucket(String fileName) { + try { + amazonS3.deleteObject(new DeleteObjectRequest(awsS3AudioBucket, fileName)); + } catch (AmazonServiceException ex) { + logger.error("error [" + ex.getMessage() + "] occurred while removing [" + fileName + "] "); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/service/impl/MetadataServiceImpl.java b/src/main/java/com/tarento/analytics/service/impl/MetadataServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..2b40047f96843926863f05110973f0989c02da14 --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/impl/MetadataServiceImpl.java @@ -0,0 +1,181 @@ +package com.tarento.analytics.service.impl; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.ConfigurationLoader; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dto.RoleDto; +import com.tarento.analytics.exception.AINException; +import com.tarento.analytics.handler.IResponseHandler; +import com.tarento.analytics.service.MetadataService; + +@Service("metadataService") +public class MetadataServiceImpl implements MetadataService { + + public static final Logger logger = LoggerFactory.getLogger(MetadataServiceImpl.class); + + @Autowired + private ConfigurationLoader configurationLoader; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private RestTemplate restTemplate; + + + @Value("${egov.mdms-service.target.url}") + private String mdmsServiceTargetUrl; + + @Override + public ArrayNode getDashboardConfiguration(String profileName, String dashboardId, String catagory, List<RoleDto> roleIds) throws AINException, IOException { + + + Calendar cal = Calendar.getInstance(); + cal.set(cal.getWeekYear()-1, Calendar.APRIL, 1); + Date startDate = cal.getTime(); + Date endDate = new Date(); + + // To show the date selection if needed + // String fyInfo = "From " + Constants.DASHBOARD_DATE_FORMAT.format(startDate) + " to " + Constants.DASHBOARD_DATE_FORMAT.format(endDate); + + ObjectNode dashboardNode = configurationLoader.getConfigForProfile(profileName, ConfigurationLoader.MASTER_DASHBOARD_CONFIG); + ArrayNode dashboardNodes = (ArrayNode) dashboardNode.findValue(Constants.DashBoardConfig.DASHBOARDS); + + ObjectNode roleMappingNode = configurationLoader.getConfigForProfile(profileName, ConfigurationLoader.ROLE_DASHBOARD_CONFIG); + ArrayNode rolesArray = (ArrayNode) roleMappingNode.findValue(Constants.DashBoardConfig.ROLES); + ArrayNode dbArray = JsonNodeFactory.instance.arrayNode(); + + rolesArray.forEach(role -> { + Object roleId = roleIds.stream() + .filter(x -> role.get(Constants.DashBoardConfig.ROLE_ID).asLong() == (x.getId())).findAny() + .orElse(null); + if (null != roleId) { + ArrayNode visArray = JsonNodeFactory.instance.arrayNode(); + ArrayNode widgetArray = JsonNodeFactory.instance.arrayNode(); + ArrayNode filterArray = JsonNodeFactory.instance.arrayNode(); + // checks role has given db id + role.get(Constants.DashBoardConfig.DASHBOARDS).forEach(db -> { + ArrayNode visibilityArray = (ArrayNode) db.findValue(Constants.DashBoardConfig.VISIBILITY); + ObjectNode copyDashboard = objectMapper.createObjectNode(); + + JsonNode name = JsonNodeFactory.instance.textNode(""); + JsonNode id = JsonNodeFactory.instance.textNode(""); + // Set the FY Info in Title if needed + // JsonNode title = JsonNodeFactory.instance.textNode(fyInfo); + + if (db.get(Constants.DashBoardConfig.ID).asText().equalsIgnoreCase(dashboardId)) { + // dasboardNodes.forEach(dbNode -> { + for (JsonNode dbNode : dashboardNodes) { + if (dbNode.get(Constants.DashBoardConfig.ID).asText().equalsIgnoreCase(dashboardId)) { + logger.info("dbNode: " + dbNode); + name = dbNode.get(Constants.DashBoardConfig.NAME); + id = dbNode.get(Constants.DashBoardConfig.ID); + dbNode.get(Constants.DashBoardConfig.VISUALISATIONS).forEach(visual -> { + visArray.add(visual); + }); + dbNode.get(Constants.DashBoardConfig.WIDGET_CHARTS).forEach(widget -> { + widgetArray.add(widget); + }); + dbNode.get(Constants.DashBoardConfig.FILTERS).forEach(filter -> { + JsonNode node = filter.deepCopy(); + applyVisilibityLayer(visibilityArray, node); + filterArray.add(node); + }); + } + copyDashboard.set(Constants.DashBoardConfig.NAME, name); + copyDashboard.set(Constants.DashBoardConfig.ID, id); + // add TITLE with variable dynamically + // copyDashboard.set(Constants.DashBoardConfig.TITLE, title); + copyDashboard.set(Constants.DashBoardConfig.WIDGET_CHARTS, widgetArray); + copyDashboard.set(Constants.DashBoardConfig.FILTERS, filterArray); + copyDashboard.set(Constants.DashBoardConfig.VISUALISATIONS, visArray); + + } // ); + dbArray.add(copyDashboard); + } + }); + } + }); + return dbArray; + } + + private void applyVisilibityLayer(ArrayNode visibilityArray, JsonNode filter) { + try { + visibilityArray.forEach(visibility -> { + String visibilityKey = visibility.get(Constants.DashBoardConfig.KEY).asText(); + String filterKey = filter.get(Constants.DashBoardConfig.KEY).asText(); + if(visibilityKey.equals(filterKey)) { + ArrayNode valuesAllowed = (ArrayNode) visibility.get(Constants.DashBoardConfig.VALUE); + ArrayNode valuesAvailable = (ArrayNode) filter.get(Constants.DashBoardConfig.VALUES); + ObjectNode availableValuesList = new ObjectMapper().createObjectNode(); + ArrayNode availableValuesArray = availableValuesList.putArray(Constants.DashBoardConfig.VALUES); + List<String> allowedValuesList = new ArrayList<>(); + valuesAllowed.forEach(allowedValue -> { + allowedValuesList.add(allowedValue.asText()); + }); + for(int i = 0 ; i < valuesAvailable.size() ; i++) { + if(allowedValuesList.contains(valuesAvailable.get(i).asText())) { + availableValuesArray.add(valuesAvailable.get(i).asText()); + } + } + if(availableValuesArray.size() > 0) { + ObjectNode filterObjectNode = (ObjectNode) filter; + filterObjectNode.put(Constants.DashBoardConfig.VALUES, availableValuesArray); + } + } + }); + } catch (Exception e) { + + } + } + + @Override + public ArrayNode getDashboardsForProfile(String profileName, List<RoleDto> roleIds) + throws AINException, IOException { + + ObjectNode roleMappingNode = configurationLoader.getConfigForProfile(profileName, + ConfigurationLoader.ROLE_DASHBOARD_CONFIG); + ArrayNode rolesArray = (ArrayNode) roleMappingNode.findValue(Constants.DashBoardConfig.ROLES); + ArrayNode dbArray = JsonNodeFactory.instance.arrayNode(); + + rolesArray.forEach(role -> { + Object roleId = roleIds.stream() + .filter(x -> role.get(Constants.DashBoardConfig.ROLE_ID).asLong() == (x.getId())).findAny() + .orElse(null); + logger.info("roleId: " + roleId); + RoleDto dto = RoleDto.class.cast(roleId); + if (dto != null && dto.getId() != null && role.get(Constants.DashBoardConfig.ROLE_ID).asLong() == dto.getId()) + role.get(Constants.DashBoardConfig.DASHBOARDS).forEach(db -> { + JsonNode name = JsonNodeFactory.instance.textNode(""); + JsonNode id = JsonNodeFactory.instance.textNode(""); + name = db.get(Constants.DashBoardConfig.NAME); + id = db.get(Constants.DashBoardConfig.ID); + ObjectNode copyDashboard = objectMapper.createObjectNode(); + copyDashboard.set(Constants.DashBoardConfig.NAME, name); + copyDashboard.set(Constants.DashBoardConfig.ID, id); + dbArray.add(copyDashboard); + }); + + }); + return dbArray; + } + +} diff --git a/src/main/java/com/tarento/analytics/service/impl/QueryServiceImpl.java b/src/main/java/com/tarento/analytics/service/impl/QueryServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..6ece4141626df6fdfff756bdb85d523a8f1e19c5 --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/impl/QueryServiceImpl.java @@ -0,0 +1,458 @@ +package com.tarento.analytics.service.impl; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.elasticsearch.action.search.MultiSearchResponse; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; +import org.elasticsearch.search.aggregations.metrics.avg.ParsedAvg; +import org.elasticsearch.search.aggregations.metrics.sum.ParsedSum; +import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.tarento.analytics.ConfigurationLoader; +import com.tarento.analytics.constant.Constants; +import com.tarento.analytics.dao.ElasticSearchDao; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.dto.AggregateRequestDto; +import com.tarento.analytics.enums.ChartType; +import com.tarento.analytics.exception.AINException; +import com.tarento.analytics.model.ElasticSearchDictator; +import com.tarento.analytics.model.KeyData; +import com.tarento.analytics.model.Query; +import com.tarento.analytics.model.ServiceQuery; +import com.tarento.analytics.service.QueryService; +import com.tarento.analytics.utils.ElasticProperties; + +@Component +public class QueryServiceImpl implements QueryService { + + public static final Logger logger = LoggerFactory.getLogger(QueryServiceImpl.class); + /* + * @Autowired private MetadataDao metaDataDao; + */ + + @Autowired + private ElasticSearchDao elasticSearchDao; + + /* + * @Autowired private DecoratorService decoratorService; + * + */ + @Autowired + private ConfigurationLoader configurationLoader; + + private static final Map<Integer, String> WeekDayMap = createMap(); + + private static Map<Integer, String> createMap() { + Map<Integer, String> result = new HashMap<Integer, String>(); + result.put(1, "SUN"); + result.put(2, "MON"); + result.put(3, "TUE"); + result.put(4, "WED"); + result.put(5, "THU"); + result.put(6, "FRI"); + result.put(7, "SAT"); + + return Collections.unmodifiableMap(result); + } + + /* + * @Override public Aggregations getAggregateData(AggregateRequestDto + * aggregateDto, String orgId) throws AINException { + * + * //public List<Aggregation> getAggregateData(AggregateRequestDto aggregateDto, + * String orgId) throws AINException { Aggregations aggregations = null; + * ElasticSearchDictator dictator = null; ServiceQuery elasticServiceQuery = + * metaDataDao.getServiceQuery(Long.parseLong(orgId), + * aggregateDto.getServiceApi()); + * + * ObjectMapper mapper = new ObjectMapper(); + */ + /* Map<KeyData, Object> resonseMap = null; *//* + * + * try { + * + * //Query Builder Query queryJson = + * mapper.readValue(elasticServiceQuery.getQueryJson(), + * Query.class); // Set index name and document Type for elastic + * Map<String, Object> queryMap = queryJson.getAggregation(); + * + * //Label Mapping Map<String,String> labelMap = new HashMap<>(); + * getAggregateLabelRecursively(queryMap,labelMap); + * + * dictator = elasticSearchDao.createSearchDictator(aggregateDto, + * elasticServiceQuery.getIndexName(), + * elasticServiceQuery.getDocumentType(), + * queryJson.getDateFilterField()); + * dictator.setQueryAggregationMap(queryMap); SearchRequest + * searchRequest = + * elasticSearchDao.buildElasticSearchQuery(dictator); + * List<SearchRequest> searchRequestList = new ArrayList<>(); + * searchRequestList.add(searchRequest); MultiSearchResponse + * response = + * elasticSearchDao.executeMultiSearchRequest(searchRequestList, + * Boolean.TRUE); SearchResponse searchResponse = + * response.getResponses()[0].getResponse(); aggregations = + * searchResponse.getAggregations(); + */ + /* + * resonseMap= translateResponse(response, + * aggregateDto.getChartType(),aggregateDto.getInterval(), labelMap); + * decoratorService.getChartData(aggregateDto, chartFormat, chartType, + * serviceApi, chartCode) + *//* + * + * } catch (Exception e) { // TODO Auto-generated catch block + * e.printStackTrace(); } return aggregations; } + * + * @Override public Aggregations getAggregateDataV2(AggregateRequestDtoV2 + * aggregateDto, String orgId) throws AINException, JsonParseException, + * JsonMappingException, IOException { Boolean primaryOrNot = Boolean.TRUE; + * ServiceQuery elasticServiceQuery = new ServiceQuery(); String + * visualizationCode = aggregateDto.getVisualizationCode(); ObjectNode + * configNode = configurationLoader.get(API_CONFIG_JSON); + * elasticServiceQuery.setIndexName(configNode.get(visualizationCode).get( + * INDEX_NAME).asText()); + * elasticServiceQuery.setDocumentType(configNode.get(visualizationCode).get( + * DOCUMENT_TYPE).asText()); + * elasticServiceQuery.setQueryJson(configNode.get(visualizationCode).get( + * AGG_QUERY_JSON).asText()); + * if(elasticServiceQuery.getIndexName().equals("dss-col-v1")) { primaryOrNot = + * Boolean.TRUE; } else { primaryOrNot = Boolean.FALSE; } ObjectMapper mapper = + * new ObjectMapper(); Query queryJson = + * mapper.readValue(elasticServiceQuery.getQueryJson(), Query.class); + * + * // Set index name and document Type for elastic Map<String, Object> queryMap + * = queryJson.getAggregation(); + * + * //Label Mapping Map<String,String> labelMap = new HashMap<>(); + * getAggregateLabelRecursively(queryMap,labelMap); + * + * Aggregations aggregations = null; ElasticSearchDictator dictator = null; + * + * try { dictator = elasticSearchDao.createSearchDictatorV2(aggregateDto, + * elasticServiceQuery.getIndexName(), elasticServiceQuery.getDocumentType(), + * queryJson.getDateFilterField()); dictator.setQueryAggregationMap(queryMap); + * SearchRequest searchRequest = + * elasticSearchDao.buildElasticSearchQuery(dictator); List<SearchRequest> + * searchRequestList = new ArrayList<>(); searchRequestList.add(searchRequest); + * MultiSearchResponse response = + * elasticSearchDao.executeMultiSearchRequest(searchRequestList, primaryOrNot); + * SearchResponse searchResponse = response.getResponses()[0].getResponse(); + * aggregations = searchResponse.getAggregations(); } catch (Exception e) { + * logger.error("Encountered an error while getting the Aggregated Data : " + + * e.getMessage()); } return aggregations; } + */ + + /* + * private Map<KeyData, Object> translateResponse(MultiSearchResponse response, + * ChartType chartType, String interval, Map<String,String> labelMap) { + * SearchResponse searchResponse = response.getResponses()[0].getResponse(); + * List<Aggregation> aggregations = searchResponse.getAggregations().asList(); + * Map<KeyData, Object> responseMap = new HashMap<>(); + * + * if (chartType.equals(ChartType.BAR) || + * chartType.equals(ChartType.HORIZONTALBAR) || chartType.equals(ChartType.PIE) + * || chartType.equals(ChartType.DOUGHNUT)) { + * + * if (aggregations.get(0) instanceof ParsedLongTerms) { responseMap = + * parseParseLongTerms((ParsedLongTerms) aggregations.get(0), chartType, + * labelMap); } } + * + * return responseMap; } + */ + + @SuppressWarnings("unchecked") + void getAggregateLabelRecursively(Map<String, Object> queryMap, Map<String, String> labelMap) { + try { + if (queryMap.containsKey(ElasticProperties.Query.AGGREGATION_CONDITION.toLowerCase())) { + + Map<String, Object> valueMap = (HashMap<String, Object>) queryMap + .get(ElasticProperties.Query.AGGREGATION_CONDITION.toLowerCase()); + getAggregateLabelRecursively(valueMap, labelMap); + } + for (Map.Entry<String, Object> itrQuery : queryMap.entrySet()) { + if (itrQuery.getKey().equals(ElasticProperties.Query.AGGREGATION_CONDITION.toLowerCase())) { + continue; + } + Map<String, Object> propertiesMap = (HashMap<String, Object>) itrQuery.getValue(); + labelMap.put(itrQuery.getKey(), + propertiesMap.get(ElasticProperties.Query.LABEL.toLowerCase()).toString()); + } + } catch (Exception e) { + logger.error("Exception in getAggregateLabelRecursively {} ", e.getMessage()); + + } + } + /* + * private Map<KeyData, Object> parseDateHistogramForMultiLine(List<Aggregation> + * aggregations, ChartType chartType, String interval) { Map<KeyData, Object> + * responseMap = new HashMap<>(); + * + * for (Histogram.Bucket bucket : ((ParsedDateHistogram) + * aggregations.get(0)).getBuckets()) { List<Aggregation> subAggregations = + * bucket.getAggregations().asList(); if (subAggregations.get(0) instanceof + * ParsedLongTerms) { parseParseLongTermsOnInterval(bucket, chartType, interval, + * responseMap); } + * + * } + * + * return responseMap; + * + * } + */ + + /* + * private Map<KeyData, Object> parseNested(List<Aggregation> aggregations, + * ChartType chartType, String interval) { + * + * Map<KeyData, Object> responseMap = new HashMap<>(); for (Aggregation + * aggregationData : ((ParsedNested) aggregations.get(0)).getAggregations()) { + * if (aggregationData instanceof ParsedLongTerms) { responseMap = + * parseParseLongTerms((ParsedLongTerms) aggregationData, chartType, interval); + * } + * + * } return responseMap; + * + * } + */ + + /* + * private Map<KeyData, Object> parseDateHistogram(List<Aggregation> + * aggregations, ChartType chartType, String interval) { + * + * Map<KeyData, Object> responseMap = new HashMap<>(); if + * (interval.equals(ElasticSearchConstants.DAY_OF_WEEK)) { Map<Integer, Object> + * dayWiseObjectMap = new HashMap<Integer, Object>() { { put(1, null); put(2, + * null); put(3, null); put(4, null); put(5, null); put(6, null); put(7, null); + * + * } }; + * + * for (Histogram.Bucket bucket : ((ParsedDateHistogram) + * aggregations.get(0)).getBuckets()) { Calendar cal = Calendar.getInstance(); + * cal.setTimeInMillis(Long.parseLong(bucket.getKeyAsString())); int dayofWeek = + * cal.get(Calendar.DAY_OF_WEEK); + * + * Object val = getAggregationValue(bucket); if + * (dayWiseObjectMap.containsKey(dayofWeek)) { Object dayWiseCount = + * dayWiseObjectMap.get(dayofWeek); if (val instanceof Double) { Double + * doubleValue = 0.0; if (dayWiseCount == null) { doubleValue = 0.0; } else { + * doubleValue = (Double) dayWiseCount; } doubleValue += + * getFormattedDouble((Double) val); dayWiseCount = doubleValue; } else if (val + * instanceof Long) { Long longValue = 0L; if (dayWiseCount == null) { longValue + * = 0L; } else { longValue = (Long) dayWiseCount; } longValue += (Long) val; + * dayWiseCount = longValue; } dayWiseObjectMap.put(dayofWeek, dayWiseCount); } + * else { if (val instanceof Double) { Double doubleValue = (Double) val; + * doubleValue += getFormattedDouble((Double) val); val = doubleValue; } + * dayWiseObjectMap.put(dayofWeek, val); } } for (Map.Entry<Integer, Object> + * entry : dayWiseObjectMap.entrySet()) { + * + * responseMap.put(WeekDayMap.get(entry.getKey()), (entry.getValue())); } } else + * { for (Histogram.Bucket bucket : ((ParsedDateHistogram) + * aggregations.get(0)).getBuckets()) { List<Aggregation> subAggregations = + * bucket.getAggregations().asList(); if (subAggregations.get(0) instanceof + * ParsedLongTerms) { + * + * + * responseMap = parseParseLongTermsOnInterval(bucket, chartType, interval); } + * else { responseMap = parseDateHistogramBasedOnInterval(bucket, chartType, + * interval); } + * + * } } return responseMap; + * + * } + */ + /* + * private void parseParseLongTermsOnInterval(Bucket buckets, ChartType + * chartType, String interval, Map<KeyData, Object> responseMap) { Calendar cal + * = Calendar.getInstance(); + * cal.setTimeInMillis(Long.parseLong(buckets.getKeyAsString())); String key = + * null; if (interval.equals(ElasticSearchConstants.DAY)) { int day = + * cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH) + 1; + * + * key = day + "/" + month; } else if + * (interval.equals(ElasticSearchConstants.HOUR)) { int hour = + * cal.get(Calendar.HOUR_OF_DAY); String suffix = "AM"; if + * (cal.get(Calendar.AM_PM) > 0) suffix = "PM"; + * + * key = String.valueOf(hour + 1) + suffix; + * + * } Map<String, Object> innerResponseMap = new HashMap<>(); + * + * for (Terms.Bucket bucket : ((ParsedLongTerms) buckets).getBuckets()) { + * + * Map<String, Aggregation> valueMap = bucket.getAggregations().getAsMap(); + * Object val = null; + * + * for (Map.Entry<String, Aggregation> aggregation : valueMap.entrySet()) { if + * (aggregation.getKey().contains(ElasticProperties.Query.SUM.toLowerCase()) ) { + * ParsedSum sum = (ParsedSum) aggregation.getValue(); val = sum.getValue(); } + * else if + * (aggregation.getKey().contains(ElasticProperties.Query.AVG.toLowerCase()) ) { + * ParsedAvg avg = (ParsedAvg) aggregation.getValue(); val = avg.getValue(); + * + * } else if + * (aggregation.getKey().contains(ElasticProperties.Query.COUNT.toLowerCase( ))) + * { ParsedValueCount count = (ParsedValueCount) aggregation.getValue(); val = + * count.getValue(); + * + * } + * + * } innerResponseMap.put(bucket.getKeyAsString(), val); } responseMap.put(key, + * innerResponseMap); + * + * } + */ + + /* + * private Map<KeyData, Object> parseDateHistogramBasedOnInterval(Bucket bucket, + * ChartType chartType, String interval) { Map<KeyData, Object> responseMap = + * new HashMap<>(); Calendar cal = Calendar.getInstance(); + * cal.setTimeInMillis(Long.parseLong(bucket.getKeyAsString())); String key = + * null; Object val = null; if (interval.equals(ElasticSearchConstants.DAY)) { + * int day = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH) + * + 1; + * + * key = day + "/" + month; val = getAggregationValue(bucket); + * + * } else if (interval.equals(ElasticSearchConstants.HOUR)) { int hour = + * cal.get(Calendar.HOUR_OF_DAY); String suffix = "AM"; if + * (cal.get(Calendar.AM_PM) > 0) suffix = "PM"; val = + * getAggregationValue(bucket); + * + * key = String.valueOf(hour + 1) + suffix; + * + * } responseMap.put(key, val); return responseMap; + * + * } + */ + + /* + * private Object getAggregationValue(Bucket bucket) { Map<String, Aggregation> + * valueMap = bucket.getAggregations().getAsMap(); Object val = null; + * + * for (Map.Entry<String, Aggregation> aggregation : valueMap.entrySet()) { if + * (aggregation.getKey().contains(ElasticProperties.Query.SUM.toLowerCase()) ) { + * ParsedSum sum = (ParsedSum) aggregation.getValue(); val = sum.getValue(); } + * else if + * (aggregation.getKey().contains(ElasticProperties.Query.AVG.toLowerCase()) ) { + * ParsedAvg avg = (ParsedAvg) aggregation.getValue(); val = avg.getValue(); + * + * } else if + * (aggregation.getKey().contains(ElasticProperties.Query.COUNT.toLowerCase( ))) + * { ParsedValueCount count = (ParsedValueCount) aggregation.getValue(); val = + * count.getValue(); + * + * } } return val; } + */ + + private Map<KeyData, Object> parseParseLongTerms(ParsedLongTerms aggregations, ChartType chartType, + Map<String, String> labelMap) { + + Map<KeyData, Object> keyValueMap = new HashMap<>(); + + for (Terms.Bucket bucket : aggregations.getBuckets()) { + KeyData keyData = new KeyData(); + KeyData valueData = new KeyData(); + + String key = bucket.getKey().toString(); + + Map<String, Aggregation> valueMap = bucket.getAggregations().getAsMap(); + Object val = null; + String valueLabel = null; + for (Map.Entry<String, Aggregation> aggregation : valueMap.entrySet()) { + if (aggregation.getKey().contains(ElasticProperties.Query.SUM.toLowerCase())) { + ParsedSum sum = (ParsedSum) aggregation.getValue(); + val = sum.getValue(); + valueLabel = ElasticProperties.Query.SUM.toLowerCase(); + } else if (aggregation.getKey().contains(ElasticProperties.Query.AVG.toLowerCase())) { + ParsedAvg avg = (ParsedAvg) aggregation.getValue(); + val = avg.getValue(); + valueLabel = ElasticProperties.Query.SUM.toLowerCase(); + + } else if (aggregation.getKey().contains(ElasticProperties.Query.COUNT.toLowerCase())) { + ParsedValueCount count = (ParsedValueCount) aggregation.getValue(); + val = count.getValue(); + valueLabel = ElasticProperties.Query.SUM.toLowerCase(); + + } + + } + keyData.setKey(key); + keyData.setLabel(labelMap.get(ElasticProperties.Query.TERM.toLowerCase())); + valueData.setKey(val); + valueData.setLabel(labelMap.get(valueLabel)); + keyValueMap.put(keyData, valueData); + + } + return keyValueMap; + + } + + private Double getFormattedDouble(double val) { + return (Math.round(new Double(val).isInfinite() ? 0.0 : new Double(val) * 100.0) / 100.0); + } + + @Override + public ObjectNode getChartConfigurationQuery(AggregateRequestDto request, JsonNode query, String indexName, + String interval) { + String aggrQuery = query.get(Constants.JsonPaths.AGGREGATION_QUERY).asText(); + if (interval != null && !interval.isEmpty()) + aggrQuery = aggrQuery.replace(Constants.JsonPaths.INTERVAL_VAL, interval); + String rqMs = query.get(Constants.JsonPaths.REQUEST_QUERY_MAP).asText(); + String dateReferenceField = query.get(Constants.JsonPaths.DATE_REF_FIELD).asText(); + JsonNode requestQueryMaps = null; + ObjectNode objectNode = null; + ObjectMapper mapper = new ObjectMapper(); + Map<String, Object> esFilterMap = new HashMap<>(); + try { + requestQueryMaps = new ObjectMapper().readTree(rqMs); + request.setEsFilters(esFilterMap); + if (query.get(Constants.JsonPaths.MODULE).asText().equals(Constants.Modules.COMMON) + && !request.getModuleLevel().equals(Constants.Modules.HOME_REVENUE) + && !request.getModuleLevel().equals(Constants.Modules.HOME_SERVICES)) { + request.getFilters().put(Constants.Filters.MODULE, request.getModuleLevel()); + } + Iterator<Entry<String, Object>> filtersItr = request.getFilters().entrySet().iterator(); + while (filtersItr.hasNext()) { + Entry<String, Object> entry = filtersItr.next(); + if (!String.valueOf(entry.getValue()).equals(Constants.Filters.FILTER_ALL)) { + String esQueryKey = requestQueryMaps.get(entry.getKey()).asText(); + request.getEsFilters().put(esQueryKey, entry.getValue()); + } + } + ElasticSearchDictator dictator = elasticSearchDao.createSearchDictatorV2(request, indexName, "", + dateReferenceField); + SearchRequest searchRequest = elasticSearchDao.buildElasticSearchQuery(dictator); + JsonNode querySegment = mapper.readTree(searchRequest.source().toString()); + objectNode = (ObjectNode) querySegment; + JsonNode aggrNode = mapper.readTree(aggrQuery).get(Constants.JsonPaths.AGGS); + objectNode.put(Constants.JsonPaths.AGGS, mapper.readTree(aggrQuery).get(Constants.JsonPaths.AGGS)); + } catch (Exception ex) { + logger.error("Encountered an Exception while parsing the JSON : {} ", ex.getMessage()); + } + return objectNode; + + } + +} diff --git a/src/main/java/com/tarento/analytics/service/impl/RestService.java b/src/main/java/com/tarento/analytics/service/impl/RestService.java new file mode 100644 index 0000000000000000000000000000000000000000..a61f1eeb09dfce4f71e4049896e81d04ee88e0d2 --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/impl/RestService.java @@ -0,0 +1,195 @@ +package com.tarento.analytics.service.impl; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.tarento.analytics.constant.Constants; + +import org.apache.tomcat.util.codec.binary.Base64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.*; +import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import static javax.servlet.http.HttpServletRequest.BASIC_AUTH; +import static org.apache.commons.codec.CharEncoding.US_ASCII; +import static org.springframework.http.HttpHeaders.AUTHORIZATION; + +@Component +public class RestService { + public static final Logger LOGGER = LoggerFactory.getLogger(RestService.class); + + @Value("${services.esindexer.primary.host}") + private String indexServicePrimaryHost; + @Value("${services.esindexer.secondary.host}") + private String indexServiceSecondaryHost; + @Value("${services.esindexer.ternary.host}") + private String indexServiceTernaryHost; + @Value("${es.services.esindexer.host.search}") + private String indexServiceHostSearch; + @Value("${services.esindexer.primary.username}") + private String primaryUsername; + @Value("${services.esindexer.primary.password}") + private String primaryPassword; + @Value("${services.esindexer.secondary.username}") + private String secondaryUsername; + @Value("${services.esindexer.secondary.password}") + private String secondaryPassword; + @Value("${services.esindexer.ternary.username}") + private String ternaryUsername; + @Value("${services.esindexer.ternary.password}") + private String ternaryPassword; + @Value("${services.esindexer.quadnary.username}") + private String quadnaryUsername; + @Value("${services.esindexer.quadnary.password}") + private String quadnaryPassword; + @Value("${services.esindexer.quadnary.host}") + private String indexServiceQuadnaryHost; + + @Autowired + private RetryTemplate retryTemplate; + + /** + * search on Elastic search for a search query + * + * @param index + * elastic search index name against which search operation + * @param searchQuery + * search query as request body + * @return + * @throws IOException + */ + public JsonNode search(String index, String searchQuery, String instance) { + String url = ""; + if (instance.equals(Constants.PRIMARY)) { + url = (indexServicePrimaryHost) + index + indexServiceHostSearch; + } else if (instance.equals(Constants.SECONDARY)) { + url = (indexServiceSecondaryHost) + index + indexServiceHostSearch; + } else if (instance.equals(Constants.TERNARY)) { + url = (indexServiceTernaryHost) + index + indexServiceHostSearch; + } else if (instance.equals(Constants.QUADNARY)) { + url = (indexServiceQuadnaryHost) + index + indexServiceHostSearch; + } + + HttpHeaders headers = getHttpHeaders(instance); + headers.setContentType(MediaType.APPLICATION_JSON); + LOGGER.info("ES URL hitting : " + url); + LOGGER.info("Index Name : " + index); + LOGGER.info("Searching ES for Query: " + searchQuery); + HttpEntity<String> requestEntity = new HttpEntity<>(searchQuery, headers); + String reqBody = requestEntity.getBody(); + JsonNode responseNode = null; + + try { + ResponseEntity<Object> response = retryTemplate.postForEntity(url, requestEntity); + responseNode = new ObjectMapper().convertValue(response.getBody(), JsonNode.class); + } catch (HttpClientErrorException e) { + LOGGER.error("client error while searching ES : {} ", e.getMessage()); + } + return responseNode; + } + + /** + * makes a client rest api call of Http POST option + * + * @param uri + * @param authToken + * @param requestNode + * @return + * @throws IOException + */ + public JsonNode post(String uri, String authToken, JsonNode requestNode) { + + HttpHeaders headers = new HttpHeaders(); + if (authToken != null && !authToken.isEmpty()) + headers.add("Authorization", "Bearer " + authToken); + headers.setContentType(MediaType.APPLICATION_JSON); + + LOGGER.info("Request Node: " + requestNode); + HttpEntity<String> requestEntity = null; + if (requestNode != null) + requestEntity = new HttpEntity<>(requestNode.toString(), headers); + else + requestEntity = new HttpEntity<>("{}", headers); + + JsonNode responseNode = null; + + try { + ResponseEntity<Object> response = retryTemplate.postForEntity(uri, requestEntity); + responseNode = new ObjectMapper().convertValue(response.getBody(), JsonNode.class); + LOGGER.info("RestTemplate response :- " + responseNode); + + } catch (HttpClientErrorException e) { + LOGGER.error("post client exception: " + e.getMessage()); + } + return responseNode; + } + + /** + * makes a client rest api call of Http GET option + * + * @param uri + * @param authToken + * @return + * @throws IOException + */ + public JsonNode get(String uri, String authToken) { + + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", "Bearer " + authToken); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity<String> headerEntity = new HttpEntity<>("{}", headers); + + JsonNode responseNode = null; + try { + ResponseEntity<Object> response = retryTemplate.getForEntity(uri, headerEntity); + responseNode = new ObjectMapper().convertValue(response.getBody(), JsonNode.class); + LOGGER.info("RestTemplate response :- " + responseNode); + + } catch (HttpClientErrorException e) { + LOGGER.error("get client exception: " + e.getMessage()); + } + return responseNode; + } + + private HttpHeaders getHttpHeaders(String instance) { + HttpHeaders headers = new HttpHeaders(); + String username = null; + String password = null; + if (instance.equals(Constants.PRIMARY)) { + username = primaryUsername; + password = primaryPassword; + } else if (instance.equals(Constants.SECONDARY)) { + username = secondaryUsername; + password = secondaryPassword; + } else if (instance.equals(Constants.TERNARY)) { + username = ternaryUsername; + password = ternaryPassword; + } else if (instance.equals(Constants.QUADNARY)) { + username = quadnaryUsername; + password = quadnaryPassword; + } + String plainCreds = String.format("%s:%s", username, password); + byte[] plainCredsBytes = plainCreds.getBytes(); + byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); + String base64Creds = new String(base64CredsBytes); + + headers.add(AUTHORIZATION, "Basic " + base64Creds); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + headers.setContentType(MediaType.APPLICATION_JSON); + + List<MediaType> mediaTypes = new ArrayList<>(); + mediaTypes.add(MediaType.APPLICATION_JSON); + headers.setAccept(mediaTypes); + return headers; + } + +} diff --git a/src/main/java/com/tarento/analytics/service/impl/RetryTemplate.java b/src/main/java/com/tarento/analytics/service/impl/RetryTemplate.java new file mode 100644 index 0000000000000000000000000000000000000000..e4f1f59f8257da18d8efeaf66d55e1bfb5035aad --- /dev/null +++ b/src/main/java/com/tarento/analytics/service/impl/RetryTemplate.java @@ -0,0 +1,38 @@ +package com.tarento.analytics.service.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.retry.annotation.Backoff; +import org.springframework.retry.annotation.Retryable; +import org.springframework.stereotype.Component; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; +import javax.naming.ServiceUnavailableException; + +/** + * Wraps rest template with retry + */ +@Component +public class RetryTemplate { + + @Autowired + private RestTemplate restTemplate; + + @Retryable(value = {RuntimeException.class, ResourceAccessException.class, ServiceUnavailableException.class}, + maxAttemptsExpression = "#{${service.retry.maxAttempts}}", + backoff = @Backoff(delayExpression = "#{${service.retry.backoff.delay}}")) + public ResponseEntity<Object> postForEntity(String url, Object request) { + return restTemplate.postForEntity(url, request, Object.class); + } + + @Retryable(value = {RuntimeException.class, ResourceAccessException.class, ServiceUnavailableException.class}, + maxAttemptsExpression = "#{${service.retry.maxAttempts}}", + backoff = @Backoff(delayExpression = "#{${service.retry.backoff.delay}}")) + public ResponseEntity<Object> getForEntity(String url, HttpEntity headerEntity) { + return restTemplate.exchange(url, HttpMethod.GET, headerEntity, Object.class); + } + +} + diff --git a/src/main/java/com/tarento/analytics/utils/ElasticProperties.java b/src/main/java/com/tarento/analytics/utils/ElasticProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..c27e7b79a5c33967c4b8413eebdb7c7337c8ae81 --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/ElasticProperties.java @@ -0,0 +1,73 @@ +package com.tarento.analytics.utils; + +public interface ElasticProperties { + + public interface Property { + final String TRANSACTION_TRANSDATE = "TransDate"; + final String TRANSACTION_STOREID = "StoreId"; + final String TRANSACTION_NETAMOUNT = "NetAmt"; + final String TARGET_TARGETDATE = "targetDate"; + final String TARGET_STORECODE = "storeCode"; + final String TARGET_VERSION = "version"; + final String TARGET_SALESVALUE = "salesValue"; + final String TARGET_AVGVALUE = "avgValue"; + final String TARGET_RECEIPTVALUE = "receiptsValue"; + final String ITEM_DETAILS_CATEGORY_ID = "itemDetails.categoryId"; + final String ITEM_DETAILS_PRICE = "itemDetails.price" ; + final String AVERAGE_RATING = "AvgRating"; + final String SORT_ORDER_DESCENDING = "desc"; + final String SORT_ORDER_ASCENDING = "asc"; + final String COUNTS = "counts"; + final String COUNTS_RATING = "CountsRating"; + final String FEEDBACK_VALUE_RESPONSE = "feedback_value"; + } + + public interface SuccessMessages { + final String STORE_USER = "User and Store successfully mapped" ; + final String STORE_TIMING = "Store Times entered successfully" ; + } + + public interface Query { + final String NESTED = "NESTED"; + final String MATCH_CONDITION = "MATCH"; + final String RANGE_CONDITION = "RANGE"; + final String AGGREGATION_CONDITION = "AGGREGATION"; + final String TRANSACTION_DATE_FIELD = "transDate"; + final String FEEDBACK_DATE_TIME = "serverDateTime"; + final String COUNT_STORE_CODE = "storeId"; + final String COUNT_RATING_VALUE = "value"; + final String COUNT_REASON_KEYWORD = "reasons.keyword"; + final String COUNT_GENDER_KEYWORD = "gender.keyword"; + final String COUNT_AGEGROUP_KEYWORD = "ageGroup.keyword"; + final String SUM = "SUM"; + final String VALUE_COUNT="value_count"; + final String AVG = "AVG"; + final String CUMMULATIVE_SUM="cumulative_sum"; + final String FIELD = "FIELD"; + final String COUNT = "COUNT"; + final String BUCKETS_PATH = "BUCKETS_PATH"; + final String DATE_HISTOGRAM = "DATE_HISTOGRAM"; + final String EXTENDED_BOUNDS = "EXTENDED_BOUNDS"; + final String PATH ="PATH"; + final String MIN = "MIN"; + final String MAX = "MAX"; + final String INTERVAL = "INTERVAL"; + final String HOUR ="HOUR"; + final String DAY ="DAY"; + final String DAY_OF_WEEK ="dayOfWeek"; + final String ASC="ASC"; + final String DESC ="DESC"; + + final String MINUTE ="MINUTE"; + final String MONTH ="MONTH"; + final String TERM ="TERMS"; + final String SIZE="SIZE"; + final String ORDER="ORDER"; + final Integer TOP_CSAT_STORE_COUNT = 5; + final String LABEL="label"; + + + + } + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/analytics/utils/ElasticSearchClient.java b/src/main/java/com/tarento/analytics/utils/ElasticSearchClient.java new file mode 100644 index 0000000000000000000000000000000000000000..92d98f8fe485323e5f8e381e525349e8335a56c5 --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/ElasticSearchClient.java @@ -0,0 +1,48 @@ +package com.tarento.analytics.utils; + +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + + +@Component +public class ElasticSearchClient { + private final String hostName; + private final Integer port; + private final static String HTTP = "http"; + private final Integer topPerformanceCount; + private final String transactionIndex; + private final String transactionType; + private final String targetIndex; + private final String targetType; + + public ElasticSearchClient(@Value("${services.esindexer.primary.host.name}") String hostName, + @Value("${services.esindexer.primary.host.port}") Integer port, + @Value("${top.performance.record.count}") Integer topPerformanceCount, + @Value("${es.index.name}") String transactionIndex, + @Value("${es.document.type}") String transactionType, + @Value("${es.target.index.name}") String targetIndex, + @Value("${es.target.document.type}") String targetType){ + + this.hostName = hostName; + this.port = port; + this.topPerformanceCount = topPerformanceCount; + this.transactionIndex = transactionIndex; + this.transactionType = transactionType; + this.targetIndex = targetIndex; + this.targetType = targetType; + + } + + public RestHighLevelClient getClient() { + return new RestHighLevelClient( + RestClient.builder(new HttpHost(hostName, port,HTTP))); + + } + + + + +} diff --git a/src/main/java/com/tarento/analytics/utils/JSONObjectUtil.java b/src/main/java/com/tarento/analytics/utils/JSONObjectUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..6a7e33e2c367fb053b0c1c7fd52a6bb6f4b3e8f3 --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/JSONObjectUtil.java @@ -0,0 +1,41 @@ +package com.tarento.analytics.utils; +import org.springframework.beans.factory.annotation.Autowired; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; + +public class JSONObjectUtil { + @Autowired + public ObjectMapper mapper; + @Autowired + public Gson gson; + + + /** + * @return + */ + public static String getJsonString(ObjectMapper objectMapper,Object object) throws JsonProcessingException { + if(objectMapper != null){ + return objectMapper.writeValueAsString(object); + } + return null; + } + + public ObjectMapper getMapper() { + return mapper; + } + + public void setObjectMapper(ObjectMapper objectMapper){ + mapper=objectMapper; + } + + public Gson getGson() { + return gson; + } + + public void setGson(Gson gsonn) + { + gson = gsonn; + } +} diff --git a/src/main/java/com/tarento/analytics/utils/JsonKey.java b/src/main/java/com/tarento/analytics/utils/JsonKey.java new file mode 100644 index 0000000000000000000000000000000000000000..d60097bc7944a76226bf9b2e56f57f52ee1ed26b --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/JsonKey.java @@ -0,0 +1,20 @@ +/** + * + */ +package com.tarento.analytics.utils; + +/** + * @author Satish + * + */ +public class JsonKey { + + public static final String STATUS_CODE = "statusCode"; + public static final String STATUS = "statusInfo"; + public static final String STATUS_MESSAGE = "statusMessage"; + public static final String ERROR_MESSAGE = "errorMessage"; + + public static final String RESPONSE_DATA = "responseData"; + + +} diff --git a/src/main/java/com/tarento/analytics/utils/MapConstants.java b/src/main/java/com/tarento/analytics/utils/MapConstants.java new file mode 100644 index 0000000000000000000000000000000000000000..9f0de95be8a18b5e645e4b12d6f73d0e18d79d29 --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/MapConstants.java @@ -0,0 +1,759 @@ +package com.tarento.analytics.utils; + +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.springframework.stereotype.Component; + +@Component +public class MapConstants { + + private MapConstants() { + } + + public static final Long DEFAULT_FORM_ID = 1588649441368l; + public static final Map<String, String> DISTRICT_STATE_MAP = Stream.of(new String[][] { + {"Kupwara","Jammu And Kashmir"}, + {"Baramula","Jammu And Kashmir"}, + {"Srinagar","Jammu And Kashmir"}, + {"Badgam","Jammu And Kashmir"}, + {"Pulwama","Jammu And Kashmir"}, + {"Anantnag","Jammu And Kashmir"}, + {"Leh (Ladakh)","Jammu And Kashmir"}, + {"Kargil","Jammu And Kashmir"}, + {"Doda","Jammu And Kashmir"}, + {"Udhampur","Jammu And Kashmir"}, + {"Punch","Jammu And Kashmir"}, + {"Rajauri","Jammu And Kashmir"}, + {"Jammu","Jammu And Kashmir"}, + {"Kathua","Jammu And Kashmir"}, + {"Shopian","Jammu And Kashmir"}, + {"Bandipora","Jammu And Kashmir"}, + {"Kishtwar","Jammu And Kashmir"}, + {"Ramban","Jammu And Kashmir"}, + {"Ganderbal","Jammu And Kashmir"}, + {"Kulgam","Jammu And Kashmir"}, + {"Reasi","Jammu And Kashmir"}, + {"Samba","Jammu And Kashmir"}, + {"Chamba","Himachal Pradesh"}, + {"Kangra","Himachal Pradesh"}, + {"Lahul & Spiti","Himachal Pradesh"}, + {"Kullu","Himachal Pradesh"}, + {"Mandi","Himachal Pradesh"}, + {"Hamirpur (H.P.)","Himachal Pradesh"}, + {"Una","Himachal Pradesh"}, + {"Bilaspur (H.P.)","Himachal Pradesh"}, + {"Solan","Himachal Pradesh"}, + {"Sirmaur","Himachal Pradesh"}, + {"Shimla","Himachal Pradesh"}, + {"Kinnaur","Himachal Pradesh"}, + {"Gurdaspur","Punjab"}, + {"Amritsar","Punjab"}, + {"Kapurthala","Punjab"}, + {"Jalandhar","Punjab"}, + {"Hoshiarpur","Punjab"}, + {"Shaheed Bhagat Singh Nagar","Punjab"}, + {"Rupnagar","Punjab"}, + {"Fatehgarh Sahib","Punjab"}, + {"Ludhiana","Punjab"}, + {"Moga","Punjab"}, + {"Ferozepur","Punjab"}, + {"Sri Muktsar Sahib","Punjab"}, + {"Faridkot","Punjab"}, + {"Bathinda","Punjab"}, + {"Mansa","Punjab"}, + {"Sangrur","Punjab"}, + {"Patiala","Punjab"}, + {"Sahibzada Ajit Singh Nagar","Punjab"}, + {"Tarn Taran","Punjab"}, + {"Barnala","Punjab"}, + {"Fazilka","Punjab"}, + {"Pathankot","Punjab"}, + {"Chandigarh","Chandigarh"}, + {"Uttarkashi","Uttarakhand"}, + {"Chamoli","Uttarakhand"}, + {"Rudraprayag","Uttarakhand"}, + {"Tehri Garhwal","Uttarakhand"}, + {"Dehradun","Uttarakhand"}, + {"Garhwal","Uttarakhand"}, + {"Pithoragarh","Uttarakhand"}, + {"Bageshwar","Uttarakhand"}, + {"Almora","Uttarakhand"}, + {"Champawat","Uttarakhand"}, + {"Nainital","Uttarakhand"}, + {"Udham Singh Nagar","Uttarakhand"}, + {"Hardwar","Uttarakhand"}, + {"Panchkula","Haryana"}, + {"Ambala","Haryana"}, + {"Yamunanagar","Haryana"}, + {"Kurukshetra","Haryana"}, + {"Kaithal","Haryana"}, + {"Karnal","Haryana"}, + {"Panipat","Haryana"}, + {"Sonipat","Haryana"}, + {"Jind","Haryana"}, + {"Fatehabad","Haryana"}, + {"Sirsa","Haryana"}, + {"Hisar","Haryana"}, + {"Bhiwani","Haryana"}, + {"Rohtak","Haryana"}, + {"Jhajjar","Haryana"}, + {"Mahendragarh","Haryana"}, + {"Rewari","Haryana"}, + {"Gurugram","Haryana"}, + {"Mewat","Haryana"}, + {"Faridabad","Haryana"}, + {"Palwal","Haryana"}, + {"Charkhi Dadri","Haryana"}, + {"North West Delhi","Delhi"}, + {"North Delhi","Delhi"}, + {"North East Delhi","Delhi"}, + {"East Delhi","Delhi"}, + {"New Delhi","Delhi"}, + {"Central Delhi","Delhi"}, + {"West Delhi","Delhi"}, + {"South West Delhi","Delhi"}, + {"South Delhi","Delhi"}, + {"South East Delhi","Delhi"}, + {"Shahdara","Delhi"}, + {"Ganganagar","Rajasthan"}, + {"Hanumangarh","Rajasthan"}, + {"Bikaner","Rajasthan"}, + {"Churu","Rajasthan"}, + {"Jhunjhunu","Rajasthan"}, + {"Alwar","Rajasthan"}, + {"Bharatpur","Rajasthan"}, + {"Dhaulpur","Rajasthan"}, + {"Karauli","Rajasthan"}, + {"Sawai Madhopur","Rajasthan"}, + {"Dausa","Rajasthan"}, + {"Jaipur","Rajasthan"}, + {"Sikar","Rajasthan"}, + {"Nagaur","Rajasthan"}, + {"Jodhpur","Rajasthan"}, + {"Jaisalmer","Rajasthan"}, + {"Barmer","Rajasthan"}, + {"Jalor","Rajasthan"}, + {"Sirohi","Rajasthan"}, + {"Pali","Rajasthan"}, + {"Ajmer","Rajasthan"}, + {"Tonk","Rajasthan"}, + {"Bundi","Rajasthan"}, + {"Bhilwara","Rajasthan"}, + {"Rajsamand","Rajasthan"}, + {"Udaipur","Rajasthan"}, + {"Dungarpur","Rajasthan"}, + {"Banswara","Rajasthan"}, + {"Chittaurgarh","Rajasthan"}, + {"Kota","Rajasthan"}, + {"Baran","Rajasthan"}, + {"Jhalawar","Rajasthan"}, + {"Pratapgarh","Rajasthan"}, + {"Agra","Uttar Pradesh"}, + {"Hathras","Uttar Pradesh"}, + {"Aligarh","Uttar Pradesh"}, + {"Mathura","Uttar Pradesh"}, + {"Firozabad","Uttar Pradesh"}, + {"Mainpuri","Uttar Pradesh"}, + {"Etah","Uttar Pradesh"}, + {"Bareilly","Uttar Pradesh"}, + {"Budaun","Uttar Pradesh"}, + {"Shahjahanpur","Uttar Pradesh"}, + {"Pilibhit","Uttar Pradesh"}, + {"Moradabad","Uttar Pradesh"}, + {"Amroha","Uttar Pradesh"}, + {"Rampur","Uttar Pradesh"}, + {"Bijnor","Uttar Pradesh"}, + {"Kanpur Nagar","Uttar Pradesh"}, + {"Kanpur Dehat","Uttar Pradesh"}, + {"Kannauj","Uttar Pradesh"}, + {"Farrukhabad","Uttar Pradesh"}, + {"Raebareli","Uttar Pradesh"}, + {"Rai Bareilly","Uttar Pradesh"}, + {"Etawah","Uttar Pradesh"}, + {"Meerut","Uttar Pradesh"}, + {"Baghpat","Uttar Pradesh"}, + {"Ghaziabad","Uttar Pradesh"}, + {"Gautam Buddh Nagar","Uttar Pradesh"}, + {"G B Nagar", "Uttar Pradesh"}, + {"Bulandshehr","Uttar Pradesh"}, + {"Buland Shahar","Uttar Pradesh"}, + {"Lucknow","Uttar Pradesh"}, + {"Saharanpur","Uttar Pradesh"}, + {"Muzaffarnagar","Uttar Pradesh"}, + {"Muzaffar Nagar","Uttar Pradesh"}, + {"Unnao","Uttar Pradesh"}, + {"Auraiya","Uttar Pradesh"}, + {"Sitapur","Uttar Pradesh"}, + {"Hardoi","Uttar Pradesh"}, + {"Lakhimpur","Uttar Pradesh"}, + {"Lakhimpur Khiri","Uttar Pradesh"}, + {"Allahabad","Uttar Pradesh"}, + {"Barabanki","Uttar Pradesh"}, + {"Fatehpur","Uttar Pradesh"}, + {"Pratapgadh","Uttar Pradesh"}, + {"Jhansi","Uttar Pradesh"}, + {"Lalitpur","Uttar Pradesh"}, + {"Jalaun","Uttar Pradesh"}, + {"Varanasi","Uttar Pradesh"}, + {"Chandauli","Uttar Pradesh"}, + {"Jaunpur","Uttar Pradesh"}, + {"Ghazipur","Uttar Pradesh"}, + {"Mirzapur","Uttar Pradesh"}, + {"Sonbhadra","Uttar Pradesh"}, + {"Sant Ravidas Nagar","Uttar Pradesh"}, + {"Azamgarh","Uttar Pradesh"}, + {"Mau","Uttar Pradesh"}, + {"Ballia","Uttar Pradesh"}, + {"Balia","Uttar Pradesh"}, + {"Gorakhpur","Uttar Pradesh"}, + {"Maharajganj","Uttar Pradesh"}, + {"Deoria","Uttar Pradesh"}, + {"Kushinagar","Uttar Pradesh"}, + {"Kushi Nagar", "Uttar Pradesh"}, + {"Basti","Uttar Pradesh"}, + {"Sant Kabeer Nagar","Uttar Pradesh"}, + {"Sant Kabir Nagar","Uttar Pradesh"}, + {"Siddhartha Nagar","Uttar Pradesh"}, + {"Siddharth Nagar","Uttar Pradesh"}, + {"Faizabad","Uttar Pradesh"}, + {"Ambedkar Nagar","Uttar Pradesh"}, + {"Sultanpur","Uttar Pradesh"}, + {"Gonda","Uttar Pradesh"}, + {"Balrampur","Uttar Pradesh"}, + {"Behraich","Uttar Pradesh"}, + {"Bahraich","Uttar Pradesh"}, + {"Shravasti","Uttar Pradesh"}, + {"Srawasti","Uttar Pradesh"}, + {"Hapur","Uttar Pradesh"}, + {"Banda","Uttar Pradesh"}, + {"Mahoba","Uttar Pradesh"}, + {"Hamirpur","Uttar Pradesh"}, + {"Kasganj","Uttar Pradesh"}, + {"Amethi - Csm Nagar","Uttar Pradesh"}, + {"Amethi","Uttar Pradesh"}, + {"Shamli (Prabudh Nagar)","Uttar Pradesh"}, + {"Shamli", "Uttar Pradesh"}, + {"Sambhal","Uttar Pradesh"}, + {"Chitrakoot","Uttar Pradesh"}, + {"Kaushambi","Uttar Pradesh"}, + {"Bhadohi","Uttar Pradesh"}, + {"Pashchim Champaran","Bihar"}, + {"Purba Champaran","Bihar"}, + {"Sheohar","Bihar"}, + {"Sitamarhi","Bihar"}, + {"Madhubani","Bihar"}, + {"Supaul","Bihar"}, + {"Araria","Bihar"}, + {"Kishanganj","Bihar"}, + {"Purnia","Bihar"}, + {"Katihar","Bihar"}, + {"Madhepura","Bihar"}, + {"Saharsa","Bihar"}, + {"Darbhanga","Bihar"}, + {"Muzaffarpur","Bihar"}, + {"Gopalganj","Bihar"}, + {"Siwan","Bihar"}, + {"Saran","Bihar"}, + {"Vaishali","Bihar"}, + {"Samastipur","Bihar"}, + {"Begusarai","Bihar"}, + {"Khagaria","Bihar"}, + {"Bhagalpur","Bihar"}, + {"Banka","Bihar"}, + {"Munger","Bihar"}, + {"Lakhisarai","Bihar"}, + {"Sheikhpura","Bihar"}, + {"Nalanda","Bihar"}, + {"Patna","Bihar"}, + {"Bhojpur","Bihar"}, + {"Buxar","Bihar"}, + {"Kaimur (Bhabua)","Bihar"}, + {"Rohtas","Bihar"}, + {"Jehanabad","Bihar"}, + {"Aurangabad (Bihar)","Bihar"}, + {"Gaya","Bihar"}, + {"Nawada","Bihar"}, + {"Jamui","Bihar"}, + {"Arwal","Bihar"}, + {"North Sikkim","Sikkim"}, + {"West Sikkim","Sikkim"}, + {"South Sikkim","Sikkim"}, + {"East Sikkim","Sikkim"}, + {"Tawang","Arunachal Pradesh"}, + {"West Kameng","Arunachal Pradesh"}, + {"East Kameng","Arunachal Pradesh"}, + {"Papum Pare","Arunachal Pradesh"}, + {"Lower Subansiri","Arunachal Pradesh"}, + {"Upper Subansiri","Arunachal Pradesh"}, + {"West Siang","Arunachal Pradesh"}, + {"East Siang","Arunachal Pradesh"}, + {"Upper Siang","Arunachal Pradesh"}, + {"Dibang Valley","Arunachal Pradesh"}, + {"Lohit","Arunachal Pradesh"}, + {"Changlang","Arunachal Pradesh"}, + {"Tirap","Arunachal Pradesh"}, + {"Kurung Kumey","Arunachal Pradesh"}, + {"Lower Dibang Valley","Arunachal Pradesh"}, + {"Anjaw","Arunachal Pradesh"}, + {"Longding","Arunachal Pradesh"}, + {"Namsai","Arunachal Pradesh"}, + {"Kra Dadi","Arunachal Pradesh"}, + {"Siang","Arunachal Pradesh"}, + {"Kamle","Arunachal Pradesh"}, + {"Lepa Rada","Arunachal Pradesh"}, + {"Lower Siang","Arunachal Pradesh"}, + {"Pakke Kessang","Arunachal Pradesh"}, + {"Shi Yomi","Arunachal Pradesh"}, + {"Mon","Nagaland"}, + {"Tuensang","Nagaland"}, + {"Mokokchung","Nagaland"}, + {"Zunheboto","Nagaland"}, + {"Wokha","Nagaland"}, + {"Dimapur","Nagaland"}, + {"Kohima","Nagaland"}, + {"Phek","Nagaland"}, + {"Longleng","Nagaland"}, + {"Kiphire","Nagaland"}, + {"Peren","Nagaland"}, + {"Senapati","Manipur"}, + {"Tamenglong","Manipur"}, + {"Churachandpur","Manipur"}, + {"Bishnupur","Manipur"}, + {"Thoubal","Manipur"}, + {"Imphal West","Manipur"}, + {"Imphal East","Manipur"}, + {"Ukhrul","Manipur"}, + {"Chandel","Manipur"}, + {"Jiribam","Manipur"}, + {"Kakching","Manipur"}, + {"Kamjong","Manipur"}, + {"Kangpokpi","Manipur"}, + {"Noney","Manipur"}, + {"Pherzawl","Manipur"}, + {"Tengnoupal","Manipur"}, + {"Mamit","Mizoram"}, + {"Kolasib","Mizoram"}, + {"Aizawl","Mizoram"}, + {"Champhai","Mizoram"}, + {"Serchhip","Mizoram"}, + {"Lunglei","Mizoram"}, + {"Lawngtlai","Mizoram"}, + {"Saiha","Mizoram"}, + {"West Tripura","Tripura"}, + {"South Tripura","Tripura"}, + {"Dhalai","Tripura"}, + {"North Tripura","Tripura"}, + {"Sepahijala","Tripura"}, + {"Khowai","Tripura"}, + {"Gomati","Tripura"}, + {"Unakoti","Tripura"}, + {"West Garo Hills","Meghalaya"}, + {"East Garo Hills","Meghalaya"}, + {"South Garo Hills","Meghalaya"}, + {"West Khasi Hills","Meghalaya"}, + {"Ri Bhoi","Meghalaya"}, + {"East Khasi Hills","Meghalaya"}, + {"West Jaintia Hills","Meghalaya"}, + {"North Garo Hills","Meghalaya"}, + {"East Jaintia Hills","Meghalaya"}, + {"South West Khasi Hills","Meghalaya"}, + {"South West Garo Hills","Meghalaya"}, + {"Kokrajhar","Assam"}, + {"Dhubri","Assam"}, + {"Goalpara","Assam"}, + {"Bongaigaon","Assam"}, + {"Barpeta","Assam"}, + {"Kamrup Rural","Assam"}, + {"Nalbari","Assam"}, + {"Darrang","Assam"}, + {"Morigaon","Assam"}, + {"Nagaon","Assam"}, + {"Sonitpur","Assam"}, + {"Lakimpur","Assam"}, + {"Dhemaji","Assam"}, + {"Tinsukia","Assam"}, + {"Dibrugarh","Assam"}, + {"Sivasagar","Assam"}, + {"Jorhat","Assam"}, + {"Golaghat","Assam"}, + {"Karbi Anglong","Assam"}, + {"Dima Hasao","Assam"}, + {"Cachar","Assam"}, + {"Karimganj","Assam"}, + {"Hailakandi","Assam"}, + {"Baksa","Assam"}, + {"Chirang","Assam"}, + {"Udalguri","Assam"}, + {"Kamrup Metropolitan","Assam"}, + {"Biswanath","Assam"}, + {"Charaideo","Assam"}, + {"Hojai","Assam"}, + {"Majuli","Assam"}, + {"South Salmara-Mankachar","Assam"}, + {"West Karbi Anglong","Assam"}, + {"Darjiling","West Bengal"}, + {"Jalpaiguri","West Bengal"}, + {"Koch Bihar","West Bengal"}, + {"Uttar Dinajpur","West Bengal"}, + {"Dakshin Dinajpur","West Bengal"}, + {"Maldah","West Bengal"}, + {"Murshidabad","West Bengal"}, + {"Birbhum","West Bengal"}, + {"Nadia","West Bengal"}, + {"North Twenty Four Pargana","West Bengal"}, + {"Hugli","West Bengal"}, + {"Bankura","West Bengal"}, + {"Puruliya","West Bengal"}, + {"Haora","West Bengal"}, + {"Kolkata","West Bengal"}, + {"South Twenty Four Pargan","West Bengal"}, + {"Purba Medinipur","West Bengal"}, + {"Paschim Medinipur","West Bengal"}, + {"Alipurduar","West Bengal"}, + {"Jhargram","West Bengal"}, + {"Kalimpong","West Bengal"}, + {"Paschim Burdwan (West Bardhaman)","West Bengal"}, + {"Purba Burdwan (East Bardhaman)","West Bengal"}, + {"Garhwa","Jharkhand"}, + {"Palamu","Jharkhand"}, + {"Chatra","Jharkhand"}, + {"Hazaribagh","Jharkhand"}, + {"Koderma","Jharkhand"}, + {"Giridih","Jharkhand"}, + {"Deoghar","Jharkhand"}, + {"Godda","Jharkhand"}, + {"Sahibganj","Jharkhand"}, + {"Pakur","Jharkhand"}, + {"Dumka","Jharkhand"}, + {"Dhanbad","Jharkhand"}, + {"Bokaro","Jharkhand"}, + {"Ranchi","Jharkhand"}, + {"Lohardaga","Jharkhand"}, + {"Gumla","Jharkhand"}, + {"Pashchimi Singhbhum","Jharkhand"}, + {"Purbi Singhbhum","Jharkhand"}, + {"Jamtara","Jharkhand"}, + {"Saraikela-Kharsawan","Jharkhand"}, + {"Simdega","Jharkhand"}, + {"Latehar","Jharkhand"}, + {"Khunti","Jharkhand"}, + {"Ramgarh","Jharkhand"}, + {"Baragarh","Odisha"}, + {"Jharsuguda","Odisha"}, + {"Sambalpur","Odisha"}, + {"Deogarh","Odisha"}, + {"Sundergarh","Odisha"}, + {"Keonjhar","Odisha"}, + {"Mayurbhanj","Odisha"}, + {"Balasore","Odisha"}, + {"Bhadrak","Odisha"}, + {"Kendrapara","Odisha"}, + {"Jagatsinghpur","Odisha"}, + {"Cuttack","Odisha"}, + {"Jajpur","Odisha"}, + {"Dhenkanal","Odisha"}, + {"Angul","Odisha"}, + {"Nayagarh","Odisha"}, + {"Khurdha","Odisha"}, + {"Puri","Odisha"}, + {"Ganjam","Odisha"}, + {"Gajapati","Odisha"}, + {"Kandhamal","Odisha"}, + {"Boudh","Odisha"}, + {"Sonepur","Odisha"}, + {"Bolangir","Odisha"}, + {"Nuapada","Odisha"}, + {"Kalahandi","Odisha"}, + {"Rayagada","Odisha"}, + {"Nabarangpur","Odisha"}, + {"Koraput","Odisha"}, + {"Malkangiri","Odisha"}, + {"Koriya","Chhattisgarh"}, + {"Surguja","Chhattisgarh"}, + {"Jashpur","Chhattisgarh"}, + {"Raigarh","Chhattisgarh"}, + {"Korba","Chhattisgarh"}, + {"Janjgir-Champa","Chhattisgarh"}, + {"Bilaspur","Chhattisgarh"}, + {"Kawardha","Chhattisgarh"}, + {"Rajnandgaon","Chhattisgarh"}, + {"Durg","Chhattisgarh"}, + {"Raipur","Chhattisgarh"}, + {"Mahasamund","Chhattisgarh"}, + {"Dhamtari","Chhattisgarh"}, + {"Kanker","Chhattisgarh"}, + {"Bastar","Chhattisgarh"}, + {"Dantewada","Chhattisgarh"}, + {"Kondagaon","Chhattisgarh"}, + {"Narayanpur","Chhattisgarh"}, + {"Mungeli","Chhattisgarh"}, + {"Bijapur","Chhattisgarh"}, + {"Sukma","Chhattisgarh"}, + {"Balod","Chhattisgarh"}, + {"Bemetara","Chhattisgarh"}, + {"Balodabazar","Chhattisgarh"}, + {"Gariaband","Chhattisgarh"}, + {"Balrampurh","Chhattisgarh"}, + {"Surajpur","Chhattisgarh"}, + {"Gaurela-Pendra-Marvahi","Chhattisgarh"}, + {"Sheopur","Madhya Pradesh"}, + {"Morena","Madhya Pradesh"}, + {"Bhind","Madhya Pradesh"}, + {"Gwalior","Madhya Pradesh"}, + {"Datia","Madhya Pradesh"}, + {"Shivpuri","Madhya Pradesh"}, + {"Guna","Madhya Pradesh"}, + {"Tikamgarh","Madhya Pradesh"}, + {"Chhatarpur","Madhya Pradesh"}, + {"Panna","Madhya Pradesh"}, + {"Sagar","Madhya Pradesh"}, + {"Damoh","Madhya Pradesh"}, + {"Satna","Madhya Pradesh"}, + {"Rewa","Madhya Pradesh"}, + {"Umaria","Madhya Pradesh"}, + {"Shahdol","Madhya Pradesh"}, + {"Sidhi","Madhya Pradesh"}, + {"Neemuch","Madhya Pradesh"}, + {"Mandsaur","Madhya Pradesh"}, + {"Ratlam","Madhya Pradesh"}, + {"Ujjain","Madhya Pradesh"}, + {"Shajapur","Madhya Pradesh"}, + {"Dewas","Madhya Pradesh"}, + {"Jhabua","Madhya Pradesh"}, + {"Dhar","Madhya Pradesh"}, + {"Indore","Madhya Pradesh"}, + {"Khargone","Madhya Pradesh"}, + {"Barwani","Madhya Pradesh"}, + {"Khandwa","Madhya Pradesh"}, + {"Rajgarh","Madhya Pradesh"}, + {"Vidisha","Madhya Pradesh"}, + {"Bhopal","Madhya Pradesh"}, + {"Sehore","Madhya Pradesh"}, + {"Raisen","Madhya Pradesh"}, + {"Betul","Madhya Pradesh"}, + {"Harda","Madhya Pradesh"}, + {"Hoshangabad","Madhya Pradesh"}, + {"Katni","Madhya Pradesh"}, + {"Jabalpur","Madhya Pradesh"}, + {"Narsinghpur","Madhya Pradesh"}, + {"Dindori","Madhya Pradesh"}, + {"Mandla","Madhya Pradesh"}, + {"Chhindwara","Madhya Pradesh"}, + {"Seoni","Madhya Pradesh"}, + {"Balaghat","Madhya Pradesh"}, + {"Ashoknagar","Madhya Pradesh"}, + {"Anuppur","Madhya Pradesh"}, + {"Burhanpur","Madhya Pradesh"}, + {"Alirajpur","Madhya Pradesh"}, + {"Singrauli","Madhya Pradesh"}, + {"Agar Malwa","Madhya Pradesh"}, + {"Niwari","Madhya Pradesh"}, + {"Kachchh","Gujarat"}, + {"Banaskantha","Gujarat"}, + {"Patan","Gujarat"}, + {"Mahesana","Gujarat"}, + {"Sabar Kantha","Gujarat"}, + {"Gandhinagar","Gujarat"}, + {"Ahmedabad","Gujarat"}, + {"Surendranagar","Gujarat"}, + {"Rajkot","Gujarat"}, + {"Jamnagar","Gujarat"}, + {"Porbandar","Gujarat"}, + {"Junagadh","Gujarat"}, + {"Amreli","Gujarat"}, + {"Bhavnagar","Gujarat"}, + {"Anand","Gujarat"}, + {"Kheda","Gujarat"}, + {"Panch Mahals","Gujarat"}, + {"Dahod","Gujarat"}, + {"Vadodara","Gujarat"}, + {"Narmada","Gujarat"}, + {"Bharuch","Gujarat"}, + {"Surat","Gujarat"}, + {"The Dangs","Gujarat"}, + {"Navsari","Gujarat"}, + {"Valsad","Gujarat"}, + {"Tapi","Gujarat"}, + {"Aravalli","Gujarat"}, + {"Botad","Gujarat"}, + {"Devbhoomi Dwarka","Gujarat"}, + {"Gir Somnath","Gujarat"}, + {"Mahisagar","Gujarat"}, + {"Chhotaudepur","Gujarat"}, + {"Morbi","Gujarat"}, + {"Diu","Daman & Diu"}, + {"Daman","Daman & Diu"}, + {"Dadra And Nagar Haveli(Ut)","Dadra & Nagar Haveli"}, + {"Nandurbar","Maharashtra"}, + {"Dhule","Maharashtra"}, + {"Jalgaon","Maharashtra"}, + {"Buldana","Maharashtra"}, + {"Akola","Maharashtra"}, + {"Washim","Maharashtra"}, + {"Amravati","Maharashtra"}, + {"Wardha","Maharashtra"}, + {"Nagpur","Maharashtra"}, + {"Bhandara","Maharashtra"}, + {"Gondia","Maharashtra"}, + {"Gadchiroli","Maharashtra"}, + {"Chandrapur","Maharashtra"}, + {"Yavatmal","Maharashtra"}, + {"Nanded","Maharashtra"}, + {"Hingoli","Maharashtra"}, + {"Parbhani","Maharashtra"}, + {"Jalna","Maharashtra"}, + {"Aurangabad","Maharashtra"}, + {"Nashik","Maharashtra"}, + {"Thane","Maharashtra"}, + {"Mumbai (Suburban)","Maharashtra"}, + {"Mumbai","Maharashtra"}, + {"Raigad","Maharashtra"}, + {"Pune","Maharashtra"}, + {"Ahmednagar","Maharashtra"}, + {"Beed","Maharashtra"}, + {"Latur","Maharashtra"}, + {"Osmanabad","Maharashtra"}, + {"Solapur","Maharashtra"}, + {"Satara","Maharashtra"}, + {"Ratnagiri","Maharashtra"}, + {"Sindhudurg","Maharashtra"}, + {"Kolhapur","Maharashtra"}, + {"Sangli","Maharashtra"}, + {"Palghar","Maharashtra"}, + {"Srikakulam","Andhra Pradesh"}, + {"Vizianagaram","Andhra Pradesh"}, + {"Visakhapatnam","Andhra Pradesh"}, + {"East Godavari","Andhra Pradesh"}, + {"West Godavari","Andhra Pradesh"}, + {"Krishna","Andhra Pradesh"}, + {"Guntur","Andhra Pradesh"}, + {"Prakasam","Andhra Pradesh"}, + {"Nellore","Andhra Pradesh"}, + {"Kadapa","Andhra Pradesh"}, + {"Kurnool","Andhra Pradesh"}, + {"Anantapur","Andhra Pradesh"}, + {"Chittoor","Andhra Pradesh"}, + {"Belagavi","Karnataka"}, + {"Bagalkot","Karnataka"}, + {"Vijayapura","Karnataka"}, + {"Kalaburgi","Karnataka"}, + {"Bidar","Karnataka"}, + {"Raichur","Karnataka"}, + {"Koppal","Karnataka"}, + {"Gadag","Karnataka"}, + {"Dharwad","Karnataka"}, + {"Uttara Kannada","Karnataka"}, + {"Haveri","Karnataka"}, + {"Ballari","Karnataka"}, + {"Chitradurga","Karnataka"}, + {"Davanagere","Karnataka"}, + {"Shivamogga","Karnataka"}, + {"Udupi","Karnataka"}, + {"Chikkamagaluru","Karnataka"}, + {"Tumakuru","Karnataka"}, + {"Kolar","Karnataka"}, + {"Bengaluru Urban","Karnataka"}, + {"Bengaluru Rural","Karnataka"}, + {"Mandya","Karnataka"}, + {"Hassan","Karnataka"}, + {"Dakshina Kannada","Karnataka"}, + {"Kodagu","Karnataka"}, + {"Mysuru","Karnataka"}, + {"Chamarajanagara","Karnataka"}, + {"Chikkaballapura","Karnataka"}, + {"Ramanagara","Karnataka"}, + {"Yadgir","Karnataka"}, + {"North Goa","Goa"}, + {"South Goa","Goa"}, + {"Lakshadweep","Lakshadweep"}, + {"Thiruvananthapuram","Kerala"}, + {"Kollam","Kerala"}, + {"Pathanamthitta","Kerala"}, + {"Alappuzha","Kerala"}, + {"Kottayam","Kerala"}, + {"Idukki","Kerala"}, + {"Ernakulam","Kerala"}, + {"Thrissur","Kerala"}, + {"Palakkad","Kerala"}, + {"Malappuram","Kerala"}, + {"Kozhikode","Kerala"}, + {"Wayanad","Kerala"}, + {"Kannur","Kerala"}, + {"Kasaragod","Kerala"}, + {"Kancheepuram","Tamil Nadu"}, + {"Tiruvallur","Tamil Nadu"}, + {"Cuddalore","Tamil Nadu"}, + {"Villupuram","Tamil Nadu"}, + {"Vellore","Tamil Nadu"}, + {"Tiruvannamalai","Tamil Nadu"}, + {"Salem","Tamil Nadu"}, + {"Namakkal","Tamil Nadu"}, + {"Dharmapuri","Tamil Nadu"}, + {"Erode","Tamil Nadu"}, + {"Coimbatore","Tamil Nadu"}, + {"The Nilgiris","Tamil Nadu"}, + {"Thanjavur","Tamil Nadu"}, + {"Nagapattinam","Tamil Nadu"}, + {"Tiruvarur","Tamil Nadu"}, + {"Tiruchirappalli","Tamil Nadu"}, + {"Karur","Tamil Nadu"}, + {"Perambalur","Tamil Nadu"}, + {"Pudukkottai","Tamil Nadu"}, + {"Madurai","Tamil Nadu"}, + {"Theni","Tamil Nadu"}, + {"Dindigul","Tamil Nadu"}, + {"Ramanathapuram","Tamil Nadu"}, + {"Virudhunagar","Tamil Nadu"}, + {"Sivagangai","Tamil Nadu"}, + {"Tirunelveli","Tamil Nadu"}, + {"Thoothukkudi","Tamil Nadu"}, + {"Kanniyakumari","Tamil Nadu"}, + {"Chennai","Tamil Nadu"}, + {"Krishnagiri","Tamil Nadu"}, + {"Ariyalur","Tamil Nadu"}, + {"Tiruppur","Tamil Nadu"}, + {"Yanam","Puducherry"}, + {"Pondicherry","Puducherry"}, + {"Mahe","Puducherry"}, + {"Karaikal","Puducherry"}, + {"Andamans","Andaman & Nicobar Islands"}, + {"Nicobars","Andaman & Nicobar Islands"}, + {"Middle And North Andamans","Andaman & Nicobar Islands"}, + {"Adilabad","Telangana"}, + {"Komaram Bheem","Telangana"}, + {"Mancherial","Telangana"}, + {"Nirmal","Telangana"}, + {"Nizamabad","Telangana"}, + {"Jagtial","Telangana"}, + {"Peddapalli","Telangana"}, + {"Jayashankar","Telangana"}, + {"Bhadradri","Telangana"}, + {"Mahabubabad","Telangana"}, + {"Warangal Rural","Telangana"}, + {"Warangal Urban","Telangana"}, + {"Karimnagar","Telangana"}, + {"Rajanna","Telangana"}, + {"Kamareddy","Telangana"}, + {"Sangareddy","Telangana"}, + {"Medak","Telangana"}, + {"Siddipet","Telangana"}, + {"Jangaon","Telangana"}, + {"Yadadri","Telangana"}, + {"Medchal-Malkajgiri","Telangana"}, + {"Hyderbad","Telangana"}, + {"Rangareddy","Telangana"}, + {"Vikarabad","Telangana"}, + {"Mahabubnagar","Telangana"}, + {"Jogulamba","Telangana"}, + {"Wanaparthy","Telangana"}, + {"Nagarkurnool","Telangana"}, + {"Nalgonda","Telangana"}, + {"Suryapet","Telangana"}, + {"Khammam","Telangana"}, + {"Mulugu","Telangana"}, + {"Narayanpet","Telangana"} + }).collect(Collectors.toMap(data -> data[0], data -> data[1])); + +} diff --git a/src/main/java/com/tarento/analytics/utils/PathRoutes.java b/src/main/java/com/tarento/analytics/utils/PathRoutes.java new file mode 100644 index 0000000000000000000000000000000000000000..69ac68fb3c468a5e6338a5aa51844bfa6f422d54 --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/PathRoutes.java @@ -0,0 +1,44 @@ +package com.tarento.analytics.utils; + +public interface PathRoutes { + + public interface DashboardApi { + final String DASHBOARD_ROOT_PATH = "/dashboard"; + + final String TEST_PATH = "/test"; + + final String GET_CHART = "/getChart"; + final String GET_CHART_V2 = "/getChartV2"; + final String GET_REPORT = "/getReport"; + final String GET_DASHBOARD_CONFIG = "/getDashboardConfig"; + final String GET_HOME_CONFIG = "/getHomeConfig"; + final String GET_ALL_VISUALIZATIONS = "/getAllVisualizations"; + final String ADD_NEW_DASHBOARD = "/addNewDashboard"; + final String MAP_DASHBOARD_VISUALIZATIOn = "/mapVisualizationToDashboard"; + final String MAP_VISUALIZATION_ROLE = "/mapVisualizationToRole"; + final String GET_HEADER_DATA = "/getDashboardHeader"; + final String GET_FEEDBACK_MESSAGE="/getPulseFeedbackMessage"; + final String TARGET_DISTRICT_ULB = "/targetDistrict"; + final String GET_DASHBOARDS_FOR_PROFILE= "/getDashboardsForProfile"; + + } + + public interface MetadataApi { + final String METADATA_ROOT_PATH = "/meta"; + + final String GET_CATEGORIES = "/getCategories"; + final String GET_SUB_CATEGORIES = "/getSubCategories"; + final String GET_ITEMS = "/getItems"; + final String GET_MASTERS = "/getMasters"; + final String FLUSH_MASTERS = "/flushMasters"; + final String GET_PULSE_RATING_CONFIG="/getPulseRatingConfig"; + final String GET_RATING_CONFIG = "/getRatingConfiguration"; + final String GET_RATING_CONFIG_ENCODE = "/getRatingConfig"; + final String GET_CONFIG_VERSION = "/getConfigVersion"; + final String PULSE_VERIFY_ORG_PIN = "/verifyOrgs"; + final String PUT_ORG_INDEX ="/putIndex"; + final String PUT_QUERY ="/createQuery"; + + + } +} diff --git a/src/main/java/com/tarento/analytics/utils/ResponseCode.java b/src/main/java/com/tarento/analytics/utils/ResponseCode.java new file mode 100644 index 0000000000000000000000000000000000000000..ac4dc39622e63ec6ce0038dd252aa4828639fa74 --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/ResponseCode.java @@ -0,0 +1,87 @@ +package com.tarento.analytics.utils; + +import com.tarento.analytics.constant.Constants; + +/** + * + * @author Abhishek + * + */ +public enum ResponseCode { + UnAuthorised(Constants.UNAUTHORIZED_ID, Constants.UNAUTHORIZED), Success( + Constants.SUCCESS_ID, Constants.SUCCESS),FAILURE( + Constants.FAILURE_ID, Constants.PROCESS_FAIL); + /** + * error code contains int value + */ + private int errorCode; + /** + * errorMessage contains proper error message. + */ + private String errorMessage; + + + + /** + * @param errorCode + * @param errorMessage + */ + private ResponseCode(int errorCode, String errorMessage) { + this.errorCode = errorCode; + this.errorMessage = errorMessage; + } + + /** + * + * @param errorCode + * @return + */ + public String getMessage(int errorCode) { + return ""; + } + + /** + * @return + */ + public int getErrorCode() { + return errorCode; + } + + /** + * @param errorCode + */ + public void setErrorCode(int errorCode) { + this.errorCode = errorCode; + } + + /** + * @return + */ + public String getErrorMessage() { + return errorMessage; + } + + /** + * @param errorMessage + */ + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + /** + * This method will provide status message based on code + * + * @param code + * @return String + */ + public static String getResponseMessage(int code) { + String value = ""; + ResponseCode responseCodes[] = ResponseCode.values(); + for (ResponseCode actionState : responseCodes) { + if (actionState.getErrorCode() == code) { + value = actionState.getErrorMessage(); + } + } + return value; + } +} diff --git a/src/main/java/com/tarento/analytics/utils/ResponseGenerator.java b/src/main/java/com/tarento/analytics/utils/ResponseGenerator.java new file mode 100644 index 0000000000000000000000000000000000000000..ed7a8984582a8db583323439eb27f760c8fb976a --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/ResponseGenerator.java @@ -0,0 +1,86 @@ +package com.tarento.analytics.utils; + + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class ResponseGenerator { + + + private static ObjectMapper objectMapper = new ObjectMapper(); + + public static String failureResponse() throws JsonProcessingException{ + ObjectNode response = objectMapper.createObjectNode(); + response.put(JsonKey.STATUS_CODE, ResponseCode.FAILURE.getErrorCode()); + response.put(JsonKey.STATUS_MESSAGE, + ResponseCode.FAILURE.getErrorMessage()); + response.put(JsonKey.ERROR_MESSAGE, + ResponseCode.FAILURE.getErrorMessage()); + return JSONObjectUtil.getJsonString(objectMapper,response); + } + + + public static String failureResponse(String message) throws JsonProcessingException{ + ObjectNode actualResponse = objectMapper.createObjectNode(); + + ObjectNode response = objectMapper.createObjectNode(); + response.put(JsonKey.STATUS_CODE, ResponseCode.FAILURE.getErrorCode()); + response.put(JsonKey.STATUS_MESSAGE, + ResponseCode.FAILURE.getErrorMessage()); + response.put(JsonKey.ERROR_MESSAGE,message); + actualResponse.putPOJO(JsonKey.STATUS,response); + + return JSONObjectUtil.getJsonString(objectMapper,actualResponse); + } + + public static String failureResponse(String errorCode, String message) throws JsonProcessingException{ + ObjectNode actualResponse = objectMapper.createObjectNode(); + + ObjectNode response = objectMapper.createObjectNode(); + response.put(errorCode, message); + response.put(JsonKey.STATUS_MESSAGE, + message); + response.put(JsonKey.ERROR_MESSAGE,message); + actualResponse.putPOJO(JsonKey.STATUS,response); + + return JSONObjectUtil.getJsonString(objectMapper,actualResponse); + } + + public static String successResponse(Object obj) throws JsonProcessingException { + ObjectNode actualResponse = objectMapper.createObjectNode(); + + ObjectNode response = objectMapper.createObjectNode(); + response.put(JsonKey.STATUS_CODE, ResponseCode.Success.getErrorCode()); + response.put(JsonKey.STATUS_MESSAGE, ResponseCode.Success.getErrorMessage()); + response.put(JsonKey.ERROR_MESSAGE, ""); + actualResponse.putPOJO(JsonKey.STATUS,response); + if (obj != null) { + actualResponse.putPOJO(JsonKey.RESPONSE_DATA, obj); + } + + return JSONObjectUtil.getJsonString(objectMapper,actualResponse); + } + + /** + * this method will crate success response and send to controller. + * + * @return ObjectNode object. + */ + public static String successResponse(String message) throws JsonProcessingException{ + ObjectNode actualResponse = objectMapper.createObjectNode(); + ObjectNode response = objectMapper.createObjectNode(); + response.put(JsonKey.STATUS_CODE, ResponseCode.Success.getErrorCode()); + response.put(JsonKey.STATUS_MESSAGE, + ResponseCode.Success.getErrorMessage()); + //response.put(JsonKey.SUCCESS_MESSAGE, message); + actualResponse.putPOJO(JsonKey.STATUS,response); + response.put(JsonKey.ERROR_MESSAGE, ""); + actualResponse.putPOJO(JsonKey.STATUS,response); + if (message != null) { + actualResponse.putPOJO(JsonKey.RESPONSE_DATA, message); + } + + return JSONObjectUtil.getJsonString(objectMapper,actualResponse); + } +} diff --git a/src/main/java/com/tarento/analytics/utils/ResponseRecorder.java b/src/main/java/com/tarento/analytics/utils/ResponseRecorder.java new file mode 100644 index 0000000000000000000000000000000000000000..047f8cd8ac859ebd34dc5eda4a1b6301120a7f0f --- /dev/null +++ b/src/main/java/com/tarento/analytics/utils/ResponseRecorder.java @@ -0,0 +1,46 @@ +package com.tarento.analytics.utils; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.tarento.analytics.constant.Constants; + +@Component("responseRecorder") +public class ResponseRecorder { + + private static Logger logger = LoggerFactory.getLogger(ResponseRecorder.class); + protected static final ConcurrentHashMap<String, Map<String, Object>> requestModuleResponseMap = new ConcurrentHashMap<>(); + + public Object get(String visualizationCode, String module) { + if (StringUtils.isNotBlank(module)) { + module = Constants.Modules.COMMON.toString(); + } + if (requestModuleResponseMap.get(visualizationCode) != null + && requestModuleResponseMap.get(visualizationCode).get(module) != null) { + return requestModuleResponseMap.get(visualizationCode).get(module); + } + return null; + } + + public Boolean put(String visualizationCode, String module, Object object) { + if (StringUtils.isNotBlank(module)) { + module = Constants.Modules.COMMON.toString(); + } + if (requestModuleResponseMap.containsKey(visualizationCode)) { + Map<String, Object> innerMap = requestModuleResponseMap.get(visualizationCode); + innerMap.put(module, object); + } else { + Map<String, Object> innerMap = new HashMap<>(); + innerMap.put(module, object); + requestModuleResponseMap.put(visualizationCode, innerMap); + } + return Boolean.TRUE; + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000000000000000000000000000000000000..cc08681168ecb19a5ace6c1a641943cd19f5b1f3 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,77 @@ +server.port=8091 + +spring.datasource.url=jdbc:mysql://localhost:3306/database +spring.datasource.username=root +spring.datasource.password=root +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration + +#----------------------------- ELASTIC SEARCH CONFIGURATIONS ------------------------------# +services.esindexer.quadnary.host=http://10.4.0.5/ +services.esindexer.quadnary.host.name=10.4.0.5 +services.esindexer.quadnary.host.port=9200 +services.esindexer.quadnary.username=elastic +services.esindexer.quadnary.password=Elastic123 + +# Kronos-RAIN Production +services.esindexer.primary.host=https://elastic.pulz.app/ +services.esindexer.primary.host.name=elastic.pulz.app +services.esindexer.primary.host.port=9200 +services.esindexer.primary.username=elastic +services.esindexer.primary.password=PuAz@itAqwsaR34bYu + +#services.esindexer.secondary.host=http://172.17.18.193:9200/ +#services.esindexer.secondary.host.name=172.17.18.193 +#services.esindexer.secondary.host.port=9200 +#services.esindexer.secondary.username=elastprod +#services.esindexer.secondary.password=El@st1cp + +# Anuvad +services.esindexer.secondary.host=http://es.rain.idc.tarento.com/ +services.esindexer.secondary.host.name=es.rain.idc.tarento.com +services.esindexer.secondary.host.port=9200 +services.esindexer.secondary.username=elastic +services.esindexer.secondary.password=Elastic123 + + + +services.esindexer.ternary.host=http://elasticsearch.idc.tarento.com/ +services.esindexer.ternary.host.name=elasticsearch.idc.tarento.com +services.esindexer.ternary.host.port=9200 +services.esindexer.ternary.username=elastprod +services.esindexer.ternary.password=El@st1cp +es.index.name=_transaction +es.document.type=transaction +es.target.index.name=store_target +es.target.document.type=storeTarget +config.schema.paths=classpath*:schema/*.json + +#----------------------------- MISCELLANEOUS CONFIGURATION ----------------------------------# +top.performance.record.count=5 + +es.services.esindexer.host.search=/_search + +es.username=egov-admin +es.password=TUSYns9mEcRPy77n + +#----------------------------- AMAZON S3 CREDENTIALS -------------------------------------------------# +aws.access.key.id = +aws.access.key.secret = +aws.region = ap-south-1 +aws.s3.audio.bucket = dss-project-bucket + +#---------------------------- MDMS ---------------------------------------------------------------------# +egov.mdms-service.target.url=https://mseva-uat.lgpunjab.gov.in/egov-mdms-service/v1/_search +egov.mdms-service.request ={"RequestInfo":{"authToken":""},"MdmsCriteria":{"tenantId":"pb","moduleDetails":[{"moduleName":"tenant","masterDetails":[{"name":"tenants"}]}]}} + +# KAFKA SERVER CONFIGURATIONS +spring.kafka.bootstrap.servers=localhost:9092 + +spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer +spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer + +# KAFKA PRODUCER CONFIGURATIONS +kafka.producer.config.retries_config=0 +kafka.producer.config.batch_size_config=100000 +kafka.producer.config.linger_ms_config=100 +kafka.producer.config.buffer_memory_config=66554432 diff --git a/src/main/resources/application.properties.j2 b/src/main/resources/application.properties.j2 new file mode 100644 index 0000000000000000000000000000000000000000..8c8f1dcdc8fac678bb55945d53749ace1d6c149d --- /dev/null +++ b/src/main/resources/application.properties.j2 @@ -0,0 +1,47 @@ +server.port=8091 + +spring.datasource.url=jdbc:mysql://localhost:3306/database +spring.datasource.username=root +spring.datasource.password=tiger +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration + +#----------------------------- ELASTIC SEARCH CONFIGURATIONS ------------------------------# +services.esindexer.primary.host=http://172.17.18.193:9200/ +services.esindexer.primary.host.name=172.17.18.193 +services.esindexer.primary.host.port=9200 +services.esindexer.secondary.host=http://52.88.54.220:9200/ +services.esindexer.secondary.host.name=52.88.54.220 +services.esindexer.secondary.host.port=9200 +services.esindexer.secondary.username=username +services.esindexer.secondary.password=password + +services.esindexer.ternary.host=http://elasticsearch.idc.tarento.com/ +services.esindexer.ternary.host.name=elasticsearch.idc.tarento.com +services.esindexer.ternary.host.port=9200 +services.esindexer.username=elastic +services.esindexer.password=Ela12#$ +es.index.name=_transaction +es.document.type=transaction +es.target.index.name=store_target +es.target.document.type=storeTarget +config.schema.paths=classpath*:schema/*.json + +#----------------------------- MISCELLANEOUS CONFIGURATION ----------------------------------# +top.performance.record.count=5 + +es.services.esindexer.host.search=/_search + +es.username=egov-admin +es.password=TUSYns9mEcRPy77n + +#----------------------------- AMAZON S3 CREDENTIALS -------------------------------------------------# +aws.access.key.id = +aws.access.key.secret = +aws.region = ap-south-1 +aws.s3.audio.bucket = dss-project-bucket + +#---------------------------- MDMS ---------------------------------------------------------------------# +egov.mdms-service.target.url=https://mseva-uat.lgpunjab.gov.in/egov-mdms-service/v1/_search +#https://egov-micro-dev.egovernments.org/egov-mdms-service/v1/_search +egov.mdms-service.request ={"RequestInfo":{"authToken":""},"MdmsCriteria":{"tenantId":"pb","moduleDetails":[{"moduleName":"tenant","masterDetails":[{"name":"tenants"}]}]}} diff --git a/src/main/resources/schema/SMF_ChartApiConfig.json b/src/main/resources/schema/SMF_ChartApiConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..e563d4471eeb398389ff222342fbbc689d00e5b7 --- /dev/null +++ b/src/main/resources/schema/SMF_ChartApiConfig.json @@ -0,0 +1,31 @@ +{ + "applicationStatusWise": { + "chartName": "Applications Status Wise", + "queries": [ + { + "module": "COMMON", + "requestQueryMap": "{\r\n \"ts\" : \"timestamp\"}", + "dateRefField": "timestamp", + "indexName": "fs-forms-data", + "esInstance": "QUADNARY", + "aggrQuery": "{\"aggs\":{\"Application Status\":{\"terms\":{\"field\":\"status.keyword\",\"size\":10}}}}" + } + ], + "chartType": "bar", + "resultType": "value", + "valueType": "number", + "isDecimal": true, + "action": "", + "filter": "", + "drillChart": "none", + "documentType": "_doc", + "aggregationPaths": [ + "Application Status" + ], + "isCumulative": false, + "interval": "day", + "alwaysView": "", + "insight": {}, + "_comment": " " + } +} \ No newline at end of file diff --git a/src/main/resources/schema/SMF_MasterDashboardConfig.json b/src/main/resources/schema/SMF_MasterDashboardConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..460ac8c6eadb66236e9130f14bbcce29363f89a0 --- /dev/null +++ b/src/main/resources/schema/SMF_MasterDashboardConfig.json @@ -0,0 +1,54 @@ +{ + "_comment": "Master dashboard which holds all the possible visualisation. Note new dashboards/visualisations can be added", + "dashboards": [ + { + "name": "SMF Dashboards", + "id": "home", + "isActive": "", + "style": "tabbed", + "filters": [ + { + "name": "Status", + "id": 1, + "key": "status", + "values": [ + "ALL", + "New", + "Approved", + "Rejected" + ] + } + ], + "widgetCharts": [], + "visualizations": [ + { + "row": 1, + "name": "Application Status Count", + "vizArray": [ + { + "id": 111, + "name": "Overview", + "dimensions": { + "height": 250, + "width": 4 + }, + "vizType": "bar", + "noUnit": true, + "isCollapsible": false, + "charts": [ + { + "id": "applicationStatusWise", + "name": "Application Status Count", + "code": "", + "chartType": "bar", + "filter": "", + "headers": [] + } + ] + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/resources/schema/SMF_ReportApiConfig.json b/src/main/resources/schema/SMF_ReportApiConfig.json new file mode 100644 index 0000000000000000000000000000000000000000..e563d4471eeb398389ff222342fbbc689d00e5b7 --- /dev/null +++ b/src/main/resources/schema/SMF_ReportApiConfig.json @@ -0,0 +1,31 @@ +{ + "applicationStatusWise": { + "chartName": "Applications Status Wise", + "queries": [ + { + "module": "COMMON", + "requestQueryMap": "{\r\n \"ts\" : \"timestamp\"}", + "dateRefField": "timestamp", + "indexName": "fs-forms-data", + "esInstance": "QUADNARY", + "aggrQuery": "{\"aggs\":{\"Application Status\":{\"terms\":{\"field\":\"status.keyword\",\"size\":10}}}}" + } + ], + "chartType": "bar", + "resultType": "value", + "valueType": "number", + "isDecimal": true, + "action": "", + "filter": "", + "drillChart": "none", + "documentType": "_doc", + "aggregationPaths": [ + "Application Status" + ], + "isCumulative": false, + "interval": "day", + "alwaysView": "", + "insight": {}, + "_comment": " " + } +} \ No newline at end of file diff --git a/src/main/resources/schema/SMF_RoleDashboardMappingsConf.json b/src/main/resources/schema/SMF_RoleDashboardMappingsConf.json new file mode 100644 index 0000000000000000000000000000000000000000..ede4d93478a8cda53b5e7a46322758854d61699e --- /dev/null +++ b/src/main/resources/schema/SMF_RoleDashboardMappingsConf.json @@ -0,0 +1,18 @@ +{ + "_comment": "Holds mapping for each role with and its associated dashboards", + "roles" : [ + { + "_comment":"This is the SMF Admin role who sees mostly all the dashboards", + "roleId": 2068, + "roleName" : "SMF Admin", + "isSuper" : "", + "orgId": "", + "dashboards": [ + { + "name": "SMF Dashboards", + "id": "home" + } + ] + } + ] +} \ No newline at end of file