diff --git a/lib/plugins/cob.js b/lib/plugins/cob.js index bc769197c2b..f3bb1902ad6 100644 --- a/lib/plugins/cob.js +++ b/lib/plugins/cob.js @@ -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; } diff --git a/lib/profilefunctions.js b/lib/profilefunctions.js index c15860bab26..4e348529155 100644 --- a/lib/profilefunctions.js +++ b/lib/profilefunctions.js @@ -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; @@ -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