@etainabl/nodejs-sdk 1.1.31 → 1.1.32
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/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/reporting.d.ts +2 -0
- package/dist/reporting.js +52 -0
- package/dist/reporting.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +3 -1
- package/src/reporting.ts +60 -0
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ import slack from './slack.js';
|
|
|
5
5
|
import * as units from './units.js';
|
|
6
6
|
import * as consumption from './consumption.js';
|
|
7
7
|
import * as monitoring from './monitoring.js';
|
|
8
|
-
|
|
8
|
+
import * as reporting from './reporting.js';
|
|
9
|
+
export { api, logger, consumption, monitoring, db, slack, units, reporting };
|
package/dist/index.js
CHANGED
|
@@ -5,5 +5,6 @@ import slack from './slack.js';
|
|
|
5
5
|
import * as units from './units.js';
|
|
6
6
|
import * as consumption from './consumption.js';
|
|
7
7
|
import * as monitoring from './monitoring.js';
|
|
8
|
-
|
|
8
|
+
import * as reporting from './reporting.js';
|
|
9
|
+
export { api, logger, consumption, monitoring, db, slack, units, reporting };
|
|
9
10
|
//# 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,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;AAChD,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAC;
|
|
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;AAChD,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EACL,GAAG,EACH,MAAM,EACN,WAAW,EACX,UAAU,EACV,EAAE,EACF,KAAK,EACL,KAAK,EACL,SAAS,EACV,CAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
moment.locale('en', {
|
|
3
|
+
week: {
|
|
4
|
+
dow: 1
|
|
5
|
+
}
|
|
6
|
+
});
|
|
7
|
+
const getNextRunTime = (startDate, schedule, taskTime) => {
|
|
8
|
+
const [num, freq] = schedule.frequency.split('|');
|
|
9
|
+
const targetDate = moment(startDate).add(num, freq);
|
|
10
|
+
if (schedule.frequencyPeriod === 'first') {
|
|
11
|
+
targetDate.startOf(freq);
|
|
12
|
+
}
|
|
13
|
+
else if (schedule.frequencyPeriod === 'last') {
|
|
14
|
+
targetDate.endOf(freq);
|
|
15
|
+
}
|
|
16
|
+
const isWeekday = targetDate.isoWeekday() > 0 && targetDate.isoWeekday() < 6;
|
|
17
|
+
const isSaturday = targetDate.isoWeekday() === 6;
|
|
18
|
+
// The weekday or weekend chosen should be within the same month as the target date
|
|
19
|
+
if (schedule.frequencyDay === 'weekdays' && !isWeekday) {
|
|
20
|
+
if (targetDate.date() / 7 < 2) {
|
|
21
|
+
targetDate.add(isSaturday ? 2 : 1, 'days');
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
targetDate.subtract(isSaturday ? 1 : 2, 'days');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else if (schedule.frequencyDay === 'weekends' && isWeekday) {
|
|
28
|
+
if (targetDate.date() / 7 < 2) {
|
|
29
|
+
targetDate.isoWeekday(6);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
targetDate.isoWeekday(0);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (taskTime.isAfter(targetDate, 'minute')) {
|
|
36
|
+
return getNextRunTime(targetDate, schedule, taskTime);
|
|
37
|
+
}
|
|
38
|
+
return targetDate;
|
|
39
|
+
};
|
|
40
|
+
export const getScheduledReportRunTimes = (schedule, limit = 1, taskTime = moment()) => {
|
|
41
|
+
if (!schedule.startDate || !schedule.enabled)
|
|
42
|
+
return [];
|
|
43
|
+
const originalStartDate = moment.utc(schedule.startDate);
|
|
44
|
+
let startDate = originalStartDate;
|
|
45
|
+
const runTimes = Array.from(Array(limit).keys()).map(() => {
|
|
46
|
+
const nextRunTime = getNextRunTime(startDate, schedule, taskTime);
|
|
47
|
+
startDate = nextRunTime.hour(originalStartDate.hour()).minute(originalStartDate.minute());
|
|
48
|
+
return nextRunTime;
|
|
49
|
+
});
|
|
50
|
+
return runTimes;
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=reporting.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reporting.js","sourceRoot":"","sources":["../src/reporting.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE;IAClB,IAAI,EAAE;QACJ,GAAG,EAAE,CAAC;KACP;CACF,CAAC,CAAC;AAEH,MAAM,cAAc,GAAG,CAAC,SAAwB,EAAE,QAAa,EAAE,QAAuB,EAAiB,EAAE;IACzG,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAClD,MAAM,UAAU,G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AAC,CAAC;KACvD;IAED,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,QAAa,EAAE,KAAK,GAAG,CAAC,EAAE,QAAQ,GAAG,MAAM,EAAE,EAAmB,EAAE;IAC3G,IAAI,CAAC,QAAQ,CAAC,SAAS,IAAI,CAAC,QAAQ,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IAExD,MAAM,iBAAiB,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;IAEzD,IAAI,SAAS,GAAG,iBAAiB,CAAC;IAElC,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE;QACxD,MAAM,WAAW,GAAG,cAAc,CAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAElE,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,iBAAiB,CAAC,MAAM,EAAE,CAAC,CAAC;QAE1F,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import slack from './slack.js';
|
|
|
5
5
|
import * as units from './units.js';
|
|
6
6
|
import * as consumption from './consumption.js';
|
|
7
7
|
import * as monitoring from './monitoring.js';
|
|
8
|
+
import * as reporting from './reporting.js';
|
|
8
9
|
|
|
9
10
|
export {
|
|
10
11
|
api,
|
|
@@ -13,5 +14,6 @@ export {
|
|
|
13
14
|
monitoring,
|
|
14
15
|
db,
|
|
15
16
|
slack,
|
|
16
|
-
units
|
|
17
|
+
units,
|
|
18
|
+
reporting
|
|
17
19
|
}
|
package/src/reporting.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
|
|
3
|
+
moment.locale('en', {
|
|
4
|
+
week: {
|
|
5
|
+
dow: 1
|
|
6
|
+
}
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const getNextRunTime = (startDate: moment.Moment, schedule: any, taskTime: moment.Moment): moment.Moment => {
|
|
10
|
+
const [num, freq] = schedule.frequency.split('|');
|
|
11
|
+
const targetDate = moment(startDate).add(num, freq as moment.unitOfTime.Base);
|
|
12
|
+
|
|
13
|
+
if (schedule.frequencyPeriod === 'first') {
|
|
14
|
+
targetDate.startOf(freq as moment.unitOfTime.StartOf);
|
|
15
|
+
} else if (schedule.frequencyPeriod === 'last') {
|
|
16
|
+
targetDate.endOf(freq as moment.unitOfTime.StartOf);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const isWeekday = targetDate.isoWeekday() > 0 && targetDate.isoWeekday() < 6;
|
|
20
|
+
const isSaturday = targetDate.isoWeekday() === 6;
|
|
21
|
+
|
|
22
|
+
// The weekday or weekend chosen should be within the same month as the target date
|
|
23
|
+
if (schedule.frequencyDay === 'weekdays' && !isWeekday) {
|
|
24
|
+
if (targetDate.date() / 7 < 2) {
|
|
25
|
+
targetDate.add(isSaturday ? 2 : 1, 'days');
|
|
26
|
+
} else {
|
|
27
|
+
targetDate.subtract(isSaturday ? 1 : 2, 'days');
|
|
28
|
+
}
|
|
29
|
+
} else if (schedule.frequencyDay === 'weekends' && isWeekday) {
|
|
30
|
+
if (targetDate.date() / 7 < 2) {
|
|
31
|
+
targetDate.isoWeekday(6);
|
|
32
|
+
} else {
|
|
33
|
+
targetDate.isoWeekday(0);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (taskTime.isAfter(targetDate, 'minute')) {
|
|
38
|
+
return getNextRunTime(targetDate, schedule, taskTime);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return targetDate;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const getScheduledReportRunTimes = (schedule: any, limit = 1, taskTime = moment()): moment.Moment[] => {
|
|
45
|
+
if (!schedule.startDate || !schedule.enabled) return [];
|
|
46
|
+
|
|
47
|
+
const originalStartDate = moment.utc(schedule.startDate);
|
|
48
|
+
|
|
49
|
+
let startDate = originalStartDate;
|
|
50
|
+
|
|
51
|
+
const runTimes = Array.from(Array(limit).keys()).map(() => {
|
|
52
|
+
const nextRunTime = getNextRunTime(startDate, schedule, taskTime);
|
|
53
|
+
|
|
54
|
+
startDate = nextRunTime.hour(originalStartDate.hour()).minute(originalStartDate.minute());
|
|
55
|
+
|
|
56
|
+
return nextRunTime;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return runTimes;
|
|
60
|
+
};
|