@etainabl/nodejs-sdk 1.1.21 → 1.1.23

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.
@@ -1,18 +1,9 @@
1
- import moment from 'moment';
2
- export interface Reading {
3
- submittedAt: string;
4
- value: number;
1
+ interface ConsumptionData {
2
+ date: Date;
3
+ consumption: number;
5
4
  }
6
- export interface ApportionedReading {
7
- date: moment.Moment;
8
- reading: number;
9
- consumption?: number;
10
- }
11
- declare const _default: {
12
- apportionedReadings: (readings: Reading[], granularity?: moment.unitOfTime.Diff, interpolationMethod?: string, startDate?: string | undefined, endDate?: string | undefined) => ApportionedReading[];
13
- consumptionFromReadings: (readings: Reading[], startDate: string, endDate: string, granularity?: moment.unitOfTime.Diff, interpolationMethod?: string) => {
14
- totalConsumption: any;
15
- apportioned: ApportionedReading[];
16
- };
17
- };
18
- export default _default;
5
+ export declare const dayNightConsumption: (data: ConsumptionData[]) => any;
6
+ export declare const calcMaxConsumptionValue: (data: ConsumptionData[]) => number;
7
+ export declare const calcMaxDemand: (data: ConsumptionData[]) => number;
8
+ export declare const calcPeakLoad: (consumption: number, maxDemand: number, startDate: Date, endDate: Date) => number;
9
+ export {};
@@ -1,47 +1,23 @@
1
1
  import moment from 'moment';
2
- import { interpolate } from './lib/readingsInterpolate.js';
3
- const apportionedReadings = (readings, granularity = 'days', interpolationMethod = 'linear', startDate, endDate) => {
4
- // Readings sorted from newest to oldest
5
- const sortedReadings = [...readings].sort((a, b) => moment(a.submittedAt).diff(b.submittedAt));
6
- // Array of every hour/day etc between the first and last reading
7
- const intervals = moment(sortedReadings[sortedReadings.length - 1].submittedAt).diff(moment(sortedReadings[0].submittedAt), granularity);
8
- const dates = [...Array(intervals).keys()]
9
- .slice(1)
10
- .map((interval) => moment(sortedReadings[0].submittedAt).startOf(granularity).add(interval, granularity));
11
- let interpolatedReadings = dates.map((date) => {
12
- const reading = interpolate(sortedReadings, date, interpolationMethod);
13
- return {
14
- date,
15
- reading
16
- };
17
- });
18
- interpolatedReadings = interpolatedReadings.map((reading, index) => {
19
- const prevReading = interpolatedReadings[index - 1];
20
- if (prevReading) {
21
- return {
22
- ...reading,
23
- consumption: reading.reading - prevReading.reading
24
- };
25
- }
26
- return reading;
27
- });
28
- if (startDate) {
29
- interpolatedReadings = interpolatedReadings.filter((reading) => moment(reading.date).isSameOrAfter(startDate, granularity));
2
+ export const dayNightConsumption = (data) => data.reduce((acc, item) => {
3
+ const hour = moment(item.date).hour(); // End Time of HH consumption period
4
+ if (hour >= 0 && hour < 7) {
5
+ acc.nightConsumption += item.consumption;
30
6
  }
31
- if (endDate) {
32
- interpolatedReadings = interpolatedReadings.filter((reading) => moment(reading.date).isSameOrBefore(endDate, granularity));
7
+ else {
8
+ acc.dayConsumption += item.consumption;
33
9
  }
34
- return interpolatedReadings;
35
- };
36
- const consumptionFromReadings = (readings, startDate, endDate, granularity = 'hours', interpolationMethod = 'linear') => {
37
- // Split the readings into hourly readings and calculate the consumption for each hour
38
- const apportioned = apportionedReadings(readings, granularity, interpolationMethod, startDate, endDate);
39
- // Calculate the total consumption between the start and end dates
40
- const totalConsumption = apportioned.reduce((acc, reading) => acc + reading.consumption, 0);
41
- return { totalConsumption, apportioned };
42
- };
43
- export default {
44
- apportionedReadings,
45
- consumptionFromReadings
10
+ acc.consumption += item.consumption;
11
+ return acc;
12
+ }, {
13
+ dayConsumption: 0,
14
+ nightConsumption: 0,
15
+ consumption: 0
16
+ });
17
+ export const calcMaxConsumptionValue = (data) => Math.max(...data.map((item) => item.consumption));
18
+ export const calcMaxDemand = (data) => calcMaxConsumptionValue(data) * 2;
19
+ export const calcPeakLoad = (consumption, maxDemand, startDate, endDate) => {
20
+ const days = Math.ceil(moment(endDate).diff(moment(startDate), 'days', true));
21
+ return maxDemand === 0 ? 0 : ((consumption / (maxDemand * 24 * days)) * 100);
46
22
  };
47
23
  //# sourceMappingURL=consumption.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"consumption.js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AO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAE9E,OAAO,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,GAAG,CAAC,SAAS,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;AAC/E,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import api from './api.js';
2
- import consumption from './consumption.js';
3
2
  import logger from './logger.js';
4
3
  import db from './db.js';
5
4
  import slack from './slack.js';
6
5
  import * as units from './units.js';
6
+ import * as consumption from './consumption.js';
7
7
  export { api, logger, consumption, db, slack, units };
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import api from './api.js';
2
- import consumption from './consumption.js';
3
2
  import logger from './logger.js';
4
3
  import db from './db.js';
5
4
  import slack from './slack.js';
6
5
  import * as units from './units.js';
6
+ import * as consumption from './consumption.js';
7
7
  export { api, logger, consumption, db, slack, units };
8
8
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,WAAW,MAAM,kBAAkB,CAAC;AAC3C,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAEpC,OAAO,EACL,GAAG,EACH,MAAM,EACN,WAAW,EACX,EAAE,EACF,KAAK,EACL,KAAK,EACN,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AACpC,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC;AAEhD,OAAO,EACL,GAAG,EACH,MAAM,EACN,WAAW,EACX,EAAE,EACF,KAAK,EACL,KAAK,EACN,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etainabl/nodejs-sdk",
3
- "version": "1.1.21",
3
+ "version": "1.1.23",
4
4
  "main": "dist/index.js",
5
5
  "author": "Jonathan Lambert <jonathan@etainabl.com>",
6
6
  "type": "module",
@@ -1,73 +1,34 @@
1
1
  import moment from 'moment';
2
- import { interpolate } from './lib/readingsInterpolate.js';
3
2
 
4
- export interface Reading {
5
- submittedAt: string,
6
- value: number
3
+ interface ConsumptionData {
4
+ date: Date;
5
+ consumption: number;
7
6
  }
8
7
 
9
- export interface ApportionedReading {
10
- date: moment.Moment,
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
- interpolatedReadings = interpolatedReadings.map((reading: ApportionedReading, index: number) => {
36
- const prevReading = interpolatedReadings[index - 1];
37
-
38
- if (prevReading) {
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
- if (endDate) {
54
- interpolatedReadings = interpolatedReadings.filter((reading: ApportionedReading) => moment(reading.date).isSameOrBefore(endDate, granularity));
55
- }
17
+ acc.consumption += item.consumption;
56
18
 
57
- return interpolatedReadings;
58
- };
19
+ return acc;
20
+ }, {
21
+ dayConsumption: 0,
22
+ nightConsumption: 0,
23
+ consumption: 0
24
+ });
59
25
 
60
- const consumptionFromReadings = (readings: Reading[], startDate: string, endDate: string, granularity: moment.unitOfTime.Diff = 'hours', interpolationMethod: string = 'linear') => {
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
- // Calculate the total consumption between the start and end dates
65
- const totalConsumption = apportioned.reduce((acc: any, reading) => acc + reading.consumption, 0);
28
+ export const calcMaxDemand = (data: ConsumptionData[]) => calcMaxConsumptionValue(data) * 2;
66
29
 
67
- return { totalConsumption, apportioned };
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
- export default {
71
- apportionedReadings,
72
- consumptionFromReadings
73
- };
33
+ return maxDemand === 0 ? 0 : ((consumption / (maxDemand * 24 * days)) * 100);
34
+ };
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import api from './api.js';
2
- import consumption from './consumption.js';
3
2
  import logger from './logger.js';
4
3
  import db from './db.js';
5
4
  import slack from './slack.js';
6
5
  import * as units from './units.js';
6
+ import * as consumption from './consumption.js';
7
7
 
8
8
  export {
9
9
  api,