@@ -65,17 +65,17 @@ |
|
@Override |
public void doProcess() throws Exception { |
- String mongo_uri = getStringParam("mongo_uri"); |
- String mongo_collection = getStringParam("mongo_collection"); |
- String start_date = getStringParam("start_date", null); |
- String end_date = getStringParam("end_date", null); |
- boolean metadata_only = getBooleanParam("metadata_only", true); |
- boolean compute_stats = getBooleanParam("compute_stats", false); |
- boolean compute_stats_local = getBooleanParam("compute_stats_local", false); |
- String search_id = getStringParam("search_id", null); |
- JSONObject search_feature = getJSONParam("search_feature", null); |
+ String mongoUri = getStringParam("mongo_uri"); |
+ String mongoCollection = getStringParam("mongo_collection"); |
+ String startDate = getStringParam("start_date", null); |
+ String endDate = getStringParam("end_date", null); |
+ boolean metadataOnly = getBooleanParam("metadata_only", true); |
+ boolean computeStats = getBooleanParam("compute_stats", false); |
+ boolean computeStatsLocal = getBooleanParam("compute_stats_local", false); |
+ String searchId = getStringParam("search_id", null); |
+ JSONObject searchFeature = getJSONParam("search_feature", null); |
|
- run(mongo_uri, mongo_collection, search_feature, search_id, start_date, end_date, metadata_only, compute_stats, compute_stats_local); |
+ run(mongoUri, mongoCollection, searchFeature, searchId, startDate, endDate, metadataOnly, computeStats, computeStatsLocal); |
} |
|
@Override |
@@ -83,60 +83,61 @@ |
putResult("result", results); |
} |
|
- void run(String mongo_uri, String mongo_collection, JSONObject search_feature, String search_id, |
- String start_date_str, String end_date_str, boolean metadata_only, boolean compute_stats, boolean compute_stats_local) throws JSONException, ServiceException { |
+ void run(String mongoUri, String mongoCollection, JSONObject searchFeature, String searchId, |
+ String startDateStr, String endDateStr, boolean metadataOnly, boolean computeStats, boolean computeStatsLocal) |
+ throws JSONException, ServiceException { |
|
- open(mongo_uri); |
- MongoCollection<Document> collection = db.getCollection(mongo_collection); |
+ open(mongoUri); |
+ MongoCollection<Document> collection = db.getCollection(mongoCollection); |
|
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd"); |
ft.setTimeZone(TimeZone.getTimeZone("UTC")); |
- Date start_date = null; |
- Date end_date = null; |
- if (start_date_str != null) { |
+ Date startDate = null; |
+ Date endDate = null; |
+ if (startDateStr != null) { |
try { |
- start_date = ft.parse(start_date_str); |
- end_date = ft.parse(end_date_str); |
+ startDate = ft.parse(startDateStr); |
+ endDate = ft.parse(endDateStr); |
} catch (ParseException ex) { |
throw new ServiceException(ex); |
} |
} |
|
Document locationsOnly = new Document().append("location_id", new Document().append("$exists", false)); |
- if (search_feature == null) { |
+ if (searchFeature == null) { |
FindIterable<Document> c; |
- if (search_id == null) { |
+ if (searchId == null) { |
// No search and no location filter, so get all docs in collection |
c = collection.find(locationsOnly); |
} else { |
// Search by document ID. |
// Check for object ID (will be a hexidecimal string) |
- boolean id_test = search_id.matches("\\p{XDigit}+"); |
- if (id_test) { |
- ObjectId search_objid = new ObjectId(search_id); |
+ boolean idTest = searchId.matches("\\p{XDigit}+"); |
+ if (idTest) { |
+ ObjectId search_objid = new ObjectId(searchId); |
c = collection.find(Filters.eq("_id", search_objid)); |
} else { |
// Simple search using string |
- c = collection.find(Filters.eq("_id", search_id)); |
+ c = collection.find(Filters.eq("_id", searchId)); |
} |
} |
for (Document locationDoc : c) { |
- JSONObject res = getResult(collection, locationDoc, start_date, end_date, metadata_only, compute_stats, compute_stats_local); |
+ JSONObject res = getResult(collection, locationDoc, startDate, endDate, metadataOnly, computeStats, computeStatsLocal); |
results.put(res); |
} |
} else { |
- JSONArray features = search_feature.getJSONArray("features"); |
+ JSONArray features = searchFeature.getJSONArray("features"); |
for (int i = 0; i < features.length(); i++) { |
JSONObject feat = features.getJSONObject(i); |
- List<Bson> agg_match = null; |
+ List<Bson> aggMatch = null; |
try { |
- agg_match = getMatch(feat, search_id); |
+ aggMatch = getMatch(feat, searchId); |
} catch (IOException ex) { |
throw new ServiceException(ex); |
} |
- AggregateIterable<Document> c = collection.aggregate(agg_match); |
+ AggregateIterable<Document> c = collection.aggregate(aggMatch); |
for (Document locationDoc : c) { |
- JSONObject res = getResult(collection, locationDoc, start_date, end_date, metadata_only, compute_stats, compute_stats_local); |
+ JSONObject res = getResult(collection, locationDoc, startDate, endDate, metadataOnly, computeStats, computeStatsLocal); |
results.put(res); |
} |
} |
@@ -144,13 +145,13 @@ |
close(); |
} |
|
- private JSONObject getResult(MongoCollection<Document> c, Document locationDoc, Date start_date, Date end_date, |
- boolean metadata_only, boolean compute_stats, boolean compute_stats_local) throws JSONException { |
- String date_fmt = (String) locationDoc.get("date_fmt"); |
- if (date_fmt == null) { |
- date_fmt = "yyyy-MM-dd"; |
+ private JSONObject getResult(MongoCollection<Document> c, Document locationDoc, Date startDate, Date endDate, |
+ boolean metadataOnly, boolean computeStats, boolean computeStatsLocal) throws JSONException { |
+ String dateFormat = (String) locationDoc.get("date_fmt"); |
+ if (dateFormat == null) { |
+ dateFormat = "yyyy-MM-dd"; |
} |
- SimpleDateFormat ft = new SimpleDateFormat(date_fmt); |
+ SimpleDateFormat ft = new SimpleDateFormat(dateFormat); |
ft.setTimeZone(TimeZone.getTimeZone("UTC")); |
|
List<String> header = (ArrayList) locationDoc.get("header"); |
@@ -159,15 +160,15 @@ |
JSONObject statResult = new JSONObject(); |
JSONObject statResultLocal = new JSONObject(); |
|
- if (!metadata_only) { |
+ if (!metadataOnly || computeStats) { |
Document datesQuery = new Document(); |
- if ((start_date != null || end_date != null)) { |
+ if ((startDate != null || endDate != null)) { |
datesQuery = new Document(); |
- if (start_date != null) { |
- datesQuery.append("$gte", start_date); |
+ if (startDate != null) { |
+ datesQuery.append("$gte", startDate); |
} |
- if (end_date != null) { |
- datesQuery.append("$lte", end_date); |
+ if (endDate != null) { |
+ datesQuery.append("$lte", endDate); |
} |
datesQuery = new Document("data.0", datesQuery); |
} |
@@ -200,7 +201,7 @@ |
filtered.add(row); |
} |
|
- if (compute_stats || compute_stats_local) { |
+ if (computeStats || computeStatsLocal) { |
Document statsDoc = new Document("_id", null); |
List<String> stats = Arrays.asList("min", "max", "avg", "stdDevPop"); |
for (String stat : stats) { |
@@ -210,13 +211,13 @@ |
} |
|
aggList = Arrays.asList( |
- Aggregates.match(new Document("_id", locationDoc.getString("_id"))), |
+ Aggregates.match(new Document("location_id", locationDoc.getString("_id"))), |
Aggregates.unwind("$data"), |
Aggregates.match(datesQuery), |
Aggregates.project(project), |
new Document("$group", statsDoc) |
); |
- if (compute_stats) { |
+ if (computeStats) { |
iter = c.aggregate(aggList).allowDiskUse(true); |
for (Document d : iter) { |
for (String stat : stats) { |
@@ -229,7 +230,7 @@ |
} |
|
// Add locally computed stats |
- if (compute_stats_local) { |
+ if (computeStatsLocal) { |
for (List<Object> objList : filtered) { |
for (int i = 1; i < header.size(); i++) { |
double[] vals = new double[filtered.size()]; |
@@ -265,13 +266,13 @@ |
res.put("location", coords); |
} |
res.put("header", header); |
- if (!metadata_only) { |
+ if (!metadataOnly) { |
res.put("data", filtered); |
} |
Document metadata = (Document) locationDoc.get("metadata"); |
res.put("metadata", new JSONObject(metadata.toJson())); |
|
- if (compute_stats) { |
+ if (computeStats) { |
res.put("statistics", statResult); |
res.put("statistics_local", statResultLocal); |
} |