Skip to content

Commit

Permalink
Fix api v3 users seeing gaps in CGM data (nightscout#7853)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sulkaharo committed Jan 24, 2023
1 parent 26a8cf1 commit 0426b01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/api/entries/index.js
Expand Up @@ -474,12 +474,19 @@ function configure (app, wares, ctx, env) {
inMemoryCollection = ctx.cache.getData('entries');

inMemoryCollection = _.sortBy(inMemoryCollection, function(item) {
return item.mills;
const age = item.mills | item.date;
return age;
}).reverse();
}

if (inMemoryPossible && query.count <= inMemoryCollection.length) {
res.entries = _.cloneDeep(_.take(inMemoryCollection,query.count));

for (let i = 0; i < res.entries.length; i++) {
let e = res.entries[i];
e.mills = e.mills || e.date;
}

res.entries_err = null;
return next();
}
Expand Down
14 changes: 11 additions & 3 deletions lib/server/cache.js
Expand Up @@ -29,23 +29,31 @@ function cache (env, ctx) {
, entries: constants.TWO_DAYS
};

function getObjectAge(object) {
let age = object.mills || object.date;
if (isNaN(age) && object.created_at) age = Date.parse(object.created_at).valueOf();
return age;
}

function mergeCacheArrays (oldData, newData, retentionPeriod) {

const ageLimit = Date.now() - retentionPeriod;

var filteredOld = filterForAge(oldData, ageLimit);
var filteredOld = filterForAge(oldData, ageLimit);
var filteredNew = filterForAge(newData, ageLimit);

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

return _.sortBy(merged, function(item) {
return -item.mills;
const age = getObjectAge(item);
return -age;
});

function filterForAge(data, ageLimit) {
return _.filter(data, function hasId(object) {
const hasId = !_.isEmpty(object._id);
const isFresh = object.mills >= ageLimit;
const age = getObjectAge(object);
const isFresh = age >= ageLimit;
return isFresh && hasId;
});
}
Expand Down

0 comments on commit 0426b01

Please sign in to comment.