Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.5 KB
Raw Blame
import { z } from "zod";

import type { StatsAuthenticationContext, StatsProvider } from "../types";

const checkSchema = z
  .object({
    status: z.enum(["new", "up", "grace", "down", "paused"]),
  })
  .passthrough();

const checksResponseSchema = z
  .object({
    checks: z.array(checkSchema),
  })
  .passthrough();

const countSchema = z.number().finite().int().nonnegative();

const getHttpAuthentication = (context: StatsAuthenticationContext) => ({
  headers: { "X-Api-Key": context.secret("apiKey") },
});

export const healthchecksStatsProvider = {
  getHttpAuthentication,
  metrics: [
    { key: "checksUp", label: "Up checks", unit: "count" },
    { key: "checksDown", label: "Down checks", unit: "count" },
    { key: "checksGrace", label: "Checks in grace", unit: "count" },
    { key: "checksNew", label: "New checks", unit: "count" },
  ],
  async fetchAsync(context) {
    const response = await context.requestAsync("/api/v3/checks/", {
      headers: getHttpAuthentication(context).headers,
      signal: context.signal,
    });
    const checks = checksResponseSchema.parse(response).checks;

    return {
      checksUp: countSchema.parse(checks.filter((check) => check.status === "up").length),
      checksDown: countSchema.parse(checks.filter((check) => check.status === "down").length),
      checksGrace: countSchema.parse(checks.filter((check) => check.status === "grace").length),
      checksNew: countSchema.parse(checks.filter((check) => check.status === "new").length),
    };
  },
} satisfies StatsProvider;