add server filters to health check table

This commit is contained in:
miloschwartz
2026-04-21 18:35:38 -07:00
parent 6f07156075
commit 88eb1649e4
6 changed files with 562 additions and 73 deletions

View File

@@ -3,7 +3,9 @@ import DismissableBanner from "@app/components/DismissableBanner";
import { internal } from "@app/lib/api";
import { authCookieHeader } from "@app/lib/api/cookies";
import { ListHealthChecksResponse } from "@server/routers/healthChecks/types";
import { AxiosResponse } from "axios";
import { GetResourceResponse } from "@server/routers/resource/getResource";
import { GetSiteResponse } from "@server/routers/site/getSite";
import type ResponseT from "@server/types/Response";
import { HeartPulse } from "lucide-react";
import type { Metadata } from "next";
import { getTranslations } from "next-intl/server";
@@ -26,24 +28,70 @@ function parsePositiveInt(s: string | undefined): number | undefined {
return n;
}
function appendListFilters(
apiSp: URLSearchParams,
searchParams: URLSearchParams
) {
const query = searchParams.get("query");
if (query) apiSp.set("query", query);
const hcMode = searchParams.get("hcMode");
if (
hcMode === "http" ||
hcMode === "tcp" ||
hcMode === "snmp" ||
hcMode === "ping"
) {
apiSp.set("hcMode", hcMode);
}
const hcHealth = searchParams.get("hcHealth");
if (
hcHealth === "healthy" ||
hcHealth === "unhealthy" ||
hcHealth === "unknown"
) {
apiSp.set("hcHealth", hcHealth);
}
const hcEnabled = searchParams.get("hcEnabled");
if (hcEnabled === "true" || hcEnabled === "false") {
apiSp.set("hcEnabled", hcEnabled);
}
const siteId = parsePositiveInt(searchParams.get("siteId") ?? undefined);
if (siteId) {
apiSp.set("siteId", String(siteId));
}
const resourceId = parsePositiveInt(
searchParams.get("resourceId") ?? undefined
);
if (resourceId) {
apiSp.set("resourceId", String(resourceId));
}
}
export default async function AlertingHealthChecksPage(
props: AlertingHealthChecksPageProps
) {
const params = await props.params;
const searchParams = new URLSearchParams(await props.searchParams);
const page = Math.max(1, parsePositiveInt(searchParams.get("page") ?? undefined) ?? 1);
const page = Math.max(
1,
parsePositiveInt(searchParams.get("page") ?? undefined) ?? 1
);
const pageSize = Math.max(
1,
parsePositiveInt(searchParams.get("pageSize") ?? undefined) ?? 20
);
const pageIndex = page - 1;
const query = searchParams.get("query") ?? undefined;
const apiSp = new URLSearchParams();
apiSp.set("limit", String(pageSize));
apiSp.set("offset", String(pageIndex * pageSize));
if (query) apiSp.set("query", query);
appendListFilters(apiSp, searchParams);
let healthChecks: ListHealthChecksResponse["healthChecks"] = [];
let pagination: ListHealthChecksResponse["pagination"] = {
@@ -51,18 +99,80 @@ export default async function AlertingHealthChecksPage(
limit: pageSize,
offset: pageIndex * pageSize
};
const siteIdParam = parsePositiveInt(
searchParams.get("siteId") ?? undefined
);
const resourceIdParam = parsePositiveInt(
searchParams.get("resourceId") ?? undefined
);
const header = await authCookieHeader();
try {
const res = await internal.get<AxiosResponse<ListHealthChecksResponse>>(
const res = await internal.get(
`/org/${params.orgId}/health-checks?${apiSp.toString()}`,
await authCookieHeader()
header
);
const responseData = res.data.data;
healthChecks = responseData.healthChecks;
pagination = responseData.pagination;
const responseData = (res.data as ResponseT<ListHealthChecksResponse>)
.data;
if (responseData) {
healthChecks = responseData.healthChecks;
pagination = responseData.pagination;
}
} catch {
// leave defaults
}
let initialFilterSite: {
siteId: number;
name: string;
type: string;
} | null = null;
if (siteIdParam) {
try {
const siteRes = await internal.get(`/site/${siteIdParam}`, header);
const s = (siteRes.data as ResponseT<GetSiteResponse>).data;
if (s && s.orgId === params.orgId) {
initialFilterSite = {
siteId: s.siteId,
name: s.name,
type: s.type
};
}
} catch {
// leave null
}
}
let initialFilterResource: {
name: string;
resourceId: number;
fullDomain: string | null;
niceId: string;
ssl: boolean;
} | null = null;
if (resourceIdParam) {
try {
const resourceRes = await internal.get(
`/resource/${resourceIdParam}`,
header
);
const r = (resourceRes.data as ResponseT<GetResourceResponse>).data;
if (r && r.orgId === params.orgId) {
initialFilterResource = {
name: r.name,
resourceId: r.resourceId,
fullDomain: r.fullDomain,
niceId: r.niceId,
ssl: r.ssl
};
}
} catch {
// leave null
}
}
const t = await getTranslations();
return (
@@ -80,6 +190,12 @@ export default async function AlertingHealthChecksPage(
orgId={params.orgId}
healthChecks={healthChecks}
rowCount={pagination.total}
pagination={{
pageIndex,
pageSize
}}
initialFilterSite={initialFilterSite}
initialFilterResource={initialFilterResource}
/>
</div>
);