Skip to content

Commit 0426b01

Browse files
authored
Fix api v3 users seeing gaps in CGM data (nightscout#7853)
* Change runtime cache to support item timestamp be defined in either mills or date field. * Fix typo * Return mills in v1 api when returning data from cache * Also parse created_at in the cache * Fix copy paste error
1 parent 26a8cf1 commit 0426b01

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/api/entries/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,19 @@ function configure (app, wares, ctx, env) {
474474
inMemoryCollection = ctx.cache.getData('entries');
475475

476476
inMemoryCollection = _.sortBy(inMemoryCollection, function(item) {
477-
return item.mills;
477+
const age = item.mills | item.date;
478+
return age;
478479
}).reverse();
479480
}
480481

481482
if (inMemoryPossible && query.count <= inMemoryCollection.length) {
482483
res.entries = _.cloneDeep(_.take(inMemoryCollection,query.count));
484+
485+
for (let i = 0; i < res.entries.length; i++) {
486+
let e = res.entries[i];
487+
e.mills = e.mills || e.date;
488+
}
489+
483490
res.entries_err = null;
484491
return next();
485492
}

lib/server/cache.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,31 @@ function cache (env, ctx) {
2929
, entries: constants.TWO_DAYS
3030
};
3131

32+
function getObjectAge(object) {
33+
let age = object.mills || object.date;
34+
if (isNaN(age) && object.created_at) age = Date.parse(object.created_at).valueOf();
35+
return age;
36+
}
37+
3238
function mergeCacheArrays (oldData, newData, retentionPeriod) {
3339

3440
const ageLimit = Date.now() - retentionPeriod;
3541

36-
var filteredOld = filterForAge(oldData, ageLimit);
42+
var filteredOld = filterForAge(oldData, ageLimit);
3743
var filteredNew = filterForAge(newData, ageLimit);
3844

3945
const merged = ctx.ddata.idMergePreferNew(filteredOld, filteredNew);
4046

4147
return _.sortBy(merged, function(item) {
42-
return -item.mills;
48+
const age = getObjectAge(item);
49+
return -age;
4350
});
4451

4552
function filterForAge(data, ageLimit) {
4653
return _.filter(data, function hasId(object) {
4754
const hasId = !_.isEmpty(object._id);
48-
const isFresh = object.mills >= ageLimit;
55+
const age = getObjectAge(object);
56+
const isFresh = age >= ageLimit;
4957
return isFresh && hasId;
5058
});
5159
}

0 commit comments

Comments
 (0)