ServiceMap: update tests and backend/app (+7 areas)
Public changes compared with the previous published export: 89 files changed. Affected areas: - backend/app: 21 files - dependencies: 5 files - deployment and CI: 6 files - documentation: 2 files - frontend: 3 files - frontend/src: 20 files - project files: 1 file - tests: 30 files - unraid: 1 file Changed files: - Updated: ".env.example" - Updated: "Dockerfile.unraid" - Updated: "README.md" - Updated: "backend/Dockerfile" - Added: "backend/app/alerts.py" - Updated: "backend/app/auth.py" - Added: "backend/app/backups.py" - Updated: "backend/app/config_io.py" - Updated: "backend/app/demo.py" - Updated: "backend/app/discovery.py" - Updated: "backend/app/discovery_store.py" - Updated: "backend/app/docker_discovery.py" - Updated: "backend/app/events.py" - Updated: "backend/app/evidence.py" - Updated: "backend/app/health.py" - Updated: "backend/app/incidents.py" - Added: "backend/app/loops.py" - Updated: "backend/app/main.py" - Updated: "backend/app/models.py" - Updated: "backend/app/observation.py" - Updated: "backend/app/probes.py" - Added: "backend/app/response_cache.py" - Updated: "backend/app/scheduler.py" - Updated: "backend/app/semantic.py" - Updated: "backend/app/store.py" - Updated: "backend/pyproject.toml" - Updated: "backend/tests/test_api.py" - Updated: "backend/tests/test_config_io.py" - Updated: "backend/tests/test_demo.py" - Updated: "backend/tests/test_discovery_v120.py" - Updated: "backend/tests/test_health.py" - Updated: "backend/tests/test_probes.py" - Updated: "backend/tests/test_scheduler.py" - Updated: "backend/tests/test_v130_probe_reachability.py" - Updated: "backend/tests/test_v160_evidence_changes.py" - Added: "backend/tests/test_v163_alerts.py" - Added: "backend/tests/test_v163_backups.py" - Added: "backend/tests/test_v163_data_integrity.py" - Added: "backend/tests/test_v163_efficiency.py" - Added: "backend/tests/test_v163_health_performance.py" - Added: "backend/tests/test_v163_loops.py" - Added: "backend/tests/test_v163_management.py" - Added: "backend/tests/test_v163_realtime.py" - Added: "backend/tests/test_v163_security.py" - Added: "backend/tests/test_v163_semantic_etag.py" - Added: "compose.demo.yaml" - Updated: "compose.yaml" - Added: "docker-compose.broker.yml" - Updated: "docker-compose.yml" - Updated: "docs/ARCHITECTURE.md" - Added: "frontend/e2e/a11y.spec.ts" - Updated: "frontend/e2e/demo.spec.ts" - Updated: "frontend/index.html" - Updated: "frontend/nginx.conf" - Updated: "frontend/package-lock.json" - Updated: "frontend/package.json" - Updated: "frontend/playwright.config.ts" - Updated: "frontend/src/App.tsx" - Updated: "frontend/src/LivingEdge.tsx" - Added: "frontend/src/activityView.test.ts" - Added: "frontend/src/activityView.ts" - Updated: "frontend/src/api.ts" - Added: "frontend/src/apiAuth.test.ts" - Added: "frontend/src/components/ActionsMenu.tsx" - Added: "frontend/src/components/ConfirmDialog.test.tsx" - Added: "frontend/src/components/ConfirmDialog.tsx" - Added: "frontend/src/components/StatusIcon.tsx" - Updated: "frontend/src/demo/DemoWalkthrough.test.ts" - Updated: "frontend/src/demo/DemoWalkthrough.tsx" - Added: "frontend/src/elk-worker-url.d.ts" - Added: "frontend/src/labels.ts" - Updated: "frontend/src/main.tsx" - Added: "frontend/src/realtime/refreshPlan.test.ts" - Added: "frontend/src/realtime/refreshPlan.ts" - Updated: "frontend/src/realtime/useRealtimeEvents.ts" - Added: "frontend/src/routing/urlState.test.ts" - Added: "frontend/src/routing/urlState.ts" - Updated: "frontend/src/styles.css" - Added: "frontend/src/topology/GraphListView.test.tsx" - Added: "frontend/src/topology/GraphListView.tsx" - Updated: "frontend/src/topology/ServiceNode.tsx" - Updated: "frontend/src/topology/TopologyCommandBar.tsx" - Added: "frontend/src/topology/semanticGraph.test.ts" - Updated: "frontend/src/topology/semanticGraph.ts" - Added: "frontend/src/topologyLayout.test.ts" - Updated: "frontend/src/topologyLayout.ts" - Updated: "package-lock.json" - Updated: "package.json" - Updated: "unraid/my-servicemap.xml" Export provenance/manifests refreshed; excluded from the change summary. Publish curated ServiceMap source from af46f684e468dffd3a5190e28d3b572d4c26bfd7
This commit is contained in:
1 parent
d7319250e8
commit
adc2c70c21
91 files changed
+9205
-1691
No files matched your search
+148
-30
@@ -1,3 +1,5 @@
|
||||
import { SSE_EVENT_TYPES } from "./realtime/refreshPlan";
|
||||
|
||||
export type HealthStatus =
|
||||
| "healthy"
|
||||
| "degraded"
|
||||
@@ -198,6 +200,7 @@ export type ServiceSnapshot = {
|
||||
evidence: RelationEvidence[];
|
||||
health_evidence: HealthEvidence[];
|
||||
health_resolution: HealthResolution;
|
||||
maintenance_until?: string | null;
|
||||
};
|
||||
|
||||
export type ServiceSource = {
|
||||
@@ -485,6 +488,7 @@ export type OverviewSummary = {
|
||||
|
||||
export type DemoState = {
|
||||
active: boolean;
|
||||
enabled: boolean;
|
||||
synthetic: boolean;
|
||||
scenario_name: string | null;
|
||||
service_count: number;
|
||||
@@ -682,37 +686,145 @@ export type ReconcileResult = {
|
||||
service_ids: string[];
|
||||
};
|
||||
|
||||
let accessToken: string | null = null;
|
||||
// The admin token is kept per browser tab (sessionStorage): it survives a
|
||||
// reload but not closing the tab, and never reaches topology config.
|
||||
const TOKEN_KEY = "servicemap.adminToken";
|
||||
export const AUTH_REQUIRED_EVENT = "servicemap:auth-required";
|
||||
|
||||
function readStoredToken(): string | null {
|
||||
try {
|
||||
return window.sessionStorage.getItem(TOKEN_KEY);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
let accessToken: string | null = typeof window === "undefined" ? null : readStoredToken();
|
||||
|
||||
/** An optional in-memory OIDC bearer token; never persisted in topology config. */
|
||||
export function setApiAccessToken(token: string | null): void {
|
||||
accessToken = token?.trim() || null;
|
||||
try {
|
||||
if (accessToken) window.sessionStorage.setItem(TOKEN_KEY, accessToken);
|
||||
else window.sessionStorage.removeItem(TOKEN_KEY);
|
||||
} catch {
|
||||
// Storage may be unavailable (private mode); the token stays in memory.
|
||||
}
|
||||
}
|
||||
|
||||
function requestHeaders(init?: RequestInit): HeadersInit {
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
|
||||
...init?.headers,
|
||||
};
|
||||
export function hasApiAccessToken(): boolean {
|
||||
return accessToken !== null;
|
||||
}
|
||||
|
||||
async function request<T>(url: string, init?: RequestInit): Promise<T> {
|
||||
const response = await fetch(url, {
|
||||
headers: requestHeaders(init),
|
||||
...init,
|
||||
});
|
||||
/** An API failure that keeps the HTTP status, e.g. 401 for a locked editor. */
|
||||
export class ApiError extends Error {
|
||||
readonly status: number;
|
||||
|
||||
constructor(message: string, status: number) {
|
||||
super(message);
|
||||
this.name = "ApiError";
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
function errorMessage(detail: unknown, status: number): string {
|
||||
if (typeof detail === "string" && detail) return detail;
|
||||
if (Array.isArray(detail)) {
|
||||
// FastAPI validation errors: [{loc: [...], msg: "..."}]
|
||||
const parts = detail
|
||||
.map((item) => {
|
||||
const entry = item as { loc?: unknown[]; msg?: string };
|
||||
const field = Array.isArray(entry.loc) ? entry.loc.filter((part) => part !== "body").join(".") : "";
|
||||
return entry.msg ? (field ? `${field}: ${entry.msg}` : entry.msg) : "";
|
||||
})
|
||||
.filter(Boolean);
|
||||
if (parts.length) return parts.join("; ");
|
||||
}
|
||||
return `Verzoek mislukt (${status})`;
|
||||
}
|
||||
|
||||
function requestHeaders(init?: RequestInit, token: string | null = accessToken): Headers {
|
||||
const headers = new Headers(init?.headers);
|
||||
if (init?.body !== undefined && !headers.has("Content-Type")) {
|
||||
headers.set("Content-Type", "application/json");
|
||||
}
|
||||
if (token && !headers.has("Authorization")) headers.set("Authorization", `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
|
||||
async function request<T>(url: string, init?: RequestInit, token: string | null = accessToken): Promise<T> {
|
||||
// Spread init first: a caller's headers are merged above, never dropped.
|
||||
const response = await fetch(url, { ...init, headers: requestHeaders(init, token) });
|
||||
|
||||
if (!response.ok) {
|
||||
const payload = await response.json().catch(() => null);
|
||||
throw new Error(payload?.detail || `Request failed (${response.status})`);
|
||||
if (response.status === 401 && token === accessToken && typeof window !== "undefined") {
|
||||
// The tab's token is missing or no longer valid: forget it and let the
|
||||
// UI ask for it. Explicit candidate tokens (verifyWriteAccess) don't.
|
||||
if (accessToken) setApiAccessToken(null);
|
||||
window.dispatchEvent(new Event(AUTH_REQUIRED_EVENT));
|
||||
}
|
||||
throw new ApiError(errorMessage(payload?.detail, response.status), response.status);
|
||||
}
|
||||
|
||||
if (response.status === 204) return undefined as T;
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
export type AlertSettings = {
|
||||
enabled: boolean;
|
||||
ntfy_server: string;
|
||||
ntfy_topic: string;
|
||||
min_severity: Criticality;
|
||||
delay_seconds: number;
|
||||
notify_resolved: boolean;
|
||||
reminder_minutes: number;
|
||||
public_url: string;
|
||||
ntfy_token_set?: boolean;
|
||||
};
|
||||
|
||||
export type BackupInfo = {
|
||||
name: string;
|
||||
kind: "backup" | "pre-restore";
|
||||
created_at: string;
|
||||
size_bytes: number;
|
||||
};
|
||||
|
||||
/** Download a backup with the tab's token (a plain link carries none). */
|
||||
export async function downloadBackupFile(name: string): Promise<Blob> {
|
||||
const response = await fetch(`/api/v1/backups/${encodeURIComponent(name)}`, {
|
||||
headers: requestHeaders(),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const payload = await response.json().catch(() => null);
|
||||
throw new ApiError(errorMessage(payload?.detail, response.status), response.status);
|
||||
}
|
||||
return response.blob();
|
||||
}
|
||||
|
||||
export type ServiceMapMeta = {
|
||||
version: string;
|
||||
auth_mode: string;
|
||||
write_auth?: "none" | "admin_token" | "oidc";
|
||||
};
|
||||
|
||||
export const api = {
|
||||
alertSettings: () => request<AlertSettings>("/api/v1/settings/alerts"),
|
||||
/** ``ntfyToken``: undefined keeps the stored token, "" removes it. */
|
||||
saveAlertSettings: (settings: AlertSettings, ntfyToken?: string) =>
|
||||
request<AlertSettings>("/api/v1/settings/alerts", {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ ...settings, ntfy_token_set: undefined, ntfy_token: ntfyToken }),
|
||||
}),
|
||||
backups: () => request<BackupInfo[]>("/api/v1/backups"),
|
||||
createBackup: () => request<BackupInfo>("/api/v1/backups", { method: "POST" }),
|
||||
restoreBackup: (name: string) =>
|
||||
request<BackupInfo>(`/api/v1/backups/${encodeURIComponent(name)}/restore`, { method: "POST" }),
|
||||
testAlert: () =>
|
||||
request<{ ok: boolean; detail: string }>("/api/v1/settings/alerts/test", { method: "POST" }),
|
||||
meta: () => request<ServiceMapMeta>("/api/v1/meta"),
|
||||
/** Resolves only when ``token`` may change ServiceMap. */
|
||||
verifyWriteAccess: (token: string) =>
|
||||
request<{ ok: boolean }>("/api/v1/auth/verify", { method: "POST" }, token.trim()),
|
||||
listServices: (includeHidden = false) =>
|
||||
request<ServiceSnapshot[]>(
|
||||
`/api/v1/services?include_hidden=${includeHidden}`,
|
||||
@@ -756,13 +868,27 @@ export const api = {
|
||||
request<IncidentDetail>(`/api/v1/incidents/${incidentId}/acknowledge`, {
|
||||
method: "POST",
|
||||
}),
|
||||
events: (limit = 50) =>
|
||||
request<EventRecord[]>(`/api/v1/events?limit=${limit}`),
|
||||
events: (limit = 50, options: { eventType?: string; before?: string } = {}) => {
|
||||
const params = new URLSearchParams({ limit: String(limit) });
|
||||
if (options.eventType) params.set("event_type", options.eventType);
|
||||
if (options.before) params.set("before", options.before);
|
||||
return request<EventRecord[]>(`/api/v1/events?${params.toString()}`);
|
||||
},
|
||||
resolveIncident: (incidentId: string, note?: string) =>
|
||||
request<IncidentDetail>(`/api/v1/incidents/${encodeURIComponent(incidentId)}/resolve`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ note: note || null }),
|
||||
}),
|
||||
addIncidentNote: (incidentId: string, text: string) =>
|
||||
request<IncidentDetail>(`/api/v1/incidents/${encodeURIComponent(incidentId)}/notes`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ text }),
|
||||
}),
|
||||
overview: () => request<OverviewSummary>("/api/v1/overview"),
|
||||
maintenance: (serviceId: string, enabled: boolean, reason?: string) =>
|
||||
maintenance: (serviceId: string, enabled: boolean, reason?: string, until?: string | null) =>
|
||||
request<Service>(`/api/v1/services/${serviceId}/maintenance`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ enabled, reason }),
|
||||
body: JSON.stringify({ enabled, reason, until: until ?? null }),
|
||||
}),
|
||||
demoState: () => request<DemoState>("/api/v1/demo"),
|
||||
loadDemo: () =>
|
||||
@@ -782,7 +908,7 @@ export const api = {
|
||||
),
|
||||
exportConfig: async (format: "yaml" | "json") => {
|
||||
const response = await fetch(`/api/v1/config?format=${format}`, { headers: requestHeaders() });
|
||||
if (!response.ok) throw new Error(`Export failed (${response.status})`);
|
||||
if (!response.ok) throw new Error(`Exporteren mislukt (${response.status})`);
|
||||
return response.text();
|
||||
},
|
||||
discoverDocker: (dockerHost: string) =>
|
||||
@@ -921,17 +1047,9 @@ export function subscribeToEvents(
|
||||
}
|
||||
};
|
||||
source.onmessage = emit;
|
||||
for (const eventType of [
|
||||
"probe.completed",
|
||||
"service.state_changed",
|
||||
"service.maintenance_changed",
|
||||
"flapping.detected",
|
||||
"incident.opened",
|
||||
"incident.updated",
|
||||
"incident.acknowledged",
|
||||
"incident.resolved",
|
||||
"topology.changed",
|
||||
]) {
|
||||
// Named SSE events are only delivered to explicit listeners; a type missing
|
||||
// here is silently dropped by EventSource.
|
||||
for (const eventType of SSE_EVENT_TYPES) {
|
||||
source.addEventListener(eventType, emit);
|
||||
}
|
||||
source.onerror = () => onError?.();
|
||||
|
||||
Reference in new issue
Block a user