@@ -10,27 +10,16 @@ |
import com.mongodb.client.FindIterable; |
import com.mongodb.client.MongoCollection; |
import com.mongodb.client.MongoDatabase; |
-import com.mongodb.client.model.UpdateOptions; |
+import csip.annotations.*; |
import javax.ws.rs.Path; |
-import oms3.annotations.*; |
import csip.ModelDataService; |
import csip.SessionLogger; |
-import java.io.File; |
-import java.io.FileNotFoundException; |
-import java.io.IOException; |
-import java.text.ParseException; |
-import java.text.SimpleDateFormat; |
import java.util.ArrayList; |
-import java.util.Arrays; |
-import java.util.Collections; |
-import java.util.Iterator; |
import java.util.List; |
-import java.util.TimeZone; |
import org.bson.Document; |
import org.codehaus.jettison.json.JSONArray; |
import org.codehaus.jettison.json.JSONException; |
import org.codehaus.jettison.json.JSONObject; |
-import utils.TimeseriesTable; |
|
/** |
* Mongodb timeseries data deletion |
@@ -39,6 +28,8 @@ |
*/ |
@Name("Timeseries delete") |
@Description("Delete a timeseries collection or document in mongo.") |
+@Author(org = "CSU") |
+@License(License.MIT) |
@Path("m/delete/1.0") |
public class V1_0 extends ModelDataService { |
|
@@ -46,6 +37,7 @@ |
MongoDatabase db; |
JSONObject results = new JSONObject(); |
|
+ |
protected void open(String mongoclienturi) { |
MongoClientURI u = new MongoClientURI(mongoclienturi); |
mongo = new MongoClient(u); |
@@ -69,25 +61,29 @@ |
String mongo_uri = getStringParam("mongo_uri"); |
String mongo_collection = getStringParam("mongo_collection"); |
JSONArray ids = getJSONArrayParam("ids", null); |
+ |
if (ids != null) { |
- deleteIds(mongo_uri, mongo_collection, ids, LOG); |
+ deleteIds(mongo_uri, mongo_collection, ids); |
} else { |
// If no ids specified, then delete the collection. |
- deleteCollection(mongo_uri, mongo_collection, LOG); |
+ deleteCollection(mongo_uri, mongo_collection); |
} |
} |
- |
- public void deleteCollection(String mongo_uri, String mongo_collection, SessionLogger LOG) throws JSONException { |
+ |
+ |
+ public void deleteCollection(String mongo_uri, String mongo_collection) throws JSONException { |
open(mongo_uri); |
MongoCollection<Document> collection = db.getCollection(mongo_collection); |
collection.drop(); |
results.put("deleted_collection", mongo_collection); |
+ close(); |
} |
|
- public void deleteIds(String mongo_uri, String mongo_collection, JSONArray ids, SessionLogger LOG) throws JSONException { |
+ |
+ public void deleteIds(String mongo_uri, String mongo_collection, JSONArray ids) throws JSONException { |
open(mongo_uri); |
MongoCollection<Document> collection = db.getCollection(mongo_collection); |
- |
+ |
List<String> ids_list = new ArrayList(); |
for (int i = 0; i < ids.length(); i++) { |
ids_list.add(ids.getString(i)); |
@@ -95,16 +91,16 @@ |
|
List<String> deleted_ids = new ArrayList(); |
FindIterable<Document> c = collection.find(); |
- for (Document finddoc: c) { |
- String find_id = (String)finddoc.get("_id"); |
+ for (Document finddoc : c) { |
+ String find_id = (String) finddoc.get("_id"); |
if (ids_list.contains(find_id)) { |
LOG.info("Deleting existing doc"); |
collection.deleteOne(finddoc); |
deleted_ids.add(find_id); |
} |
} |
- |
results.put("deleted_document_ids", new JSONArray(deleted_ids)); |
+ close(); |
} |
|
|
@@ -112,16 +108,17 @@ |
protected void postProcess() throws Exception { |
putResult("result", results); |
} |
- |
- public static void main(String [] args) throws JSONException { |
+ |
+ |
+ public static void main(String[] args) throws JSONException { |
V1_0 timeseries = new V1_0(); |
- SessionLogger LOG = new SessionLogger(); |
JSONArray delete_ids = new JSONArray(); |
delete_ids.put("test_ref"); |
- timeseries.deleteIds("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll", delete_ids, LOG); |
- timeseries.deleteCollection("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll", LOG); |
+ timeseries.deleteIds("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll", delete_ids); |
+ timeseries.deleteCollection("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll"); |
} |
- |
+ |
+ |
public static String[] toStringArray(List<String> data) { |
return (String[]) data.stream().map(String::toString).toArray(); |
} |
@@ -12,7 +12,6 @@ |
import com.mongodb.client.MongoDatabase; |
import com.mongodb.client.model.UpdateOptions; |
import javax.ws.rs.Path; |
-import oms3.annotations.*; |
import csip.ModelDataService; |
import csip.SessionLogger; |
import java.io.File; |
@@ -29,7 +28,8 @@ |
import org.codehaus.jettison.json.JSONException; |
import org.codehaus.jettison.json.JSONObject; |
import utils.TimeseriesTable; |
- |
+import csip.annotations.*; |
+ |
/** |
* Mongodb timeseries data insertion |
* |
@@ -37,6 +37,9 @@ |
*/ |
@Name("Timeseries insert") |
@Description("Update a timeseries collection in mongo.") |
+@Author(org="CSU") |
+@License(License.MIT) |
+ |
@Path("m/insert/1.0") |
public class V1_0 extends ModelDataService { |
|
@@ -53,7 +56,6 @@ |
if (!u.getDatabase().equals("csip_timeseries")) { |
throw new RuntimeException("Can only modify csip_timeseries collection"); |
} |
- |
db = mongo.getDatabase(u.getDatabase()); |
} |
|
@@ -75,11 +77,13 @@ |
String date_fmt = getStringParam("date_format"); |
JSONArray locationArray = getJSONArrayParam("location"); |
List<Double> location = Arrays.asList(Double.parseDouble(locationArray.get(0).toString()), Double.parseDouble(locationArray.get(1).toString())); |
+ |
run(mongo_uri, mongo_collection, doc_id, data_file, data_col, metadata, date_fmt, location, LOG); |
} |
|
- public void run(String mongo_uri, String mongo_collection, String doc_id, File data_file, String data_col, JSONObject metadata, |
+ void run(String mongo_uri, String mongo_collection, String doc_id, File data_file, String data_col, JSONObject metadata, |
String date_fmt, List<Double> location, SessionLogger LOG) throws FileNotFoundException, IOException, JSONException, ParseException { |
+ |
open(mongo_uri); |
MongoCollection<Document> collection = db.getCollection(mongo_collection); |
|
@@ -127,6 +131,7 @@ |
} |
results = new JSONObject(); |
results.put("updated_document_id", doc.get("_id").toString()); |
+ close(); |
} |
|
|
@@ -14,10 +14,10 @@ |
import com.mongodb.client.model.Aggregates; |
import com.mongodb.client.model.Filters; |
import javax.ws.rs.Path; |
-import oms3.annotations.*; |
import csip.ModelDataService; |
import csip.ServiceException; |
import csip.SessionLogger; |
+import csip.annotations.*; |
import java.io.FileNotFoundException; |
import java.io.IOException; |
import java.text.ParseException; |
@@ -39,6 +39,7 @@ |
import org.codehaus.jettison.json.JSONObject; |
import java.util.logging.Level; |
import java.util.logging.Logger; |
+import org.apache.catalina.ant.ServerinfoTask; |
|
/** |
* Mongodb timeseries data insertion |
@@ -47,6 +48,8 @@ |
*/ |
@Name("Timeseries query") |
@Description("Query a timeseries collection in mongo.") |
+@Author(org = "CSU") |
+@License(License.MIT) |
@Path("m/query/1.0") |
public class V1_0 extends ModelDataService { |
|
@@ -55,6 +58,7 @@ |
|
JSONArray results = new JSONArray(); |
|
+ |
protected void open(String mongoclienturi) { |
MongoClientURI u = new MongoClientURI(mongoclienturi); |
mongo = new MongoClient(u); |
@@ -79,17 +83,21 @@ |
boolean metadata_only = getBooleanParam("metadata_only", true); |
boolean compute_stats = getBooleanParam("compute_stats", false); |
String search_id = getStringParam("search_id", null); |
- JSONObject search_feature; |
- try { |
- search_feature = getJSONParam("search_feature", null); |
- } catch (ServiceException ex) { |
- throw new RuntimeException(ex); |
- } |
- run(mongo_uri, mongo_collection, search_feature, search_id, start_date, end_date, metadata_only, compute_stats, LOG); |
+ JSONObject search_feature = getJSONParam("search_feature", null); |
+ |
+ run(mongo_uri, mongo_collection, search_feature, search_id, start_date, end_date, metadata_only, compute_stats); |
} |
- |
- public 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, SessionLogger LOG) throws JSONException, ServiceException { |
+ |
+ |
+ @Override |
+ protected void postProcess() throws Exception { |
+ 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) throws JSONException, ServiceException { |
+ |
open(mongo_uri); |
MongoCollection<Document> collection = db.getCollection(mongo_collection); |
|
@@ -118,37 +126,39 @@ |
c = collection.find(Filters.eq("_id", search_id)); |
} |
} |
- for (Document doc: c) { |
+ for (Document doc : c) { |
JSONObject res = getResult(collection, doc, start_date, end_date, metadata_only, compute_stats); |
results.put(res); |
} |
} else { |
JSONArray features = search_feature.getJSONArray("features"); |
- |
for (int i = 0; i < features.length(); i++) { |
JSONObject feat = features.getJSONObject(i); |
List<Bson> agg_match = null; |
try { |
agg_match = getMatch(feat, search_id); |
} catch (IOException ex) { |
- Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, null, ex); |
+ throw new ServiceException(ex); |
} |
AggregateIterable<Document> c = collection.aggregate(agg_match); |
- for (Document doc: c) { |
+ for (Document doc : c) { |
JSONObject res = getResult(collection, doc, start_date, end_date, metadata_only, compute_stats); |
results.put(res); |
} |
} |
} |
+ close(); |
} |
|
+ |
private JSONObject getResult(MongoCollection<Document> c, Document doc, Date start_date, Date end_date, |
boolean metadata_only, boolean compute_stats) throws JSONException { |
- String date_fmt = (String)doc.get("date_fmt"); |
+ String date_fmt = (String) doc.get("date_fmt"); |
SimpleDateFormat ft = new SimpleDateFormat(date_fmt); |
ft.setTimeZone(TimeZone.getTimeZone("UTC")); |
|
- List<String> header = (ArrayList)doc.get("header"); |
+ List<String> header = (ArrayList) doc.get("header"); |
+ List<List<Object>> data = (ArrayList) doc.get("data"); |
JSONArray output = new JSONArray(); |
List<List<Object>> filtered = new ArrayList(); |
JSONObject statResult = new JSONObject(); |
@@ -195,7 +205,6 @@ |
} |
} |
|
- |
if (compute_stats) { |
Document statsDoc = new Document("_id", null); |
List<String> stats = Arrays.asList("min", "max", "avg", "stdDevPop"); |
@@ -223,39 +232,33 @@ |
} |
} |
} |
- |
+ |
JSONObject res = new JSONObject(); |
res.put("_id", doc.get("_id")); |
- Document location = (Document)doc.get("location"); |
- List<Double> coords = (List<Double>)location.get("coordinates"); |
+ Document location = (Document) doc.get("location"); |
+ List<Double> coords = (List<Double>) location.get("coordinates"); |
res.put("location", coords); |
res.put("header", header); |
if (!metadata_only) { |
res.put("data", filtered); |
} |
- Document metadata = (Document)doc.get("metadata"); |
+ Document metadata = (Document) doc.get("metadata"); |
res.put("metadata", new JSONObject(metadata.toJson())); |
|
if (compute_stats) { |
res.put("statistics", statResult); |
} |
|
- |
return res; |
} |
|
|
- @Override |
- protected void postProcess() throws Exception { |
- putResult("result", results); |
- } |
- |
- public List<Bson> getMatch(JSONObject feat, String search_id) throws JSONException, IOException { |
- String geom = feat.getJSONObject("geometry").toString(); |
+ public List<Bson> getMatch(JSONObject feat, String search_id) throws JSONException, IOException { |
+ String geom = feat.getJSONObject("geometry").toString(); |
Document match_arg = new Document("location", |
- new Document("$geoWithin", |
- new Document("$geometry", Document.parse(geom)) |
- ) |
+ new Document("$geoWithin", |
+ new Document("$geometry", Document.parse(geom)) |
+ ) |
); |
if (search_id != null) { |
// Check for object ID (will be a hexidecimal string) |
@@ -272,52 +275,52 @@ |
agg_match.add(new_match); |
return agg_match; |
} |
- |
- public static void main(String [] args) throws FileNotFoundException, IOException, JSONException, ParseException, ServiceException { |
+ |
+ |
+ public static void main(String[] args) throws FileNotFoundException, IOException, JSONException, ParseException, ServiceException { |
V1_0 timeseries = new V1_0(); |
- JSONObject searchFeature = new JSONObject("{\n" + |
-" \"type\": \"FeatureCollection\",\n" + |
-" \"features\": [\n" + |
-" {\n" + |
-" \"geometry\": {\n" + |
-" \"type\": \"MultiPolygon\",\n" + |
-" \"coordinates\": [\n" + |
-" [\n" + |
-" [\n" + |
-" [\n" + |
-" -105.09928580027,\n" + |
-" 40.701406829918\n" + |
-" ],\n" + |
-" [\n" + |
-" -105.09447928172,\n" + |
-" 40.365324016903\n" + |
-" ],\n" + |
-" [\n" + |
-" -104.72849722611,\n" + |
-" 40.358522384503\n" + |
-" ],\n" + |
-" [\n" + |
-" -104.72437735306,\n" + |
-" 40.701927386425\n" + |
-" ],\n" + |
-" [\n" + |
-" -105.09928580027,\n" + |
-" 40.701406829918\n" + |
-" ]\n" + |
-" ]\n" + |
-" ]\n" + |
-" ]\n" + |
-" },\n" + |
-" \"type\": \"Feature\",\n" + |
-" \"properties\": {\n" + |
-" \"gid\": 1,\n" + |
-" \"name\": \"gid 1\"\n" + |
-" }\n" + |
-" }\n" + |
-" ]\n" + |
-" }"); |
- SessionLogger LOG = new SessionLogger(); |
+ JSONObject searchFeature = new JSONObject("{\n" |
+ + " \"type\": \"FeatureCollection\",\n" |
+ + " \"features\": [\n" |
+ + " {\n" |
+ + " \"geometry\": {\n" |
+ + " \"type\": \"MultiPolygon\",\n" |
+ + " \"coordinates\": [\n" |
+ + " [\n" |
+ + " [\n" |
+ + " [\n" |
+ + " -105.09928580027,\n" |
+ + " 40.701406829918\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -105.09447928172,\n" |
+ + " 40.365324016903\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -104.72849722611,\n" |
+ + " 40.358522384503\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -104.72437735306,\n" |
+ + " 40.701927386425\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -105.09928580027,\n" |
+ + " 40.701406829918\n" |
+ + " ]\n" |
+ + " ]\n" |
+ + " ]\n" |
+ + " ]\n" |
+ + " },\n" |
+ + " \"type\": \"Feature\",\n" |
+ + " \"properties\": {\n" |
+ + " \"gid\": 1,\n" |
+ + " \"name\": \"gid 1\"\n" |
+ + " }\n" |
+ + " }\n" |
+ + " ]\n" |
+ + " }"); |
String search_id = null; |
- timeseries.run("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll", searchFeature, search_id, "2014-04-04", "2014-05-08", false, true, LOG); |
+ timeseries.run("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll", searchFeature, search_id, "2014-04-04", "2014-05-08", false, true); |
} |
} |