Skip to content

Commit

Permalink
Fix value caching in the profile functions. Looks like when Circadian…
Browse files Browse the repository at this point in the history
… profile support was added, the caching of value fetches broke, causing the calls to get profile data to be > 100x slower than before. Additionally, this changes the COB plugin to not calculate COB locally at all if a recent value is available from status uploads (#5123)
  • Loading branch information
sulkaharo authored and PieterGit committed Oct 20, 2019
1 parent c11eb82 commit f33bd55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
11 changes: 7 additions & 4 deletions lib/plugins/cob.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ function init (ctx) {
}

var devicestatusCOB = cob.lastCOBDeviceStatus(devicestatus, time);
var result = devicestatusCOB;

var treatmentCOB = (treatments !== undefined && treatments.length) ? cob.fromTreatments(treatments, devicestatus, profile, time, spec_profile) : {};
const TEN_MINUTES = 10 * 60 * 1000;

if (_.isEmpty(result) || _.isNil(result.cob) || (Date.now() - result.mills) > TEN_MINUTES) {

console.log('Calculating COB');
var treatmentCOB = (treatments !== undefined && treatments.length) ? cob.fromTreatments(treatments, devicestatus, profile, time, spec_profile) : {};

var result = devicestatusCOB;
if (_.isEmpty(result)) {
result = treatmentCOB;
result.source = 'Care Portal';
} else if (treatmentCOB) {
result.treatmentCOB = treatmentCOB;
}

Expand Down
20 changes: 10 additions & 10 deletions lib/profilefunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ function init (profileData) {
profile.getValueByTime = function getValueByTime (time, valueType, spec_profile) {
if (!time) { time = Date.now(); }

//round to the minute for better caching
var minuteTime = Math.round(time / 60000) * 60000;

var cacheKey = (minuteTime + valueType + spec_profile + profile.profiletreatments_hash);
var returnValue = cache.get(cacheKey);

if (returnValue) {
return returnValue;
}

// CircadianPercentageProfile support
var timeshift = 0;
var percentage = 100;
Expand All @@ -83,16 +93,6 @@ function init (profileData) {
var offset = timeshift % 24;
time = time + offset * times.hours(offset).msecs;

//round to the minute for better caching
var minuteTime = Math.round(time / 60000) * 60000;

var cacheKey = (minuteTime + valueType + spec_profile + profile.profiletreatments_hash);
var returnValue = cache.get(cacheKey);

if (returnValue) {
return returnValue;
}

var valueContainer = profile.getCurrentProfile(time, spec_profile)[valueType];

// Assumes the timestamps are in UTC
Expand Down

0 comments on commit f33bd55

Please sign in to comment.