Detect missing shifts
This commit is contained in:
+10
-1
@@ -3,15 +3,22 @@ function getTemperatureStatus(targetDateString, previousDaysInCycle) {
|
|||||||
.filter(day => day.temperature)
|
.filter(day => day.temperature)
|
||||||
.map(day => !day.temperature.exclude && rounded(day.temperature.value, 0.05))
|
.map(day => !day.temperature.exclude && rounded(day.temperature.value, 0.05))
|
||||||
|
|
||||||
|
let detectingPotentialHighLevel = false
|
||||||
|
|
||||||
return tempValues.reduce((acc, curr) => {
|
return tempValues.reduce((acc, curr) => {
|
||||||
// if we don't yet have 6 lower temps, we just collect
|
// if we don't yet have 6 lower temps, we just collect
|
||||||
// if no shift has been detected, we collect low temps
|
// if no shift has been detected, we collect low temps
|
||||||
// after the shift has been detected, we count them as part
|
// after the shift has been detected, we count them as part
|
||||||
// of the higher temperature phase
|
// 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.low.push(curr)
|
||||||
acc.ltl = Math.max(...acc.low.slice(-6))
|
acc.ltl = Math.max(...acc.low.slice(-6))
|
||||||
} else {
|
} else {
|
||||||
|
detectingPotentialHighLevel = true
|
||||||
acc.high.push(curr)
|
acc.high.push(curr)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,10 +26,12 @@ function getTemperatureStatus(targetDateString, previousDaysInCycle) {
|
|||||||
// we round the difference because of JS decimal weirdness
|
// we round the difference because of JS decimal weirdness
|
||||||
if (acc.high.length === 3 && rounded(curr - acc.ltl, 0.01) >= 0.2) {
|
if (acc.high.length === 3 && rounded(curr - acc.ltl, 0.01) >= 0.2) {
|
||||||
acc.shiftDetected = true
|
acc.shiftDetected = true
|
||||||
|
detectingPotentialHighLevel = false
|
||||||
}
|
}
|
||||||
// 1st exception rule
|
// 1st exception rule
|
||||||
if (acc.high.length === 4 && curr > acc.ltl) {
|
if (acc.high.length === 4 && curr > acc.ltl) {
|
||||||
acc.shiftDetected = true
|
acc.shiftDetected = true
|
||||||
|
detectingPotentialHighLevel = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc
|
return acc
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}]
|
||||||
+61
@@ -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
|
||||||
|
}
|
||||||
|
}]
|
||||||
+49
-23
@@ -1,40 +1,66 @@
|
|||||||
import chai from 'chai'
|
import chai from 'chai'
|
||||||
import { getTemperatureStatus } from '../lib/sensiplan'
|
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 lowerTempDays from './fixtures/lower-temps.json'
|
||||||
import firstException from './fixtures/first-exception-rule.json'
|
import firstException from './fixtures/first-exception-rule.json'
|
||||||
|
import firstExceptionNoShift from './fixtures/first-exception-rule-no-shift.json'
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
|
||||||
describe.only('sensiplan', () => {
|
describe.only('sensiplan', () => {
|
||||||
describe('getTemperatureStatus', () => {
|
describe('getTemperatureStatus', () => {
|
||||||
it('reports lower temperature status before shift', function () {
|
describe('regular rule', () => {
|
||||||
const status = getTemperatureStatus('2018-06-09', lowerTempDays)
|
it('reports lower temperature status before shift', function () {
|
||||||
expect(status).to.eql({
|
const status = getTemperatureStatus('2018-06-09', lowerTempDays)
|
||||||
low: [36.7, 36.55, 36.45, 36.5, 36.55],
|
expect(status).to.eql({
|
||||||
ltl: 36.7,
|
low: [36.7, 36.55, 36.45, 36.5, 36.55],
|
||||||
high: [],
|
ltl: 36.7,
|
||||||
shiftDetected: false
|
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 () {
|
describe('1st exception rule', () => {
|
||||||
const status = getTemperatureStatus('2018-06-14', tempShift)
|
it('detects temperature shift', function () {
|
||||||
expect(status).to.eql({
|
const status = getTemperatureStatus('2018-06-14', firstException)
|
||||||
low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55],
|
expect(status).to.eql({
|
||||||
ltl: 36.6,
|
low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55],
|
||||||
high: [36.8, 36.85, 36.8],
|
ltl: 36.6,
|
||||||
shiftDetected: true
|
high: [36.8, 36.85, 36.75, 36.65],
|
||||||
|
shiftDetected: true
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
it('detects temperature shift according to 1st exception rule', function () {
|
it('detects missing temperature shift correctly', function () {
|
||||||
const status = getTemperatureStatus('2018-06-14', firstException)
|
const status = getTemperatureStatus('2018-06-14', firstExceptionNoShift)
|
||||||
expect(status).to.eql({
|
expect(status).to.eql({
|
||||||
low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55],
|
low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55],
|
||||||
ltl: 36.6,
|
ltl: 36.6,
|
||||||
high: [36.8, 36.85, 36.75, 36.65],
|
high: [36.8, 36.85, 36.75, 36.55],
|
||||||
shiftDetected: true
|
shiftDetected: false
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user