Skip to content

Commit 6b02a23

Browse files
authored
Merge pull request #33 from OpenSRP/issue/15-disable-incremental-counts
Disable incremental counts & add support for returning 0 in multi-result queries
2 parents a7ddffc + 57c14a1 commit 6b02a23

File tree

20 files changed

+499
-145
lines changed

20 files changed

+499
-145
lines changed

.travis.yml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ cache:
1414
- $HOME/.gradle/caches/
1515
- $HOME/.gradle/wrapper/
1616

17-
env:
18-
matrix:
19-
- ANDROID_TARGET=android-22 ANDROID_ABI=armeabi-v7a
20-
global:
21-
# wait up to 10 minutes for adb to connect to emulator
22-
- MALLOC_ARENA_MAX=2
23-
- ADB_INSTALL_TIMEOUT=20 #increment timeout to 20 mins
24-
2517
android:
2618
components:
2719
# tools required
@@ -35,17 +27,6 @@ android:
3527
- extra-google-m2repository
3628
- extra-android-m2repository
3729

38-
# Specify at least one system image,
39-
# if you need to run emulator(s) during your tests
40-
- sys-img-armeabi-v7a-android-22
41-
42-
before_script:
43-
# Emulator Management: Create, Start and Wait
44-
- echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
45-
- emulator -avd test -no-skin -no-audio -no-window &
46-
- android-wait-for-emulator
47-
- adb shell input keyevent 82 &
48-
4930
script:
5031
- echo "Travis branch is $TRAVIS_BRANCH"
5132
- echo "Travis branch is in pull request $TRAVIS_PULL+REQUEST"

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION_NAME=0.0.8-SNAPSHOT
1+
VERSION_NAME=0.0.9-SNAPSHOT
22
VERSION_CODE=1
33
GROUP=org.smartregister
44
POM_SETTING_DESCRIPTION=OpenSRP Client Reporting Library

opensrp-reporting/src/androidTest/java/org/smartregister/reporting/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

opensrp-reporting/src/main/java/org/smartregister/reporting/ReportingLibrary.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,11 @@ public void readConfigFile(String configFilePath, SQLiteDatabase sqLiteDatabase)
226226
indicatorQuery = new IndicatorQuery(null, indicatorYamlConfigItem.getKey()
227227
, indicatorYamlConfigItem.getIndicatorQuery()
228228
, 0
229-
, indicatorYamlConfigItem.isMultiResult());
229+
, indicatorYamlConfigItem.isMultiResult()
230+
, indicatorYamlConfigItem.getExpectedIndicators());
230231
indicatorQueries.add(indicatorQuery);
231232
}
233+
232234
reportIndicators.add(indicator);
233235
}
234236
}

opensrp-reporting/src/main/java/org/smartregister/reporting/dao/ReportIndicatorDaoImpl.java

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
5+
import android.support.annotation.VisibleForTesting;
6+
import android.text.TextUtils;
57

68
import com.google.gson.Gson;
79

@@ -16,12 +18,12 @@
1618
import org.smartregister.reporting.repository.DailyIndicatorCountRepository;
1719
import org.smartregister.reporting.repository.IndicatorQueryRepository;
1820
import org.smartregister.reporting.repository.IndicatorRepository;
19-
import org.smartregister.reporting.util.AppProperties;
2021
import org.smartregister.repository.EventClientRepository;
2122

2223
import java.text.ParseException;
2324
import java.text.SimpleDateFormat;
2425
import java.util.ArrayList;
26+
import java.util.Calendar;
2527
import java.util.Date;
2628
import java.util.HashMap;
2729
import java.util.LinkedHashMap;
@@ -39,12 +41,12 @@
3941
*/
4042

4143
public class ReportIndicatorDaoImpl implements ReportIndicatorDao {
42-
4344
public static final String REPORT_LAST_PROCESSED_DATE = "REPORT_LAST_PROCESSED_DATE";
45+
public static String DAILY_TALLY_DATE_FORMAT = "yyyy-MM-dd";
46+
4447
public static String PREVIOUS_REPORT_DATES_QUERY = "select distinct eventDate, " + EventClientRepository.event_column.updatedAt + " from "
4548
+ EventClientRepository.Table.event.name();
4649

47-
private static String TAG = ReportIndicatorDaoImpl.class.getCanonicalName();
4850
private static String eventDateFormat = "yyyy-MM-dd HH:mm:ss";
4951
private IndicatorQueryRepository indicatorQueryRepository;
5052
private DailyIndicatorCountRepository dailyIndicatorCountRepository;
@@ -80,13 +82,19 @@ public void generateDailyIndicatorTallies(String lastProcessedDate) {
8082
float count;
8183
SQLiteDatabase database = ReportingLibrary.getInstance().getRepository().getWritableDatabase();
8284

83-
LinkedHashMap<String, Date> reportEventDates = getReportEventDates(lastProcessedDate, database);
85+
Date timeNow = Calendar.getInstance().getTime();
86+
LinkedHashMap<String, Date> reportEventDates = getReportEventDates(timeNow, lastProcessedDate, database);
87+
8488
Map<String, IndicatorQuery> indicatorQueries = indicatorQueryRepository.getAllIndicatorQueries();
8589

8690
if (!reportEventDates.isEmpty() && !indicatorQueries.isEmpty()) {
87-
String lastUpdatedDate = "";
91+
String lastUpdatedDate = null;
92+
8893
for (Map.Entry<String, Date> dates : reportEventDates.entrySet()) {
89-
lastUpdatedDate = new SimpleDateFormat(eventDateFormat, Locale.getDefault()).format(dates.getValue());
94+
if (dates.getValue().getTime() != timeNow.getTime()) {
95+
lastUpdatedDate = new SimpleDateFormat(eventDateFormat, Locale.getDefault()).format(dates.getValue());
96+
}
97+
9098
for (Map.Entry<String, IndicatorQuery> entry : indicatorQueries.entrySet()) {
9199
IndicatorQuery indicatorQuery = entry.getValue();
92100
CompositeIndicatorTally tally = null;
@@ -95,10 +103,11 @@ public void generateDailyIndicatorTallies(String lastProcessedDate) {
95103
ArrayList<Object> result = executeQueryAndReturnMultiResult(indicatorQuery.getQuery(), dates.getKey(), database);
96104

97105
// If the size contains actual result other than the column names which are at index 0
98-
if (result.size() > 1) {
106+
if (result.size() > 1 || (indicatorQuery.getExpectedIndicators() != null && indicatorQuery.getExpectedIndicators().size() > 0)) {
99107
tally = new CompositeIndicatorTally();
100-
tally.setValueSet(new Gson().toJson(result));
101108
tally.setValueSetFlag(true);
109+
tally.setValueSet(new Gson().toJson(result));
110+
tally.setExpectedIndicators(indicatorQuery.getExpectedIndicators());
102111
}
103112
} else {
104113
count = executeQueryAndReturnCount(indicatorQuery.getQuery(), dates.getKey(), database);
@@ -122,12 +131,17 @@ public void generateDailyIndicatorTallies(String lastProcessedDate) {
122131
}
123132
}
124133

125-
ReportingLibrary.getInstance().getContext().allSharedPreferences().savePreference(REPORT_LAST_PROCESSED_DATE, lastUpdatedDate);
134+
if (!TextUtils.isEmpty(lastUpdatedDate)) {
135+
ReportingLibrary.getInstance().getContext().allSharedPreferences().savePreference(REPORT_LAST_PROCESSED_DATE, lastUpdatedDate);
136+
}
137+
126138
Timber.i("generateDailyIndicatorTallies: Generate daily tallies complete");
127139
}
128140
}
129141

130-
private LinkedHashMap<String, Date> getReportEventDates(String lastProcessedDate, SQLiteDatabase database) {
142+
@VisibleForTesting
143+
@NonNull
144+
protected LinkedHashMap<String, Date> getReportEventDates(@NonNull Date timeNow, @Nullable String lastProcessedDate, @NonNull SQLiteDatabase database) {
131145

132146
ArrayList<HashMap<String, String>> values;
133147
if (lastProcessedDate == null || lastProcessedDate.isEmpty()) {
@@ -136,37 +150,39 @@ private LinkedHashMap<String, Date> getReportEventDates(String lastProcessedDate
136150
values = dailyIndicatorCountRepository.rawQuery(database, PREVIOUS_REPORT_DATES_QUERY.concat(" where " + EventClientRepository.event_column.updatedAt + " > '" + lastProcessedDate + "'" + " order by eventDate asc"));
137151
}
138152

139-
LinkedHashMap<String, Date> result = new LinkedHashMap<>();
153+
LinkedHashMap<String, Date> reportEventDates = new LinkedHashMap<>();
140154

141155
Date eventDate;
142156
Date updateDate;
143157
for (HashMap<String, String> val : values) {
144158
eventDate = formatDate(val.get(EventClientRepository.event_column.eventDate.name()), eventDateFormat);
145159
updateDate = formatDate(val.get(EventClientRepository.event_column.updatedAt.name()), eventDateFormat);
146160

147-
String keyDate = new SimpleDateFormat(eventDateFormat, Locale.getDefault()).format(eventDate);
161+
String keyDate = new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.getDefault()).format(eventDate);
148162

149-
if (result.get(keyDate) != null && updateDate != null) {
150-
if (result.get(keyDate).getTime() < updateDate.getTime()) {
151-
result.put(keyDate, updateDate);
163+
if (reportEventDates.get(keyDate) != null && updateDate != null) {
164+
if (reportEventDates.get(keyDate).getTime() < updateDate.getTime()) {
165+
reportEventDates.put(keyDate, updateDate);
152166
}
153167
} else {
154-
result.put(keyDate, updateDate);
168+
reportEventDates.put(keyDate, updateDate);
155169
}
156170
}
157-
return result;
171+
172+
String dateToday = new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.getDefault()).format(timeNow);
173+
174+
if (reportEventDates.get(dateToday) == null) {
175+
reportEventDates.put(dateToday, timeNow);
176+
}
177+
178+
return reportEventDates;
158179
}
159180

160181
private float executeQueryAndReturnCount(String queryString, String date, SQLiteDatabase database) {
161182
// Use date in querying if specified
162183
String query = "";
163184
if (date != null) {
164-
if(!ReportingLibrary.getInstance().getAppProperties().hasProperty(AppProperties.KEY.COUNT_INCREMENTAL)
165-
|| ReportingLibrary.getInstance().getAppProperties().getPropertyBoolean(AppProperties.KEY.COUNT_INCREMENTAL)) {
166-
query = queryString.contains("%s") ? queryString.replaceAll("%s", date) : queryString;
167-
} else {
168-
query = queryString.contains("%s") ? queryString.replaceAll("%s", date.split(" ")[0]) : queryString;
169-
}
185+
query = queryString.contains("%s") ? queryString.replaceAll("%s", date) : queryString;
170186
Timber.i("QUERY : %s", query);
171187
}
172188

@@ -198,12 +214,13 @@ private float executeQueryAndReturnCount(String queryString, String date, SQLite
198214
return count;
199215
}
200216

217+
@NonNull
201218
private ArrayList<Object> executeQueryAndReturnMultiResult(@NonNull String queryString, @Nullable String date, @NonNull SQLiteDatabase database) {
202219
// Use date in querying if specified
203220
String query = "";
204221
if (date != null) {
205222
Timber.i("QUERY : %s", queryString);
206-
query = queryString.contains("'%s'") ? String.format(queryString, date) : queryString;
223+
query = queryString.contains("%s") ? queryString.replaceAll("%s", date) : queryString;
207224
}
208225
Cursor cursor = null;
209226
ArrayList<Object> rows = new ArrayList<>();
@@ -254,6 +271,7 @@ private ArrayList<Object> executeQueryAndReturnMultiResult(@NonNull String query
254271
return rows;
255272
}
256273

274+
@Nullable
257275
private Date formatDate(String date, String format) {
258276
try {
259277
return new SimpleDateFormat(format, Locale.getDefault()).parse(date);

opensrp-reporting/src/main/java/org/smartregister/reporting/domain/CompositeIndicatorTally.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.smartregister.reporting.domain;
22

33
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
45

56
import java.util.Date;
7+
import java.util.List;
68

79
/**
810
* Created by Ephraim Kigamba - ekigamba@ona.io on 2019-07-10
@@ -13,6 +15,9 @@ public class CompositeIndicatorTally extends IndicatorTally {
1315
private String valueSet;
1416
private boolean isValueSet;
1517

18+
@Nullable
19+
private List<String> expectedIndicators;
20+
1621
public CompositeIndicatorTally() {
1722
}
1823

@@ -42,4 +47,13 @@ public boolean isValueSet() {
4247
public void setValueSetFlag(boolean valueSet) {
4348
isValueSet = valueSet;
4449
}
50+
51+
@Nullable
52+
public List<String> getExpectedIndicators() {
53+
return expectedIndicators;
54+
}
55+
56+
public void setExpectedIndicators(@Nullable List<String> expectedIndicators) {
57+
this.expectedIndicators = expectedIndicators;
58+
}
4559
}

opensrp-reporting/src/main/java/org/smartregister/reporting/domain/IndicatorQuery.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package org.smartregister.reporting.domain;
22

3+
import android.support.annotation.Nullable;
4+
5+
import java.util.List;
6+
37
public class IndicatorQuery {
48
private Long id;
59
private String indicatorCode;
610
private String query;
711
private int dbVersion;
812
private boolean isMultiResult;
13+
private List<String> expectedIndicators;
914

10-
public IndicatorQuery(Long id, String indicatorCode, String query, int dbVersion, boolean isMultiResult) {
15+
public IndicatorQuery(Long id, String indicatorCode, String query, int dbVersion, boolean isMultiResult, @Nullable List<String> expectedIndicators) {
1116
this.id = id;
1217
this.indicatorCode = indicatorCode;
1318
this.query = query;
1419
this.dbVersion = dbVersion;
1520
this.isMultiResult = isMultiResult;
21+
this.expectedIndicators = expectedIndicators;
1622
}
1723

1824
public IndicatorQuery() {
@@ -57,4 +63,12 @@ public boolean isMultiResult() {
5763
public void setMultiResult(boolean multiResult) {
5864
isMultiResult = multiResult;
5965
}
66+
67+
public List<String> getExpectedIndicators() {
68+
return expectedIndicators;
69+
}
70+
71+
public void setExpectedIndicators(List<String> expectedIndicators) {
72+
this.expectedIndicators = expectedIndicators;
73+
}
6074
}

opensrp-reporting/src/main/java/org/smartregister/reporting/domain/IndicatorYamlConfigItem.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.smartregister.reporting.domain;
22

3+
import java.util.List;
4+
35
public class IndicatorYamlConfigItem {
46

57
public static String INDICATOR_PROPERTY = "indicatorData";
@@ -8,6 +10,8 @@ public class IndicatorYamlConfigItem {
810
private String description;
911
private String indicatorQuery;
1012
private boolean isMultiResult;
13+
private List<String> expectedIndicators;
14+
1115

1216
public String getKey() {
1317
return key;
@@ -40,4 +44,12 @@ public boolean isMultiResult() {
4044
public void setMultiResult(boolean multiResult) {
4145
isMultiResult = multiResult;
4246
}
47+
48+
public List<String> getExpectedIndicators() {
49+
return expectedIndicators;
50+
}
51+
52+
public void setExpectedIndicators(List<String> expectedIndicators) {
53+
this.expectedIndicators = expectedIndicators;
54+
}
4355
}

0 commit comments

Comments
 (0)