busroot-sdk 0.0.7-alpha.0 → 0.0.7-alpha.10
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/build/index.d.ts +4205 -5
- package/build/index.js +1629 -17
- package/package.json +15 -11
- package/build/client.d.ts +0 -99
- package/build/client.d.ts.map +0 -1
- package/build/client.js +0 -234
- package/build/client.test.d.ts +0 -2
- package/build/client.test.d.ts.map +0 -1
- package/build/client.test.js +0 -6
- package/build/hooks.d.ts +0 -8
- package/build/hooks.d.ts.map +0 -1
- package/build/hooks.js +0 -45
- package/build/index.d.ts.map +0 -1
- package/build/types.d.ts +0 -3145
- package/build/types.d.ts.map +0 -1
- package/build/types.js +0 -796
- package/build/utils.d.ts +0 -3
- package/build/utils.d.ts.map +0 -1
- package/build/utils.js +0 -50
package/build/index.js
CHANGED
|
@@ -1,19 +1,1631 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
10
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// ../../packages/common/build/time.js
|
|
34
|
+
var require_time = __commonJS({
|
|
35
|
+
"../../packages/common/build/time.js"(exports2) {
|
|
36
|
+
"use strict";
|
|
37
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
38
|
+
exports2.Crons = exports2.TimeIntervals = exports2.TimeRange = void 0;
|
|
39
|
+
exports2.getTimeIntervalFromString = getTimeIntervalFromString;
|
|
40
|
+
exports2.millisToSec = millisToSec;
|
|
41
|
+
var zod_1 = require("zod");
|
|
42
|
+
exports2.TimeRange = zod_1.z.object({
|
|
43
|
+
from: zod_1.z.number(),
|
|
44
|
+
to: zod_1.z.number(),
|
|
45
|
+
duration: zod_1.z.number(),
|
|
46
|
+
fromIso: zod_1.z.string(),
|
|
47
|
+
toIso: zod_1.z.string(),
|
|
48
|
+
durationFormatted: zod_1.z.string()
|
|
49
|
+
});
|
|
50
|
+
exports2.TimeIntervals = {
|
|
51
|
+
"1ms": { ms: 1, sec: 1e-3, min: 1 / 6e4, shorthand: "1ms" },
|
|
52
|
+
"10ms": { ms: 10, sec: 0.01, min: 10 / 6e4, shorthand: "10ms" },
|
|
53
|
+
"100ms": { ms: 100, sec: 0.1, min: 100 / 6e4, shorthand: "100ms" },
|
|
54
|
+
"1s": { ms: 1e3, sec: 1, min: 1 / 60, shorthand: "1s" },
|
|
55
|
+
"2s": { ms: 2e3, sec: 2, min: 2 / 60, shorthand: "2s" },
|
|
56
|
+
"5s": { ms: 5e3, sec: 5, min: 5 / 60, shorthand: "5s" },
|
|
57
|
+
"10s": { ms: 1e4, sec: 10, min: 10 / 60, shorthand: "10s" },
|
|
58
|
+
"30s": { ms: 3e4, sec: 30, min: 30 / 60, shorthand: "30s" },
|
|
59
|
+
"1m": { ms: 6e4, sec: 60, min: 1, shorthand: "1m" },
|
|
60
|
+
"2m": { ms: 12e4, sec: 120, min: 2, shorthand: "2m" },
|
|
61
|
+
"5m": { ms: 3e5, sec: 300, min: 5, shorthand: "5m" },
|
|
62
|
+
"10m": { ms: 6e5, sec: 600, min: 10, shorthand: "10m" },
|
|
63
|
+
"15m": { ms: 9e5, sec: 900, min: 15, shorthand: "15m" },
|
|
64
|
+
"30m": { ms: 18e5, sec: 1800, min: 30, shorthand: "30m" },
|
|
65
|
+
"1h": { ms: 36e5, sec: 3600, min: 60, shorthand: "1h" },
|
|
66
|
+
"2h": { ms: 72e5, sec: 7200, min: 120, shorthand: "2h" },
|
|
67
|
+
"6h": { ms: 216e5, sec: 21600, min: 360, shorthand: "6h" },
|
|
68
|
+
"12h": { ms: 432e5, sec: 43200, min: 720, shorthand: "12h" },
|
|
69
|
+
"1d": { ms: 864e5, sec: 86400, min: 1440, shorthand: "1d" },
|
|
70
|
+
"2d": { ms: 1728e5, sec: 172800, min: 2880, shorthand: "2d" },
|
|
71
|
+
"7d": { ms: 6048e5, sec: 604800, min: 10080, shorthand: "7d" },
|
|
72
|
+
"30d": { ms: 2592e6, sec: 2592e3, min: 43200, shorthand: "30d" },
|
|
73
|
+
"60d": { ms: 5184e6, sec: 5184e3, min: 86400, shorthand: "60d" },
|
|
74
|
+
"90d": { ms: 7776e6, sec: 7776e3, min: 129600, shorthand: "90d" },
|
|
75
|
+
"180d": { ms: 7776e6 * 2, sec: 7776e3 * 2, min: 259200, shorthand: "180d" },
|
|
76
|
+
"1y": { ms: 7776e6 * 4, sec: 7776e3 * 4, min: 525600, shorthand: "1y" },
|
|
77
|
+
"2y": { ms: 7776e6 * 8, sec: 7776e3 * 8, min: 1051200, shorthand: "2y" },
|
|
78
|
+
"5y": { ms: 7776e6 * 20, sec: 7776e3 * 20, min: 2628e3, shorthand: "5y" }
|
|
79
|
+
};
|
|
80
|
+
function getTimeIntervalFromString(timeInterval) {
|
|
81
|
+
if (!timeInterval || !Object.prototype.hasOwnProperty.call(exports2.TimeIntervals, timeInterval)) {
|
|
82
|
+
throw new Error(`Invalid time interval: ${timeInterval}`);
|
|
83
|
+
}
|
|
84
|
+
return exports2.TimeIntervals[timeInterval];
|
|
85
|
+
}
|
|
86
|
+
function millisToSec(millis) {
|
|
87
|
+
return millis / 1e3;
|
|
88
|
+
}
|
|
89
|
+
exports2.Crons = {
|
|
90
|
+
every5Seconds: "*/5 * * * * *",
|
|
91
|
+
everyMinute: "* * * * *",
|
|
92
|
+
every5minutes: "*/5 * * * *",
|
|
93
|
+
every10minutes: "*/10 * * * *",
|
|
94
|
+
every15minutes: "*/15 * * * *",
|
|
95
|
+
every30minutes: "*/15 * * * *",
|
|
96
|
+
everyHour: "0 * * * *",
|
|
97
|
+
atMidnight: "0 0 * * *",
|
|
98
|
+
atMidday: "0 12 * * *"
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// ../../packages/common/build/error-key.js
|
|
104
|
+
var require_error_key = __commonJS({
|
|
105
|
+
"../../packages/common/build/error-key.js"(exports2) {
|
|
106
|
+
"use strict";
|
|
107
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
108
|
+
exports2.FORBIDDEN = exports2.INVALID_ACCESS_CODE = exports2.EXPIRED_API_KEY = exports2.UNSUPPORTED_ARCHIVE_LAST_PLANT = exports2.PLANT_NOT_EXISTS = exports2.NOT_EMPTY_STATION_GROUP = exports2.INVALID_AGGREGATE_WINDOW = exports2.DUPLICATED_SCHEDULE = exports2.INVALID_SCHEDULE_ORDER_TYPE = exports2.INVALID_PRODUCTION_MULTIPLIER = exports2.INVALID_SKU_GROUP_NAME = exports2.SKU_GROUP_NOT_FOUND = exports2.INVALID_SKU_GROUP = exports2.INVALID_DOWNTIMNE_DURATION_THRESHOLD_MULTIPLIER = exports2.BOTH_SKU_OR_SKU_GROUP_PROVIDED = exports2.UNSUPPORTED_SCHEDULE_OPERATION = exports2.INVALID_RESOLUTION_MODE = exports2.EXISTED_EVENT = exports2.INVALID_SEARCH_TEXT = exports2.INVALID_SKIP = exports2.INVALID_LIMIT = exports2.INVALID_WINDOW_TIME = exports2.INVALID_NOTIFICATION_PREFERENCE = exports2.OVERLAPPED_SHIFT = exports2.INVALID_SHIFT_NAME = exports2.INVALID_WEEK_DAY = exports2.INVALID_FILE_TYPE = exports2.INVALID_HEADER_FIELDS = exports2.DELETE_LAST_SHIFT_PATTERN = exports2.INVALID_ACTUAL_START_AT_STATE = exports2.INVALID_DOMAIN = exports2.SKU_NOT_FOUND = exports2.STATION_GROUP_NOT_FOUND = exports2.WORK_ORDER_ENDED = exports2.SCHEDULE_NOT_FOUND = exports2.INVALID_SKU = exports2.INVALID_SCHEDULE = exports2.EXPIRED_TOKEN = exports2.UNAUTHORIZED_USER = exports2.UNAUTHENTICATED_USER = exports2.SHIFT_PATTERN_NOT_FOUND = exports2.INVALID_SHIFT_PATTERN_TITLE = exports2.INVALID_SHIFT_PATTERN_ID = exports2.INVALID_DELETE_SHIFTS = exports2.UNSUPPORTED_SHIFT_OVER_MID_NIGHT = exports2.INVALID_SHIFTS = exports2.INVALID_PLANTCODE = exports2.INVALID_RANGECODE = exports2.INTERNAL_SERVER_ERROR = exports2.CACHE_NOT_COMPLETE = void 0;
|
|
109
|
+
exports2.RESET_EXCEED_30_DAYS = exports2.INVALID_REASON_CODE = exports2.SELF_REFERENCING_CODE = exports2.PARENT_CODE_IN_USE = exports2.DUPLICATED_REASON_CODE = exports2.DATABASE_ERROR_MESSAGE = exports2.INVALID_API_KEY = exports2.UNSUPPORTED_API_KEY_OPERATION = exports2.ACCOUNT_DENIED = exports2.ACCOUNT_NOT_FOUND = exports2.ACCOUNT_MISMATCH = exports2.USER_ACCOUNT_IS_BLOCKED = exports2.ACCOUNT_NOT_SELECTED = exports2.INVALID_USER_ACCOUNT = exports2.NO_ACCOUNT_AVAILABLE = exports2.EXPIRED_USER = exports2.USER_IS_BLOCKED = exports2.INVALID_USER = exports2.USER_NOT_FOUND = exports2.MULTIPLE_ACCOUNT_ASSOCIATED = exports2.UNSUPPORTED_USER_OPERATION = exports2.EMAIL_TYPE_NOT_FOUND = exports2.INVALID_STATION_CODE = exports2.DUPLICATED_STATION_CODE = exports2.DUPLICATED_SKU_GROUP_CODE = exports2.DUPLICATED_STATION_GROUP_CODE = exports2.INVALID_INPUT = exports2.DUPLICATED_SKU_CODE = exports2.INVALID_SKU_CODE = exports2.TOO_LARGE = exports2.BUSINESS_CONFLICT = void 0;
|
|
110
|
+
exports2.CACHE_NOT_COMPLETE = "cacheNotComplete";
|
|
111
|
+
exports2.INTERNAL_SERVER_ERROR = "internalServerError";
|
|
112
|
+
exports2.INVALID_RANGECODE = "invalidRangeCode";
|
|
113
|
+
exports2.INVALID_PLANTCODE = "invalidPlantCode";
|
|
114
|
+
exports2.INVALID_SHIFTS = "invalidShifts";
|
|
115
|
+
exports2.UNSUPPORTED_SHIFT_OVER_MID_NIGHT = "unsupportedShiftOverMidNight";
|
|
116
|
+
exports2.INVALID_DELETE_SHIFTS = "invalidDeleteShifts";
|
|
117
|
+
exports2.INVALID_SHIFT_PATTERN_ID = "invalidShiftPatternId";
|
|
118
|
+
exports2.INVALID_SHIFT_PATTERN_TITLE = "invalidShiftPatternTitle";
|
|
119
|
+
exports2.SHIFT_PATTERN_NOT_FOUND = "shiftPatternNotFound";
|
|
120
|
+
exports2.UNAUTHENTICATED_USER = "unauthenticatedUser";
|
|
121
|
+
exports2.UNAUTHORIZED_USER = "unauthorizedUser";
|
|
122
|
+
exports2.EXPIRED_TOKEN = "expiredToken";
|
|
123
|
+
exports2.INVALID_SCHEDULE = "invalidSchedule";
|
|
124
|
+
exports2.INVALID_SKU = "invalidSku";
|
|
125
|
+
exports2.SCHEDULE_NOT_FOUND = "scheduleNotFound";
|
|
126
|
+
exports2.WORK_ORDER_ENDED = "workOrderEnded";
|
|
127
|
+
exports2.STATION_GROUP_NOT_FOUND = "stationGroupNotFound";
|
|
128
|
+
exports2.SKU_NOT_FOUND = "skuNotFound";
|
|
129
|
+
exports2.INVALID_DOMAIN = "invalidDomain";
|
|
130
|
+
exports2.INVALID_ACTUAL_START_AT_STATE = "invalidActualStartAtState";
|
|
131
|
+
exports2.DELETE_LAST_SHIFT_PATTERN = "deleteLastShiftPattern";
|
|
132
|
+
exports2.INVALID_HEADER_FIELDS = "invalidHeaderFields";
|
|
133
|
+
exports2.INVALID_FILE_TYPE = "invalidFileType";
|
|
134
|
+
exports2.INVALID_WEEK_DAY = "invalidWeekday";
|
|
135
|
+
exports2.INVALID_SHIFT_NAME = "invalidShiftName";
|
|
136
|
+
exports2.OVERLAPPED_SHIFT = "overlappedShift";
|
|
137
|
+
exports2.INVALID_NOTIFICATION_PREFERENCE = "invalidNotificationPreference";
|
|
138
|
+
exports2.INVALID_WINDOW_TIME = "invalidWindowTime";
|
|
139
|
+
exports2.INVALID_LIMIT = "invalidLimit";
|
|
140
|
+
exports2.INVALID_SKIP = "invalidSkip";
|
|
141
|
+
exports2.INVALID_SEARCH_TEXT = "invalidSearchText";
|
|
142
|
+
exports2.EXISTED_EVENT = "existedEvent";
|
|
143
|
+
exports2.INVALID_RESOLUTION_MODE = "invalidResolutionMode";
|
|
144
|
+
exports2.UNSUPPORTED_SCHEDULE_OPERATION = "unsupportedScheduleOperation";
|
|
145
|
+
exports2.BOTH_SKU_OR_SKU_GROUP_PROVIDED = "skuCodeAndSkuGroupProvided";
|
|
146
|
+
exports2.INVALID_DOWNTIMNE_DURATION_THRESHOLD_MULTIPLIER = "invalidDowntimeDurationThresholdMultiplier";
|
|
147
|
+
exports2.INVALID_SKU_GROUP = "invalidSkuGroup";
|
|
148
|
+
exports2.SKU_GROUP_NOT_FOUND = "skuGroupNotFound";
|
|
149
|
+
exports2.INVALID_SKU_GROUP_NAME = "invalidSkuGroupName";
|
|
150
|
+
exports2.INVALID_PRODUCTION_MULTIPLIER = "invalidProductionMultiplier";
|
|
151
|
+
exports2.INVALID_SCHEDULE_ORDER_TYPE = "invalidScheduleOrderType";
|
|
152
|
+
exports2.DUPLICATED_SCHEDULE = "duplicatedSchedule";
|
|
153
|
+
exports2.INVALID_AGGREGATE_WINDOW = "invalidAggregateWindow";
|
|
154
|
+
exports2.NOT_EMPTY_STATION_GROUP = "notEmptyStationGroup";
|
|
155
|
+
exports2.PLANT_NOT_EXISTS = "plantNotExists";
|
|
156
|
+
exports2.UNSUPPORTED_ARCHIVE_LAST_PLANT = "unsupportedArchiveLastPlant";
|
|
157
|
+
exports2.EXPIRED_API_KEY = "expiredApiKey";
|
|
158
|
+
exports2.INVALID_ACCESS_CODE = "invalidAccessCode";
|
|
159
|
+
exports2.FORBIDDEN = "forbidden";
|
|
160
|
+
exports2.BUSINESS_CONFLICT = "conflict";
|
|
161
|
+
exports2.TOO_LARGE = "tooLarge";
|
|
162
|
+
exports2.INVALID_SKU_CODE = "invalidSkuCode";
|
|
163
|
+
exports2.DUPLICATED_SKU_CODE = "duplicatedSkuCode";
|
|
164
|
+
exports2.INVALID_INPUT = "invalidInput";
|
|
165
|
+
exports2.DUPLICATED_STATION_GROUP_CODE = "duplicatedStationGroupCode";
|
|
166
|
+
exports2.DUPLICATED_SKU_GROUP_CODE = "duplicatedSkuGroupCode";
|
|
167
|
+
exports2.DUPLICATED_STATION_CODE = "duplicateStationCode";
|
|
168
|
+
exports2.INVALID_STATION_CODE = "invalidStationCode";
|
|
169
|
+
exports2.EMAIL_TYPE_NOT_FOUND = "emailTypeNotFound";
|
|
170
|
+
exports2.UNSUPPORTED_USER_OPERATION = "unsupportedUserOperation";
|
|
171
|
+
exports2.MULTIPLE_ACCOUNT_ASSOCIATED = "multipleAccountAssociated";
|
|
172
|
+
exports2.USER_NOT_FOUND = "userNotFound";
|
|
173
|
+
exports2.INVALID_USER = "invalidUser";
|
|
174
|
+
exports2.USER_IS_BLOCKED = "userIsBlocked";
|
|
175
|
+
exports2.EXPIRED_USER = "expiredUser";
|
|
176
|
+
exports2.NO_ACCOUNT_AVAILABLE = "noAccountAvailable";
|
|
177
|
+
exports2.INVALID_USER_ACCOUNT = "invalidUserAccount";
|
|
178
|
+
exports2.ACCOUNT_NOT_SELECTED = "accountNotSelected";
|
|
179
|
+
exports2.USER_ACCOUNT_IS_BLOCKED = "userAccountIsBlocked";
|
|
180
|
+
exports2.ACCOUNT_MISMATCH = "accountMismatch";
|
|
181
|
+
exports2.ACCOUNT_NOT_FOUND = "accountNotFound";
|
|
182
|
+
exports2.ACCOUNT_DENIED = "accountDenied";
|
|
183
|
+
exports2.UNSUPPORTED_API_KEY_OPERATION = "unsupportedApiTokenOperation";
|
|
184
|
+
exports2.INVALID_API_KEY = "invalidApiKey";
|
|
185
|
+
exports2.DATABASE_ERROR_MESSAGE = {
|
|
186
|
+
"23505": "Duplicate entry. The value you are trying to add already exists.",
|
|
187
|
+
"23503": "Invalid reference. You are trying to use a non-existent foreign key.",
|
|
188
|
+
"23502": "Null violation. A required field cannot be empty."
|
|
189
|
+
};
|
|
190
|
+
exports2.DUPLICATED_REASON_CODE = "duplicatedReasonCode";
|
|
191
|
+
exports2.PARENT_CODE_IN_USE = "parentCodeInUse";
|
|
192
|
+
exports2.SELF_REFERENCING_CODE = "selfReferencingCode";
|
|
193
|
+
exports2.INVALID_REASON_CODE = "invalidReasonCode";
|
|
194
|
+
exports2.RESET_EXCEED_30_DAYS = "resetExceed30Days";
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// ../../packages/common/build/utils/times.js
|
|
199
|
+
var require_times = __commonJS({
|
|
200
|
+
"../../packages/common/build/utils/times.js"(exports2) {
|
|
201
|
+
"use strict";
|
|
202
|
+
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
203
|
+
if (k2 === void 0) k2 = k;
|
|
204
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
205
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
206
|
+
desc = { enumerable: true, get: function() {
|
|
207
|
+
return m[k];
|
|
208
|
+
} };
|
|
209
|
+
}
|
|
210
|
+
Object.defineProperty(o, k2, desc);
|
|
211
|
+
}) : (function(o, m, k, k2) {
|
|
212
|
+
if (k2 === void 0) k2 = k;
|
|
213
|
+
o[k2] = m[k];
|
|
214
|
+
}));
|
|
215
|
+
var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
216
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
217
|
+
}) : function(o, v) {
|
|
218
|
+
o["default"] = v;
|
|
219
|
+
});
|
|
220
|
+
var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
|
|
221
|
+
var ownKeys = function(o) {
|
|
222
|
+
ownKeys = Object.getOwnPropertyNames || function(o2) {
|
|
223
|
+
var ar = [];
|
|
224
|
+
for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
|
|
225
|
+
return ar;
|
|
226
|
+
};
|
|
227
|
+
return ownKeys(o);
|
|
228
|
+
};
|
|
229
|
+
return function(mod) {
|
|
230
|
+
if (mod && mod.__esModule) return mod;
|
|
231
|
+
var result = {};
|
|
232
|
+
if (mod != null) {
|
|
233
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
234
|
+
}
|
|
235
|
+
__setModuleDefault(result, mod);
|
|
236
|
+
return result;
|
|
237
|
+
};
|
|
238
|
+
})();
|
|
239
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
240
|
+
exports2.getDurationAsHumanize = exports2.getDuration = exports2.hourToMoment = exports2.convertToUniversal = exports2.AcceptedDateTimeFormats = exports2.getOverlappedTimeRange = exports2.intersectTimeWindowsIso = exports2.intersectTimeWindows = exports2.intersectTimeRanges = exports2.getCoverageRanges = exports2.convertNumberToMomentTime = exports2.getTimesBetweenRangeTimes = exports2.formattedDurationShort = exports2.formattedDuration = exports2.RelativeRanges = exports2.SUPPORTED_DURATION = exports2.RangeType = void 0;
|
|
241
|
+
exports2.getAllowedAggregationWindowsForDuration = getAllowedAggregationWindowsForDuration;
|
|
242
|
+
exports2.getTimestampFromString = getTimestampFromString;
|
|
243
|
+
exports2.getTimestampInSecond = getTimestampInSecond;
|
|
244
|
+
exports2.formattedDateTime = formattedDateTime2;
|
|
245
|
+
exports2.formattedRelativeTime = formattedRelativeTime;
|
|
246
|
+
exports2.timestampsToTimeRange = timestampsToTimeRange;
|
|
247
|
+
exports2.padTimeRange = padTimeRange;
|
|
248
|
+
exports2.padStartTimeRange = padStartTimeRange;
|
|
249
|
+
exports2.padEndTimeRange = padEndTimeRange;
|
|
250
|
+
exports2.getApiTimeByDayOffset = getApiTimeByDayOffset;
|
|
251
|
+
exports2.addHourToSpecificHour = addHourToSpecificHour;
|
|
252
|
+
exports2.getRangeFromSpecificHour = getRangeFromSpecificHour;
|
|
253
|
+
exports2.toSpecificHourString = toSpecificHourString;
|
|
254
|
+
exports2.fromSpecificHourString = fromSpecificHourString;
|
|
255
|
+
exports2.getSpecificHourAndCountFromRangeCode = getSpecificHourAndCountFromRangeCode;
|
|
256
|
+
exports2.getTimeRangeFromRangeCode = getTimeRangeFromRangeCode;
|
|
257
|
+
exports2.getCoverage = getCoverage;
|
|
258
|
+
exports2.getWindowAtTime = getWindowAtTime;
|
|
259
|
+
exports2.getWeeksInQuarter = getWeeksInQuarter;
|
|
260
|
+
exports2.getQuarterFromWeekNo = getQuarterFromWeekNo;
|
|
261
|
+
exports2.getDaysInWeek = getDaysInWeek;
|
|
262
|
+
exports2.getComponentsFromDayRangeCode = getComponentsFromDayRangeCode;
|
|
263
|
+
exports2.getComponentsFromWeekRangeCode = getComponentsFromWeekRangeCode;
|
|
264
|
+
exports2.getComponentsFromQuarterRangeCode = getComponentsFromQuarterRangeCode;
|
|
265
|
+
exports2.getCurrentDayRangeCode = getCurrentDayRangeCode;
|
|
266
|
+
exports2.getDayRangeCode = getDayRangeCode;
|
|
267
|
+
exports2.getCurrentWeekRangeCode = getCurrentWeekRangeCode;
|
|
268
|
+
exports2.generateTimeWindows = generateTimeWindows;
|
|
269
|
+
exports2.BusrootDateTimeParse = BusrootDateTimeParse;
|
|
270
|
+
exports2.decimalHoursToTime = decimalHoursToTime;
|
|
271
|
+
var moment_timezone_1 = __importStar(require("moment-timezone"));
|
|
272
|
+
var time_1 = require_time();
|
|
273
|
+
var error_key_1 = require_error_key();
|
|
274
|
+
var RangeType;
|
|
275
|
+
(function(RangeType2) {
|
|
276
|
+
RangeType2["MINUTE"] = "minute";
|
|
277
|
+
RangeType2["HOUR"] = "hour";
|
|
278
|
+
RangeType2["TODAY"] = "today";
|
|
279
|
+
RangeType2["YESTERDAY"] = "yesterday";
|
|
280
|
+
RangeType2["DAY"] = "day";
|
|
281
|
+
RangeType2["WEEK"] = "week";
|
|
282
|
+
RangeType2["MONTH"] = "month";
|
|
283
|
+
RangeType2["QUARTER"] = "quarter";
|
|
284
|
+
RangeType2["YEAR"] = "year";
|
|
285
|
+
RangeType2["RELATIVE"] = "relative";
|
|
286
|
+
RangeType2["CUSTOM"] = "custom";
|
|
287
|
+
})(RangeType || (exports2.RangeType = RangeType = {}));
|
|
288
|
+
exports2.SUPPORTED_DURATION = ["months", "days", "hours", "minutes"];
|
|
289
|
+
exports2.RelativeRanges = [
|
|
290
|
+
{
|
|
291
|
+
rangeCode: "RELATIVE_HOURS_1",
|
|
292
|
+
label: "Last 1 Hour",
|
|
293
|
+
durationMs: time_1.TimeIntervals["1h"].ms
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
rangeCode: "RELATIVE_HOURS_4",
|
|
297
|
+
label: "Last 4 Hours",
|
|
298
|
+
durationMs: time_1.TimeIntervals["1h"].ms * 4
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
rangeCode: "RELATIVE_HOURS_6",
|
|
302
|
+
label: "Last 6 Hours",
|
|
303
|
+
durationMs: time_1.TimeIntervals["1h"].ms * 6
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
rangeCode: "RELATIVE_HOURS_12",
|
|
307
|
+
label: "Last 12 Hours",
|
|
308
|
+
durationMs: time_1.TimeIntervals["12h"].ms
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
rangeCode: "RELATIVE_HOURS_24",
|
|
312
|
+
label: "Last 24 Hours",
|
|
313
|
+
durationMs: time_1.TimeIntervals["1d"].ms
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
rangeCode: "RELATIVE_HOURS_48",
|
|
317
|
+
label: "Last 48 Hours",
|
|
318
|
+
durationMs: time_1.TimeIntervals["1d"].ms * 2
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
rangeCode: "RELATIVE_HOURS_72",
|
|
322
|
+
label: "Last 72 Hours",
|
|
323
|
+
durationMs: time_1.TimeIntervals["1d"].ms * 3
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
rangeCode: "RELATIVE_DAYS_7",
|
|
327
|
+
label: "Last 7 Days",
|
|
328
|
+
durationMs: time_1.TimeIntervals["7d"].ms
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
rangeCode: "RELATIVE_DAYS_28",
|
|
332
|
+
label: "Last 4 Weeks",
|
|
333
|
+
durationMs: time_1.TimeIntervals["7d"].ms * 4
|
|
334
|
+
}
|
|
335
|
+
// {
|
|
336
|
+
// rangeCode: "RELATIVE_DAYS_84",
|
|
337
|
+
// label: "12 Weeks",
|
|
338
|
+
// durationMs: TimeIntervals["7d"].ms * 12,
|
|
339
|
+
// },
|
|
340
|
+
// {
|
|
341
|
+
// rangeCode: "RELATIVE_DAYS_168",
|
|
342
|
+
// label: "24 Weeks",
|
|
343
|
+
// durationMs: TimeIntervals["7d"].ms * 24,
|
|
344
|
+
// },
|
|
345
|
+
];
|
|
346
|
+
var AllowedAggregationWindows = [
|
|
347
|
+
// 1 Hour
|
|
348
|
+
{
|
|
349
|
+
durationMs: time_1.TimeIntervals["1h"].ms,
|
|
350
|
+
allowedAggregationWindows: ["1m", "5m"]
|
|
351
|
+
},
|
|
352
|
+
// 12 Hours
|
|
353
|
+
{
|
|
354
|
+
durationMs: time_1.TimeIntervals["12h"].ms,
|
|
355
|
+
allowedAggregationWindows: ["5m", "15m", "1h"]
|
|
356
|
+
},
|
|
357
|
+
// 1 Day
|
|
358
|
+
{
|
|
359
|
+
durationMs: time_1.TimeIntervals["1d"].ms,
|
|
360
|
+
allowedAggregationWindows: ["5m", "15m", "30m", "1h"]
|
|
361
|
+
},
|
|
362
|
+
// 1 Week
|
|
363
|
+
{
|
|
364
|
+
durationMs: time_1.TimeIntervals["7d"].ms,
|
|
365
|
+
allowedAggregationWindows: ["1h", "1d"]
|
|
366
|
+
},
|
|
367
|
+
// 4 Weeks (about 1 Month)
|
|
368
|
+
{
|
|
369
|
+
durationMs: time_1.TimeIntervals["7d"].ms * 4,
|
|
370
|
+
allowedAggregationWindows: ["1d"]
|
|
371
|
+
},
|
|
372
|
+
// 12 Weeks (about 3 Months)
|
|
373
|
+
{
|
|
374
|
+
durationMs: time_1.TimeIntervals["7d"].ms * 12,
|
|
375
|
+
allowedAggregationWindows: ["1d", "7d"]
|
|
376
|
+
},
|
|
377
|
+
// 24 Weeks (about 6 months)
|
|
378
|
+
{
|
|
379
|
+
durationMs: time_1.TimeIntervals["7d"].ms * 24,
|
|
380
|
+
allowedAggregationWindows: ["1d", "7d"]
|
|
381
|
+
}
|
|
382
|
+
];
|
|
383
|
+
function getAllowedAggregationWindowsForDuration(durationMs) {
|
|
384
|
+
const allowedAggregationWindows = AllowedAggregationWindows.find((a) => durationMs <= a.durationMs)?.allowedAggregationWindows;
|
|
385
|
+
return allowedAggregationWindows;
|
|
386
|
+
}
|
|
387
|
+
function getTimestampFromString(timeString) {
|
|
388
|
+
let unixTimestamp = Number(timeString);
|
|
389
|
+
if (!isNaN(unixTimestamp)) {
|
|
390
|
+
return unixTimestamp;
|
|
391
|
+
}
|
|
392
|
+
timeString = timeString.trim();
|
|
393
|
+
timeString = timeString.replace(/\//g, "-");
|
|
394
|
+
timeString = timeString.replace(/T/g, " ");
|
|
395
|
+
const validFormats = [
|
|
396
|
+
"YYYY-MM-DD",
|
|
397
|
+
"YYYY-MM-DD HH:mm",
|
|
398
|
+
"YYYY-MM-DD HH:mm:ss",
|
|
399
|
+
"YYYY-MM-DD HH:mm:ss.SSS",
|
|
400
|
+
"YYYY-MM-DD HH:mm:ss.SSSSSS",
|
|
401
|
+
"YYYY-MM-DD HH:mm:ss.SSSSSSSSS"
|
|
402
|
+
];
|
|
403
|
+
unixTimestamp = moment_timezone_1.default.utc(timeString, validFormats, true).valueOf();
|
|
404
|
+
if (!isNaN(unixTimestamp)) {
|
|
405
|
+
return unixTimestamp;
|
|
406
|
+
}
|
|
407
|
+
return null;
|
|
408
|
+
}
|
|
409
|
+
function getTimestampInSecond(unixTimestamp) {
|
|
410
|
+
return (0, moment_timezone_1.default)(unixTimestamp).unix();
|
|
411
|
+
}
|
|
412
|
+
var formattedDuration2 = (millis, roundTo, isShort = false, includeSeconds, includeMillis) => {
|
|
413
|
+
if (!millis) {
|
|
414
|
+
return "0sec";
|
|
415
|
+
}
|
|
416
|
+
if (!roundTo) {
|
|
417
|
+
roundTo = "months";
|
|
418
|
+
}
|
|
419
|
+
let units = ["months", "days", "hours", "minutes"];
|
|
420
|
+
let i = 0;
|
|
421
|
+
for (i = 0; i < units.length; i++) {
|
|
422
|
+
if (units[i] === roundTo) {
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
units = units.splice(i, units.length);
|
|
427
|
+
let durationAsMilis = Math.floor(moment_timezone_1.default.duration(millis).asMilliseconds());
|
|
428
|
+
let durationString = "";
|
|
429
|
+
if (includeSeconds) {
|
|
430
|
+
units.push("seconds");
|
|
431
|
+
}
|
|
432
|
+
if (includeMillis) {
|
|
433
|
+
units = units.filter((s) => s !== "seconds");
|
|
434
|
+
units.push("seconds");
|
|
435
|
+
units.push("millis");
|
|
436
|
+
}
|
|
437
|
+
units.forEach((u) => {
|
|
438
|
+
if (u === "months") {
|
|
439
|
+
const month = Math.floor(durationAsMilis / time_1.TimeIntervals["30d"].ms);
|
|
440
|
+
durationAsMilis = durationAsMilis - month * time_1.TimeIntervals["30d"].ms;
|
|
441
|
+
if (month) {
|
|
442
|
+
durationString += month + (isShort ? "M " : "mth ");
|
|
443
|
+
}
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (u === "days") {
|
|
447
|
+
const days = Math.floor(durationAsMilis / time_1.TimeIntervals["1d"].ms);
|
|
448
|
+
durationAsMilis = durationAsMilis - days * time_1.TimeIntervals["1d"].ms;
|
|
449
|
+
if (days) {
|
|
450
|
+
durationString += days + (isShort ? "D " : "d ");
|
|
451
|
+
}
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (u === "hours") {
|
|
455
|
+
const hours = Math.floor(durationAsMilis / time_1.TimeIntervals["1h"].ms);
|
|
456
|
+
durationAsMilis = durationAsMilis - hours * time_1.TimeIntervals["1h"].ms;
|
|
457
|
+
if (hours) {
|
|
458
|
+
durationString += hours + (isShort ? "H " : "hr ");
|
|
459
|
+
}
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (u === "minutes") {
|
|
463
|
+
const minutes = Math.floor(durationAsMilis / time_1.TimeIntervals["1m"].ms);
|
|
464
|
+
durationAsMilis = durationAsMilis - minutes * time_1.TimeIntervals["1m"].ms;
|
|
465
|
+
if (minutes) {
|
|
466
|
+
durationString += minutes + (isShort ? "m " : "min ");
|
|
467
|
+
}
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (u === "seconds") {
|
|
471
|
+
const seconds = Math.floor(durationAsMilis / time_1.TimeIntervals["1s"].ms);
|
|
472
|
+
durationAsMilis = durationAsMilis - seconds * time_1.TimeIntervals["1s"].ms;
|
|
473
|
+
if (seconds) {
|
|
474
|
+
durationString += seconds + (isShort ? "s" : "sec ");
|
|
475
|
+
}
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
if (u === "millis") {
|
|
479
|
+
if (durationAsMilis) {
|
|
480
|
+
durationString += durationAsMilis + (isShort ? "ms" : "ms");
|
|
481
|
+
}
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
return durationString.trim();
|
|
486
|
+
};
|
|
487
|
+
exports2.formattedDuration = formattedDuration2;
|
|
488
|
+
function formattedDateTime2(input, timezone = "Europe/London", includeFromNow = false, roundTo) {
|
|
489
|
+
if (input == null) {
|
|
490
|
+
return void 0;
|
|
491
|
+
}
|
|
492
|
+
const inputMoment = moment_timezone_1.default.utc(input).tz(timezone);
|
|
493
|
+
const dateTimeString = inputMoment.format("HH:mm | DD-MM-YYYY");
|
|
494
|
+
if (includeFromNow) {
|
|
495
|
+
const diff = inputMoment.diff((0, moment_timezone_1.default)()).valueOf();
|
|
496
|
+
if (diff > 0) {
|
|
497
|
+
return dateTimeString + " (in " + (0, exports2.formattedDuration)(diff, roundTo) + ")";
|
|
498
|
+
} else {
|
|
499
|
+
return dateTimeString + " (" + (0, exports2.formattedDuration)(-diff, roundTo) + " ago)";
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return dateTimeString;
|
|
503
|
+
}
|
|
504
|
+
function formattedRelativeTime(input, timezone = "Europe/London", roundTo) {
|
|
505
|
+
if (input == null) {
|
|
506
|
+
return void 0;
|
|
507
|
+
}
|
|
508
|
+
const inputMoment = moment_timezone_1.default.utc(input).tz(timezone).valueOf();
|
|
509
|
+
const nowMoment = moment_timezone_1.default.utc().tz(timezone).valueOf();
|
|
510
|
+
const diff = nowMoment - inputMoment;
|
|
511
|
+
if (Math.abs(diff) < 12e4) {
|
|
512
|
+
return "just now...";
|
|
513
|
+
} else if (diff > 0) {
|
|
514
|
+
return (0, exports2.formattedDuration)(diff, roundTo) + " ago...";
|
|
515
|
+
} else {
|
|
516
|
+
return "...in " + (0, exports2.formattedDuration)(-diff, roundTo);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
var formattedDurationShort = (millis, roundTo) => {
|
|
520
|
+
if (!millis) {
|
|
521
|
+
return "00:00";
|
|
522
|
+
}
|
|
523
|
+
const duration = moment_timezone_1.default.duration(millis);
|
|
524
|
+
duration.add(-duration.milliseconds(), "millisecond");
|
|
525
|
+
if (roundTo === "minutes") {
|
|
526
|
+
const min = duration.minutes();
|
|
527
|
+
if (Math.abs(min) > 0) {
|
|
528
|
+
duration.add(-duration.seconds(), "seconds");
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return `${duration.days().toString().padStart(2, "0")} ${duration.hours().toString().padStart(2, "0")}:${duration.minutes().toString().padStart(2, "0")}:00`;
|
|
532
|
+
};
|
|
533
|
+
exports2.formattedDurationShort = formattedDurationShort;
|
|
534
|
+
function timestampsToTimeRange(range) {
|
|
535
|
+
return {
|
|
536
|
+
from: range.from,
|
|
537
|
+
to: range.to,
|
|
538
|
+
duration: range.to - range.from,
|
|
539
|
+
fromIso: (0, moment_timezone_1.default)(range.from).toISOString(),
|
|
540
|
+
toIso: (0, moment_timezone_1.default)(range.to).toISOString(),
|
|
541
|
+
durationFormatted: (0, exports2.formattedDuration)(range.to - range.from, void 0, void 0, true, false)
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
function padTimeRange(timeRange, amountInMilliseconds) {
|
|
545
|
+
timeRange.from -= amountInMilliseconds;
|
|
546
|
+
timeRange.to += amountInMilliseconds;
|
|
547
|
+
return timestampsToTimeRange(timeRange);
|
|
548
|
+
}
|
|
549
|
+
function padStartTimeRange(timeRange, amountInMilliseconds) {
|
|
550
|
+
timeRange.from -= amountInMilliseconds;
|
|
551
|
+
return timestampsToTimeRange(timeRange);
|
|
552
|
+
}
|
|
553
|
+
function padEndTimeRange(timeRange, amountInMilliseconds) {
|
|
554
|
+
timeRange.to += amountInMilliseconds;
|
|
555
|
+
return timestampsToTimeRange(timeRange);
|
|
556
|
+
}
|
|
557
|
+
function getApiTimeByDayOffset(timezone, dayOffset) {
|
|
558
|
+
const now = moment_timezone_1.default.tz(timezone).valueOf();
|
|
559
|
+
return timestampsToTimeRange({
|
|
560
|
+
from: moment_timezone_1.default.tz(timezone).add(dayOffset, "day").hour(0).minute(0).second(0).millisecond(0).valueOf(),
|
|
561
|
+
to: Math.min(now, moment_timezone_1.default.tz(timezone).add(dayOffset + 1, "day").hour(0).minute(0).second(0).millisecond(0).valueOf())
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
function addHourToSpecificHour(specificHour, hoursToAdd) {
|
|
565
|
+
const m = moment_timezone_1.default.utc({
|
|
566
|
+
year: specificHour.year,
|
|
567
|
+
month: specificHour.month - 1,
|
|
568
|
+
day: specificHour.day,
|
|
569
|
+
hour: specificHour.hour,
|
|
570
|
+
minute: specificHour.minute || 0
|
|
571
|
+
});
|
|
572
|
+
m.add(hoursToAdd, "hours");
|
|
573
|
+
return { year: m.year(), month: m.month() + 1, day: m.date(), hour: m.hour(), minute: m.minute() };
|
|
574
|
+
}
|
|
575
|
+
function getRangeFromSpecificHour(specificHour, hourCount = 1) {
|
|
576
|
+
const m = moment_timezone_1.default.utc({ year: specificHour.year, month: specificHour.month - 1, day: specificHour.day, hour: specificHour.hour });
|
|
577
|
+
if (specificHour.minute) {
|
|
578
|
+
m.add(specificHour.minute, "minutes");
|
|
579
|
+
}
|
|
580
|
+
return timestampsToTimeRange({ from: m.valueOf(), to: m.add(hourCount, "hour").valueOf() });
|
|
581
|
+
}
|
|
582
|
+
function toSpecificHourString(specificHour) {
|
|
583
|
+
return `${specificHour.year}_${specificHour.month}_${specificHour.day}_${specificHour.hour}`;
|
|
584
|
+
}
|
|
585
|
+
function fromSpecificHourString(input) {
|
|
586
|
+
if (!input || !input.includes("_")) {
|
|
587
|
+
return null;
|
|
588
|
+
}
|
|
589
|
+
const parts = input.split("_");
|
|
590
|
+
return {
|
|
591
|
+
year: parseInt(parts[0]),
|
|
592
|
+
month: parseInt(parts[1]),
|
|
593
|
+
day: parseInt(parts[2]),
|
|
594
|
+
hour: parseInt(parts[3])
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
function getSpecificHourAndCountFromRangeCode(rangeCode, plant) {
|
|
598
|
+
const parts = rangeCode.split("_");
|
|
599
|
+
const rangeType = parts[0].toLowerCase();
|
|
600
|
+
if (rangeType === RangeType.TODAY) {
|
|
601
|
+
const day = moment_timezone_1.default.tz(plant.timezone).startOf("day");
|
|
602
|
+
day.add(plant.dayStartHour, "hour");
|
|
603
|
+
day.utc();
|
|
604
|
+
return {
|
|
605
|
+
specificHour: {
|
|
606
|
+
year: day.year(),
|
|
607
|
+
month: day.month() + 1,
|
|
608
|
+
day: day.date(),
|
|
609
|
+
hour: day.hour(),
|
|
610
|
+
minute: day.minute()
|
|
611
|
+
},
|
|
612
|
+
hourCount: 24
|
|
613
|
+
};
|
|
614
|
+
} else if (rangeType === RangeType.YESTERDAY) {
|
|
615
|
+
const day = moment_timezone_1.default.tz(plant.timezone).startOf("day").add(-1, "day");
|
|
616
|
+
day.add(plant.dayStartHour, "hour");
|
|
617
|
+
day.utc();
|
|
618
|
+
return {
|
|
619
|
+
specificHour: {
|
|
620
|
+
year: day.year(),
|
|
621
|
+
month: day.month() + 1,
|
|
622
|
+
day: day.date(),
|
|
623
|
+
hour: day.hour(),
|
|
624
|
+
minute: day.minute()
|
|
625
|
+
},
|
|
626
|
+
hourCount: 24
|
|
627
|
+
};
|
|
628
|
+
} else if (rangeType === RangeType.DAY) {
|
|
629
|
+
const day = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}_${parseInt(parts[3])}`, "YYYY_MM_DD", plant.timezone);
|
|
630
|
+
day.add(plant.dayStartHour, "hour");
|
|
631
|
+
day.utc();
|
|
632
|
+
return {
|
|
633
|
+
specificHour: {
|
|
634
|
+
year: day.year(),
|
|
635
|
+
month: day.month() + 1,
|
|
636
|
+
day: day.date(),
|
|
637
|
+
hour: day.hour(),
|
|
638
|
+
minute: day.minute()
|
|
639
|
+
},
|
|
640
|
+
hourCount: 24
|
|
641
|
+
};
|
|
642
|
+
} else if (rangeType === RangeType.WEEK) {
|
|
643
|
+
const week = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}}`, "YYYY_WW", plant.timezone).startOf("isoWeek");
|
|
644
|
+
week.add(plant.dayStartHour, "hour");
|
|
645
|
+
week.utc();
|
|
646
|
+
return {
|
|
647
|
+
specificHour: {
|
|
648
|
+
year: week.year(),
|
|
649
|
+
month: week.month() + 1,
|
|
650
|
+
day: week.date(),
|
|
651
|
+
hour: plant.dayStartHour,
|
|
652
|
+
minute: 0
|
|
653
|
+
},
|
|
654
|
+
hourCount: 24 * 7
|
|
655
|
+
};
|
|
656
|
+
} else if (rangeType === RangeType.MONTH) {
|
|
657
|
+
const month = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}}`, "YYYY_MM", plant.timezone).startOf("month");
|
|
658
|
+
month.add(plant.dayStartHour, "hour");
|
|
659
|
+
month.utc();
|
|
660
|
+
return {
|
|
661
|
+
specificHour: {
|
|
662
|
+
year: month.year(),
|
|
663
|
+
month: month.month() + 1,
|
|
664
|
+
day: month.date(),
|
|
665
|
+
hour: month.hour(),
|
|
666
|
+
minute: month.minute()
|
|
667
|
+
},
|
|
668
|
+
hourCount: month.daysInMonth() * 24
|
|
669
|
+
};
|
|
670
|
+
} else if (rangeType === RangeType.RELATIVE) {
|
|
671
|
+
const value = parseInt(parts[2]);
|
|
672
|
+
const unit = parts[1].toLowerCase();
|
|
673
|
+
const allowedUnits = [
|
|
674
|
+
{
|
|
675
|
+
label: "hours",
|
|
676
|
+
value: 24
|
|
677
|
+
},
|
|
678
|
+
{
|
|
679
|
+
label: "days",
|
|
680
|
+
value: 7
|
|
681
|
+
},
|
|
682
|
+
{
|
|
683
|
+
label: "days",
|
|
684
|
+
value: 14
|
|
685
|
+
},
|
|
686
|
+
{
|
|
687
|
+
label: "days",
|
|
688
|
+
value: 30
|
|
689
|
+
}
|
|
690
|
+
];
|
|
691
|
+
if (allowedUnits.find((a) => a.label === unit && a.value === value)) {
|
|
692
|
+
const timestamp = (0, moment_timezone_1.default)().tz(plant.timezone).subtract(value, unit === "hours" ? "h" : "d");
|
|
693
|
+
timestamp.utc();
|
|
694
|
+
return {
|
|
695
|
+
specificHour: {
|
|
696
|
+
year: timestamp.year(),
|
|
697
|
+
month: timestamp.month() + 1,
|
|
698
|
+
day: timestamp.date(),
|
|
699
|
+
hour: timestamp.hour(),
|
|
700
|
+
minute: timestamp.minute()
|
|
701
|
+
},
|
|
702
|
+
hourCount: unit === "hours" ? value : value * 24
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
throw new Error("invalidRangeCode");
|
|
707
|
+
}
|
|
708
|
+
function getTimeRangeFromRangeCode(range, plant, aggregateWindow, now) {
|
|
709
|
+
if (range.custom != null && range.custom.from != null && range.custom.to != null) {
|
|
710
|
+
let fromTimestamp = range.custom.from;
|
|
711
|
+
let toTimestamp = range.custom.to;
|
|
712
|
+
const fromTimestampMomentStartOfMinute = (0, moment_timezone_1.default)(fromTimestamp).tz(plant?.timezone || "UTC").startOf("minute");
|
|
713
|
+
const toTimestampMomentStartOfMinute = (0, moment_timezone_1.default)(toTimestamp).tz(plant?.timezone || "UTC").startOf("minute");
|
|
714
|
+
const currentFromTimestampMinute = fromTimestampMomentStartOfMinute.minute();
|
|
715
|
+
const currentToTimestampMinute = toTimestampMomentStartOfMinute.minute();
|
|
716
|
+
if (aggregateWindow === "5m") {
|
|
717
|
+
fromTimestamp = fromTimestampMomentStartOfMinute.add(currentFromTimestampMinute % 5 + 1, "minutes").valueOf();
|
|
718
|
+
toTimestamp = toTimestampMomentStartOfMinute.add(currentToTimestampMinute % 5 + 1, "minutes").valueOf();
|
|
719
|
+
} else if (aggregateWindow === "15m") {
|
|
720
|
+
fromTimestamp = fromTimestampMomentStartOfMinute.add(currentFromTimestampMinute % 15 + 1, "minutes").valueOf();
|
|
721
|
+
toTimestamp = toTimestampMomentStartOfMinute.add(currentToTimestampMinute % 15 + 1, "minutes").valueOf();
|
|
722
|
+
} else if (aggregateWindow === "30m") {
|
|
723
|
+
fromTimestamp = fromTimestampMomentStartOfMinute.add(currentFromTimestampMinute % 30 + 1, "minutes").valueOf();
|
|
724
|
+
toTimestamp = toTimestampMomentStartOfMinute.add(currentToTimestampMinute % 30 + 1, "minutes").valueOf();
|
|
725
|
+
} else if (aggregateWindow === "1h") {
|
|
726
|
+
fromTimestamp = fromTimestampMomentStartOfMinute.clone().startOf("hour").add(1, "hour").valueOf();
|
|
727
|
+
toTimestamp = toTimestampMomentStartOfMinute.clone().startOf("hour").add(1, "hour").valueOf();
|
|
728
|
+
} else if (aggregateWindow === "1d") {
|
|
729
|
+
fromTimestamp = fromTimestampMomentStartOfMinute.clone().startOf("day").add(1, "day").valueOf();
|
|
730
|
+
toTimestamp = toTimestampMomentStartOfMinute.clone().startOf("day").add(1, "day").valueOf();
|
|
731
|
+
} else if (aggregateWindow === "7d") {
|
|
732
|
+
fromTimestamp = fromTimestampMomentStartOfMinute.clone().startOf("isoWeek").add(1, "week").valueOf();
|
|
733
|
+
toTimestamp = toTimestampMomentStartOfMinute.clone().startOf("isoWeek").add(1, "week").valueOf();
|
|
734
|
+
}
|
|
735
|
+
return timestampsToTimeRange({ from: fromTimestamp, to: toTimestamp });
|
|
736
|
+
}
|
|
737
|
+
if (range.code == null) {
|
|
738
|
+
throw new Error("Either Range Code or custom Time Range required");
|
|
739
|
+
}
|
|
740
|
+
if (!plant || plant.dayStartHour != null && plant.dayStartHour % 1 !== 0) {
|
|
741
|
+
throw new Error("Plant day start hour must be a whole hour. This is because of the way Busroot's caching system works.");
|
|
742
|
+
}
|
|
743
|
+
const parts = range.code.split("_");
|
|
744
|
+
const handlers = {
|
|
745
|
+
minute: () => {
|
|
746
|
+
const day = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}_${parseInt(parts[3])}_${parseInt(parts[4])}_${parseInt(parts[5])}`, "YYYY_MM_DD_HH_mm", plant.timezone);
|
|
747
|
+
day.utc();
|
|
748
|
+
return timestampsToTimeRange({ from: day.valueOf(), to: day.add(1, "minute").valueOf() });
|
|
749
|
+
},
|
|
750
|
+
hour: () => {
|
|
751
|
+
const day = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}_${parseInt(parts[3])}_${parseInt(parts[4])}`, "YYYY_MM_DD_HH", plant.timezone);
|
|
752
|
+
day.utc();
|
|
753
|
+
return timestampsToTimeRange({ from: day.valueOf(), to: day.add(1, "hours").valueOf() });
|
|
754
|
+
},
|
|
755
|
+
today: () => {
|
|
756
|
+
const day = (0, moment_timezone_1.default)(now).tz(plant.timezone).add(-plant.dayStartHour, "hour").startOf("day").add(plant.dayStartHour, "hour");
|
|
757
|
+
day.utc();
|
|
758
|
+
return timestampsToTimeRange({ from: day.valueOf(), to: day.add(24, "hours").valueOf() });
|
|
759
|
+
},
|
|
760
|
+
yesterday: () => {
|
|
761
|
+
const day = (0, moment_timezone_1.default)(now).tz(plant.timezone).add(-plant.dayStartHour, "hour").startOf("day").add(-1, "day").add(plant.dayStartHour, "hour");
|
|
762
|
+
day.utc();
|
|
763
|
+
return timestampsToTimeRange({ from: day.valueOf(), to: day.add(24, "hours").valueOf() });
|
|
764
|
+
},
|
|
765
|
+
day: () => {
|
|
766
|
+
const day = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}_${parseInt(parts[3])}`, "YYYY_MM_DD", plant.timezone);
|
|
767
|
+
day.add(plant.dayStartHour, "hour");
|
|
768
|
+
day.utc();
|
|
769
|
+
return timestampsToTimeRange({ from: day.valueOf(), to: day.add(24, "hours").valueOf() });
|
|
770
|
+
},
|
|
771
|
+
week: () => {
|
|
772
|
+
const week = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}}`, "YYYY_WW", plant.timezone).startOf("isoWeek");
|
|
773
|
+
week.add(plant.dayStartHour, "hour");
|
|
774
|
+
week.utc();
|
|
775
|
+
return timestampsToTimeRange({ from: week.valueOf(), to: week.add(24 * 7, "hours").valueOf() });
|
|
776
|
+
},
|
|
777
|
+
month: () => {
|
|
778
|
+
const month = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}}`, "YYYY_MM", plant.timezone).startOf("month");
|
|
779
|
+
month.add(plant.dayStartHour, "hour");
|
|
780
|
+
month.utc();
|
|
781
|
+
return timestampsToTimeRange({ from: month.valueOf(), to: month.add(month.daysInMonth() * 24, "hours").valueOf() });
|
|
782
|
+
},
|
|
783
|
+
quarter: () => timestampsToTimeRange({ from: 0, to: 0 }),
|
|
784
|
+
year: () => timestampsToTimeRange({ from: 0, to: 0 }),
|
|
785
|
+
custom: () => timestampsToTimeRange({ from: 0, to: 0 }),
|
|
786
|
+
// Already handled above
|
|
787
|
+
relative: () => {
|
|
788
|
+
const rollingRange = exports2.RelativeRanges.find((relativeRange) => range.code === relativeRange.rangeCode);
|
|
789
|
+
if (rollingRange != null) {
|
|
790
|
+
const durationMs = rollingRange.durationMs;
|
|
791
|
+
const nowMoment = (0, moment_timezone_1.default)(now).tz(plant.timezone);
|
|
792
|
+
let toTimestamp;
|
|
793
|
+
toTimestamp = nowMoment.clone().startOf("minute").valueOf();
|
|
794
|
+
const moment_ = nowMoment.clone().startOf("minute");
|
|
795
|
+
const currentMinute = moment_.minute();
|
|
796
|
+
if (aggregateWindow === "5m") {
|
|
797
|
+
toTimestamp = moment_.add(currentMinute % 5 + 1, "minutes").valueOf();
|
|
798
|
+
} else if (aggregateWindow === "15m") {
|
|
799
|
+
toTimestamp = moment_.add(currentMinute % 15 + 1, "minutes").valueOf();
|
|
800
|
+
} else if (aggregateWindow === "30m") {
|
|
801
|
+
toTimestamp = moment_.add(currentMinute % 30 + 1, "minutes").valueOf();
|
|
802
|
+
} else if (aggregateWindow === "1h") {
|
|
803
|
+
toTimestamp = nowMoment.clone().startOf("hour").add(1, "hour").valueOf();
|
|
804
|
+
} else if (aggregateWindow === "1d") {
|
|
805
|
+
toTimestamp = nowMoment.clone().startOf("day").add(1, "day").valueOf();
|
|
806
|
+
} else if (aggregateWindow === "7d") {
|
|
807
|
+
toTimestamp = nowMoment.clone().startOf("isoWeek").add(1, "week").valueOf();
|
|
808
|
+
}
|
|
809
|
+
const fromTimestamp = toTimestamp - durationMs;
|
|
810
|
+
return timestampsToTimeRange({ from: fromTimestamp, to: toTimestamp });
|
|
811
|
+
} else {
|
|
812
|
+
throw new Error(error_key_1.INVALID_RANGECODE);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
return handlers[parts[0].toLowerCase()]();
|
|
817
|
+
}
|
|
818
|
+
var getTimesBetweenRangeTimes = (fromIso, toIso, unit) => {
|
|
819
|
+
const dates = [];
|
|
820
|
+
const from = (0, moment_timezone_1.default)(fromIso);
|
|
821
|
+
const to = (0, moment_timezone_1.default)(toIso);
|
|
822
|
+
do {
|
|
823
|
+
dates.push(from.clone());
|
|
824
|
+
} while (from.add(1, unit).diff(to) < 0);
|
|
825
|
+
return dates;
|
|
826
|
+
};
|
|
827
|
+
exports2.getTimesBetweenRangeTimes = getTimesBetweenRangeTimes;
|
|
828
|
+
var convertNumberToMomentTime = (number, isUTC, timeZone) => {
|
|
829
|
+
if (isUTC) {
|
|
830
|
+
return (0, moment_timezone_1.utc)().startOf("day").add(number, "hours");
|
|
831
|
+
}
|
|
832
|
+
if (timeZone) {
|
|
833
|
+
return moment_timezone_1.default.tz(timeZone).startOf("day").add(number, "hours");
|
|
834
|
+
}
|
|
835
|
+
return (0, moment_timezone_1.default)().startOf("day").add(number, "hours");
|
|
836
|
+
};
|
|
837
|
+
exports2.convertNumberToMomentTime = convertNumberToMomentTime;
|
|
838
|
+
function getCoverage(outerRange, innerRanges) {
|
|
839
|
+
const coveredRange = (0, exports2.getCoverageRanges)(outerRange, innerRanges);
|
|
840
|
+
return coveredRange.reduce((a, b) => a + b.duration, 0);
|
|
841
|
+
}
|
|
842
|
+
var getCoverageRanges = (range, innerRanges) => {
|
|
843
|
+
const coverageRange = [];
|
|
844
|
+
innerRanges.sort((a, b) => a.from - b.from);
|
|
845
|
+
let from = range.from;
|
|
846
|
+
const to = range.to;
|
|
847
|
+
for (const inner of innerRanges) {
|
|
848
|
+
const intersectRange = (0, exports2.intersectTimeWindows)(from, to, inner.from, inner.to);
|
|
849
|
+
if (intersectRange.duration) {
|
|
850
|
+
from = intersectRange.to;
|
|
851
|
+
coverageRange.push(intersectRange);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return coverageRange;
|
|
855
|
+
};
|
|
856
|
+
exports2.getCoverageRanges = getCoverageRanges;
|
|
857
|
+
function getWindowAtTime(timestamp, windows) {
|
|
858
|
+
return windows.find((window2) => window2.from <= timestamp && window2.to > timestamp);
|
|
859
|
+
}
|
|
860
|
+
var intersectTimeRanges = (range1, range2) => {
|
|
861
|
+
return (0, exports2.intersectTimeWindows)(range1.from, range1.to, range2.from, range2.to);
|
|
862
|
+
};
|
|
863
|
+
exports2.intersectTimeRanges = intersectTimeRanges;
|
|
864
|
+
var intersectTimeWindows = (start1, end1, start2, end2) => {
|
|
865
|
+
const from = Math.max(start1, start2);
|
|
866
|
+
const to = Math.min(end1, end2);
|
|
867
|
+
if (from > to) {
|
|
868
|
+
return timestampsToTimeRange({ from: 0, to: 0 });
|
|
869
|
+
} else {
|
|
870
|
+
return timestampsToTimeRange({ from, to });
|
|
871
|
+
}
|
|
872
|
+
};
|
|
873
|
+
exports2.intersectTimeWindows = intersectTimeWindows;
|
|
874
|
+
var intersectTimeWindowsIso = (start1, end1, start2, end2) => {
|
|
875
|
+
const start = moment_timezone_1.default.max((0, moment_timezone_1.default)(start1), (0, moment_timezone_1.default)(start2));
|
|
876
|
+
const end = moment_timezone_1.default.min((0, moment_timezone_1.default)(end1), (0, moment_timezone_1.default)(end2));
|
|
877
|
+
if (start > end) {
|
|
878
|
+
return timestampsToTimeRange({ from: 0, to: 0 });
|
|
879
|
+
} else {
|
|
880
|
+
return timestampsToTimeRange({ from: start.valueOf(), to: end.valueOf() });
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
exports2.intersectTimeWindowsIso = intersectTimeWindowsIso;
|
|
884
|
+
function getWeeksInQuarter(year, quarter) {
|
|
885
|
+
const weeksInYear = (0, moment_timezone_1.default)({ year }).isoWeeksInISOWeekYear();
|
|
886
|
+
if (quarter === 1) {
|
|
887
|
+
return new Array(13).fill("").map((v, i) => i + 1);
|
|
888
|
+
}
|
|
889
|
+
if (quarter === 2) {
|
|
890
|
+
return new Array(13).fill("").map((v, i) => i + 14);
|
|
891
|
+
}
|
|
892
|
+
if (quarter === 3) {
|
|
893
|
+
return new Array(13).fill("").map((v, i) => i + 27);
|
|
894
|
+
}
|
|
895
|
+
if (quarter === 4) {
|
|
896
|
+
return new Array(weeksInYear > 52 ? 14 : 13).fill("").map((v, i) => i + 40);
|
|
897
|
+
}
|
|
898
|
+
throw new Error("Invalid quarter: " + quarter);
|
|
899
|
+
}
|
|
900
|
+
function getQuarterFromWeekNo(weekNo) {
|
|
901
|
+
return weekNo < 14 ? 1 : weekNo < 27 ? 2 : weekNo < 40 ? 3 : 4;
|
|
902
|
+
}
|
|
903
|
+
function getDaysInWeek(year, weekNo) {
|
|
904
|
+
const days = [];
|
|
905
|
+
const dayMoment = (0, moment_timezone_1.default)().isoWeekYear(year).isoWeek(weekNo).startOf("isoWeek");
|
|
906
|
+
for (let di = 0; di < 7; di++) {
|
|
907
|
+
days.push({
|
|
908
|
+
label: dayMoment.format("ddd, MMM DD"),
|
|
909
|
+
startTimestamp: dayMoment.valueOf(),
|
|
910
|
+
rangeCode: "DAY_" + dayMoment.format("YYYY_MM_DD")
|
|
911
|
+
});
|
|
912
|
+
dayMoment.add(1, "day");
|
|
913
|
+
}
|
|
914
|
+
return days;
|
|
915
|
+
}
|
|
916
|
+
function getComponentsFromDayRangeCode(range) {
|
|
917
|
+
let rangeCode = range.code;
|
|
918
|
+
if (rangeCode == null && range.custom?.from != null && range.custom.to != null) {
|
|
919
|
+
const datetime = moment_timezone_1.default.utc(range.custom.from);
|
|
920
|
+
return { year: datetime.year(), month: datetime.month() + 1, day: datetime.date() };
|
|
921
|
+
}
|
|
922
|
+
rangeCode = rangeCode == null ? "" : rangeCode;
|
|
923
|
+
if (rangeCode.startsWith("TODAY")) {
|
|
924
|
+
const now = (0, moment_timezone_1.default)();
|
|
925
|
+
return { year: now.year(), month: now.month() + 1, day: now.date() };
|
|
926
|
+
}
|
|
927
|
+
if (!rangeCode.startsWith("DAY_")) {
|
|
928
|
+
throw new Error("Not DAY range code.");
|
|
929
|
+
}
|
|
930
|
+
const components = rangeCode.split("_");
|
|
931
|
+
return { year: Number(components[1]), month: Number(components[2]), day: Number(components[3]) };
|
|
932
|
+
}
|
|
933
|
+
function getComponentsFromWeekRangeCode(rangeCode) {
|
|
934
|
+
if (!rangeCode.startsWith("WEEK_")) {
|
|
935
|
+
throw new Error("Not WEEK range code.");
|
|
936
|
+
}
|
|
937
|
+
const components = rangeCode.split("_");
|
|
938
|
+
return { year: Number(components[1]), week: Number(components[2]) };
|
|
939
|
+
}
|
|
940
|
+
function getComponentsFromQuarterRangeCode(rangeCode) {
|
|
941
|
+
if (!rangeCode.startsWith("QUARTER_")) {
|
|
942
|
+
throw new Error("Not QUARTER range code.");
|
|
943
|
+
}
|
|
944
|
+
const components = rangeCode.split("_");
|
|
945
|
+
return { year: Number(components[1]), quarter: Number(components[2]) };
|
|
946
|
+
}
|
|
947
|
+
function getCurrentDayRangeCode(timezone) {
|
|
948
|
+
return getDayRangeCode((0, moment_timezone_1.default)().valueOf(), timezone);
|
|
949
|
+
}
|
|
950
|
+
function getDayRangeCode(time, timezone) {
|
|
951
|
+
if (timezone) {
|
|
952
|
+
return (0, moment_timezone_1.default)(time).tz(timezone).format(`[DAY_]YYYY_MM_DD`);
|
|
953
|
+
}
|
|
954
|
+
return (0, moment_timezone_1.default)(time).format(`[DAY_]YYYY_MM_DD`);
|
|
955
|
+
}
|
|
956
|
+
function getCurrentWeekRangeCode(timezone) {
|
|
957
|
+
if (timezone) {
|
|
958
|
+
return moment_timezone_1.default.tz(timezone).format(`[WEEK_]YYYY_WW`);
|
|
959
|
+
}
|
|
960
|
+
return (0, moment_timezone_1.default)().format(`[WEEK_]YYYY_WW`);
|
|
961
|
+
}
|
|
962
|
+
var getOverlappedTimeRange = (range) => {
|
|
963
|
+
if (!range.length) {
|
|
964
|
+
return range;
|
|
965
|
+
}
|
|
966
|
+
const overlapedTimeRange = [];
|
|
967
|
+
range.sort((a, b) => a.from - b.from);
|
|
968
|
+
overlapedTimeRange.push(range[0]);
|
|
969
|
+
let i = 1;
|
|
970
|
+
while (i < range.length) {
|
|
971
|
+
const startAt = Math.max(range[i].from, overlapedTimeRange[i - 1].to);
|
|
972
|
+
overlapedTimeRange.push(timestampsToTimeRange({
|
|
973
|
+
from: startAt,
|
|
974
|
+
to: range[i].to
|
|
975
|
+
}));
|
|
976
|
+
i++;
|
|
977
|
+
}
|
|
978
|
+
return overlapedTimeRange.filter((s) => (0, moment_timezone_1.default)(s.fromIso).isBefore((0, moment_timezone_1.default)(s.toIso)));
|
|
979
|
+
};
|
|
980
|
+
exports2.getOverlappedTimeRange = getOverlappedTimeRange;
|
|
981
|
+
exports2.AcceptedDateTimeFormats = [
|
|
982
|
+
"DD/MM/YYYY",
|
|
983
|
+
"DD-MM-YYYY",
|
|
984
|
+
"DD-MM-YYYY",
|
|
985
|
+
"DD/MM/YYYY HH:mm",
|
|
986
|
+
"DD-MM-YYYY HH:mm",
|
|
987
|
+
"YYYY-MM-DD HH:mm",
|
|
988
|
+
"YYYY-MM-DDTHH:mm:ssZ",
|
|
989
|
+
"YYYY-MM-DDTHH:mm:ss.SSSZ"
|
|
990
|
+
];
|
|
991
|
+
var convertToUniversal = (date, timezone, includedTime = false, parseToIso = false) => {
|
|
992
|
+
if (date == null) {
|
|
993
|
+
return void 0;
|
|
994
|
+
}
|
|
995
|
+
const time = timezone ? moment_timezone_1.default.tz(date, exports2.AcceptedDateTimeFormats, true, timezone) : (0, moment_timezone_1.default)(date, exports2.AcceptedDateTimeFormats, true);
|
|
996
|
+
if (!time.isValid()) {
|
|
997
|
+
return void 0;
|
|
998
|
+
}
|
|
999
|
+
time.seconds(0);
|
|
1000
|
+
time.milliseconds(0);
|
|
1001
|
+
if (parseToIso) {
|
|
1002
|
+
return time.toISOString();
|
|
1003
|
+
}
|
|
1004
|
+
if (includedTime) {
|
|
1005
|
+
return time.format("DD/MM/YYYY HH:mm:ss");
|
|
1006
|
+
}
|
|
1007
|
+
return time.format("DD/MM/YYYY");
|
|
1008
|
+
};
|
|
1009
|
+
exports2.convertToUniversal = convertToUniversal;
|
|
1010
|
+
var hourToMoment = (hour) => {
|
|
1011
|
+
return hour !== null && hour !== void 0 ? (0, moment_timezone_1.default)().startOf("day").add(hour, "hours") : void 0;
|
|
1012
|
+
};
|
|
1013
|
+
exports2.hourToMoment = hourToMoment;
|
|
1014
|
+
var getDuration = (start, end) => {
|
|
1015
|
+
const startHour = (0, moment_timezone_1.default)(start, "HH:mm");
|
|
1016
|
+
const endHour = (0, moment_timezone_1.default)(end, "HH:mm");
|
|
1017
|
+
if (!startHour.isValid() || !endHour.isValid()) {
|
|
1018
|
+
return void 0;
|
|
1019
|
+
}
|
|
1020
|
+
if (end === "00:00") {
|
|
1021
|
+
endHour.add(1, "d");
|
|
1022
|
+
}
|
|
1023
|
+
if (startHour.isSameOrAfter(endHour)) {
|
|
1024
|
+
return void 0;
|
|
1025
|
+
}
|
|
1026
|
+
const duration = moment_timezone_1.default.duration(endHour.diff(startHour));
|
|
1027
|
+
const hours = duration.hours() + (duration.asHours() >= 24 ? 24 : 0);
|
|
1028
|
+
const minutes = duration.minutes();
|
|
1029
|
+
const seconds = duration.seconds();
|
|
1030
|
+
return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
|
|
1031
|
+
};
|
|
1032
|
+
exports2.getDuration = getDuration;
|
|
1033
|
+
var getDurationAsHumanize = (start, end) => {
|
|
1034
|
+
const startMoment = (0, moment_timezone_1.default)(start);
|
|
1035
|
+
const endMoment = (0, moment_timezone_1.default)(end);
|
|
1036
|
+
return moment_timezone_1.default.duration(startMoment.diff(endMoment)).humanize(true);
|
|
1037
|
+
};
|
|
1038
|
+
exports2.getDurationAsHumanize = getDurationAsHumanize;
|
|
1039
|
+
function generateTimeWindows(startAt, windowDuration, count, limitAt) {
|
|
1040
|
+
const diffToMultiple = startAt % windowDuration;
|
|
1041
|
+
const correctedStartAt = startAt - diffToMultiple;
|
|
1042
|
+
const windows = [];
|
|
1043
|
+
let endAtTimestamp = 0;
|
|
1044
|
+
for (let i = 0; i < count; i++) {
|
|
1045
|
+
const windowStartAtTimestamp = correctedStartAt + windowDuration * i;
|
|
1046
|
+
const windowEndAtTimestamp = windowStartAtTimestamp + windowDuration;
|
|
1047
|
+
if (limitAt && windowEndAtTimestamp >= limitAt) {
|
|
1048
|
+
break;
|
|
1049
|
+
}
|
|
1050
|
+
endAtTimestamp = windowEndAtTimestamp;
|
|
1051
|
+
windows.push({
|
|
1052
|
+
windowStartAtTimestamp,
|
|
1053
|
+
windowEndAtTimestamp,
|
|
1054
|
+
windowStartAt: (0, moment_timezone_1.default)(windowStartAtTimestamp).toISOString(),
|
|
1055
|
+
windowEndAt: (0, moment_timezone_1.default)(windowEndAtTimestamp).toISOString()
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
return {
|
|
1059
|
+
windows,
|
|
1060
|
+
startAtTimestamp: correctedStartAt,
|
|
1061
|
+
endAtTimestamp,
|
|
1062
|
+
startAt: (0, moment_timezone_1.default)(correctedStartAt).toISOString(),
|
|
1063
|
+
endAt: (0, moment_timezone_1.default)(endAtTimestamp).toISOString()
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
function BusrootDateTimeParse(obj, now = moment_timezone_1.default.utc().valueOf()) {
|
|
1067
|
+
const fields = ["plannedStartAt", "plannedEndAt", "actualStartAt", "actualEndAt"];
|
|
1068
|
+
for (const field of fields) {
|
|
1069
|
+
if (field in obj) {
|
|
1070
|
+
if (obj[field] === "now") {
|
|
1071
|
+
obj[field] = moment_timezone_1.default.utc(now).toISOString();
|
|
1072
|
+
} else if (obj[field] === "eod") {
|
|
1073
|
+
obj[field] = moment_timezone_1.default.utc(now).startOf("day").add(1, "day").toISOString();
|
|
1074
|
+
} else if (obj[field] === "sod") {
|
|
1075
|
+
obj[field] = moment_timezone_1.default.utc(now).startOf("day").toISOString();
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
return obj;
|
|
1080
|
+
}
|
|
1081
|
+
function decimalHoursToTime(hour) {
|
|
1082
|
+
let totalSeconds = Math.round(hour * 3600);
|
|
1083
|
+
const hours = Math.floor(totalSeconds / 3600);
|
|
1084
|
+
totalSeconds %= 3600;
|
|
1085
|
+
const minutes = Math.floor(totalSeconds / 60);
|
|
1086
|
+
const seconds = totalSeconds % 60;
|
|
1087
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
1088
|
+
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
});
|
|
1092
|
+
|
|
1093
|
+
// src/index.ts
|
|
1094
|
+
var index_exports = {};
|
|
1095
|
+
__export(index_exports, {
|
|
1096
|
+
BusrootClient: () => BusrootClient,
|
|
1097
|
+
Config: () => Config,
|
|
1098
|
+
MqttEventType: () => MqttEventType,
|
|
1099
|
+
describeCapabilities: () => describeCapabilities,
|
|
1100
|
+
formattedDateTime: () => import_times.formattedDateTime,
|
|
1101
|
+
formattedDuration: () => import_times.formattedDuration,
|
|
1102
|
+
useBusroot: () => useBusroot
|
|
1103
|
+
});
|
|
1104
|
+
module.exports = __toCommonJS(index_exports);
|
|
1105
|
+
|
|
1106
|
+
// src/client.ts
|
|
1107
|
+
var import_zod2 = require("zod");
|
|
1108
|
+
|
|
1109
|
+
// src/capabilities.ts
|
|
1110
|
+
var import_zod = require("zod");
|
|
1111
|
+
var PlantGetParams = import_zod.z.object({
|
|
1112
|
+
plantCode: import_zod.z.string().describe("Unique plant identifier code")
|
|
1113
|
+
});
|
|
1114
|
+
var PlantCreateUpdateParams = import_zod.z.object({
|
|
1115
|
+
code: import_zod.z.string().describe("Unique plant identifier code"),
|
|
1116
|
+
name: import_zod.z.string().optional().describe("Display name for the plant"),
|
|
1117
|
+
timezone: import_zod.z.string().optional().describe("IANA timezone (e.g. Europe/London)"),
|
|
1118
|
+
dayStartHour: import_zod.z.number().optional().describe("Hour the production day starts (0-23)")
|
|
1119
|
+
}).passthrough();
|
|
1120
|
+
var StationGetParams = import_zod.z.object({
|
|
1121
|
+
stationCode: import_zod.z.string().describe("Unique station identifier code")
|
|
1122
|
+
});
|
|
1123
|
+
var StationCreateUpdateParams = import_zod.z.object({
|
|
1124
|
+
code: import_zod.z.string().describe("Unique station identifier code"),
|
|
1125
|
+
name: import_zod.z.string().optional().describe("Display name for the station"),
|
|
1126
|
+
groupCode: import_zod.z.string().optional().describe("Group code to assign the station to"),
|
|
1127
|
+
shiftPatternId: import_zod.z.number().optional().describe("Shift pattern ID to assign")
|
|
1128
|
+
}).passthrough();
|
|
1129
|
+
var StationSignalParams = import_zod.z.object({
|
|
1130
|
+
stationCode: import_zod.z.string().describe("Target station code"),
|
|
1131
|
+
payload: import_zod.z.object({}).passthrough().describe("Signal payload object")
|
|
1132
|
+
});
|
|
1133
|
+
var ProductionGetParams = import_zod.z.object({
|
|
1134
|
+
plantCode: import_zod.z.string().describe("Plant code to query"),
|
|
1135
|
+
stationCodes: import_zod.z.array(import_zod.z.string()).optional().describe("Filter by station codes"),
|
|
1136
|
+
rangeCode: import_zod.z.string().describe("Time range code (e.g. today, yesterday, last7days)")
|
|
1137
|
+
});
|
|
1138
|
+
var ProductionGetRawParams = import_zod.z.object({
|
|
1139
|
+
plantCode: import_zod.z.string().describe("Plant code to query"),
|
|
1140
|
+
stationCodes: import_zod.z.array(import_zod.z.string()).optional().describe("Filter by station codes"),
|
|
1141
|
+
rangeCode: import_zod.z.string().describe("Time range code (e.g. today, yesterday, last7days)")
|
|
1142
|
+
});
|
|
1143
|
+
var MetricsGetParams = import_zod.z.object({
|
|
1144
|
+
plantCode: import_zod.z.string().describe("Plant code to query"),
|
|
1145
|
+
stationCodes: import_zod.z.array(import_zod.z.string()).optional().describe("Filter by station codes"),
|
|
1146
|
+
rangeCode: import_zod.z.string().describe("Time range code (e.g. today, yesterday, last7days)"),
|
|
1147
|
+
aggregateWindow: import_zod.z.string().describe("Aggregation window (e.g. 1h, 1d)")
|
|
1148
|
+
});
|
|
1149
|
+
var DowntimeGetParams = import_zod.z.object({
|
|
1150
|
+
plantCode: import_zod.z.string().describe("Plant code to query"),
|
|
1151
|
+
stationCodes: import_zod.z.array(import_zod.z.string()).optional().describe("Filter by station codes"),
|
|
1152
|
+
rangeCode: import_zod.z.string().describe("Time range code (e.g. today, yesterday, last7days)")
|
|
1153
|
+
});
|
|
1154
|
+
var OverviewGetParams = import_zod.z.object({
|
|
1155
|
+
plantCode: import_zod.z.string().describe("Plant code to query"),
|
|
1156
|
+
rangeCode: import_zod.z.string().describe("Time range code (e.g. today, yesterday, last7days)"),
|
|
1157
|
+
rangeFrom: import_zod.z.string().optional().describe("Custom range start (ISO 8601)"),
|
|
1158
|
+
rangeTo: import_zod.z.string().optional().describe("Custom range end (ISO 8601)")
|
|
1159
|
+
});
|
|
1160
|
+
var SkuGetParams = import_zod.z.object({
|
|
1161
|
+
skuCode: import_zod.z.string().describe("Unique SKU identifier code")
|
|
1162
|
+
});
|
|
1163
|
+
var SkuCreateUpdateParams = import_zod.z.object({
|
|
1164
|
+
code: import_zod.z.string().describe("Unique SKU identifier code"),
|
|
1165
|
+
name: import_zod.z.string().optional().describe("Display name for the SKU"),
|
|
1166
|
+
cycleTime: import_zod.z.number().optional().describe("Ideal cycle time in seconds"),
|
|
1167
|
+
value: import_zod.z.number().optional().describe("Monetary value per unit")
|
|
1168
|
+
}).passthrough();
|
|
1169
|
+
var ScheduleGetParams = import_zod.z.object({
|
|
1170
|
+
scheduleId: import_zod.z.number().describe("Numeric schedule ID")
|
|
1171
|
+
});
|
|
1172
|
+
var ScheduleCreateUpdateParams = import_zod.z.object({
|
|
1173
|
+
stationCode: import_zod.z.string().optional().describe("Station code to assign the schedule to"),
|
|
1174
|
+
skuCode: import_zod.z.string().optional().describe("SKU code for this schedule"),
|
|
1175
|
+
plannedStartAt: import_zod.z.string().optional().describe("Planned start time (ISO 8601)"),
|
|
1176
|
+
plannedQuantity: import_zod.z.number().optional().describe("Planned production quantity")
|
|
1177
|
+
}).passthrough();
|
|
1178
|
+
var AccountCreateParams = import_zod.z.object({
|
|
1179
|
+
domain: import_zod.z.string().describe("Account domain name"),
|
|
1180
|
+
plantName: import_zod.z.string().describe("Name of the initial plant to create")
|
|
1181
|
+
});
|
|
1182
|
+
var AgentRequestParams = import_zod.z.object({
|
|
1183
|
+
message: import_zod.z.string().describe("Natural language message to send to the agent")
|
|
1184
|
+
});
|
|
1185
|
+
var capabilities = [
|
|
1186
|
+
{
|
|
1187
|
+
name: "profile.get",
|
|
1188
|
+
description: "Get the authenticated user's profile",
|
|
1189
|
+
httpMethod: "GET",
|
|
1190
|
+
params: null
|
|
1191
|
+
},
|
|
1192
|
+
{
|
|
1193
|
+
name: "account.create",
|
|
1194
|
+
description: "Create a new account with an initial plant",
|
|
1195
|
+
httpMethod: "POST",
|
|
1196
|
+
params: AccountCreateParams
|
|
1197
|
+
},
|
|
1198
|
+
{
|
|
1199
|
+
name: "apiKey.create",
|
|
1200
|
+
description: "Create a new API key for the current account",
|
|
1201
|
+
httpMethod: "POST",
|
|
1202
|
+
params: null
|
|
1203
|
+
},
|
|
1204
|
+
{
|
|
1205
|
+
name: "plant.get",
|
|
1206
|
+
description: "Get a plant by its code",
|
|
1207
|
+
httpMethod: "GET",
|
|
1208
|
+
params: PlantGetParams
|
|
1209
|
+
},
|
|
1210
|
+
{
|
|
1211
|
+
name: "plant.createUpdate",
|
|
1212
|
+
description: "Create or update a plant",
|
|
1213
|
+
httpMethod: "POST",
|
|
1214
|
+
params: PlantCreateUpdateParams
|
|
1215
|
+
},
|
|
1216
|
+
{
|
|
1217
|
+
name: "station.get",
|
|
1218
|
+
description: "Get stations by station code",
|
|
1219
|
+
httpMethod: "GET",
|
|
1220
|
+
params: StationGetParams
|
|
1221
|
+
},
|
|
1222
|
+
{
|
|
1223
|
+
name: "station.createUpdate",
|
|
1224
|
+
description: "Create or update a station",
|
|
1225
|
+
httpMethod: "POST",
|
|
1226
|
+
params: StationCreateUpdateParams
|
|
1227
|
+
},
|
|
1228
|
+
{
|
|
1229
|
+
name: "station.signal",
|
|
1230
|
+
description: "Send a signal to a station",
|
|
1231
|
+
httpMethod: "POST",
|
|
1232
|
+
params: StationSignalParams
|
|
1233
|
+
},
|
|
1234
|
+
{
|
|
1235
|
+
name: "production.get",
|
|
1236
|
+
description: "Get aggregated production data for a plant and time range",
|
|
1237
|
+
httpMethod: "GET",
|
|
1238
|
+
params: ProductionGetParams
|
|
1239
|
+
},
|
|
1240
|
+
{
|
|
1241
|
+
name: "production.getRaw",
|
|
1242
|
+
description: "Get raw production data for a plant and time range",
|
|
1243
|
+
httpMethod: "GET",
|
|
1244
|
+
params: ProductionGetRawParams
|
|
1245
|
+
},
|
|
1246
|
+
{
|
|
1247
|
+
name: "metrics.get",
|
|
1248
|
+
description: "Get aggregated station metrics (windows) for a plant and time range",
|
|
1249
|
+
httpMethod: "GET",
|
|
1250
|
+
params: MetricsGetParams
|
|
1251
|
+
},
|
|
1252
|
+
{
|
|
1253
|
+
name: "downtime.get",
|
|
1254
|
+
description: "Get aggregated downtime data for a plant and time range",
|
|
1255
|
+
httpMethod: "GET",
|
|
1256
|
+
params: DowntimeGetParams
|
|
1257
|
+
},
|
|
1258
|
+
{
|
|
1259
|
+
name: "overview.get",
|
|
1260
|
+
description: "Get a full overview of plant performance for a time range",
|
|
1261
|
+
httpMethod: "GET",
|
|
1262
|
+
params: OverviewGetParams
|
|
1263
|
+
},
|
|
1264
|
+
{
|
|
1265
|
+
name: "sku.get",
|
|
1266
|
+
description: "Get a SKU by its code",
|
|
1267
|
+
httpMethod: "GET",
|
|
1268
|
+
params: SkuGetParams
|
|
1269
|
+
},
|
|
1270
|
+
{
|
|
1271
|
+
name: "sku.createUpdate",
|
|
1272
|
+
description: "Create or update a SKU",
|
|
1273
|
+
httpMethod: "POST",
|
|
1274
|
+
params: SkuCreateUpdateParams
|
|
1275
|
+
},
|
|
1276
|
+
{
|
|
1277
|
+
name: "schedule.get",
|
|
1278
|
+
description: "Get a schedule by its ID",
|
|
1279
|
+
httpMethod: "GET",
|
|
1280
|
+
params: ScheduleGetParams
|
|
1281
|
+
},
|
|
1282
|
+
{
|
|
1283
|
+
name: "schedule.createUpdate",
|
|
1284
|
+
description: "Create or update a schedule",
|
|
1285
|
+
httpMethod: "POST",
|
|
1286
|
+
params: ScheduleCreateUpdateParams
|
|
1287
|
+
},
|
|
1288
|
+
{
|
|
1289
|
+
name: "agent.request",
|
|
1290
|
+
description: "Send a natural language message to the Busroot AI agent",
|
|
1291
|
+
httpMethod: "POST",
|
|
1292
|
+
params: AgentRequestParams
|
|
1293
|
+
}
|
|
1294
|
+
];
|
|
1295
|
+
function resolveZodType(schema) {
|
|
1296
|
+
if (schema instanceof import_zod.z.ZodOptional || schema instanceof import_zod.z.ZodNullable) {
|
|
1297
|
+
return resolveZodType(schema.unwrap());
|
|
1298
|
+
}
|
|
1299
|
+
if (schema instanceof import_zod.z.ZodString) {
|
|
1300
|
+
return "string";
|
|
1301
|
+
}
|
|
1302
|
+
if (schema instanceof import_zod.z.ZodNumber) {
|
|
1303
|
+
return "number";
|
|
1304
|
+
}
|
|
1305
|
+
if (schema instanceof import_zod.z.ZodBoolean) {
|
|
1306
|
+
return "boolean";
|
|
1307
|
+
}
|
|
1308
|
+
if (schema instanceof import_zod.z.ZodArray) {
|
|
1309
|
+
return "array";
|
|
1310
|
+
}
|
|
1311
|
+
if (schema instanceof import_zod.z.ZodObject) {
|
|
1312
|
+
return "object";
|
|
1313
|
+
}
|
|
1314
|
+
if (schema instanceof import_zod.z.ZodEnum) {
|
|
1315
|
+
return "string";
|
|
1316
|
+
}
|
|
1317
|
+
return "unknown";
|
|
1318
|
+
}
|
|
1319
|
+
function describeCapabilities() {
|
|
1320
|
+
return capabilities.map((cap) => ({
|
|
1321
|
+
name: cap.name,
|
|
1322
|
+
description: cap.description,
|
|
1323
|
+
method: cap.httpMethod,
|
|
1324
|
+
params: cap.params ? Object.entries(cap.params.shape).map(([key, value]) => {
|
|
1325
|
+
const zodValue = value;
|
|
1326
|
+
return {
|
|
1327
|
+
name: key,
|
|
1328
|
+
required: !zodValue.isOptional(),
|
|
1329
|
+
description: zodValue.description ?? "",
|
|
1330
|
+
type: resolveZodType(zodValue)
|
|
1331
|
+
};
|
|
1332
|
+
}) : []
|
|
1333
|
+
}));
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
// src/client.ts
|
|
1337
|
+
var import_mqtt = __toESM(require("mqtt"));
|
|
1338
|
+
var MqttEventType = {
|
|
1339
|
+
StationMetricNew: "STATION_METRIC_NEW",
|
|
1340
|
+
// When a new station window is created and saved to DB.
|
|
1341
|
+
StationChanged: "STATION_CHANGED",
|
|
1342
|
+
// When a station is added or edited. NOT IMPLEMENTED.
|
|
1343
|
+
ScheduleChanged: "SCHEDULE_CHANGED",
|
|
1344
|
+
// When a schedule is added or edited. NOT IMPLEMENTED.
|
|
1345
|
+
ScheduleStarted: "SCHEDULE_STARTED",
|
|
1346
|
+
// When a schedule starts (actualStartAt). NOT IMPLEMENTED.
|
|
1347
|
+
ScheduleEnded: "SCHEDULE_ENDED",
|
|
1348
|
+
// When a schedule ends (actualEndAt). NOT IMPLEMENTED.
|
|
1349
|
+
ProductionNew: "PRODUCTION_NEW"
|
|
1350
|
+
// When new production is added. NOT IMPLEMENTED FOR MANUAL PRODUCTION YET.
|
|
1351
|
+
};
|
|
1352
|
+
var Config = import_zod2.z.object({
|
|
1353
|
+
accountId: import_zod2.z.string().max(255),
|
|
1354
|
+
apiKey: import_zod2.z.string().max(255),
|
|
1355
|
+
apiEndpoint: import_zod2.z.string().max(255),
|
|
1356
|
+
mqttHost: import_zod2.z.string().max(255),
|
|
1357
|
+
enableMqtt: import_zod2.z.boolean().optional(),
|
|
1358
|
+
enableDebug: import_zod2.z.boolean().optional()
|
|
1359
|
+
});
|
|
1360
|
+
var BusrootClient = class {
|
|
1361
|
+
constructor(config) {
|
|
1362
|
+
this.config = config;
|
|
1363
|
+
const trimmedAccountId = config.accountId.replace("account_", "");
|
|
1364
|
+
if (config.enableMqtt === true && config.mqttHost !== null) {
|
|
1365
|
+
this.mqttClient = import_mqtt.default.connect({
|
|
1366
|
+
host: config.mqttHost,
|
|
1367
|
+
port: 8883,
|
|
1368
|
+
username: "api-key",
|
|
1369
|
+
password: this.config.apiKey,
|
|
1370
|
+
clientId: trimmedAccountId + "/sdk"
|
|
1371
|
+
});
|
|
1372
|
+
this.mqttClient.on("message", (topic, payload) => {
|
|
1373
|
+
const eventType = topic.toUpperCase().split("/").at(-1);
|
|
1374
|
+
for (const [listenerEventType, listeners] of this.listenersByEventType) {
|
|
1375
|
+
if (MqttEventType[listenerEventType] !== eventType) {
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
for (const listener of listeners) {
|
|
1379
|
+
try {
|
|
1380
|
+
listener(JSON.parse(payload.toString()));
|
|
1381
|
+
} catch (e) {
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
});
|
|
1386
|
+
this.mqttClient.on("error", (error) => {
|
|
1387
|
+
console.error(error);
|
|
1388
|
+
});
|
|
1389
|
+
this.mqttClient.on("connect", () => {
|
|
1390
|
+
console.log("Connected to Busroot MQTT...");
|
|
1391
|
+
});
|
|
1392
|
+
this.mqttClient.on("reconnect", () => {
|
|
1393
|
+
console.log("Disconnected from Busroot MQTT...");
|
|
1394
|
+
});
|
|
1395
|
+
this.mqttClient.subscribe(`busroot/v2/${trimmedAccountId}/events/#`);
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
mqttClient;
|
|
1399
|
+
listenersByEventType = /* @__PURE__ */ new Map();
|
|
1400
|
+
static version = "v0";
|
|
1401
|
+
static describe = describeCapabilities;
|
|
1402
|
+
destroy() {
|
|
1403
|
+
if (this.mqttClient != null) {
|
|
1404
|
+
this.mqttClient.end();
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
async get(path, query) {
|
|
1408
|
+
let url = this.config.apiEndpoint + path;
|
|
1409
|
+
if (query != null) {
|
|
1410
|
+
url += "?" + Object.entries(query).filter((value) => value[1] != null).map((value) => value[0] + "=" + value[1]).join("&");
|
|
1411
|
+
}
|
|
1412
|
+
if (this.config.enableDebug === true) {
|
|
1413
|
+
console.log("Get Request: " + url);
|
|
1414
|
+
}
|
|
1415
|
+
const res = await fetch(url, {
|
|
1416
|
+
headers: { "x-api-key": this.config.apiKey },
|
|
1417
|
+
method: "GET"
|
|
1418
|
+
});
|
|
1419
|
+
if (res.status !== 200) {
|
|
1420
|
+
const resBody = await res.json();
|
|
1421
|
+
throw new Error(`${res.status}: ${res.statusText}: ${resBody.errorCode}`);
|
|
1422
|
+
}
|
|
1423
|
+
const resJson = await res.json();
|
|
1424
|
+
return resJson;
|
|
1425
|
+
}
|
|
1426
|
+
async post(path, body) {
|
|
1427
|
+
const res = await fetch(this.config.apiEndpoint + path, {
|
|
1428
|
+
headers: { "x-api-key": this.config.apiKey, "Content-Type": "application/json" },
|
|
1429
|
+
method: "POST",
|
|
1430
|
+
body: JSON.stringify(body)
|
|
1431
|
+
});
|
|
1432
|
+
if (res.status !== 200) {
|
|
1433
|
+
const resBody = await res.json();
|
|
1434
|
+
throw new Error(`${res.status}: ${res.statusText}: ${resBody.errorCode}`);
|
|
1435
|
+
}
|
|
1436
|
+
const resJson = await res.json();
|
|
1437
|
+
return resJson;
|
|
1438
|
+
}
|
|
1439
|
+
profile = {
|
|
1440
|
+
get: async () => {
|
|
1441
|
+
const res = await this.get("/auth/profile");
|
|
1442
|
+
return res.result;
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
account = {
|
|
1446
|
+
create: async ({ domain, plantName }) => {
|
|
1447
|
+
const res = await this.post("/account/new", { domain, plantName });
|
|
1448
|
+
return res.result;
|
|
1449
|
+
}
|
|
1450
|
+
};
|
|
1451
|
+
apiKey = {
|
|
1452
|
+
create: async () => {
|
|
1453
|
+
const res = await this.post("/api-key", {});
|
|
1454
|
+
return res.result;
|
|
1455
|
+
}
|
|
1456
|
+
};
|
|
1457
|
+
plant = {
|
|
1458
|
+
get: async ({ plantCode }) => {
|
|
1459
|
+
const res = await this.get("/plant", { plantCode });
|
|
1460
|
+
return res.result;
|
|
1461
|
+
},
|
|
1462
|
+
createUpdate: async (plant) => {
|
|
1463
|
+
const res = await this.post("/plant", plant);
|
|
1464
|
+
return res.result;
|
|
1465
|
+
}
|
|
1466
|
+
};
|
|
1467
|
+
station = {
|
|
1468
|
+
get: async ({ stationCode }) => {
|
|
1469
|
+
const res = await this.get("/station", { stationcodes: stationCode });
|
|
1470
|
+
return res.results;
|
|
1471
|
+
},
|
|
1472
|
+
createUpdate: async (station) => {
|
|
1473
|
+
const res = await this.post("/station", station);
|
|
1474
|
+
return res.result;
|
|
1475
|
+
},
|
|
1476
|
+
signal: (_params) => {
|
|
1477
|
+
throw new Error("noop");
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
production = {
|
|
1481
|
+
get: async ({ plantCode, stationCodes, rangeCode }) => {
|
|
1482
|
+
const res = await this.get("/production", {
|
|
1483
|
+
plantcode: plantCode,
|
|
1484
|
+
stationcodes: stationCodes?.join(","),
|
|
1485
|
+
rangecode: rangeCode
|
|
1486
|
+
});
|
|
1487
|
+
return res.results;
|
|
1488
|
+
},
|
|
1489
|
+
getRaw: async ({ plantCode, stationCodes, rangeCode }) => {
|
|
1490
|
+
const res = await this.get("/production/raw", {
|
|
1491
|
+
plantcode: plantCode,
|
|
1492
|
+
stationcodes: stationCodes?.join(","),
|
|
1493
|
+
rangecode: rangeCode
|
|
1494
|
+
});
|
|
1495
|
+
return res.results;
|
|
1496
|
+
}
|
|
1497
|
+
};
|
|
1498
|
+
metrics = {
|
|
1499
|
+
get: async ({
|
|
1500
|
+
plantCode,
|
|
1501
|
+
stationCodes,
|
|
1502
|
+
rangeCode,
|
|
1503
|
+
aggregateWindow
|
|
1504
|
+
}) => {
|
|
1505
|
+
const res = await this.get("/station-windows", {
|
|
1506
|
+
plantcode: plantCode,
|
|
1507
|
+
stationcodes: stationCodes?.join(","),
|
|
1508
|
+
rangecode: rangeCode,
|
|
1509
|
+
aggregatewindow: aggregateWindow
|
|
1510
|
+
});
|
|
1511
|
+
return res.results;
|
|
1512
|
+
}
|
|
1513
|
+
};
|
|
1514
|
+
downtime = {
|
|
1515
|
+
get: async ({ plantCode, stationCodes, rangeCode }) => {
|
|
1516
|
+
const res = await this.get("/downtime", {
|
|
1517
|
+
plantcode: plantCode,
|
|
1518
|
+
stationcodes: stationCodes?.join(","),
|
|
1519
|
+
rangecode: rangeCode
|
|
1520
|
+
});
|
|
1521
|
+
return res.results;
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
overview = {
|
|
1525
|
+
get: async ({ plantCode, rangeCode, rangeFrom, rangeTo }) => {
|
|
1526
|
+
const res = await this.get("/overview", {
|
|
1527
|
+
plantcode: plantCode,
|
|
1528
|
+
rangecode: rangeCode,
|
|
1529
|
+
rangefrom: rangeFrom,
|
|
1530
|
+
rangeto: rangeTo
|
|
1531
|
+
});
|
|
1532
|
+
return res.result;
|
|
1533
|
+
}
|
|
1534
|
+
};
|
|
1535
|
+
sku = {
|
|
1536
|
+
get: async ({ skuCode }) => {
|
|
1537
|
+
const res = await this.get("/sku", { skucodes: skuCode });
|
|
1538
|
+
return res.result;
|
|
1539
|
+
},
|
|
1540
|
+
createUpdate: async (sku) => {
|
|
1541
|
+
const res = await this.post("/sku", sku);
|
|
1542
|
+
return res.result;
|
|
1543
|
+
}
|
|
1544
|
+
};
|
|
1545
|
+
agent = {
|
|
1546
|
+
request: async ({ message }) => {
|
|
1547
|
+
const res = await this.post("/agent", { message });
|
|
1548
|
+
return res.result;
|
|
1549
|
+
}
|
|
1550
|
+
};
|
|
1551
|
+
schedule = {
|
|
1552
|
+
get: async ({ scheduleId }) => {
|
|
1553
|
+
const res = await this.get("/schedule", { ids: String(scheduleId) });
|
|
1554
|
+
return res.results;
|
|
1555
|
+
},
|
|
1556
|
+
createUpdate: async (schedule) => {
|
|
1557
|
+
const res = await this.post("/schedule", schedule);
|
|
1558
|
+
return res.result;
|
|
1559
|
+
}
|
|
1560
|
+
};
|
|
1561
|
+
on(event, listener) {
|
|
1562
|
+
let set = this.listenersByEventType.get(event);
|
|
1563
|
+
if (!set) {
|
|
1564
|
+
set = /* @__PURE__ */ new Set();
|
|
1565
|
+
this.listenersByEventType.set(event, set);
|
|
1566
|
+
}
|
|
1567
|
+
set.add(listener);
|
|
1568
|
+
}
|
|
1569
|
+
off(event, listener) {
|
|
1570
|
+
const set = this.listenersByEventType.get(event);
|
|
1571
|
+
if (!set) {
|
|
1572
|
+
return;
|
|
1573
|
+
}
|
|
1574
|
+
set.delete(listener);
|
|
1575
|
+
if (set.size === 0) {
|
|
1576
|
+
this.listenersByEventType.delete(event);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
};
|
|
1580
|
+
|
|
1581
|
+
// src/hooks.ts
|
|
1582
|
+
var import_react = require("react");
|
|
1583
|
+
function useBusroot(props) {
|
|
1584
|
+
const enableMqtt = props && props.config != null && props.config.enableMqtt === true;
|
|
1585
|
+
const [client, setClient] = (0, import_react.useState)();
|
|
1586
|
+
(0, import_react.useEffect)(() => {
|
|
1587
|
+
if (typeof window === void 0) {
|
|
1588
|
+
return;
|
|
1589
|
+
}
|
|
1590
|
+
if (props != null && props.config != null) {
|
|
1591
|
+
setClient(new BusrootClient(props.config));
|
|
1592
|
+
} else {
|
|
1593
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
1594
|
+
const tokenParam = searchParams.get("busroot_token");
|
|
1595
|
+
if (tokenParam != null && client == null) {
|
|
1596
|
+
try {
|
|
1597
|
+
const config = Config.parse(JSON.parse(atob(tokenParam)));
|
|
1598
|
+
setClient(
|
|
1599
|
+
new BusrootClient({
|
|
1600
|
+
accountId: config.accountId,
|
|
1601
|
+
apiKey: config.apiKey,
|
|
1602
|
+
apiEndpoint: config.apiEndpoint,
|
|
1603
|
+
mqttHost: config.mqttHost,
|
|
1604
|
+
enableMqtt
|
|
1605
|
+
})
|
|
1606
|
+
);
|
|
1607
|
+
} catch (e) {
|
|
1608
|
+
console.log(e.message);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
return;
|
|
1613
|
+
}, []);
|
|
1614
|
+
const logout = () => {
|
|
1615
|
+
console.log("not implemented");
|
|
1616
|
+
};
|
|
1617
|
+
return { client, logout };
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
// src/index.ts
|
|
1621
|
+
var import_times = __toESM(require_times());
|
|
1622
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1623
|
+
0 && (module.exports = {
|
|
1624
|
+
BusrootClient,
|
|
1625
|
+
Config,
|
|
1626
|
+
MqttEventType,
|
|
1627
|
+
describeCapabilities,
|
|
1628
|
+
formattedDateTime,
|
|
1629
|
+
formattedDuration,
|
|
1630
|
+
useBusroot
|
|
1631
|
+
});
|