From 5b589a71fd9a8528f84f2a1875adb31abe74ca1f Mon Sep 17 00:00:00 2001 From: bennsimon Date: Wed, 25 Nov 2020 12:04:06 +0300 Subject: [PATCH 1/2] add miminum report data filter --- .../reporting/dao/ReportIndicatorDaoImpl.java | 18 +++++++++++++++--- .../reporting/util/Constants.java | 1 + .../reporting/util/ReportingUtils.java | 6 ++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/opensrp-reporting/src/main/java/org/smartregister/reporting/dao/ReportIndicatorDaoImpl.java b/opensrp-reporting/src/main/java/org/smartregister/reporting/dao/ReportIndicatorDaoImpl.java index c6fdaec3..d94c596e 100644 --- a/opensrp-reporting/src/main/java/org/smartregister/reporting/dao/ReportIndicatorDaoImpl.java +++ b/opensrp-reporting/src/main/java/org/smartregister/reporting/dao/ReportIndicatorDaoImpl.java @@ -10,6 +10,7 @@ import net.sqlcipher.Cursor; import net.sqlcipher.database.SQLiteDatabase; +import org.apache.commons.lang3.StringUtils; import org.smartregister.reporting.ReportingLibrary; import org.smartregister.reporting.domain.CompositeIndicatorTally; import org.smartregister.reporting.domain.IndicatorQuery; @@ -19,6 +20,7 @@ import org.smartregister.reporting.repository.IndicatorQueryRepository; import org.smartregister.reporting.repository.IndicatorRepository; import org.smartregister.reporting.util.Constants; +import org.smartregister.reporting.util.ReportingUtils; import org.smartregister.repository.EventClientRepository; import org.smartregister.util.Utils; @@ -46,8 +48,9 @@ public class ReportIndicatorDaoImpl implements ReportIndicatorDao { public static final String REPORT_LAST_PROCESSED_DATE = "REPORT_LAST_PROCESSED_DATE"; - public static String DAILY_TALLY_DATE_FORMAT = "yyyy-MM-dd"; + public static String DAILY_TALLY_DATE_FORMAT = "yyyy-MM-dd"; + public static final String MIN_REPORT_DATE = ReportingUtils.getAllSharedPreferences().getPreference(Constants.ReportingConfig.MIN_REPORT_DATE); public static String PREVIOUS_REPORT_DATES_QUERY = "select distinct eventDate, " + EventClientRepository.event_column.updatedAt + " from " + EventClientRepository.Table.event.name(); @@ -171,14 +174,23 @@ protected void saveTallies(Map indicatorQueries, Map.Ent @VisibleForTesting @NonNull protected LinkedHashMap getReportEventDates(@NonNull Date timeNow, @Nullable String lastProcessedDate, @NonNull SQLiteDatabase database) { + String monthLimitQueryFilter = StringUtils.isNotBlank(MIN_REPORT_DATE) ? + EventClientRepository.event_column.eventDate + " > '" + MIN_REPORT_DATE + "'" : ""; ArrayList> values; + String resultQuery; + boolean hasMonthLimitFilter = StringUtils.isNotBlank(monthLimitQueryFilter); if (lastProcessedDate == null || lastProcessedDate.isEmpty()) { - values = dailyIndicatorCountRepository.rawQuery(database, PREVIOUS_REPORT_DATES_QUERY); + resultQuery = hasMonthLimitFilter ? String.format("%s where %s", PREVIOUS_REPORT_DATES_QUERY, monthLimitQueryFilter) : PREVIOUS_REPORT_DATES_QUERY; } else { - values = dailyIndicatorCountRepository.rawQuery(database, PREVIOUS_REPORT_DATES_QUERY.concat(" where " + EventClientRepository.event_column.updatedAt + " > '" + lastProcessedDate + "'" + " order by eventDate asc")); + if (hasMonthLimitFilter) + monthLimitQueryFilter = " and " + monthLimitQueryFilter; + + resultQuery = PREVIOUS_REPORT_DATES_QUERY.concat(" where " + EventClientRepository.event_column.updatedAt + " > '" + lastProcessedDate + "'" + monthLimitQueryFilter + " order by eventDate asc"); } + values = dailyIndicatorCountRepository.rawQuery(database, resultQuery); + LinkedHashMap reportEventDates = new LinkedHashMap<>(); Date eventDate; diff --git a/opensrp-reporting/src/main/java/org/smartregister/reporting/util/Constants.java b/opensrp-reporting/src/main/java/org/smartregister/reporting/util/Constants.java index 0cea7b7a..e3564f74 100644 --- a/opensrp-reporting/src/main/java/org/smartregister/reporting/util/Constants.java +++ b/opensrp-reporting/src/main/java/org/smartregister/reporting/util/Constants.java @@ -39,5 +39,6 @@ interface IndicatorQueryRepository { interface ReportingConfig { String SHOULD_ALLOW_ZERO_TALLIES = "SHOULD_ALLOW_ZERO_TALLIES"; + String MIN_REPORT_DATE = "MIN_REPORT_DATE"; } } diff --git a/opensrp-reporting/src/main/java/org/smartregister/reporting/util/ReportingUtils.java b/opensrp-reporting/src/main/java/org/smartregister/reporting/util/ReportingUtils.java index 9d313de1..b7cb7ecb 100644 --- a/opensrp-reporting/src/main/java/org/smartregister/reporting/util/ReportingUtils.java +++ b/opensrp-reporting/src/main/java/org/smartregister/reporting/util/ReportingUtils.java @@ -5,13 +5,15 @@ import net.sqlcipher.Cursor; import net.sqlcipher.database.SQLiteDatabase; +import org.smartregister.util.Utils; + import java.util.ArrayList; /** * Created by Ephraim Kigamba - ekigamba@ona.io on 2019-07-09 */ -public class ReportingUtils { +public class ReportingUtils extends Utils { /** * Checks if a column exists on the table. An {@link Exception} is expected to be thrown by the sqlite @@ -83,7 +85,7 @@ public static boolean isTableExists(@NonNull SQLiteDatabase sqliteDatabase, @Non public static ArrayList performQuery(@NonNull SQLiteDatabase sqliteDatabase, @NonNull String query) { ArrayList rows = new ArrayList<>(); - Cursor cursor = sqliteDatabase.rawQuery(query,null); + Cursor cursor = sqliteDatabase.rawQuery(query, null); if (null != cursor) { int cols = cursor.getColumnCount(); rows.add(cursor.getColumnNames()); From 055ac778cd95087fd37c74cea56a6ec542d66cf6 Mon Sep 17 00:00:00 2001 From: bennsimon Date: Tue, 12 Jan 2021 14:51:06 +0300 Subject: [PATCH 2/2] trigger checks