busroot-sdk 0.0.8-dev.2797946662 → 0.0.8-dev.2803109172

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.
Files changed (2) hide show
  1. package/build/index.js +31 -32
  2. package/package.json +1 -1
package/build/index.js CHANGED
@@ -1422,40 +1422,39 @@ var require_times = __commonJS({
1422
1422
  function floorToPlantDayStart(instant, plant) {
1423
1423
  return (0, moment_timezone_1.default)(instant).tz(plant.timezone).subtract(plant.dayStartHour, "hours").startOf("day").add(plant.dayStartHour, "hours").valueOf();
1424
1424
  }
1425
+ var CUSTOM_RANGE_MINUTE_STEPS = {
1426
+ "5m": 5,
1427
+ "15m": 15,
1428
+ "30m": 30
1429
+ };
1430
+ var CUSTOM_RANGE_CALENDAR_UNITS = {
1431
+ "1h": { startOf: "hour", step: "hour" },
1432
+ "1d": { startOf: "day", step: "day" },
1433
+ "7d": { startOf: "isoWeek", step: "week" }
1434
+ };
1435
+ function snapCustomRangeEdge(timestamp, timezone, aggregateWindow, direction) {
1436
+ if (aggregateWindow == null) {
1437
+ return timestamp;
1438
+ }
1439
+ const edge = (0, moment_timezone_1.default)(timestamp).tz(timezone).startOf("minute");
1440
+ const minuteStep = CUSTOM_RANGE_MINUTE_STEPS[aggregateWindow];
1441
+ if (minuteStep != null) {
1442
+ const offsetFromBoundary = edge.minute() % minuteStep;
1443
+ return direction === "backward" ? edge.subtract(offsetFromBoundary, "minutes").valueOf() : edge.add((minuteStep - offsetFromBoundary) % minuteStep, "minutes").valueOf();
1444
+ }
1445
+ const calendarUnit = CUSTOM_RANGE_CALENDAR_UNITS[aggregateWindow];
1446
+ if (calendarUnit == null) {
1447
+ return timestamp;
1448
+ }
1449
+ const boundary = edge.clone().startOf(calendarUnit.startOf);
1450
+ return direction === "backward" || boundary.valueOf() === edge.valueOf() ? boundary.valueOf() : boundary.add(1, calendarUnit.step).valueOf();
1451
+ }
1425
1452
  function getTimeRangeFromRangeCode(range, plant, aggregateWindow, now) {
1426
1453
  if (range.custom != null && range.custom.from != null && range.custom.to != null) {
1427
- let fromTimestamp = range.custom.from;
1428
- let toTimestamp = range.custom.to;
1429
- const fromTimestampMomentStartOfMinute = (0, moment_timezone_1.default)(fromTimestamp).tz(plant?.timezone || "UTC").startOf("minute");
1430
- const toTimestampMomentStartOfMinute = (0, moment_timezone_1.default)(toTimestamp).tz(plant?.timezone || "UTC").startOf("minute");
1431
- const currentFromTimestampMinute = fromTimestampMomentStartOfMinute.minute();
1432
- const currentToTimestampMinute = toTimestampMomentStartOfMinute.minute();
1433
- if (aggregateWindow === "5m") {
1434
- fromTimestamp = fromTimestampMomentStartOfMinute.add((5 - currentFromTimestampMinute % 5) % 5, "minutes").valueOf();
1435
- toTimestamp = toTimestampMomentStartOfMinute.add((5 - currentToTimestampMinute % 5) % 5, "minutes").valueOf();
1436
- } else if (aggregateWindow === "15m") {
1437
- fromTimestamp = fromTimestampMomentStartOfMinute.add((15 - currentFromTimestampMinute % 15) % 15, "minutes").valueOf();
1438
- toTimestamp = toTimestampMomentStartOfMinute.add((15 - currentToTimestampMinute % 15) % 15, "minutes").valueOf();
1439
- } else if (aggregateWindow === "30m") {
1440
- fromTimestamp = fromTimestampMomentStartOfMinute.add((30 - currentFromTimestampMinute % 30) % 30, "minutes").valueOf();
1441
- toTimestamp = toTimestampMomentStartOfMinute.add((30 - currentToTimestampMinute % 30) % 30, "minutes").valueOf();
1442
- } else if (aggregateWindow === "1h") {
1443
- const fromStartOfHour = fromTimestampMomentStartOfMinute.clone().startOf("hour");
1444
- const toStartOfHour = toTimestampMomentStartOfMinute.clone().startOf("hour");
1445
- fromTimestamp = fromTimestampMomentStartOfMinute.valueOf() === fromStartOfHour.valueOf() ? fromStartOfHour.valueOf() : fromStartOfHour.add(1, "hour").valueOf();
1446
- toTimestamp = toTimestampMomentStartOfMinute.valueOf() === toStartOfHour.valueOf() ? toStartOfHour.valueOf() : toStartOfHour.add(1, "hour").valueOf();
1447
- } else if (aggregateWindow === "1d") {
1448
- const fromStartOfDay = fromTimestampMomentStartOfMinute.clone().startOf("day");
1449
- const toStartOfDay = toTimestampMomentStartOfMinute.clone().startOf("day");
1450
- fromTimestamp = fromTimestampMomentStartOfMinute.valueOf() === fromStartOfDay.valueOf() ? fromStartOfDay.valueOf() : fromStartOfDay.add(1, "day").valueOf();
1451
- toTimestamp = toTimestampMomentStartOfMinute.valueOf() === toStartOfDay.valueOf() ? toStartOfDay.valueOf() : toStartOfDay.add(1, "day").valueOf();
1452
- } else if (aggregateWindow === "7d") {
1453
- const fromStartOfWeek = fromTimestampMomentStartOfMinute.clone().startOf("isoWeek");
1454
- const toStartOfWeek = toTimestampMomentStartOfMinute.clone().startOf("isoWeek");
1455
- fromTimestamp = fromTimestampMomentStartOfMinute.valueOf() === fromStartOfWeek.valueOf() ? fromStartOfWeek.valueOf() : fromStartOfWeek.add(1, "week").valueOf();
1456
- toTimestamp = toTimestampMomentStartOfMinute.valueOf() === toStartOfWeek.valueOf() ? toStartOfWeek.valueOf() : toStartOfWeek.add(1, "week").valueOf();
1457
- }
1458
- return timestampsToTimeRange({ from: fromTimestamp, to: toTimestamp });
1454
+ const timezone = plant?.timezone || "UTC";
1455
+ const fromTimestamp = snapCustomRangeEdge(range.custom.from, timezone, aggregateWindow, "forward");
1456
+ const toTimestamp = snapCustomRangeEdge(range.custom.to, timezone, aggregateWindow, "backward");
1457
+ return timestampsToTimeRange({ from: fromTimestamp, to: Math.max(fromTimestamp, toTimestamp) });
1459
1458
  }
1460
1459
  if (range.code == null) {
1461
1460
  throw new Error("Either Range Code or custom Time Range required");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "busroot-sdk",
3
- "version": "0.0.8-dev.2797946662",
3
+ "version": "0.0.8-dev.2803109172",
4
4
  "description": "An SDK for accessing Busroot from output.industries",
5
5
  "homepage": "https://www.output.industries",
6
6
  "main": "./build/index.js",