All files / src/lib health.ts

100% Statements 70/70
86.84% Branches 33/38
100% Functions 5/5
100% Lines 70/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                    1x 1x 1x 1x 1x 1x               1x 7x 1x 1x 6x 7x 10x 10x 10x 10x 10x 10x 6x 6x   1x 57x 27x 27x 57x 1x 1x 1x 1x 1x 1x 36x 26x 26x 3x 3x 3x 3x 3x 3x 57x 57x 57x               1x 34x 16x 16x 34x 34x 34x 37x 37x 37x 34x 34x 34x   1x 39x 39x 39x 39x   1x 10x 7x 2x 2x 5x 5x 10x 10x  
import type { Health, Robot, Stats } from "../api/types";
 
export type OverallState = "ok" | "degraded" | "down" | "unknown";
 
export interface HealthSummary {
  state: OverallState;
  label: string;
  detail: string;
}
 
const STATE_LABELS: Record<OverallState, string> = {
  ok: "Всё в порядке",
  degraded: "Работает с деградацией",
  down: "Недоступно",
  unknown: "Нет данных",
};
 
export interface CheckEntry {
  name: string;
  status: string;
  ok: boolean;
}
 
export function checksList(ready: Health | null): CheckEntry[] {
  if (!ready) {
    return [];
  }
  const entries: CheckEntry[] = [];
  for (const [name, value] of Object.entries(ready.checks ?? {})) {
    entries.push({
      name,
      status: value,
      ok: value === "ok" || value === "connected",
    });
  }
  return entries.sort((left, right) => left.name.localeCompare(right.name));
}
 
export function summarizeHealth(live: Health | null, ready: Health | null): HealthSummary {
  if (!live) {
    return { state: "unknown", label: STATE_LABELS.unknown, detail: "Сервис не отвечает" };
  }
  if (!ready) {
    return {
      state: "degraded",
      label: STATE_LABELS.degraded,
      detail: "Процесс жив, readiness не отвечает",
    };
  }
  if (ready.status === "ok") {
    return { state: "ok", label: STATE_LABELS.ok, detail: "База и брокер доступны" };
  }
  const failing = checksList(ready)
    .filter((entry) => !entry.ok)
    .map((entry) => entry.name);
  return {
    state: "degraded",
    label: STATE_LABELS.degraded,
    detail: failing.length > 0 ? `Проблемы: ${failing.join(", ")}` : "Сервис сообщает о деградации",
  };
}
 
export interface QueueRow {
  status: string;
  count: number;
  share: number;
}
 
export function queueRows(stats: Stats | null): QueueRow[] {
  if (!stats) {
    return [];
  }
  const total = stats.task_total || Object.values(stats.tasks ?? {}).reduce((sum, value) => sum + value, 0);
  return Object.entries(stats.tasks ?? {})
    .map(([status, count]) => ({
      status,
      count,
      share: total === 0 ? 0 : Math.round((count / total) * 1000) / 10,
    }))
    .sort((left, right) => right.count - left.count);
}
 
export function robotAvailability(robots: Robot[]): { online: number; total: number; percent: number } {
  const total = robots.length;
  const online = robots.filter((robot) => robot.status !== "offline").length;
  return { online, total, percent: total === 0 ? 0 : Math.round((online / total) * 1000) / 10 };
}
 
export function staleRobots(robots: Robot[], now: Date = new Date(), thresholdSeconds = 300): Robot[] {
  return robots.filter((robot) => {
    if (!robot.last_seen_at) {
      return true;
    }
    const seen = new Date(robot.last_seen_at).getTime();
    return (now.getTime() - seen) / 1000 > thresholdSeconds;
  });
}