Skip to content

Commit d56cf2f

Browse files
authored
feat: Analytics data response (#122)
1 parent d3e38e9 commit d56cf2f

12 files changed

Lines changed: 470 additions & 14 deletions

File tree

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,32 @@ DataValueSetResponse response = dhis2.saveDataValueSet(file, options);
300300

301301
This section explains operations for the analytics engine.
302302

303+
### Get analytics data
304+
305+
To retrieve analytics data:
306+
307+
```java
308+
AnalyticsQuery query = AnalyticsQuery.instance()
309+
.addDataDimension(List.of("fbfJHSPpUQD", "cYeuwXTCPkU", "Jtf34kNZhzP"))
310+
.addPeriodDimension(List.of("202501", "202502", "202503"))
311+
.addDimension("fMZEcRHuamy", List.of("qkPbeWaFsnU", "wbrDrL2aYEc"))
312+
.addOrgUnitFilter(List.of("ImspTQPwCqd"))
313+
.setIncludeMetadataDetails(true);
314+
315+
AnalyticsData data = dhis2.getAnalyticsData(query);
316+
```
317+
303318
### Get analytics data value set
304319

305320
To retrieve analytics data in the data value set format:
306321

307322
```java
308-
DataValueSet dvs = dhis2.getAnalyticsDataValueSet(AnalyticsQuery.instance()
323+
AnalyticsQuery query = AnalyticsQuery.instance()
309324
.addDimension(Dimension.DIMENSION_DX, "cYeuwXTCPkU", "Jtf34kNZhzP")
310325
.addDimension(Dimension.DIMENSION_OU, "O6uvpzGd5pu", "fdc6uOvgoji")
311-
.addDimension(Dimension.DIMENSION_PE, "202007", "202008"));
326+
.addDimension(Dimension.DIMENSION_PE, "202007", "202008");
327+
328+
DataValueSet dvs = dhis2.getAnalyticsDataValueSet(query);
312329
```
313330

314331
### Write analytics data value set to file
@@ -322,7 +339,7 @@ AnalyticsQuery query = AnalyticsQuery.instance()
322339
.addDimension(Dimension.DIMENSION_PE, "202007", "202008");
323340

324341
File file = new File("/tmp/data-value-set.json");
325-
342+
326343
dhis2.writeAnalyticsDataValueSet(query, file);
327344
```
328345

src/main/java/org/hisp/dhis/BaseDhis2.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,13 @@ protected URI getAnalyticsQuery(URIBuilder uriBuilder, AnalyticsQuery query) {
439439
addParameter(uriBuilder, "skipData", query.getSkipData());
440440
addParameter(uriBuilder, "skipRounding", query.getSkipRounding());
441441
addParameter(uriBuilder, "ignoreLimit", query.getIgnoreLimit());
442-
addParameter(uriBuilder, "inputIdScheme", query.getInputIdScheme());
442+
addParameter(uriBuilder, "showHierarchy", query.getShowHierarchy());
443+
addParameter(uriBuilder, "includeNumDen", query.getIncludeNumDen());
444+
addParameter(uriBuilder, "includeMetadataDetails", query.getIncludeMetadataDetails());
443445
addParameter(uriBuilder, "outputIdScheme", query.getOutputIdScheme());
446+
addParameter(uriBuilder, "outputOrgUnitIdScheme", query.getOutputOrgUnitIdScheme());
447+
addParameter(uriBuilder, "outputDataElementIdScheme", query.getOutputDataElementIdScheme());
448+
addParameter(uriBuilder, "inputIdScheme", query.getInputIdScheme());
444449

445450
return HttpUtils.build(uriBuilder);
446451
}
@@ -939,7 +944,7 @@ private void handleErrorsForGet(CloseableHttpResponse response, String url)
939944
* @param content the JSON content.
940945
* @param type the object type.
941946
* @return an object.
942-
* @throws IOException
947+
* @throws IOException if reading failed.
943948
*/
944949
protected <T> T readValue(String content, Class<T> type) throws IOException {
945950
return objectMapper.readValue(content, type);

src/main/java/org/hisp/dhis/Dhis2.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import org.hisp.dhis.model.SystemSettings;
8686
import org.hisp.dhis.model.TableHook;
8787
import org.hisp.dhis.model.Visualization;
88+
import org.hisp.dhis.model.analytics.AnalyticsData;
8889
import org.hisp.dhis.model.completedatasetregistration.CompleteDataSetRegistration;
8990
import org.hisp.dhis.model.completedatasetregistration.CompleteDataSetRegistrationImportOptions;
9091
import org.hisp.dhis.model.dashboard.Dashboard;
@@ -2318,6 +2319,21 @@ public String getDataValueFile(DataValueQuery query) {
23182319
config.getResolvedUriBuilder().appendPath("dataValues/files"), query);
23192320
}
23202321

2322+
// -------------------------------------------------------------------------
2323+
// Analytics data
2324+
// -------------------------------------------------------------------------
2325+
2326+
/**
2327+
* Retrieves a {@link AnalyticsData}.
2328+
*
2329+
* @param query the {@link AnalyticsQuery}.
2330+
* @return {@link AnalyticsData}.
2331+
*/
2332+
public AnalyticsData getAnalyticsData(AnalyticsQuery query) {
2333+
return getAnalyticsResponse(
2334+
config.getResolvedUriBuilder().appendPath("analytics"), query, AnalyticsData.class);
2335+
}
2336+
23212337
// -------------------------------------------------------------------------
23222338
// Analytics data value set
23232339
// -------------------------------------------------------------------------
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2004-2025, University of Oslo
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
*
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* Neither the name of the HISP project nor the names of its contributors may
14+
* be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
package org.hisp.dhis.model.analytics;
29+
30+
import com.fasterxml.jackson.annotation.JsonProperty;
31+
import java.util.List;
32+
import lombok.Getter;
33+
import lombok.NoArgsConstructor;
34+
import lombok.Setter;
35+
import lombok.ToString;
36+
37+
@Getter
38+
@Setter
39+
@ToString
40+
@NoArgsConstructor
41+
public class AnalyticsData {
42+
@JsonProperty private List<AnalyticsHeader> headers;
43+
44+
@JsonProperty private AnalyticsMetaData metaData;
45+
46+
@JsonProperty private List<List<String>> rows;
47+
48+
@JsonProperty private Integer height;
49+
50+
@JsonProperty private Integer width;
51+
52+
@JsonProperty private Integer headerWidth;
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2004-2025, University of Oslo
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
*
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* Neither the name of the HISP project nor the names of its contributors may
14+
* be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
package org.hisp.dhis.model.analytics;
29+
30+
import com.fasterxml.jackson.annotation.JsonProperty;
31+
import lombok.Getter;
32+
import lombok.NoArgsConstructor;
33+
import lombok.Setter;
34+
import lombok.ToString;
35+
import org.hisp.dhis.model.ValueType;
36+
37+
@Getter
38+
@Setter
39+
@ToString
40+
@NoArgsConstructor
41+
public class AnalyticsHeader {
42+
@JsonProperty private String name;
43+
44+
@JsonProperty private String column;
45+
46+
@JsonProperty private ValueType valueType;
47+
48+
@JsonProperty private Boolean hidden;
49+
50+
@JsonProperty private Boolean meta;
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2004-2025, University of Oslo
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
*
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* Neither the name of the HISP project nor the names of its contributors may
14+
* be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
package org.hisp.dhis.model.analytics;
29+
30+
import com.fasterxml.jackson.annotation.JsonProperty;
31+
import java.util.List;
32+
import java.util.Map;
33+
import lombok.Getter;
34+
import lombok.NoArgsConstructor;
35+
import lombok.Setter;
36+
import lombok.ToString;
37+
38+
@Getter
39+
@Setter
40+
@ToString
41+
@NoArgsConstructor
42+
public class AnalyticsMetaData {
43+
@JsonProperty private Map<String, MetaDataItem> items;
44+
45+
@JsonProperty private Map<String, List<String>> dimensions;
46+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2004-2025, University of Oslo
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
*
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* Neither the name of the HISP project nor the names of its contributors may
14+
* be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
package org.hisp.dhis.model.analytics;
29+
30+
import com.fasterxml.jackson.annotation.JsonProperty;
31+
import lombok.Getter;
32+
import lombok.NoArgsConstructor;
33+
import lombok.Setter;
34+
import lombok.ToString;
35+
36+
@Getter
37+
@Setter
38+
@ToString
39+
@NoArgsConstructor
40+
public class MetaDataItem {
41+
@JsonProperty private String uid;
42+
43+
@JsonProperty private String name;
44+
45+
@JsonProperty private String dimensionType;
46+
47+
@JsonProperty private String code;
48+
49+
@JsonProperty private String dimensionItemType;
50+
51+
@JsonProperty private String valueType;
52+
53+
@JsonProperty private String totalAggregationType;
54+
55+
@JsonProperty private String startDate;
56+
57+
@JsonProperty private String endDate;
58+
59+
@JsonProperty private String legendSet;
60+
61+
@JsonProperty private String aggregationType;
62+
}

0 commit comments

Comments
 (0)