Search in sources :

Example 1 with Timer

use of org.hisp.dhis.util.Timer in project dhis2-core by dhis2.

the class DefaultAnalyticsService method getAggregatedValueMap.

/**
     * Generates a mapping between a dimension key and the aggregated value. The
     * dimension key is a concatenation of the identifiers of the dimension items
     * separated by "-".
     *
     * @param params the {@link DataQueryParams}.
     * @param tableName the table name to use for the query.
     * @param queryGroupers the list of additional query groupers to use for
     *        query planning, use empty list for none.
     * @return a mapping between a dimension key and aggregated values.
     */
private Map<String, Object> getAggregatedValueMap(DataQueryParams params, String tableName, List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers) {
    queryPlanner.validateMaintenanceMode();
    int optimalQueries = MathUtils.getWithin(getProcessNo(), 1, MAX_QUERIES);
    int maxLimit = params.isIgnoreLimit() ? 0 : (Integer) systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_MAX_LIMIT);
    Timer timer = new Timer().start().disablePrint();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(optimalQueries).withTableName(tableName).withQueryGroupers(queryGroupers).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    timer.getSplitTime("Planned analytics query, got: " + queryGroups.getLargestGroupSize() + " for optimal: " + optimalQueries);
    Map<String, Object> map = new HashMap<>();
    for (List<DataQueryParams> queries : queryGroups.getSequentialQueries()) {
        List<Future<Map<String, Object>>> futures = new ArrayList<>();
        for (DataQueryParams query : queries) {
            futures.add(analyticsManager.getAggregatedDataValues(query, maxLimit));
        }
        for (Future<Map<String, Object>> future : futures) {
            try {
                Map<String, Object> taskValues = future.get();
                if (taskValues != null) {
                    map.putAll(taskValues);
                }
            } catch (Exception ex) {
                log.error(DebugUtils.getStackTrace(ex));
                log.error(DebugUtils.getStackTrace(ex.getCause()));
                throw new RuntimeException("Error during execution of aggregation query task", ex);
            }
        }
    }
    timer.getTime("Got analytics values");
    return map;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) ProcessingHint(org.hisp.dhis.analytics.ProcessingHint) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Timer(org.hisp.dhis.util.Timer) Future(java.util.concurrent.Future) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) HashMap(java.util.HashMap) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 2 with Timer

use of org.hisp.dhis.util.Timer in project dhis2-core by dhis2.

the class AuditRepositoryTest method testAuditInsert200k.

@Test
@Disabled
void testAuditInsert200k() {
    List<Audit> audits = new ArrayList<>();
    IntStream.rangeClosed(1, 200_000).forEach(n -> {
        String uid = CodeGenerator.generateUid();
        String code = CodeGenerator.generateUid();
        Audit audit = Audit.builder().auditType(AuditType.CREATE).auditScope(AuditScope.AGGREGATE).createdAt(LocalDateTime.of(2000, 1, 1, 0, 0)).createdBy("test-user").klass(DataElement.class.getName()).uid(uid).code(code).data("{}").build();
        audits.add(audit);
    });
    Timer timer = new Timer().start();
    audits.forEach(audit -> auditRepository.save(audit));
    System.err.println("Single Insert: " + timer.getTimeInS() + "s");
    assertEquals(200_000, auditRepository.count(AuditQuery.builder().build()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Timer(org.hisp.dhis.util.Timer) ArrayList(java.util.ArrayList) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with Timer

use of org.hisp.dhis.util.Timer in project dhis2-core by dhis2.

the class DefaultEnrollmentAnalyticsService method addEventData.

@Override
protected long addEventData(Grid grid, EventQueryParams params) {
    Timer timer = new Timer().start().disablePrint();
    params = queryPlanner.planEnrollmentQuery(params);
    timer.getSplitTime("Planned event query, got partitions: " + params.getPartitions());
    long count = 0;
    if (params.isPaging()) {
        count += enrollmentAnalyticsManager.getEnrollmentCount(params);
    }
    enrollmentAnalyticsManager.getEnrollments(params, grid, queryValidator.getMaxLimit());
    timer.getTime("Got enrollments " + grid.getHeight());
    return count;
}
Also used : Timer(org.hisp.dhis.util.Timer)

Example 4 with Timer

use of org.hisp.dhis.util.Timer in project dhis2-core by dhis2.

the class DataHandler method getAggregatedValueMap.

/**
 * Generates a mapping between a dimension key and the aggregated value. The
 * dimension key is a concatenation of the identifiers of the dimension
 * items separated by "-".
 *
 * @param params the {@link DataQueryParams}.
 * @param tableType the {@link AnalyticsTableType}.
 * @param queryGroupers the list of additional query groupers to use for
 *        query planning, use empty list for none.
 * @return a mapping between a dimension key and aggregated values.
 */
private Map<String, Object> getAggregatedValueMap(DataQueryParams params, AnalyticsTableType tableType, List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers) {
    queryValidator.validateMaintenanceMode();
    int optimalQueries = getWithin(getProcessNo(), 1, MAX_QUERIES);
    int maxLimit = params.isIgnoreLimit() ? 0 : systemSettingManager.getIntSetting(ANALYTICS_MAX_LIMIT);
    Timer timer = new Timer().start().disablePrint();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(optimalQueries).withTableType(tableType).withQueryGroupers(queryGroupers).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    timer.getSplitTime("Planned analytics query, got: " + queryGroups.getLargestGroupSize() + " for optimal: " + optimalQueries);
    Map<String, Object> map = new HashMap<>();
    for (List<DataQueryParams> queries : queryGroups.getSequentialQueries()) {
        executeQueries(tableType, maxLimit, map, queries);
    }
    timer.getTime("Got analytics values");
    return map;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) EventQueryParams.fromDataQueryParams(org.hisp.dhis.analytics.event.EventQueryParams.fromDataQueryParams) Timer(org.hisp.dhis.util.Timer) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) AnalyticsUtils.getRoundedValueObject(org.hisp.dhis.analytics.util.AnalyticsUtils.getRoundedValueObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 5 with Timer

use of org.hisp.dhis.util.Timer in project dhis2-core by dhis2.

the class DefaultEventAnalyticsService method getAggregatedEventDataGrid.

private Grid getAggregatedEventDataGrid(EventQueryParams params) {
    params.removeProgramIndicatorItems();
    Grid grid = new ListGrid();
    int maxLimit = queryValidator.getMaxLimit();
    if (!params.isSkipData() || params.analyzeOnly()) {
        if (params.isCollapseDataDimensions() || params.isAggregateData()) {
            grid.addHeader(new GridHeader(DimensionalObject.DATA_COLLAPSED_DIM_ID, DataQueryParams.DISPLAY_NAME_DATA_X, ValueType.TEXT, false, true));
        } else {
            for (QueryItem item : params.getItems()) {
                grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getDisplayProperty(params.getDisplayProperty()), item.getValueType(), false, true, item.getOptionSet(), item.getLegendSet()));
            }
        }
        for (DimensionalObject dimension : params.getDimensions()) {
            grid.addHeader(new GridHeader(dimension.getDimension(), dimension.getDisplayProperty(params.getDisplayProperty()), TEXT, false, true));
        }
        grid.addHeader(new GridHeader(VALUE_ID, VALUE_HEADER_NAME, NUMBER, false, false));
        if (params.isIncludeNumDen()) {
            grid.addHeader(new GridHeader(NUMERATOR_ID, NUMERATOR_HEADER_NAME, NUMBER, false, false)).addHeader(new GridHeader(DENOMINATOR_ID, DENOMINATOR_HEADER_NAME, NUMBER, false, false)).addHeader(new GridHeader(FACTOR_ID, FACTOR_HEADER_NAME, NUMBER, false, false)).addHeader(new GridHeader(MULTIPLIER_ID, MULTIPLIER_HEADER_NAME, NUMBER, false, false)).addHeader(new GridHeader(DIVISOR_ID, DIVISOR_HEADER_NAME, NUMBER, false, false));
        }
        // -----------------------------------------------------------------
        // Data
        // -----------------------------------------------------------------
        Timer timer = new Timer().start().disablePrint();
        List<EventQueryParams> queries = queryPlanner.planAggregateQuery(params);
        timer.getSplitTime("Planned event query, got partitions: " + params.getPartitions());
        for (EventQueryParams query : queries) {
            if (query.hasEnrollmentProgramIndicatorDimension()) {
                enrollmentAnalyticsManager.getAggregatedEventData(query, grid, maxLimit);
            } else {
                eventAnalyticsManager.getAggregatedEventData(query, grid, maxLimit);
            }
        }
        timer.getTime("Got aggregated events");
        if (maxLimit > 0 && grid.getHeight() > maxLimit) {
            throwIllegalQueryEx(ErrorCode.E7128, maxLimit);
        }
        if (params.hasSortOrder() && grid.getHeight() > 0) {
            grid.sortGrid(1, params.getSortOrderAsInt());
        }
        if (params.hasLimit() && grid.getHeight() > params.getLimit()) {
            grid.limitGrid(params.getLimit());
        }
    }
    maybeApplyIdScheme(params, grid);
    // ---------------------------------------------------------------------
    // Meta-data
    // ---------------------------------------------------------------------
    addMetadata(params, grid);
    return grid;
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) QueryItem(org.hisp.dhis.common.QueryItem) Timer(org.hisp.dhis.util.Timer) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Aggregations

Timer (org.hisp.dhis.util.Timer)7 ArrayList (java.util.ArrayList)3 DimensionalObject (org.hisp.dhis.common.DimensionalObject)3 HashMap (java.util.HashMap)2 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)2 DataQueryGroups (org.hisp.dhis.analytics.DataQueryGroups)2 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)2 QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)2 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)2 Disabled (org.junit.jupiter.api.Disabled)2 Test (org.junit.jupiter.api.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Future (java.util.concurrent.Future)1 ArrayListValuedHashMap (org.apache.commons.collections4.multimap.ArrayListValuedHashMap)1 ProcessingHint (org.hisp.dhis.analytics.ProcessingHint)1 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)1