Search in sources :

Example 56 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class DataElement method getLatestOpenFuturePeriod.

/**
     * Returns the latest period which is open for data input. Returns null if
     * data element is not associated with any data sets.
     *
     * @return the latest period which is open for data input.
     */
public Period getLatestOpenFuturePeriod() {
    int periods = getOpenFuturePeriods();
    PeriodType periodType = getPeriodType();
    if (periodType != null) {
        Period period = periodType.createPeriod();
        // Rewind one as 0 open periods implies current period is locked
        period = periodType.getPreviousPeriod(period);
        return periodType.getNextPeriod(period, periods);
    }
    return null;
}
Also used : YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) Period(org.hisp.dhis.period.Period)

Example 57 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class DefaultAnalyticsService method addReportingRates.

/**
     * Adds reporting rates to the given grid based on the given data query
     * parameters and reporting rate metric.
     *
     * @param params the {@link DataQueryParams}.
     * @param grid the grid.
     * @param metric the reporting rate metric.
     */
private void addReportingRates(DataQueryParams params, Grid grid, ReportingRateMetric metric) {
    if (!params.getReportingRates().isEmpty() && !params.isSkipData()) {
        if (!COMPLETENESS_DIMENSION_TYPES.containsAll(params.getDimensionTypes())) {
            return;
        }
        DataQueryParams targetParams = DataQueryParams.newBuilder(params).withSkipPartitioning(true).withTimely(false).withRestrictByOrgUnitOpeningClosedDate(true).withRestrictByCategoryOptionStartEndDate(true).withAggregationType(AggregationType.SUM).build();
        Map<String, Double> targetMap = getAggregatedCompletenessTargetMap(targetParams);
        Map<String, Double> dataMap = metric != EXPECTED_REPORTS ? getAggregatedCompletenessValueMap(params) : new HashMap<>();
        Integer periodIndex = params.getPeriodDimensionIndex();
        Integer dataSetIndex = DataQueryParams.DX_INDEX;
        Map<String, PeriodType> dsPtMap = params.getDataSetPeriodTypeMap();
        PeriodType filterPeriodType = params.getFilterPeriodType();
        for (Map.Entry<String, Double> entry : targetMap.entrySet()) {
            List<String> dataRow = Lists.newArrayList(entry.getKey().split(DIMENSION_SEP));
            Double target = entry.getValue();
            Double actual = dataMap.get(entry.getKey());
            if (target != null && (actual != null || metric == EXPECTED_REPORTS)) {
                // ---------------------------------------------------------
                // Multiply target value by number of periods in time span
                // ---------------------------------------------------------
                PeriodType queryPt = filterPeriodType != null ? filterPeriodType : getPeriodTypeFromIsoString(dataRow.get(periodIndex));
                PeriodType dataSetPt = dsPtMap.get(dataRow.get(dataSetIndex));
                target = target * queryPt.getPeriodSpan(dataSetPt);
                // ---------------------------------------------------------
                // Calculate reporting rate and replace data set with rate
                // ---------------------------------------------------------
                Double value = 0d;
                if (EXPECTED_REPORTS == metric) {
                    value = target;
                } else if (ACTUAL_REPORTS == metric || ACTUAL_REPORTS_ON_TIME == metric) {
                    value = actual;
                } else if (// REPORTING_RATE or REPORTING_RATE_ON_TIME
                !MathUtils.isZero(target)) {
                    value = (actual * PERCENT) / target;
                }
                String reportingRate = DimensionalObjectUtils.getDimensionItem(dataRow.get(DX_INDEX), metric);
                dataRow.set(DX_INDEX, reportingRate);
                grid.addRow().addValues(dataRow.toArray()).addValue(params.isSkipRounding() ? value : MathUtils.getRounded(value));
                if (params.isIncludeNumDen()) {
                    grid.addValue(actual).addValue(target).addValue(PERCENT);
                }
            }
        }
    }
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) PeriodType(org.hisp.dhis.period.PeriodType) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) 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)

Example 58 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class PeriodResourceTable method getPopulateTempTableContent.

@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
    Calendar calendar = PeriodType.getCalendar();
    List<Object[]> batchArgs = new ArrayList<>();
    Set<String> uniqueIsoDates = new HashSet<>();
    for (Period period : objects) {
        if (period != null && period.isValid()) {
            final PeriodType rowType = period.getPeriodType();
            final String isoDate = period.getIsoDate();
            if (!uniqueIsoDates.add(isoDate)) {
                log.warn("Duplicate ISO date for period, ignoring: " + period + ", ISO date: " + isoDate);
                continue;
            }
            List<Object> values = new ArrayList<>();
            values.add(period.getId());
            values.add(isoDate);
            values.add(period.getDaysInPeriod());
            values.add(period.getStartDate());
            values.add(period.getEndDate());
            for (PeriodType periodType : PeriodType.PERIOD_TYPES) {
                if (rowType.getFrequencyOrder() < periodType.getFrequencyOrder() || rowType.equals(periodType)) {
                    Period targetPeriod = IdentifiableObjectUtils.getPeriodByPeriodType(period, periodType, calendar);
                    values.add(IdentifiableObjectUtils.getLocalPeriodIdentifier(targetPeriod, calendar));
                } else {
                    values.add(null);
                }
            }
            batchArgs.add(values.toArray());
        }
    }
    return Optional.of(batchArgs);
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Calendar(org.hisp.dhis.calendar.Calendar) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) HashSet(java.util.HashSet)

Example 59 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class PeriodResourceTable method getCreateTempTableStatement.

@Override
public String getCreateTempTableStatement() {
    String sql = "CREATE TABLE " + getTempTableName() + " (periodid INTEGER NOT NULL PRIMARY KEY, iso VARCHAR(15) NOT NULL, daysno INTEGER NOT NULL, startdate DATE NOT NULL, enddate DATE NOT NULL";
    for (PeriodType periodType : PeriodType.PERIOD_TYPES) {
        sql += ", " + columnQuote + periodType.getName().toLowerCase() + columnQuote + " VARCHAR(15)";
    }
    sql += ")";
    return sql;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType)

Example 60 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class DataElementResourceTable method getPopulateTempTableContent.

@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
    List<Object[]> batchArgs = new ArrayList<>();
    for (DataElement dataElement : objects) {
        List<Object> values = new ArrayList<>();
        final DataSet dataSet = dataElement.getApprovalDataSet();
        final PeriodType periodType = dataElement.getPeriodType();
        // -----------------------------------------------------------------
        // Use highest approval level if data set does not require approval,
        // or null if approval is required.
        // -----------------------------------------------------------------
        values.add(dataElement.getId());
        values.add(dataElement.getUid());
        values.add(dataElement.getName());
        values.add(dataSet != null ? dataSet.getId() : null);
        values.add(dataSet != null ? dataSet.getUid() : null);
        values.add(dataSet != null ? dataSet.getName() : null);
        values.add(dataSet != null && dataSet.isApproveData() ? null : APPROVAL_LEVEL_HIGHEST);
        values.add(dataSet != null && dataSet.isApproveData() ? dataSet.getWorkflow().getId() : null);
        values.add(periodType != null ? periodType.getId() : null);
        values.add(periodType != null ? periodType.getName() : null);
        batchArgs.add(values.toArray());
    }
    return Optional.of(batchArgs);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) PeriodType(org.hisp.dhis.period.PeriodType) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList)

Aggregations

PeriodType (org.hisp.dhis.period.PeriodType)67 Period (org.hisp.dhis.period.Period)21 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)13 ArrayList (java.util.ArrayList)12 DataSet (org.hisp.dhis.dataset.DataSet)12 DataElement (org.hisp.dhis.dataelement.DataElement)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)9 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)7 UniqueArrayList (org.hisp.dhis.commons.collection.UniqueArrayList)6 YearlyPeriodType (org.hisp.dhis.period.YearlyPeriodType)6 ProgramStage (org.hisp.dhis.program.ProgramStage)6 Test (org.junit.Test)6 Date (java.util.Date)5 HashSet (java.util.HashSet)5 QuarterlyPeriodType (org.hisp.dhis.period.QuarterlyPeriodType)5 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)5 CategoryOptionGroupSet (org.hisp.dhis.dataelement.CategoryOptionGroupSet)4 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)4 OrganisationUnitLevel (org.hisp.dhis.organisationunit.OrganisationUnitLevel)4 DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)4