mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-21 13:06:59 +02:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9cc3190e3a | |||
| 4c873e7c48 | |||
| 1580b7abff |
@@ -41,7 +41,7 @@ services:
|
|||||||
- 80:80 # Port for traefik because of the network_mode
|
- 80:80 # Port for traefik because of the network_mode
|
||||||
|
|
||||||
traefik:
|
traefik:
|
||||||
image: traefik:v3.7
|
image: traefik:v3.6
|
||||||
container_name: traefik
|
container_name: traefik
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
network_mode: service:gerbil # Ports appear on the gerbil service
|
network_mode: service:gerbil # Ports appear on the gerbil service
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ services:
|
|||||||
- 80:80{{end}}
|
- 80:80{{end}}
|
||||||
|
|
||||||
traefik:
|
traefik:
|
||||||
image: docker.io/traefik:v3.7
|
image: docker.io/traefik:v3.6
|
||||||
container_name: traefik
|
container_name: traefik
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
{{if .InstallGerbil}}network_mode: service:gerbil # Ports appear on the gerbil service{{end}}{{if not .InstallGerbil}}
|
{{if .InstallGerbil}}network_mode: service:gerbil # Ports appear on the gerbil service{{end}}{{if not .InstallGerbil}}
|
||||||
|
|||||||
+105
-4
@@ -1,6 +1,6 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { db, logsDb, statusHistory } from "@server/db";
|
import { db, logsDb, statusHistory } from "@server/db";
|
||||||
import { and, eq, gte, lt, asc, desc } from "drizzle-orm";
|
import { and, eq, gte, lt, asc, desc, inArray } from "drizzle-orm";
|
||||||
import { regionalCache as cache } from "#dynamic/lib/cache";
|
import { regionalCache as cache } from "#dynamic/lib/cache";
|
||||||
|
|
||||||
const STATUS_HISTORY_CACHE_TTL = 60; // seconds
|
const STATUS_HISTORY_CACHE_TTL = 60; // seconds
|
||||||
@@ -264,9 +264,7 @@ export function computeBuckets(
|
|||||||
|
|
||||||
// Shift by the client's offset before formatting so the label reflects
|
// Shift by the client's offset before formatting so the label reflects
|
||||||
// their local calendar date rather than the UTC date of dayStartSec
|
// their local calendar date rather than the UTC date of dayStartSec
|
||||||
const dateStr = new Date(
|
const dateStr = new Date((dayStartSec + tzOffsetMinutes * 60) * 1000)
|
||||||
(dayStartSec + tzOffsetMinutes * 60) * 1000
|
|
||||||
)
|
|
||||||
.toISOString()
|
.toISOString()
|
||||||
.slice(0, 10);
|
.slice(0, 10);
|
||||||
|
|
||||||
@@ -304,3 +302,106 @@ export function computeBuckets(
|
|||||||
|
|
||||||
return { buckets, totalDowntime };
|
return { buckets, totalDowntime };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type BatchedStatusHistoryResponse = Record<
|
||||||
|
string,
|
||||||
|
StatusHistoryResponse
|
||||||
|
>;
|
||||||
|
|
||||||
|
export async function getBatchedStatusHistory(
|
||||||
|
entityType: string,
|
||||||
|
entityIds: number[],
|
||||||
|
days: number
|
||||||
|
): Promise<BatchedStatusHistoryResponse> {
|
||||||
|
// const cacheKey = statusHistoryCacheKey(entityType, entityId, days);
|
||||||
|
// const cached = await cache.get<StatusHistoryResponse>(cacheKey);
|
||||||
|
// if (cached !== undefined) {
|
||||||
|
// return cached;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Anchor to UTC midnight so the query window aligns with stable calendar days
|
||||||
|
const utcToday = new Date();
|
||||||
|
utcToday.setUTCHours(0, 0, 0, 0);
|
||||||
|
const todayMidnightSec = Math.floor(utcToday.getTime() / 1000);
|
||||||
|
const startSec = todayMidnightSec - days * 86400;
|
||||||
|
|
||||||
|
const events = await logsDb
|
||||||
|
.select()
|
||||||
|
.from(statusHistory)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(statusHistory.entityType, entityType),
|
||||||
|
inArray(statusHistory.entityId, entityIds),
|
||||||
|
gte(statusHistory.timestamp, startSec)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.orderBy(asc(statusHistory.timestamp));
|
||||||
|
|
||||||
|
// Fetch the last known state before the window so that entities that
|
||||||
|
// haven't changed status recently still show the correct status rather
|
||||||
|
// than appearing as "no_data".
|
||||||
|
const lastKnownEvents = await logsDb
|
||||||
|
.selectDistinctOn([statusHistory.entityId, statusHistory.timestamp])
|
||||||
|
.from(statusHistory)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(statusHistory.entityType, entityType),
|
||||||
|
inArray(statusHistory.entityId, entityIds),
|
||||||
|
lt(statusHistory.timestamp, startSec)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.orderBy(desc(statusHistory.timestamp));
|
||||||
|
|
||||||
|
const eventStatusMap: Record<
|
||||||
|
number,
|
||||||
|
{
|
||||||
|
events: typeof events;
|
||||||
|
lastKnownEvent: (typeof lastKnownEvents)[number] | null;
|
||||||
|
}
|
||||||
|
> = {};
|
||||||
|
|
||||||
|
for (const event of events) {
|
||||||
|
if (!eventStatusMap[event.entityId]) {
|
||||||
|
eventStatusMap[event.entityId] = {
|
||||||
|
events: [],
|
||||||
|
lastKnownEvent:
|
||||||
|
lastKnownEvents.find(
|
||||||
|
(ev) => ev.entityId === event.entityId
|
||||||
|
) ?? null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
eventStatusMap[event.entityId].events.push(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: BatchedStatusHistoryResponse = {};
|
||||||
|
|
||||||
|
for (const entityId in eventStatusMap) {
|
||||||
|
const event = eventStatusMap[Number(entityId)];
|
||||||
|
const priorStatus = event.lastKnownEvent?.status ?? null;
|
||||||
|
|
||||||
|
const { buckets, totalDowntime } = computeBuckets(
|
||||||
|
event.events,
|
||||||
|
days,
|
||||||
|
priorStatus
|
||||||
|
);
|
||||||
|
const totalWindow = days * 86400;
|
||||||
|
const overallUptime =
|
||||||
|
totalWindow > 0
|
||||||
|
? Math.max(
|
||||||
|
0,
|
||||||
|
((totalWindow - totalDowntime) / totalWindow) * 100
|
||||||
|
)
|
||||||
|
: 100;
|
||||||
|
|
||||||
|
result[entityId] = {
|
||||||
|
entityType,
|
||||||
|
entityId: Number(entityId),
|
||||||
|
days: buckets,
|
||||||
|
overallUptimePercent: Math.round(overallUptime * 100) / 100,
|
||||||
|
totalDowntimeSeconds: totalDowntime
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// await cache.set(cacheKey, result, STATUS_HISTORY_CACHE_TTL);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
import {
|
||||||
|
getCachedStatusHistory,
|
||||||
|
statusHistoryQuerySchema,
|
||||||
|
StatusHistoryResponse
|
||||||
|
} from "@server/lib/statusHistory";
|
||||||
|
|
||||||
|
const siteIdParamsSchema = z.object({
|
||||||
|
days: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => (v ? parseInt(v, 10) : 90)),
|
||||||
|
// Minutes to add to UTC to get the requesting client's local time
|
||||||
|
// (e.g. Australia/Sydney standard time is 600). Optional and
|
||||||
|
// defaults to 0 (UTC) so older clients keep the prior behavior.
|
||||||
|
tzOffsetMinutes: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => (v ? parseInt(v, 10) : 0)),
|
||||||
|
siteIds: z
|
||||||
|
.preprocess((val) => {
|
||||||
|
if (val === undefined || val === null || val === "") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const raw = Array.isArray(val) ? val : [val];
|
||||||
|
const nums = raw
|
||||||
|
.map((v) =>
|
||||||
|
typeof v === "string" ? parseInt(v, 10) : Number(v)
|
||||||
|
)
|
||||||
|
.filter((n) => Number.isInteger(n) && n > 0);
|
||||||
|
const unique = [...new Set(nums)];
|
||||||
|
return unique.length ? unique : undefined;
|
||||||
|
}, z.array(z.number().int().positive()))
|
||||||
|
.openapi({
|
||||||
|
description: "Filter by siteIds (repeat query param)"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function getBatchedSiteStatusHistory(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedQuery = siteIdParamsSchema.safeParse(req.query);
|
||||||
|
if (!parsedQuery.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedQuery.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const entityType = "site";
|
||||||
|
const entityId = parsedParams.data.siteId;
|
||||||
|
const { days } = parsedQuery.data;
|
||||||
|
|
||||||
|
const data = await getCachedStatusHistory(entityType, entityId, days);
|
||||||
|
|
||||||
|
return response<StatusHistoryResponse>(res, {
|
||||||
|
data,
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Status history retrieved successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -640,6 +640,28 @@ export const orgQueries = {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
batchedSiteStatusHistory: ({
|
||||||
|
siteIds,
|
||||||
|
days = 90
|
||||||
|
}: {
|
||||||
|
siteIds: number[];
|
||||||
|
days?: number;
|
||||||
|
}) =>
|
||||||
|
queryOptions({
|
||||||
|
queryKey: [
|
||||||
|
"SITE_STATUS_HISTORY",
|
||||||
|
"BATCHED",
|
||||||
|
siteIds,
|
||||||
|
days
|
||||||
|
] as const,
|
||||||
|
queryFn: async ({ signal, meta }) => {
|
||||||
|
// TODO
|
||||||
|
// const res = await meta!.api.get<
|
||||||
|
// AxiosResponse<StatusHistoryResponse>
|
||||||
|
// >(`/site/${siteId}/status-history?days=${days}`, { signal });
|
||||||
|
// return res.data.data;
|
||||||
|
}
|
||||||
|
}),
|
||||||
siteStatusHistory: ({
|
siteStatusHistory: ({
|
||||||
siteId,
|
siteId,
|
||||||
days = 90
|
days = 90
|
||||||
|
|||||||
Reference in New Issue
Block a user