-
Notifications
You must be signed in to change notification settings - Fork 1
/
trend.js
85 lines (67 loc) · 2.13 KB
/
trend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const utils = require('./utils');
const TENDENCY = {
RISING: { key: 'RISING' },
FALLING: { key: 'FALLING' }
};
const TREND = {
STEADY: { key: 'STEADY', severity: 0 },
SLOWLY: { key: 'SLOWLY', severity: 1 },
CHANGING: { key: 'CHANGING', severity: 2 },
QUICKLY: { key: 'QUICKLY', severity: 3 },
RAPIDLY: { key: 'RAPIDLY', severity: 4 }
};
const THRESHOLDS_RATIO = [
{ pascal: 0.056, trend: TREND.STEADY }, //up to 10 Pa per 3 hours
{ pascal: 0.89, trend: TREND.SLOWLY }, //10-160 Pa per 3 hours
{ pascal: 2, trend: TREND.CHANGING }, //160-360 Pa per 3 hours
{ pascal: 3.33, trend: TREND.QUICKLY }, //360-600 Pa per 3 hours
{ pascal: 9999, trend: TREND.RAPIDLY }
];
function ascendingNumbers(a, b) {
return a - b;
}
function getSeverityNotion(severity, tendency) {
if(severity === 0) return severity;
return tendency === TENDENCY.RISING ? severity : -severity;
}
function calculate(pressures, from) {
if (pressures.length < 2) return null;
let subsetOfPressures = utils.getPressuresSince(pressures, from);
if (subsetOfPressures.length >= 2) {
let earlier = subsetOfPressures[0];
let later = subsetOfPressures[subsetOfPressures.length - 1];
let difference = later.value - earlier.value;
let ratio = difference / from;
let tendency = difference >= 0 ? TENDENCY.RISING : TENDENCY.FALLING;
let threshold = THRESHOLDS_RATIO.sort(ascendingNumbers).find((t) => Math.abs(ratio) < t.pascal);
return {
tendency: tendency.key,
trend: threshold.trend.key,
from: earlier,
to: later,
difference: difference,
ratio: Math.abs(ratio),
period: Math.abs(from),
severity: getSeverityNotion(threshold.trend.severity, tendency),
}
}
return null;
}
function compareSeverity(earlier, later) {
if (earlier !== null && later !== null && earlier.severity > later.severity) {
return earlier;
}
return later;
}
function getTrend(pressures) {
let threeHours = calculate(pressures, -utils.MINUTES.THREE_HOURS);
let oneHour = calculate(pressures, -utils.MINUTES.ONE_HOUR);
let actual = threeHours;
actual = compareSeverity(oneHour, actual);
return actual;
}
module.exports = {
getTrend,
TENDENCY,
TREND
}