From 8e5ddb661e988d5796db9b2ea241db618cd2a2d2 Mon Sep 17 00:00:00 2001 From: Srdjan S Date: Thu, 13 Feb 2025 10:36:27 +0100 Subject: [PATCH] Implement yesterday boundaries function in JS --- observer/lib/observer.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/observer/lib/observer.js b/observer/lib/observer.js index 6178e6d..cd1e391 100644 --- a/observer/lib/observer.js +++ b/observer/lib/observer.js @@ -131,11 +131,10 @@ export const observeRetrievalResultCodes = async (pgPoolStats, influxQueryApi) = } export const observeYesterdayDesktopUsers = async (pgPoolStats, influxQueryApi) => { + const yesterday = getYesterdayBoundaries() const rows = await influxQueryApi.collectRows(` - import "experimental/date/boundaries" - yesterday = boundaries.yesterday() from(bucket: "station-machines") - |> range(start: yesterday.start, stop: yesterday.stop) + |> range(start: ${yesterday.start}, stop: ${yesterday.stop}) |> filter(fn: (r) => r._measurement == "machine" and exists r.platform) |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value") |> map(fn: (r) => ({_time: r._time, station_id: r.station_id, platform: r.platform})) @@ -156,3 +155,27 @@ export const observeYesterdayDesktopUsers = async (pgPoolStats, influxQueryApi) rows.map(row => row.platform_count) ]) } + +/** + * Returns the start and end timestamps for yesterday's date in UTC + * @returns {Object} Object containing start and stop timestamps + */ +function getYesterdayBoundaries () { + // Get current date + const now = new Date() + + // Create start of yesterday + const start = new Date(now) + start.setDate(start.getDate() - 1) // Move to yesterday + start.setUTCHours(0, 0, 0, 0) // Set to start of day + + // Create end of yesterday + const stop = new Date(now) + stop.setDate(stop.getDate() - 1) // Move to yesterday + stop.setUTCHours(23, 59, 59, 999) // Set to end of day + + return { + start: start.toISOString(), + stop: stop.toISOString() + } +}