Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize Treatment, Entry and Device Status object dates to be all in UTC Strings #4658

Merged
merged 11 commits into from
Jul 17, 2019
Merged
Prev Previous commit
Next Next commit
Use MomentJS to correctly parse the offsets & output the offset to ob…
…jects at all times
  • Loading branch information
sulkaharo committed May 30, 2019
commit 8d6c1267f2d8d805a1e49918f1c73663d4b1c4a9
11 changes: 4 additions & 7 deletions lib/server/devicestatus.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
'use strict';

var moment = require('moment');
var find_options = require('./query');

function storage (collection, ctx) {
var ObjectID = require('mongodb').ObjectID;

function create(obj, fn) {

let d = new Date();
let d = moment();

try {
if (obj.created_at) d = new Date(obj.created_at);
if (obj.created_at) d = moment.parseZone(obj.created_at);
} catch (error) {
console.error(error);
}

// Normalize all dates to UTC ISO strings
obj.created_at = d.toISOString();
const offset = d.getTimezoneOffset();

if (offset) {
obj.timeZoneOffset = offset;
}
obj.utcOffset = d.utcOffset();

api().insert(obj, function (err, doc) {
if (err != null && err.message) {
Expand Down
9 changes: 9 additions & 0 deletions lib/server/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var es = require('event-stream');
var find_options = require('./query');
var ObjectID = require('mongodb').ObjectID;
var moment = require('moment');

/**********\
* Entries
Expand Down Expand Up @@ -87,6 +88,14 @@ function storage(env, ctx) {
totalCreated = 0;

docs.forEach(function(doc) {

// Normalize dates to be in UTC, store offset in utcOffset
var dateSource = doc.sysTime || doc.dateString || doc.date || new Date().getTime();
var _sysTime = moment.parseZone(dateSource);
doc.utcOffset = _sysTime.utcOffset();
doc.sysTime = _sysTime.toISOString();
if (doc.dateString) doc.dateString = doc.sysTime;

var query = (doc.sysTime && doc.type) ? {sysTime: doc.sysTime, type: doc.type} : doc;
api( ).update(query, doc, {upsert: true}, function (err) {
firstErr = firstErr || err;
Expand Down
14 changes: 6 additions & 8 deletions lib/server/treatments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var async = require('async');
var moment = require('moment');

var find_options = require('./query');

Expand Down Expand Up @@ -145,10 +146,10 @@ function prepareData(obj) {

// Convert all dates to UTC dates

let d = new Date();
let d = moment();

try {
if (obj.created_at) d = new Date(obj.created_at);
if (obj.created_at) d = moment.parseZone(obj.created_at);
} catch (error) {
console.error(error);
sulkaharo marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -161,12 +162,9 @@ function prepareData(obj) {
, preBolusCarbs: ''
};

const offset = d.getTimezoneOffset();

if (offset) {
obj.timeZoneOffset = offset;
results.offset = offset;
}
const offset = d.utcOffset();
obj.utcOffset = offset;
results.offset = offset;

obj.glucose = Number(obj.glucose);
obj.targetTop = Number(obj.targetTop);
Expand Down
1 change: 1 addition & 0 deletions tests/api.devicestatus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('Devicestatus API', function ( ) {
.expect(200)
.expect(function (response) {
response.body[0].xdripjs.state.should.equal(6);
response.body[0].utcOffset.should.equal(0);
})
.end(function (err) {
if (err) {
Expand Down
4 changes: 3 additions & 1 deletion tests/api.entries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ describe('Entries REST api', function ( ) {
.set('api-secret', self.env.api_secret || '')
.expect(200)
.expect(function (response) {
response.body[0].sgv.should.equal('199');
var entry = response.body[0];
entry.sgv.should.equal('199');
entry.utcOffset.should.equal(-420);
})
.end(function (err) {
if (err) {
Expand Down
3 changes: 2 additions & 1 deletion tests/api.treatments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('Treatment API', function ( ) {

var current_time = Date.now();
console.log('Testing date with local format: ', _moment(current_time).format("YYYY-MM-DDTHH:mm:ss.SSSZZ"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use moment.toISOString() instead of the format, in a few places

Copy link
Member Author

@sulkaharo sulkaharo May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasoncalabrese Note the formats are intentionally different in order to test inserting a zoned time, so changing those to toISOString() would break the test


self.ctx.treatments().remove({ }, function ( ) {
request(self.app)
.post('/api/treatments/')
Expand All @@ -89,6 +89,7 @@ describe('Treatment API', function ( ) {
sorted[0].carbs.should.equal(30);
var zonedTime = _moment(current_time).utc().format("YYYY-MM-DDTHH:mm:ss.SSS") + "Z";
sorted[0].created_at.should.equal(zonedTime);
sorted[0].utcOffset.should.equal(-1* new Date().getTimezoneOffset());
done();
});
}
Expand Down