Skip to content

Commit

Permalink
Implement yesterday boundaries function in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
pyropy committed Feb 13, 2025
1 parent 80e48b5 commit 8e5ddb6
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions observer/lib/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand All @@ -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()
}
}

0 comments on commit 8e5ddb6

Please sign in to comment.