@etainabl/nodejs-sdk 1.1.21 → 1.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/consumption.ts +22 -61
package/package.json
CHANGED
package/src/consumption.ts
CHANGED
|
@@ -1,73 +1,34 @@
|
|
|
1
1
|
import moment from 'moment';
|
|
2
|
-
import { interpolate } from './lib/readingsInterpolate.js';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
interface ConsumptionData {
|
|
4
|
+
date: Date;
|
|
5
|
+
consumption: number;
|
|
7
6
|
}
|
|
8
7
|
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
reading: number,
|
|
12
|
-
consumption?: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const apportionedReadings = (readings: Reading[], granularity: moment.unitOfTime.Diff = 'days', interpolationMethod: string = 'linear', startDate?: string, endDate?: string): ApportionedReading[] => {
|
|
16
|
-
// Readings sorted from newest to oldest
|
|
17
|
-
const sortedReadings = [...readings].sort((a, b) => moment(a.submittedAt).diff(b.submittedAt));
|
|
18
|
-
|
|
19
|
-
// Array of every hour/day etc between the first and last reading
|
|
20
|
-
const intervals = moment(sortedReadings[sortedReadings.length - 1].submittedAt).diff(moment(sortedReadings[0].submittedAt), granularity);
|
|
21
|
-
|
|
22
|
-
const dates = [...Array(intervals).keys()]
|
|
23
|
-
.slice(1)
|
|
24
|
-
.map((interval: number) => moment(sortedReadings[0].submittedAt).startOf(granularity).add(interval, granularity));
|
|
25
|
-
|
|
26
|
-
let interpolatedReadings = dates.map((date: moment.Moment) => {
|
|
27
|
-
const reading = interpolate(sortedReadings, date, interpolationMethod);
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
date,
|
|
31
|
-
reading
|
|
32
|
-
};
|
|
33
|
-
});
|
|
8
|
+
export const dayNightConsumption = (data: ConsumptionData[]) => data.reduce((acc: any, item: ConsumptionData) => {
|
|
9
|
+
const hour = moment(item.date).hour(); // End Time of HH consumption period
|
|
34
10
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
...reading,
|
|
41
|
-
consumption: reading.reading - prevReading.reading
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return reading;
|
|
46
|
-
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
if (startDate) {
|
|
50
|
-
interpolatedReadings = interpolatedReadings.filter((reading: ApportionedReading) => moment(reading.date).isSameOrAfter(startDate, granularity));
|
|
11
|
+
if (hour >= 0 && hour < 7) {
|
|
12
|
+
acc.nightConsumption += item.consumption;
|
|
13
|
+
} else {
|
|
14
|
+
acc.dayConsumption += item.consumption;
|
|
51
15
|
}
|
|
52
16
|
|
|
53
|
-
|
|
54
|
-
interpolatedReadings = interpolatedReadings.filter((reading: ApportionedReading) => moment(reading.date).isSameOrBefore(endDate, granularity));
|
|
55
|
-
}
|
|
17
|
+
acc.consumption += item.consumption;
|
|
56
18
|
|
|
57
|
-
return
|
|
58
|
-
}
|
|
19
|
+
return acc;
|
|
20
|
+
}, {
|
|
21
|
+
dayConsumption: 0,
|
|
22
|
+
nightConsumption: 0,
|
|
23
|
+
consumption: 0
|
|
24
|
+
});
|
|
59
25
|
|
|
60
|
-
const
|
|
61
|
-
// Split the readings into hourly readings and calculate the consumption for each hour
|
|
62
|
-
const apportioned = apportionedReadings(readings, granularity, interpolationMethod, startDate, endDate);
|
|
26
|
+
export const calcMaxConsumptionValue = (data: ConsumptionData[]) => Math.max(...data.map((item: ConsumptionData) => item.consumption));
|
|
63
27
|
|
|
64
|
-
|
|
65
|
-
const totalConsumption = apportioned.reduce((acc: any, reading) => acc + reading.consumption, 0);
|
|
28
|
+
export const calcMaxDemand = (data: ConsumptionData[]) => calcMaxConsumptionValue(data) * 2;
|
|
66
29
|
|
|
67
|
-
|
|
68
|
-
|
|
30
|
+
export const calcPeakLoad = (consumption: number, maxDemand: number, startDate: Date, endDate: Date) => {
|
|
31
|
+
const days = Math.ceil(moment(endDate).diff(moment(startDate), 'days', true));
|
|
69
32
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
consumptionFromReadings
|
|
73
|
-
};
|
|
33
|
+
return maxDemand === 0 ? 0 : ((consumption / (maxDemand * 24 * days)) * 100);
|
|
34
|
+
};
|