Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use start and end dates as arguments for CountFormInstances script #3971

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions scripts/data/count-forminstances.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/sh

# USAGE: ./count-forminstances.sh akvoflowsandbox

getforminstancecount() {
SERVICE_ACCOUNT="sa-$APP_ID@$APP_ID.iam.gserviceaccount.com"
DIR_NAME="$(dirname "${THIS_SCRIPT}")/../../.."
Expand All @@ -19,18 +17,23 @@ getforminstancecount() {
"${APP_ID}" \
"${SERVICE_ACCOUNT}" \
"${P12_FILE_PATH}" \
"${INSTANCE_NAME}"
"${INSTANCE_NAME}" \
"${START_DATE}" \
"${END_DATE}"
}


if [ "$#" -ne 1 ]
if [ "$#" -ne 3 ]
then
echo "USAGE:"
echo "- Single instance : ./count-forminstances.sh <appengine-folder>"
echo "- All instances : ./count-forminstances.sh --all"
echo "- Single instance : ./count-forminstances.sh <appengine-folder> <start_date:yyyy-MM-dd> <end_date:yyyy-MM-dd>"
echo "- All instances : ./count-forminstances.sh --all <start_date:yyyy-MM-dd> <end_date:yyyy-MM-dd>"
exit 1
fi

START_DATE="$2"
END_DATE="$3"

if [ "$1" == "--all" ]; then
RANDOMCODE=$(echo "${RANDOM}" | md5sum | head -c 10)
read -p "Please type the process code ${RANDOMCODE} " answer
Expand Down
64 changes: 30 additions & 34 deletions scripts/data/src/org/akvo/gae/remoteapi/CountFormInstances.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.repackaged.org.apache.commons.io.FileUtils;
import java.io.File;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -55,13 +55,17 @@ public class CountFormInstances implements Process {
@Override
public void execute(DatastoreService ds, String[] args) throws Exception {

if (args.length != 1) {
throw new IllegalArgumentException("Missing instance name");
if (args.length != 3) {
throw new IllegalArgumentException("Invalid arguments");
}

iName = args[0];
System.out.printf("Processing %s\n", iName);

ZoneId gmtZone = ZoneId.of("GMT");
Date startDate = Date.from(LocalDate.parse(args[1]).atStartOfDay(gmtZone).toInstant());
Date endDate = Date.from(LocalDate.parse(args[2]).atTime(LocalTime.MAX).atZone(gmtZone).toInstant());

String fileName = String.format("/tmp/form-instance-counts_%s.csv", iName);
final File file = new File(fileName);
final StringBuffer sb = new StringBuffer();
Expand All @@ -71,7 +75,7 @@ public void execute(DatastoreService ds, String[] args) throws Exception {
true);
sb.append("\n");
fetchSurveyGroups(ds);
fetchSurveys(ds);
fetchSurveys(ds, startDate, endDate);
drawSurveyGroupsIn(0L, "", sb);
FileUtils.write(file, sb.toString(), true);
System.out.printf("%s form instance counts written\n # filename: %s\n", iName, fileName);
Expand Down Expand Up @@ -130,7 +134,7 @@ private void fetchSurveyGroups(DatastoreService ds) {
}
}

private void fetchSurveys(DatastoreService ds) {
private void fetchSurveys(DatastoreService ds, Date startDate, Date endDate) throws Exception {

final Query survey_q = new Query("Survey");
final PreparedQuery survey_pq = ds.prepare(survey_q);
Expand All @@ -141,34 +145,10 @@ private void fetchSurveys(DatastoreService ds) {
String surveyName = (String) s.getProperty("name");
Long surveyGroup = (Long) s.getProperty("surveyGroupId");
if (surveyGroup != null) {
// FILTER THE SURVEY ID
Filter fsi = new FilterPredicate("surveyId", FilterOperator.EQUAL, surveyId);
// FILTER ONLY FOR THE LAST SEMESTER
Date dt = Date.from(ZonedDateTime.now().minusMonths(6).toInstant());
Filter fdt =
new FilterPredicate("createdDateTime", FilterOperator.GREATER_THAN_OR_EQUAL, dt);
Filter fmrg = CompositeFilterOperator.and(fsi, fdt);
/* END FILTER
/ We should consider that not using setKeysOnly will return full entities
/ Documentation :
/ https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/datastore/Query.html#setKeysOnly
Query si =
new Query("SurveyInstance")
.setFilter(fmrg)
.addSort("createdDateTime", SortDirection.ASCENDING);
*/
Query si = new Query("SurveyInstance").setFilter(fmrg).setKeysOnly();
long count = 0;
// Date surveyDate = null;
for (@SuppressWarnings("unused")
Entity sie : ds.prepare(si).asIterable(FetchOptions.Builder.withChunkSize(500))) {
// surveyDate = (Date) sie.getProperty("createdDateTime");
count++;
}

Long count = countSurveyInstance(ds, surveyId, startDate, endDate);

if (count > 0) {
// DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// String strDate = dateFormat.format(surveyDate);
// surveyDates.put(surveyId, strDate);
surveyCounts.put(surveyId, count);
surveyNames.put(surveyId, surveyName);
surveyParents.put(surveyId, surveyGroup);
Expand All @@ -177,4 +157,20 @@ private void fetchSurveys(DatastoreService ds) {
}
}
}

private Long countSurveyInstance(DatastoreService ds, Long surveyId, Date startDate, Date endDate) {
List<Filter> filters = new ArrayList<Filter>();
filters.add(new FilterPredicate("surveyId", FilterOperator.EQUAL, surveyId));
filters.add(new FilterPredicate("collectionDate", FilterOperator.GREATER_THAN_OR_EQUAL, startDate));
filters.add(new FilterPredicate("collectionDate", FilterOperator.LESS_THAN_OR_EQUAL, endDate));

Query q = new Query("SurveyInstance").setFilter(CompositeFilterOperator.and(filters)).setKeysOnly();
Long count = 0L;
Iterable<Entity> entities = ds.prepare(q).asIterable(FetchOptions.Builder.withChunkSize(500));
for (@SuppressWarnings("unused") Entity e : entities) {
count++;
}

return count;
}
}