Search in sources :

Example 6 with MinMaxDataElement

use of org.hisp.dhis.minmax.MinMaxDataElement in project dhis2-core by dhis2.

the class MinMaxDataElementController method getObjectList.

//--------------------------------------------------------------------------
// GET
//--------------------------------------------------------------------------
@GetMapping
@ResponseBody
public RootNode getObjectList(MinMaxDataElementQueryParams query) throws QueryParserException {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    query.setFilters(filters);
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    List<MinMaxDataElement> minMaxDataElements = minMaxService.getMinMaxDataElements(query);
    RootNode rootNode = NodeUtils.createMetadata();
    if (!query.isSkipPaging()) {
        query.setTotal(minMaxService.countMinMaxDataElements(query));
        rootNode.addChild(NodeUtils.createPager(query.getPager()));
    }
    rootNode.addChild(fieldFilterService.filter(MinMaxDataElement.class, minMaxDataElements, fields));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with MinMaxDataElement

use of org.hisp.dhis.minmax.MinMaxDataElement in project dhis2-core by dhis2.

the class MinMaxDataElementController method postJsonObject.

//--------------------------------------------------------------------------
// POST
//--------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
@PreAuthorize("hasRole('ALL') or hasRole('F_MINMAX_DATAELEMENT_ADD')")
public void postJsonObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
    MinMaxDataElement minMax = renderService.fromJson(request.getInputStream(), MinMaxDataElement.class);
    validate(minMax);
    minMax = getReferences(minMax);
    MinMaxDataElement persisted = minMaxService.getMinMaxDataElement(minMax.getSource(), minMax.getDataElement(), minMax.getOptionCombo());
    if (Objects.isNull(persisted)) {
        minMaxService.addMinMaxDataElement(minMax);
    } else {
        persisted.mergeWith(minMax);
        minMaxService.updateMinMaxDataElement(persisted);
    }
    WebMessage webMessage = new WebMessage();
    webMessage.setHttpStatus(HttpStatus.CREATED);
    webMessage.setStatus(Status.OK);
    webMessageService.send(webMessage, response, request);
}
Also used : MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with MinMaxDataElement

use of org.hisp.dhis.minmax.MinMaxDataElement in project dhis2-core by dhis2.

the class MinMaxDataElementBatchHandler method mapRow.

@Override
public MinMaxDataElement mapRow(ResultSet resultSet) throws SQLException {
    MinMaxDataElement mde = new MinMaxDataElement();
    mde.setMin(resultSet.getInt("minimumvalue"));
    mde.setMax(resultSet.getInt("maximumvalue"));
    mde.setGenerated(resultSet.getBoolean("generatedvalue"));
    return mde;
}
Also used : MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement)

Example 9 with MinMaxDataElement

use of org.hisp.dhis.minmax.MinMaxDataElement in project dhis2-core by dhis2.

the class MinMaxOutlierAnalysisServiceTest method testGetFindOutliers.

// ----------------------------------------------------------------------
// Business logic tests
// ----------------------------------------------------------------------
@Test
public void testGetFindOutliers() {
    dataValueA = createDataValue(dataElementA, periodI, organisationUnitA, "41", categoryOptionCombo);
    dataValueB = createDataValue(dataElementA, periodJ, organisationUnitA, "-41", categoryOptionCombo);
    dataValueService.addDataValue(createDataValue(dataElementA, periodA, organisationUnitA, "5", categoryOptionCombo));
    dataValueService.addDataValue(createDataValue(dataElementA, periodB, organisationUnitA, "-5", categoryOptionCombo));
    dataValueService.addDataValue(createDataValue(dataElementA, periodC, organisationUnitA, "5", categoryOptionCombo));
    dataValueService.addDataValue(createDataValue(dataElementA, periodD, organisationUnitA, "-5", categoryOptionCombo));
    dataValueService.addDataValue(createDataValue(dataElementA, periodE, organisationUnitA, "10", categoryOptionCombo));
    dataValueService.addDataValue(createDataValue(dataElementA, periodF, organisationUnitA, "-10", categoryOptionCombo));
    dataValueService.addDataValue(createDataValue(dataElementA, periodG, organisationUnitA, "13", categoryOptionCombo));
    dataValueService.addDataValue(createDataValue(dataElementA, periodH, organisationUnitA, "-13", categoryOptionCombo));
    dataValueService.addDataValue(dataValueA);
    dataValueService.addDataValue(dataValueB);
    minMaxDataElement = new MinMaxDataElement(organisationUnitA, dataElementA, categoryOptionCombo, -40, 40, false);
    minMaxDataElementService.addMinMaxDataElement(minMaxDataElement);
    List<Period> periods = new ArrayList<>();
    periods.add(periodI);
    periods.add(periodJ);
    periods.add(periodA);
    periods.add(periodE);
    List<DeflatedDataValue> result = minMaxOutlierAnalysisService.analyse(Lists.newArrayList(organisationUnitA), dataElementsA, periods, null, from);
    assertEquals(2, result.size());
}
Also used : DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with MinMaxDataElement

use of org.hisp.dhis.minmax.MinMaxDataElement in project dhis2-core by dhis2.

the class DefaultChartService method getJFreeChartHistory.

@Override
public JFreeChart getJFreeChartHistory(DataElement dataElement, DataElementCategoryOptionCombo categoryOptionCombo, DataElementCategoryOptionCombo attributeOptionCombo, Period lastPeriod, OrganisationUnit organisationUnit, int historyLength, I18nFormat format) {
    lastPeriod = periodService.reloadPeriod(lastPeriod);
    List<Period> periods = periodService.getPeriods(lastPeriod, historyLength);
    MinMaxDataElement minMax = minMaxDataElementService.getMinMaxDataElement(organisationUnit, dataElement, categoryOptionCombo);
    UnivariateInterpolator interpolator = new SplineInterpolator();
    Integer periodCount = 0;
    List<Double> x = new ArrayList<>();
    List<Double> y = new ArrayList<>();
    // ---------------------------------------------------------------------
    // DataValue, MinValue and MaxValue DataSets
    // ---------------------------------------------------------------------
    DefaultCategoryDataset dataValueDataSet = new DefaultCategoryDataset();
    DefaultCategoryDataset metaDataSet = new DefaultCategoryDataset();
    for (Period period : periods) {
        ++periodCount;
        period.setName(format.formatPeriod(period));
        DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
        double value = 0;
        if (dataValue != null && dataValue.getValue() != null && MathUtils.isNumeric(dataValue.getValue())) {
            value = Double.parseDouble(dataValue.getValue());
            x.add(periodCount.doubleValue());
            y.add(value);
        }
        dataValueDataSet.addValue(value, dataElement.getShortName(), period.getName());
        if (minMax != null) {
            metaDataSet.addValue(minMax.getMin(), "Min value", period.getName());
            metaDataSet.addValue(minMax.getMax(), "Max value", period.getName());
        }
    }
    if (// minimum 3 points required for interpolation
    x.size() >= 3) {
        periodCount = 0;
        double[] xa = getArray(x);
        int min = MathUtils.getMin(xa).intValue();
        int max = MathUtils.getMax(xa).intValue();
        try {
            UnivariateFunction function = interpolator.interpolate(xa, getArray(y));
            for (Period period : periods) {
                if (++periodCount >= min && periodCount <= max) {
                    metaDataSet.addValue(function.value(periodCount), "Regression value", period.getName());
                }
            }
        } catch (MathRuntimeException ex) {
            throw new RuntimeException("Failed to interpolate", ex);
        }
    }
    // ---------------------------------------------------------------------
    // Plots
    // ---------------------------------------------------------------------
    CategoryPlot plot = getCategoryPlot(dataValueDataSet, getBarRenderer(), PlotOrientation.VERTICAL, CategoryLabelPositions.UP_45);
    plot.setDataset(1, metaDataSet);
    plot.setRenderer(1, getLineRenderer());
    JFreeChart jFreeChart = getBasicJFreeChart(plot);
    return jFreeChart;
}
Also used : MathRuntimeException(org.apache.commons.math3.exception.MathRuntimeException) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) JFreeChart(org.jfree.chart.JFreeChart) MathRuntimeException(org.apache.commons.math3.exception.MathRuntimeException) MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) UnivariateInterpolator(org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset)

Aggregations

MinMaxDataElement (org.hisp.dhis.minmax.MinMaxDataElement)10 DataElement (org.hisp.dhis.dataelement.DataElement)3 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 Period (org.hisp.dhis.period.Period)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)1 SplineInterpolator (org.apache.commons.math3.analysis.interpolation.SplineInterpolator)1 UnivariateInterpolator (org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator)1 MathRuntimeException (org.apache.commons.math3.exception.MathRuntimeException)1 DhisSpringTest (org.hisp.dhis.DhisSpringTest)1 ValueType (org.hisp.dhis.common.ValueType)1 DataValue (org.hisp.dhis.datavalue.DataValue)1 DeflatedDataValue (org.hisp.dhis.datavalue.DeflatedDataValue)1 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)1 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)1 MinMaxDataElementBatchHandler (org.hisp.dhis.jdbc.batchhandler.MinMaxDataElementBatchHandler)1