Search in sources :

Example 1 with QueryModel

use of com.cognizant.devops.platformreports.assessment.datamodel.QueryModel in project Insights by CognizantOneDevOps.

the class InsightsKPIProcessor method getQueryWithScheduleDates.

protected void getQueryWithScheduleDates(long nextRuntime, WorkflowTaskEnum.WorkflowSchedule schedule, String neo4jQuery, long oneTimeEndDate, List<QueryModel> queryModelList) {
    long startTime = System.nanoTime();
    QueryModel queryModel = new QueryModel();
    Map<String, Long> dateReplaceMap = new HashMap<>();
    long fromDate = InsightsUtils.getStartFromTime(nextRuntime, schedule.name()) - 1;
    long toDate;
    if (schedule.name().equalsIgnoreCase(WorkflowTaskEnum.WorkflowSchedule.ONETIME.name())) {
        toDate = oneTimeEndDate;
    } else {
        // minus 2 sec to get time 23:59:59
        toDate = nextRuntime - 2;
    }
    dateReplaceMap.put(ReportEngineUtils.START_TIME_FIELD, fromDate);
    dateReplaceMap.put(ReportEngineUtils.END_TIME_FIELD, toDate);
    StringSubstitutor sub = new StringSubstitutor(dateReplaceMap, "{", "}");
    queryModel.setRecordDate(fromDate);
    queryModel.setQuery(sub.replace(neo4jQuery));
    queryModelList.add(queryModel);
    long processingTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
    log.debug("Type=TaskExecution  executionId={} workflowId={} ConfigId={} WorkflowType={} KpiId={} Category={} ProcessingTime={} message={}", kpiConfigDTO.getExecutionId(), kpiConfigDTO.getWorkflowId(), kpiConfigDTO.getReportId(), "-", kpiConfigDTO.getKpiId(), kpiConfigDTO.getCategory(), processingTime, "queryStartTime: " + fromDate + "queryEndTime: " + toDate + "schedule: " + kpiConfigDTO.getSchedule());
}
Also used : HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor) QueryModel(com.cognizant.devops.platformreports.assessment.datamodel.QueryModel)

Example 2 with QueryModel

use of com.cognizant.devops.platformreports.assessment.datamodel.QueryModel in project Insights by CognizantOneDevOps.

the class InsightsKPIProcessor method processKPI.

protected int processKPI(InsightsKPIConfigDTO kpiDefinition) {
    try {
        ReportDataHandler kPIQueryDataHandler = ReportDataHandlerFactory.getDataSource(kpiDefinition.getDatasource());
        String graphQuery = kpiDefinition.getdBQuery();
        List<JsonObject> listOfResultJson = new ArrayList<>(0);
        List<QueryModel> queryModelList = new ArrayList<>();
        long startDate;
        long endDate;
        int noOfDays;
        if (kpiDefinition.getSchedule().equals(WorkflowTaskEnum.WorkflowSchedule.ONETIME)) {
            startDate = kpiDefinition.getOneTimeReportStartTime();
            endDate = kpiDefinition.getOneTimeReportEndDate();
            noOfDays = InsightsUtils.getDurationBetweenDates(startDate, endDate).intValue();
        } else {
            startDate = kpiDefinition.getNextRunTime();
            // kpiDefinition.getOneTimeReportEndDate(); /// end date to nextRunTime
            endDate = startDate;
            noOfDays = kpiDefinition.getSchedule().getValue();
        }
        if (kpiDefinition.getCategory().equals(ContentCategory.THRESHOLD.name()) || kpiDefinition.getCategory().equals(ContentCategory.THRESHOLD_RANGE.name()) || kpiDefinition.getCategory().equals(ContentCategory.MINMAX.name()) || kpiDefinition.getCategory().equals(ContentCategory.TREND.name())) {
            getQueryBySchedule(startDate, kpiDefinition.getSchedule(), noOfDays, graphQuery, endDate, queryModelList);
        } else {
            getQueryWithScheduleDates(startDate, kpiDefinition.getSchedule(), graphQuery, endDate, queryModelList);
        }
        log.debug("Worlflow Detail ====  In processKPI for kpiId {} category {} queryModelList {}", kpiDefinition.getKpiId(), kpiDefinition.getCategory(), queryModelList.size());
        long startTime = System.nanoTime();
        for (QueryModel model : queryModelList) {
            if (kpiDefinition.getCategory().equalsIgnoreCase(ReportEngineUtils.PREDICTION)) {
                List<JsonObject> result = kPIQueryDataHandler.fetchKPIData(model.getQuery(), kpiDefinition, model);
                listOfResultJson.addAll(AutoMLPrediction.getPrediction(result, result.get(0).get("columnProperty").getAsJsonArray(), kpiDefinition.getUsecaseName()));
            } else {
                listOfResultJson.addAll(kPIQueryDataHandler.fetchKPIData(model.getQuery(), kpiDefinition, model));
            }
        }
        long processingTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
        log.debug("Type=TaskExecution  executionId={} workflowId={} ConfigId={} WorkflowType={} KpiId={} Category={} ProcessingTime={} message={}", kpiDefinition.getExecutionId(), kpiDefinition.getWorkflowId(), kpiDefinition.getReportId(), "-", kpiDefinition.getKpiId(), kpiDefinition.getCategory(), processingTime, " usecasename: " + kpiDefinition.getUsecaseName() + " schedule: " + kpiDefinition.getSchedule());
        ReportDataHandler kPIResultDataHandler = ReportDataHandlerFactory.getDataSource(ApplicationConfigProvider.getInstance().getAssessmentReport().getOutputDatasource());
        log.debug("Worlflow Detail ====  Number of record fetch against kpi Id {} is {}", kpiDefinition.getKpiId(), listOfResultJson.size());
        if (!listOfResultJson.isEmpty()) {
            kPIResultDataHandler.saveData(listOfResultJson);
        } else {
            log.error("Worlflow Detail ====  No result to store in database(neo4j or ES) for job : {} ", kpiConfigDTO.getKpiId());
            return ReportEngineEnum.StatusCode.NO_DATA.getValue();
        }
    } catch (Exception e) {
        log.error("Worlflow Detail ==== Some calculation job failed for kpiID -{} ", kpiDefinition.getKpiId(), e);
        log.error("Type=TaskExecution  executionId={} workflowId={} ConfigId={} WorkflowType={} KpiId={} Category={} ProcessingTime={} message={}", kpiDefinition.getExecutionId(), kpiDefinition.getWorkflowId(), kpiDefinition.getReportId(), "-", kpiDefinition.getKpiId(), kpiDefinition.getCategory(), 0, "usecasename: " + kpiDefinition.getUsecaseName() + "schedule: " + kpiDefinition.getSchedule() + e.getMessage());
        throw new InsightsJobFailedException("Something went wrong with KPI query execution " + e.getMessage());
    }
    return ReportEngineEnum.StatusCode.SUCCESS.getValue();
}
Also used : InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ReportDataHandler(com.cognizant.devops.platformreports.assessment.dal.ReportDataHandler) QueryModel(com.cognizant.devops.platformreports.assessment.datamodel.QueryModel) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException)

Example 3 with QueryModel

use of com.cognizant.devops.platformreports.assessment.datamodel.QueryModel in project Insights by CognizantOneDevOps.

the class InsightsKPIProcessor method getQueryBySchedule.

private void getQueryBySchedule(long nextRunTime, WorkflowTaskEnum.WorkflowSchedule schedule, int days, String query, long endTime, List<QueryModel> queryModelList) {
    long startTime = System.nanoTime();
    Map<String, Long> dateReplaceMap = new HashMap<>();
    List<String> neo4jQueries = new ArrayList<>();
    if (days <= 31) {
        int noOfQueries = days;
        long startDate = InsightsUtils.getStartFromTime(nextRunTime, schedule.name()) - 1;
        long endDate = InsightsUtils.addDaysInGivenTime(startDate, 1) - 1;
        if (schedule.name().equalsIgnoreCase("Monthly")) {
            noOfQueries = InsightsUtils.getMonthDays(startDate);
        }
        for (int i = 0; i < noOfQueries; i++) {
            QueryModel qmodel = new QueryModel();
            dateReplaceMap.put(ReportEngineUtils.START_TIME_FIELD, startDate);
            dateReplaceMap.put(ReportEngineUtils.END_TIME_FIELD, endDate);
            StringSubstitutor sub = new StringSubstitutor(dateReplaceMap, "{", "}");
            qmodel.setRecordDate(startDate);
            qmodel.setQuery(sub.replace(query));
            queryModelList.add(qmodel);
            startDate = InsightsUtils.addDaysInGivenTime(startDate, 1);
            endDate = InsightsUtils.addDaysInGivenTime(endDate, 1);
            long processingTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
            log.debug("Type=TaskExecution  executionId={} workflowId={} ConfigId={} WorkflowType={} KpiId={} Category={} ProcessingTime={} message={}", kpiConfigDTO.getExecutionId(), kpiConfigDTO.getWorkflowId(), kpiConfigDTO.getReportId(), "-", kpiConfigDTO.getKpiId(), kpiConfigDTO.getCategory(), processingTime, "queryStartTime: " + startDate + "queryEndTime: " + endDate + "schedule: " + kpiConfigDTO.getSchedule());
        }
    } else {
        int weeks = days / 7;
        long startOfTheDayInWeek = InsightsUtils.getStartFromTime(nextRunTime, schedule.name()) - 1;
        long endDayOfTheWeek = InsightsUtils.addDaysInGivenTime(startOfTheDayInWeek, 7) - 1;
        for (int i = 0; i < weeks; i++) {
            QueryModel qmodel = new QueryModel();
            dateReplaceMap.put(ReportEngineUtils.START_TIME_FIELD, startOfTheDayInWeek);
            dateReplaceMap.put(ReportEngineUtils.END_TIME_FIELD, endDayOfTheWeek);
            StringSubstitutor sub = new StringSubstitutor(dateReplaceMap, "{", "}");
            neo4jQueries.add(sub.replace(query));
            qmodel.setRecordDate(startOfTheDayInWeek);
            qmodel.setQuery(sub.replace(query));
            queryModelList.add(qmodel);
            startOfTheDayInWeek = InsightsUtils.addDaysInGivenTime(startOfTheDayInWeek, 7);
            endDayOfTheWeek = InsightsUtils.addDaysInGivenTime(endDayOfTheWeek, 7);
            if (endDayOfTheWeek > endTime) {
                endDayOfTheWeek = endTime;
            }
            log.debug("Type=TaskExecution  executionId={} workflowId={} ConfigId={} WorkflowType={} KpiId={} Category={} ProcessingTime={} message={}", kpiConfigDTO.getExecutionId(), kpiConfigDTO.getWorkflowId(), kpiConfigDTO.getReportId(), "-", kpiConfigDTO.getKpiId(), kpiConfigDTO.getCategory(), 0, "queryStartTime: " + startOfTheDayInWeek + "queryEndTime: " + endDayOfTheWeek + "schedule: " + kpiConfigDTO.getSchedule());
        }
    }
}
Also used : HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ArrayList(java.util.ArrayList) QueryModel(com.cognizant.devops.platformreports.assessment.datamodel.QueryModel)

Aggregations

QueryModel (com.cognizant.devops.platformreports.assessment.datamodel.QueryModel)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 StringSubstitutor (org.apache.commons.text.StringSubstitutor)2 ReportDataHandler (com.cognizant.devops.platformreports.assessment.dal.ReportDataHandler)1 InsightsJobFailedException (com.cognizant.devops.platformreports.exception.InsightsJobFailedException)1 JsonObject (com.google.gson.JsonObject)1