diff --git a/odk/controllers/get-osm-submissions.js b/odk/controllers/get-osm-submissions.js
index 94d5010b..b3fae547 100644
--- a/odk/controllers/get-osm-submissions.js
+++ b/odk/controllers/get-osm-submissions.js
@@ -91,7 +91,8 @@ function findOsmFilesInDir(dirStat, osmFiles, req, res) {
* @param res - the http response
*/
function aggregate(osmFiles, req, res) {
- aggregateOsm(osmFiles, null, function (err, osmXml) {
+ //We filter by the query parameters of the request
+ aggregateOsm(osmFiles, req.query, function (err, osmXml) {
if (err) {
res.status(500).json({
status: 500,
diff --git a/osm/aggregate-osm.js b/osm/aggregate-osm.js
index e64f2659..55320fab 100644
--- a/osm/aggregate-osm.js
+++ b/osm/aggregate-osm.js
@@ -35,37 +35,57 @@ module.exports = function (files, filter, cb) {
};
for (var i = 0; i < numFiles; i++) {
- fs.readFile(files[i], 'utf-8', function (err, xml) {
- if (err) {
- cb(err);
+ /**
+ * The file filter checks the birthtime timestamp.
+ * If we want the file, we get a bool of true in
+ * the callback.
+ */
+ filterOsm.file(files[i], filter, function (filePath, bool) {
+ if (!bool) {
+ ++filesCompleted;
+ if (filesCompleted === numFiles) {
+ cb(null, mainXmlDoc.toString());
+ }
return;
}
- var doc = libxml.parseXmlString(xml);
- var osmElements = doc.root().childNodes();
- for (var j = 0, len = osmElements.length; j < len; j++) {
- var osmElement = osmElements[j];
- // Check that the element is a node, way, or relation.
- var elementName = osmElement.name();
- if (elementName === 'node') {
- rewriteNegativeId(negIdRewriteHash, osmElement);
- } else if (elementName === 'way' || elementName === 'relation') {
- rewriteNegativeId(negIdRewriteHash, osmElement);
- // Ways and relations might need their negative refs rewritten too.
- rewriteNegativeRef(negIdRewriteHash, osmElement);
-
+ fs.readFile(filePath, 'utf-8', function (err, xml) {
+ if (err) {
+ cb(err);
+ return;
}
- if (filterOsm(osmElement, filter)) {
- mainOsmElement.addChild(osmElement);
- }
- }
- ++filesCompleted;
- if (filesCompleted === numFiles) {
- cb(null, mainXmlDoc.toString());
- }
+ var doc = libxml.parseXmlString(xml);
+ var rootEl = doc.root();
+ filterOsm.user(rootEl, filter, function (rootEl, bool) {
+ if (!bool) {
+ ++filesCompleted;
+ if (filesCompleted === numFiles) {
+ cb(null, mainXmlDoc.toString());
+ }
+ return;
+ }
+ var osmElements = rootEl.childNodes();
+ for (var j = 0, len = osmElements.length; j < len; j++) {
+ var osmElement = osmElements[j];
+ // Check that the element is a node, way, or relation.
+ var elementName = osmElement.name();
+ if (elementName === 'node') {
+ rewriteNegativeId(negIdRewriteHash, osmElement);
+ } else if (elementName === 'way' || elementName === 'relation') {
+ rewriteNegativeId(negIdRewriteHash, osmElement);
+ // Ways and relations might need their negative refs rewritten too.
+ rewriteNegativeRef(negIdRewriteHash, osmElement);
+
+ }
+ mainOsmElement.addChild(osmElement);
+ }
+ ++filesCompleted;
+ if (filesCompleted === numFiles) {
+ cb(null, mainXmlDoc.toString());
+ }
+ });
+ });
});
}
-
-
};
/**
diff --git a/osm/filter-osm.js b/osm/filter-osm.js
index ad3eca57..7f0a8fd6 100644
--- a/osm/filter-osm.js
+++ b/osm/filter-osm.js
@@ -1,11 +1,117 @@
+const fs = require('fs');
+
+const filter = module.exports = {};
+
/**
- * filter-osm.js
+ * Gets stats on a file, and filters it based on `submitTimeStart`
+ * and `submitTimeEnd` filter parameters.
*
- * @param osmElement
- * @param filter
- * @returns {boolean}
+ * @param filePath - Path to OSM file on the file system
+ * @param filterObj - Parameters to filter by
+ * @param cb - Callback object with `filePath` and `bool` value
*/
-module.exports = function (osmElement, filter) {
- if (!filter) return true;
- return false;
+filter.file = function (filePath, filterObj, cb) {
+ // if there is no filter
+ if (typeof filterObj !== 'object' || filterObj === null) {
+ cb(filePath, true);
+ return;
+ }
+
+ fs.stat(filePath, function (err, stats) {
+ // lets just let it pass through in err state
+ if (err) {
+ cb(filePath, true);
+ return;
+ }
+
+ var submitTime = stats.birthtime.getTime();
+
+ if (typeof filterObj.submitTimeStart === 'string' && typeof filterObj.submitTimeEnd === 'string') {
+ var submitTimeStart = new Date(filterObj.submitTimeStart).getTime();
+ var submitTimeEnd = new Date(filterObj.submitTimeEnd).getTime();
+ // if invalid start time
+ if (isNaN(submitTimeStart)) {
+ cb(filePath, false);
+ return;
+ }
+ // if invalid end time
+ if (isNaN(submitTimeEnd)) {
+ cb(filePath, false);
+ return;
+ }
+ if (submitTime >= submitTimeStart && submitTime <= submitTimeEnd) {
+ cb(filePath, true);
+ return;
+ }
+ cb(filePath, false);
+ return;
+ }
+
+ if (typeof filterObj.submitTimeStart === 'string') {
+ var submitTimeStart = new Date(filterObj.submitTimeStart).getTime();
+ // if invalid start time
+ if (isNaN(submitTimeStart)) {
+ cb(filePath, false);
+ return;
+ }
+ if (submitTime >= submitTimeStart) {
+ cb(filePath, true);
+ return;
+ }
+ cb(filePath, false);
+ return;
+ }
+
+ if (typeof filterObj.submitTimeEnd === 'string') {
+ var submitTimeEnd = new Date(filterObj.submitTimeEnd).getTime();
+ // if invalid end time
+ if (isNaN(submitTimeEnd)) {
+ cb(filePath, false);
+ return;
+ }
+ if (submitTime <= submitTimeEnd) {
+ cb(filePath, true);
+ return;
+ }
+ cb(filePath, false);
+ return;
+ }
+
+ // No submission time filter, pass through
+ cb(filePath, true);
+
+ });
+};
+
+
+/**
+ * Filters JOSM OSM Edit files by the user attribute OMK Android
+ * puts in the root OSM element.
+ *
+ * @param rootOsmElement - the root OSM object
+ * @param filterObj - we want to have a user filter specified in the filter object
+ * @param cb - Callback object with the `rootOsmElement` and `bool` value
+ */
+filter.user = function (rootOsmElement, filterObj, cb) {
+ // if there is no filter
+ if (typeof filterObj !== 'object' || filterObj === null) {
+ cb(rootOsmElement, true);
+ return;
+ }
+ if (typeof filterObj.user === 'string') {
+ var userAttr = rootOsmElement.attr('user');
+ // userAttr is not an attribute for the OSM edit, we dont want it
+ if (userAttr === null) {
+ cb(rootOsmElement, false);
+ return;
+ }
+ var user = userAttr.value();
+ if (user.toLowerCase() != filterObj.user.toLowerCase()) {
+ cb(rootOsmElement, false);
+ return;
+ }
+ }
+
+ // no user filter, pass through
+ cb(rootOsmElement, true);
};
diff --git a/package.json b/package.json
index 3f5cd1ba..186991e6 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "open-map-kit-server",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "OpenMapKit Server is the lightweight server component of OpenMapKit that handles the collection and aggregation of OpenStreetMap and OpenDataKit data.",
"main": "index.js",
"scripts": {
diff --git a/test/fixtures/osm/basic/OSMNode-2.osm b/test/fixtures/osm/basic/OSMNode-2.osm
index 7ecdaac9..fa444ebc 100644
--- a/test/fixtures/osm/basic/OSMNode-2.osm
+++ b/test/fixtures/osm/basic/OSMNode-2.osm
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/fixtures/osm/conflicting-negative-ids/OSMNode-1.osm b/test/fixtures/osm/conflicting-negative-ids/OSMNode-1.osm
index 57c6adcc..592365b8 100644
--- a/test/fixtures/osm/conflicting-negative-ids/OSMNode-1.osm
+++ b/test/fixtures/osm/conflicting-negative-ids/OSMNode-1.osm
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/fixtures/osm/conflicting-negative-ids/OSMNode-1_2.osm b/test/fixtures/osm/conflicting-negative-ids/OSMNode-1_2.osm
index d7b8cd8c..945ff518 100644
--- a/test/fixtures/osm/conflicting-negative-ids/OSMNode-1_2.osm
+++ b/test/fixtures/osm/conflicting-negative-ids/OSMNode-1_2.osm
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/fixtures/osm/conflicting-negative-ids/OSMWay386263542.osm b/test/fixtures/osm/conflicting-negative-ids/OSMWay386263542.osm
index c45f83af..b3880570 100644
--- a/test/fixtures/osm/conflicting-negative-ids/OSMWay386263542.osm
+++ b/test/fixtures/osm/conflicting-negative-ids/OSMWay386263542.osm
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/test/osm/filter-osm.test.js b/test/osm/filter-osm.test.js
new file mode 100644
index 00000000..e0fa52ab
--- /dev/null
+++ b/test/osm/filter-osm.test.js
@@ -0,0 +1,33 @@
+const test = require('tape');
+const fs = require('fs');
+const libxml = require('libxmljs');
+const filterOsm = require('../../osm/filter-osm');
+
+/**
+ * Get together an array of the simple set of OSM files to test.
+ */
+const simpleFiles = fs.readdirSync('./test/fixtures/osm/basic');
+const simpleFilesXml = [];
+for (var i = 0, len = simpleFiles.length; i < len; i++) {
+ var fName = simpleFiles[i];
+ var path = './test/fixtures/osm/basic/' + fName;
+ var xml = fs.readFileSync(path, {encoding:'utf-8'});
+ simpleFilesXml.push(xml);
+}
+
+test('filter-osm.js must find two users named "theoutpost" in basic osm fixture dir', function (t) {
+ t.plan(2);
+ var filter = {
+ user: 'theoutpost'
+ };
+ for (var i = 0, len = simpleFilesXml.length; i < len; i++) {
+ var xml = simpleFilesXml[i];
+ var doc = libxml.parseXmlString(xml);
+ var rootEl = doc.root();
+ filterOsm.user(rootEl, filter, function (rootEl, bool) {
+ if (bool) {
+ t.pass('got "theoutpost" user');
+ }
+ });
+ }
+});