diff --git a/lib/sensiplan.js b/lib/sensiplan.js index a9ad09d..1364665 100644 --- a/lib/sensiplan.js +++ b/lib/sensiplan.js @@ -3,15 +3,22 @@ function getTemperatureStatus(targetDateString, previousDaysInCycle) { .filter(day => day.temperature) .map(day => !day.temperature.exclude && rounded(day.temperature.value, 0.05)) + let detectingPotentialHighLevel = false + return tempValues.reduce((acc, curr) => { // if we don't yet have 6 lower temps, we just collect // if no shift has been detected, we collect low temps // after the shift has been detected, we count them as part // of the higher temperature phase - if (acc.low.length < 6 || (!acc.shiftDetected && curr <= acc.ltl)) { + if (acc.low.length < 7) { + acc.low.push(curr) + acc.ltl = Math.max(...acc.low.slice(-6)) + // TODO these are the same + } else if (curr <= acc.ltl && !detectingPotentialHighLevel && !acc.shiftDetected) { acc.low.push(curr) acc.ltl = Math.max(...acc.low.slice(-6)) } else { + detectingPotentialHighLevel = true acc.high.push(curr) } @@ -19,10 +26,12 @@ function getTemperatureStatus(targetDateString, previousDaysInCycle) { // we round the difference because of JS decimal weirdness if (acc.high.length === 3 && rounded(curr - acc.ltl, 0.01) >= 0.2) { acc.shiftDetected = true + detectingPotentialHighLevel = false } // 1st exception rule if (acc.high.length === 4 && curr > acc.ltl) { acc.shiftDetected = true + detectingPotentialHighLevel = false } return acc diff --git a/test/fixtures/first-exception-rule-no-shift.json b/test/fixtures/first-exception-rule-no-shift.json new file mode 100644 index 0000000..559aefb --- /dev/null +++ b/test/fixtures/first-exception-rule-no-shift.json @@ -0,0 +1,67 @@ +[{ + "date": "2018-06-04", + "temperature": { + "value": 36.7, + "exclude": null + } +}, { + "date": "2018-06-05", + "temperature": { + "value": 36.57, + "exclude": null + } +}, { + "date": "2018-06-06", + "temperature": { + "value": 36.47, + "exclude": null + } +}, { + "date": "2018-06-07", + "temperature": { + "value": 36.49, + "exclude": null + } +}, { + "date": "2018-06-09", + "temperature": { + "value": 36.57, + "exclude": null + } +}, { + "date": "2018-06-10", + "temperature": { + "value": 36.62, + "exclude": null + } +}, { + "date": "2018-06-11", + "temperature": { + "value": 36.55, + "exclude": null + } +}, { + "date": "2018-06-12", + "temperature": { + "value": 36.8, + "exclude": null + } +}, { + "date": "2018-06-13", + "temperature": { + "value": 36.86, + "exclude": null + } +}, { + "date": "2018-06-14", + "temperature": { + "value": 36.77, + "exclude": null + } +}, { + "date": "2018-06-15", + "temperature": { + "value": 36.57, + "exclude": null + } +}] \ No newline at end of file diff --git a/test/fixtures/regular-rule-no-shift.json b/test/fixtures/regular-rule-no-shift.json new file mode 100644 index 0000000..ca99c5a --- /dev/null +++ b/test/fixtures/regular-rule-no-shift.json @@ -0,0 +1,61 @@ +[{ + "date": "2018-06-04", + "temperature": { + "value": 36.7, + "exclude": null + } +}, { + "date": "2018-06-05", + "temperature": { + "value": 36.57, + "exclude": null + } +}, { + "date": "2018-06-06", + "temperature": { + "value": 36.47, + "exclude": null + } +}, { + "date": "2018-06-07", + "temperature": { + "value": 36.49, + "exclude": null + } +}, { + "date": "2018-06-09", + "temperature": { + "value": 36.57, + "exclude": null + } +}, { + "date": "2018-06-10", + "temperature": { + "value": 36.62, + "exclude": null + } +}, { + "date": "2018-06-11", + "temperature": { + "value": 36.55, + "exclude": null + } +}, { + "date": "2018-06-12", + "temperature": { + "value": 36.8, + "exclude": null + } +}, { + "date": "2018-06-13", + "temperature": { + "value": 36.86, + "exclude": null + } +}, { + "date": "2018-06-14", + "temperature": { + "value": 36.77, + "exclude": null + } +}] \ No newline at end of file diff --git a/test/fixtures/regular-rule.json b/test/fixtures/regular-rule-shift.json similarity index 100% rename from test/fixtures/regular-rule.json rename to test/fixtures/regular-rule-shift.json diff --git a/test/sensiplan.spec.js b/test/sensiplan.spec.js index 25ab34e..055f6db 100644 --- a/test/sensiplan.spec.js +++ b/test/sensiplan.spec.js @@ -1,40 +1,66 @@ import chai from 'chai' import { getTemperatureStatus } from '../lib/sensiplan' -import tempShift from './fixtures/regular-rule.json' +import tempShift from './fixtures/regular-rule-shift.json' +import noTempShift from './fixtures/regular-rule-no-shift.json' import lowerTempDays from './fixtures/lower-temps.json' import firstException from './fixtures/first-exception-rule.json' +import firstExceptionNoShift from './fixtures/first-exception-rule-no-shift.json' const expect = chai.expect describe.only('sensiplan', () => { describe('getTemperatureStatus', () => { - it('reports lower temperature status before shift', function () { - const status = getTemperatureStatus('2018-06-09', lowerTempDays) - expect(status).to.eql({ - low: [36.7, 36.55, 36.45, 36.5, 36.55], - ltl: 36.7, - high: [], - shiftDetected: false + describe('regular rule', () => { + it('reports lower temperature status before shift', function () { + const status = getTemperatureStatus('2018-06-09', lowerTempDays) + expect(status).to.eql({ + low: [36.7, 36.55, 36.45, 36.5, 36.55], + ltl: 36.7, + high: [], + shiftDetected: false + }) + }) + + it('detects temperature shift correctly', function () { + const status = getTemperatureStatus('2018-06-14', tempShift) + expect(status).to.eql({ + low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55], + ltl: 36.6, + high: [36.8, 36.85, 36.8], + shiftDetected: true + }) + }) + + it('detects missing temperature shift correctly', function () { + const status = getTemperatureStatus('2018-06-14', noTempShift) + expect(status).to.eql({ + low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55], + ltl: 36.6, + high: [36.8, 36.85, 36.75], + shiftDetected: false + }) }) }) - it('detects temperature shift according to regular rule', function () { - const status = getTemperatureStatus('2018-06-14', tempShift) - expect(status).to.eql({ - low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55], - ltl: 36.6, - high: [36.8, 36.85, 36.8], - shiftDetected: true + describe('1st exception rule', () => { + it('detects temperature shift', function () { + const status = getTemperatureStatus('2018-06-14', firstException) + expect(status).to.eql({ + low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55], + ltl: 36.6, + high: [36.8, 36.85, 36.75, 36.65], + shiftDetected: true + }) }) - }) - it('detects temperature shift according to 1st exception rule', function () { - const status = getTemperatureStatus('2018-06-14', firstException) - expect(status).to.eql({ - low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55], - ltl: 36.6, - high: [36.8, 36.85, 36.75, 36.65], - shiftDetected: true + it('detects missing temperature shift correctly', function () { + const status = getTemperatureStatus('2018-06-14', firstExceptionNoShift) + expect(status).to.eql({ + low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55], + ltl: 36.6, + high: [36.8, 36.85, 36.75, 36.55], + shiftDetected: false + }) }) }) })