From 95746ab5be397d1e4cf0b70f479d460fe6566169 Mon Sep 17 00:00:00 2001 From: Ben West Date: Thu, 19 Mar 2015 13:22:27 -0700 Subject: [PATCH 1/2] allow API to search things better This allows a query such as this to search for events of hypoglycemia for example: curl -g localhost:3434 \ /api/v1/entries \ '?find[sgv][$lte]=70&find[sgv][$gte]=20&count=1000 It's possible to construct most mongo queries by url encoding the query string. In this instance, mongo performs poorly when searching for lte/gte for strings. In order for ranged queries to perform properly, the query parameters must be set to integer type. There is a quick and ugly helper to ensure that some sgv queries will be respected as integer searches. --- lib/api/treatments/index.js | 2 +- lib/entries.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/api/treatments/index.js b/lib/api/treatments/index.js index 1a7c0bc4d40..259b7f00fe1 100644 --- a/lib/api/treatments/index.js +++ b/lib/api/treatments/index.js @@ -17,7 +17,7 @@ function configure (app, wares, treatments) { // List settings available api.get('/treatments/', function(req, res) { - treatments.list({}, function (err, profiles) { + treatments.list({find: req.params}, function (err, profiles) { return res.json(profiles); }); }); diff --git a/lib/entries.js b/lib/entries.js index 5854905be44..abd01989703 100644 --- a/lib/entries.js +++ b/lib/entries.js @@ -10,6 +10,18 @@ var TEN_MINS = 10 * 60 * 1000; * Encapsulate persistent storage of sgv entries. \**********/ +function find_sgv_query (opts) { + if (opts && opts.find && opts.find.sgv) { + Object.keys(opts.find.sgv).forEach(function (key) { + var is_keyword = /^\$/g; + if (is_keyword.test(key)) { + opts.find.sgv[key] = parseInt(opts.find.sgv[key]); + } + }); + } + return opts; +} + function storage(name, storage, pushover) { // TODO: Code is a little redundant. @@ -25,7 +37,8 @@ function storage(name, storage, pushover) { // determine find options function find ( ) { - var q = opts && opts.find ? opts.find : { }; + var finder = find_sgv_query(opts); + var q = finder && finder.find ? finder.find : { }; return q; // return this.find(q); } From 4f1194841a49bf4cbf631523561c3b86c3691abd Mon Sep 17 00:00:00 2001 From: Ben West Date: Fri, 20 Mar 2015 10:27:34 -0700 Subject: [PATCH 2/2] allows api queries like: ?find[type]=mbg Queries such as: ?find[type]=mbg will now serve responses with payloads with non-SGV data elements, including mbgs. In fact, the type query above filters to select only mbg records. --- lib/api/entries/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/entries/index.js b/lib/api/entries/index.js index 87dc532195d..79642cb0f77 100644 --- a/lib/api/entries/index.js +++ b/lib/api/entries/index.js @@ -37,7 +37,7 @@ function configure (app, wares, entries) { es.pipeline(output, sgvdata.format( ), res); }, json: function ( ) { - es.pipeline(output, sgvdata.lint({strict: false}), es.writeArray(function (err, out) { + es.pipeline(output, entries.map( ), es.writeArray(function (err, out) { res.json(out); })); }