From 4c2bc6a9a68a1418480aeaf5077ba471fc351b0d Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Tue, 15 Aug 2023 14:04:16 +0200 Subject: [PATCH 1/8] add support for heartrate --- lib/api3/generic/setup.js | 11 +++++++++++ lib/api3/index.js | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/api3/generic/setup.js b/lib/api3/generic/setup.js index 17e118658dd..400333b67d9 100644 --- a/lib/api3/generic/setup.js +++ b/lib/api3/generic/setup.js @@ -62,6 +62,17 @@ function setupGenericCollections (ctx, env, app) { }); } + if (_.includes(enabledCols, 'heartrate')) { + cols.food = new Collection({ + ctx, env, app, + colName: 'heartrate', + storageColName: env.food_collection || 'heartrate', + fallbackGetDate: fallbackCreatedAt, + dedupFallbackFields: ['created_at'], + fallbackDateField: 'created_at' + }); + } + if (_.includes(enabledCols, 'profile')) { cols.profile = new Collection({ ctx, env, app, diff --git a/lib/api3/index.js b/lib/api3/index.js index 2ba0aa762b1..aa5cbef55b9 100644 --- a/lib/api3/index.js +++ b/lib/api3/index.js @@ -64,7 +64,7 @@ function configure (env, ctx) { app.set('apiVersion', apiConst.API3_VERSION); app.set('units', env.DISPLAY_UNITS); app.set('ci', process.env['CI'] ? true: false); - app.set('enabledCollections', ['devicestatus', 'entries', 'food', 'profile', 'settings', 'treatments']); + app.set('enabledCollections', ['devicestatus', 'entries', 'food', 'heartrate', 'profile', 'settings', 'treatments']); self.setENVTruthy('API3_SECURITY_ENABLE', apiConst.API3_SECURITY_ENABLE); self.setENVTruthy('API3_DEDUP_FALLBACK_ENABLED', apiConst.API3_DEDUP_FALLBACK_ENABLED); From 0b3765c606038538b70db0ab410de7f66898f729 Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Tue, 15 Aug 2023 15:21:22 +0200 Subject: [PATCH 2/8] swagger api3 heartrate support --- lib/api3/swagger.json | 3 ++- lib/api3/swagger.yaml | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/api3/swagger.json b/lib/api3/swagger.json index 20a8830a214..8cb9d4cea9c 100644 --- a/lib/api3/swagger.json +++ b/lib/api3/swagger.json @@ -21,7 +21,7 @@ "tags": [ { "name": "generic", - "description": "Generic operations with each database collection (devicestatus, entries, food, profile, settings, treatments)" + "description": "Generic operations with each database collection (devicestatus, entries, food, heartrate, profile, settings, treatments)" }, { "name": "other", @@ -1334,6 +1334,7 @@ "devicestatus", "entries", "food", + "heartrate", "profile", "settings", "treatments" diff --git a/lib/api3/swagger.yaml b/lib/api3/swagger.yaml index 332b00e86bf..cf19337bbf9 100644 --- a/lib/api3/swagger.yaml +++ b/lib/api3/swagger.yaml @@ -48,6 +48,8 @@ info: `food` collection: equal `created_at` + `heartrate` collection: equal `created_at` + `profile` collection: equal `created_at` `treatments` collection: equal combination of `created_at` and `eventType` From 5683f34a9a48f73468dbe8daf385072bcc010b24 Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Tue, 15 Aug 2023 15:33:44 +0200 Subject: [PATCH 3/8] fix storagecollname for hr --- lib/api3/generic/setup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api3/generic/setup.js b/lib/api3/generic/setup.js index 400333b67d9..e85967a43f7 100644 --- a/lib/api3/generic/setup.js +++ b/lib/api3/generic/setup.js @@ -66,7 +66,7 @@ function setupGenericCollections (ctx, env, app) { cols.food = new Collection({ ctx, env, app, colName: 'heartrate', - storageColName: env.food_collection || 'heartrate', + storageColName: env.heartrate_colletion || 'heartrate', fallbackGetDate: fallbackCreatedAt, dedupFallbackFields: ['created_at'], fallbackDateField: 'created_at' From f5ecde834d2ac6a4df3f59ab04f4534d8c43cff9 Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Thu, 17 Aug 2023 16:36:18 +0200 Subject: [PATCH 4/8] hr fixes --- lib/server/bootevent.js | 1 + lib/settings.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/server/bootevent.js b/lib/server/bootevent.js index b377abcce98..13c58125627 100644 --- a/lib/server/bootevent.js +++ b/lib/server/bootevent.js @@ -270,6 +270,7 @@ function boot (env, language) { ctx.store.ensureIndexes(ctx.profile( ), ctx.profile.indexedFields); ctx.store.ensureIndexes(ctx.food( ), ctx.food.indexedFields); ctx.store.ensureIndexes(ctx.activity( ), ctx.activity.indexedFields); + ctx.store.ensureIndexes(ctx.heartrate( ), ctx.heartrate.indexedFields); next( ); } diff --git a/lib/settings.js b/lib/settings.js index d538f226591..94b551ba13a 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -178,7 +178,7 @@ function init () { } //TODO: getting sent in status.json, shouldn't be - settings.DEFAULT_FEATURES = ['bgnow', 'delta', 'direction', 'timeago', 'devicestatus', 'upbat', 'errorcodes', 'profile', 'bolus', 'dbsize', 'runtimestate', 'basal', 'careportal']; + settings.DEFAULT_FEATURES = ['bgnow', 'delta', 'direction', 'timeago', 'devicestatus', 'heartrate', 'upbat', 'errorcodes', 'profile', 'bolus', 'dbsize', 'runtimestate', 'basal', 'careportal']; var wasSet = []; From aeb0e7341d470c04144b7bdda74deed1a82fd048 Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Sat, 19 Aug 2023 19:49:57 +0200 Subject: [PATCH 5/8] fix hr* --- lib/api3/generic/setup.js | 2 +- lib/data/calcdelta.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/api3/generic/setup.js b/lib/api3/generic/setup.js index e85967a43f7..acfd3b8c789 100644 --- a/lib/api3/generic/setup.js +++ b/lib/api3/generic/setup.js @@ -63,7 +63,7 @@ function setupGenericCollections (ctx, env, app) { } if (_.includes(enabledCols, 'heartrate')) { - cols.food = new Collection({ + cols.heartrate = new Collection({ ctx, env, app, colName: 'heartrate', storageColName: env.heartrate_colletion || 'heartrate', diff --git a/lib/data/calcdelta.js b/lib/data/calcdelta.js index d58cca21c6f..29b1036e2d7 100644 --- a/lib/data/calcdelta.js +++ b/lib/data/calcdelta.js @@ -97,7 +97,7 @@ module.exports = function calcDelta (oldData, newData) { function compressArrays(delta, newData) { // array compression - var compressibleArrays = ['sgvs', 'treatments', 'mbgs', 'cals', 'devicestatus']; + var compressibleArrays = ['sgvs', 'treatments', 'mbgs', 'cals', 'devicestatus', 'heartrate']; var changesFound = false; for (var array in compressibleArrays) { From 4e474806351a8baa84b29fd7683f2a853f98e93b Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Mon, 21 Aug 2023 12:38:03 +0200 Subject: [PATCH 6/8] fix hr* and add index --- lib/server/bootevent.js | 1 + lib/server/env.js | 1 + lib/server/heartrate.js | 85 +++++++++++++++++++++++++++++++++++++++ tests/api3.status.test.js | 49 ++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 lib/server/heartrate.js create mode 100644 tests/api3.status.test.js diff --git a/lib/server/bootevent.js b/lib/server/bootevent.js index 13c58125627..6126b333e56 100644 --- a/lib/server/bootevent.js +++ b/lib/server/bootevent.js @@ -239,6 +239,7 @@ function boot (env, language) { ctx.dataloader = require('../data/dataloader')(env, ctx); ctx.notifications = require('../notifications')(env, ctx); ctx.purifier = require('./purifier')(env,ctx); + ctx.heartrate = require('./heartrate')(env, ctx); if (env.settings.isEnabled('alexa') || env.settings.isEnabled('googlehome')) { ctx.virtAsstBase = require('../plugins/virtAsstBase')(env, ctx); diff --git a/lib/server/env.js b/lib/server/env.js index afb53d51332..08660966e62 100644 --- a/lib/server/env.js +++ b/lib/server/env.js @@ -129,6 +129,7 @@ function setStorage () { env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus'); env.food_collection = readENV('MONGO_FOOD_COLLECTION', 'food'); env.activity_collection = readENV('MONGO_ACTIVITY_COLLECTION', 'activity'); + env.heartrate_collection = readENV('MONGO_HEARTRATE_COLLECTION', 'heartrate'); var DB = { url: null, collection: null } , DB_URL = DB.url ? DB.url : env.storageURI diff --git a/lib/server/heartrate.js b/lib/server/heartrate.js new file mode 100644 index 00000000000..c276db611c4 --- /dev/null +++ b/lib/server/heartrate.js @@ -0,0 +1,85 @@ +'use strict'; + +var find_options = require('./query'); + + +function storage (env, ctx) { + var ObjectID = require('mongodb').ObjectID; + + function create (obj, fn) { + obj.created_at = (new Date( )).toISOString( ); + api().insert(obj, function (err, doc) { + if (err != null && err.message) { + console.log('Heart rate data insertion error', err.message); + fn(err.message, null); + return; + } + fn(null, doc.ops); + }); + } + + function save (obj, fn) { + obj._id = new ObjectID(obj._id); + obj.created_at = (new Date( )).toISOString( ); + api().save(obj, function (err, doc) { + fn(err, doc); + }); + } + + function query_for (opts) { + return find_options(opts, storage.queryOpts); + } + + function list(opts, fn) { + // these functions, find, sort, and limit, are used to + // dynamically configure the request, based on the options we've + // been given + + // determine sort options + function sort ( ) { + return opts && opts.sort || {created_at: -1}; + } + + // configure the limit portion of the current query + function limit ( ) { + if (opts && opts.count) { + return this.limit(parseInt(opts.count)); + } + return this; + } + + // handle all the results + function toArray (err, entries) { + fn(err, entries); + } + + // now just stitch them all together + limit.call(api( ) + .find(query_for(opts)) + .sort(sort( )) + ).toArray(toArray); + } + + function remove (_id, fn) { + var objId = new ObjectID(_id); + return api( ).remove({ '_id': objId }, fn); + } + + function api ( ) { + return ctx.store.collection(env.heartrate_collection); + } + + api.list = list; + api.create = create; + api.query_for = query_for; + api.save = save; + api.remove = remove; + api.indexedFields = ['created_at']; + return api; +} + +module.exports = storage; + +storage.queryOpts = { + dateField: 'created_at' +}; diff --git a/tests/api3.status.test.js b/tests/api3.status.test.js new file mode 100644 index 00000000000..f5d5a0133a2 --- /dev/null +++ b/tests/api3.status.test.js @@ -0,0 +1,49 @@ +'use strict'; + +const request = require('supertest'); +require('should'); + +describe('API3 STATUS', function() { + const self = this + , testConst = require('./fixtures/api3/const.json') + , instance = require('./fixtures/api3/instance') + , authSubject = require('./fixtures/api3/authSubject') + ; + + this.timeout(15000); + + before(async () => { + self.instance = await instance.create({}); + self.app = self.instance.app; + self.env = self.instance.env; + + let authResult = await authSubject(self.instance.ctx.authorization.storage); + + self.subject = authResult.subject; + self.token = authResult.token; + }); + + after(function after () { + self.instance.ctx.bus.teardown(); + }); + + + it('GET /status', async () => { + let res = await request(self.app) + .get(`/api/v3/status?token=${self.token.all}`) + .expect(200); + + const apiConst = require('../lib/api3/const.json') + , software = require('../package.json') + , result = res.body.result; + + res.body.status.should.equal(200); + result.version.should.equal(software.version); + result.apiVersion.should.equal(apiConst.API3_VERSION); + result.storage.storage.should.equal("mongodb"); + result.srvDate.should.be.within(testConst.YEAR_2019, testConst.YEAR_2050); + result.apiPermissions.devicestatus.should.equal("crud") + result.apiPermissions.heartrate.should.equal("crud") + }); +}); + From a58bfd923308dc10f1d3b0b20b4716a9534bc6cb Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Mon, 21 Aug 2023 14:23:33 +0200 Subject: [PATCH 7/8] fix status test for dev branch --- tests/api3.status.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/api3.status.test.js b/tests/api3.status.test.js index f5d5a0133a2..d400c7a294f 100644 --- a/tests/api3.status.test.js +++ b/tests/api3.status.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); require('should'); describe('API3 STATUS', function() { @@ -17,20 +16,21 @@ describe('API3 STATUS', function() { self.app = self.instance.app; self.env = self.instance.env; - let authResult = await authSubject(self.instance.ctx.authorization.storage); + let authResult = await authSubject( + self.instance.ctx.authorization.storage, ['all'], self.app); self.subject = authResult.subject; - self.token = authResult.token; + self.jwt = authResult.jwt; + self.cache = self.instance.cacheMonitor; }); after(function after () { self.instance.ctx.bus.teardown(); }); - it('GET /status', async () => { - let res = await request(self.app) - .get(`/api/v3/status?token=${self.token.all}`) + let res = await self.instance + .get(`/api/v3/status`, self.jwt.all) .expect(200); const apiConst = require('../lib/api3/const.json') From a2f3938ddd976f051f173ccefbc51db866e028ad Mon Sep 17 00:00:00 2001 From: robertbuessow Date: Mon, 21 Aug 2023 14:24:03 +0200 Subject: [PATCH 8/8] add missing heartrate to swagger.json --- lib/api3/swagger.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/api3/swagger.yaml b/lib/api3/swagger.yaml index cf19337bbf9..fdec7207e07 100644 --- a/lib/api3/swagger.yaml +++ b/lib/api3/swagger.yaml @@ -53,6 +53,8 @@ info: `profile` collection: equal `created_at` `treatments` collection: equal combination of `created_at` and `eventType` + + `heartrate` collection: equal `created_at` @@ -62,7 +64,7 @@ info: tags: - name: generic - description: Generic operations with each database collection (devicestatus, entries, food, profile, settings, treatments) + description: Generic operations with each database collection (devicestatus, entries, food, profile, settings, treatments, heartrate) - name: other description: All other various operations