Openstatus www.openstatus.dev

fix: lookback period

+22 -4
+17 -2
packages/api/src/router/statusPage.ts
··· 41 41 42 42 // NOTE: this router is used on status pages only - do not confuse with the page router which is used in the dashboard for the config 43 43 44 + /** 45 + * Right now, we do not allow workspaces to have a custom lookback period. 46 + * If we decide to allow this in the future, we should move this to the database. 47 + */ 48 + const WORKSPACES = 49 + process.env.WORKSPACES_LOOKBACK_30?.split(",").map(Number) || []; 50 + 44 51 export const statusPageRouter = createTRPCRouter({ 45 52 get: publicProcedure 46 53 .input( ··· 455 462 }); 456 463 } 457 464 465 + const lookbackPeriod = WORKSPACES.includes(_page.workspaceId ?? 0) 466 + ? 30 467 + : 45; 468 + 458 469 return monitors.map((m) => { 459 470 const monitorId = m.monitor.id.toString(); 460 471 const events = getEvents({ ··· 466 477 const rawData = statusDataByMonitorId.get(monitorId) || []; 467 478 const filledData = 468 479 process.env.NOOP_UPTIME === "true" 469 - ? fillStatusDataFor45DaysNoop({ errorDays: [], degradedDays: [] }) 470 - : fillStatusDataFor45Days(rawData, monitorId); 480 + ? fillStatusDataFor45DaysNoop({ 481 + errorDays: [], 482 + degradedDays: [], 483 + lookbackPeriod, 484 + }) 485 + : fillStatusDataFor45Days(rawData, monitorId, lookbackPeriod); 471 486 const processedData = setDataByType({ 472 487 events, 473 488 data: filledData,
+5 -2
packages/api/src/router/statusPage.utils.ts
··· 17 17 export function fillStatusDataFor45Days( 18 18 data: Array<StatusData>, 19 19 monitorId: string, 20 + lookbackPeriod = 45, 20 21 ): Array<StatusData> { 21 22 const result = []; 22 23 const dataByDay = new Map(); ··· 29 30 30 31 // Generate all 45 days from today backwards 31 32 const now = new Date(); 32 - for (let i = 0; i < 45; i++) { 33 + for (let i = 0; i < lookbackPeriod; i++) { 33 34 const date = new Date(now); 34 35 date.setUTCDate(date.getUTCDate() - i); 35 36 date.setUTCHours(0, 0, 0, 0); // Set to start of day in UTC ··· 66 67 export function fillStatusDataFor45DaysNoop({ 67 68 errorDays, 68 69 degradedDays, 70 + lookbackPeriod = 45, 69 71 }: { 70 72 errorDays: number[]; 71 73 degradedDays: number[]; 74 + lookbackPeriod?: number; 72 75 }): Array<StatusData> { 73 76 const issueDays = [...errorDays, ...degradedDays]; 74 77 const data: StatusData[] = Array.from({ length: 45 }, (_, i) => { ··· 81 84 monitorId: "1", 82 85 }; 83 86 }); 84 - return fillStatusDataFor45Days(data, "1"); 87 + return fillStatusDataFor45Days(data, "1", lookbackPeriod); 85 88 } 86 89 87 90 type Event = {