Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/quartz-app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# history window config for issue #655
NEXT_PUBLIC_HISTORY_START_TYPE=rolling
NEXT_PUBLIC_HISTORY_START_OFFSET_HOURS=48
9 changes: 9 additions & 0 deletions apps/quartz-app/src/data/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { QueryFunction, useQuery } from "@tanstack/react-query";
import { components, operations, paths } from "../types/schema";
import client from "./apiClient";
import { getHistoryStartISO } from "../helpers/historyWindow";

// paths
export const GET_REGIONS = "/{source}/regions";
Expand Down Expand Up @@ -46,6 +47,7 @@ export const getGenerationQuery = (
region: operations["get_historic_timeseries_route__source___region__generation_get"]["parameters"]["path"]["region"]
): QueryFunction<components["schemas"]["GetHistoricGenerationResponse"]> => {
return async ({ meta, signal }) => {
const startISO = getHistoryStartISO();
const { accessToken } = await fetch("/api/token").then((res) => res.json());
const { data, error } = await client.GET(GET_GENERATION, {
params: {
Expand All @@ -56,6 +58,9 @@ export const getGenerationQuery = (
query: {
...sharedQueryParams,
resample_minutes: 15,
// start of history window for UI
// @ts-ignore – schema typing may not include this yet
start_datetime_utc: startISO,
},
},
// Add bearer token to headers
Expand All @@ -78,6 +83,7 @@ export const getForecastQuery = (
forecast_horizon_minutes?: number
): QueryFunction<components["schemas"]["GetForecastGenerationResponse"]> => {
return async ({ meta, signal }) => {
const startISO = getHistoryStartISO();
const { accessToken } = await fetch("/api/token").then((res) => res.json());
const { data, error } = await client.GET(GET_FORECAST, {
params: {
Expand All @@ -90,6 +96,9 @@ export const getForecastQuery = (
forecast_horizon,
forecast_horizon_minutes:
forecast_horizon === "horizon" ? forecast_horizon_minutes : null,
// start of history window for UI
// @ts-ignore – schema typing may not include this yet
start_datetime_utc: startISO,
},
},
// Add bearer token to headers
Expand Down
41 changes: 41 additions & 0 deletions apps/quartz-app/src/helpers/historyWindow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export type HistoryStartType = "fixed" | "rolling";

/**
* Returns the start Date for the UI history window.
* - rolling: now - OFFSET hours
* - fixed: midnight (UTC) N days ago, where N = round(OFFSET/24), min 1
*/
export function getHistoryStart(now = new Date()): Date {
const rawType = (
process.env.NEXT_PUBLIC_HISTORY_START_TYPE ?? "rolling"
).toLowerCase();
const type: HistoryStartType = rawType === "fixed" ? "fixed" : "rolling";

const offsetStr = process.env.NEXT_PUBLIC_HISTORY_START_OFFSET_HOURS ?? "48";
const offsetHours = Number(offsetStr);
const safeOffset =
Number.isFinite(offsetHours) && offsetHours > 0 ? offsetHours : 48;

if (type === "fixed") {
const days = Math.max(1, Math.round(safeOffset / 24));
const midnightTodayUTC = new Date(
Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
0,
0,
0,
0
)
);
midnightTodayUTC.setUTCDate(midnightTodayUTC.getUTCDate() - days);
return midnightTodayUTC;
}

// rolling
return new Date(now.getTime() - safeOffset * 3600 * 1000);
}

export const getHistoryStartISO = (now?: Date) =>
getHistoryStart(now).toISOString();
Loading