Traefik's widget listed every router with rule/TLS/status detail, which reads as noise now that other widgets in the dashboard show a single headline stat - collapse it to a route count (with an "enabled" secondary stat when some routers aren't) plus the existing middlewares count as a small footer line, same visual language as the new service widgets. Coolify's widget only reported project count; /api/v1/resources gives the deployed-resource count (apps/services/databases across all projects), which is the more useful "how much is actually running" number, so it's added as a second stat alongside Projects.
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import type { CoolifyServiceWidget } from "@/lib/config/schema";
|
|
import type { ServiceStat } from "@/lib/types/service-result";
|
|
import { serviceBaseUrl } from "./base-url";
|
|
|
|
const DETAIL_LIMIT = 5;
|
|
|
|
interface CoolifyProject {
|
|
name: string;
|
|
}
|
|
|
|
async function fetchCoolify<T>(base: string, path: string, apiToken: string): Promise<T> {
|
|
const response = await fetch(`${base}${path}`, {
|
|
headers: { Authorization: `Bearer ${apiToken}` },
|
|
signal: AbortSignal.timeout(5_000),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`Coolify API returned ${response.status}`);
|
|
}
|
|
return (await response.json()) as T;
|
|
}
|
|
|
|
export async function collectCoolify(
|
|
widget: CoolifyServiceWidget
|
|
): Promise<{ stats: ServiceStat[]; detail?: string[] }> {
|
|
const base = serviceBaseUrl(widget);
|
|
const [projects, resources] = await Promise.all([
|
|
fetchCoolify<CoolifyProject[]>(base, "/api/v1/projects", widget.apiToken),
|
|
fetchCoolify<unknown[]>(base, "/api/v1/resources", widget.apiToken),
|
|
]);
|
|
const names = projects.slice(0, DETAIL_LIMIT).map((p) => p.name);
|
|
if (projects.length > DETAIL_LIMIT) names.push(`+${projects.length - DETAIL_LIMIT} more`);
|
|
|
|
return {
|
|
stats: [
|
|
{ label: "Projects", value: String(projects.length) },
|
|
{ label: "Resources", value: String(resources.length) },
|
|
],
|
|
detail: names.length > 0 ? names : undefined,
|
|
};
|
|
}
|