forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker-status.ts
More file actions
104 lines (90 loc) · 2.75 KB
/
tracker-status.ts
File metadata and controls
104 lines (90 loc) · 2.75 KB
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
99
100
101
102
103
104
// src/lib/tracker-status.ts
//
// Functions: getTrackerHealth, getHealthBadgeVariant, getHealthLabel, getHealthDescription
//
// Single source of truth for tracker health status — type, derivation logic,
// and all visual mappings (PulseDot status, Badge variant, labels, descriptions).
import type { BadgeVariant } from "@/components/ui/Badge"
import type { PulseDotStatus } from "@/components/ui/PulseDot"
import { checkWarned } from "@/lib/tracker-events"
import type { TrackerSummary } from "@/types/api"
type TrackerHealth = "healthy" | "warning" | "critical" | "error" | "paused" | "offline"
interface HealthMeta {
label: string
description: string
pulseDot: PulseDotStatus
badge: BadgeVariant
}
const HEALTH_META: Record<TrackerHealth, HealthMeta> = {
healthy: {
label: "Healthy",
description: "Ratio \u2265 2.0 \u2014 healthy buffer",
pulseDot: "healthy",
badge: "accent",
},
warning: {
label: "Warning",
description: "Ratio 1.0\u20132.0 or zero active seeds",
pulseDot: "warning",
badge: "warn",
},
critical: {
label: "Critical",
description: "Ratio < 1.0 \u2014 needs seeding",
pulseDot: "critical",
badge: "danger",
},
paused: {
label: "Paused",
description: "Polling paused after consecutive failures",
pulseDot: "paused",
badge: "danger",
},
error: {
label: "Error",
description: "Last poll returned an error",
pulseDot: "error",
badge: "danger",
},
offline: {
label: "Offline",
description: "No data available",
pulseDot: "offline",
badge: "default",
},
}
function getTrackerHealth(tracker: TrackerSummary): TrackerHealth {
if (tracker.pausedAt) return "paused"
if (tracker.lastError) return "error"
if (!tracker.latestStats) return "offline"
const { ratio, seedingCount } = tracker.latestStats
if (ratio === null) return "offline"
// Warned by tracker is always critical — potential ban risk
if (checkWarned(tracker.latestStats?.warned)) return "critical"
let status: TrackerHealth
if (ratio >= 2) status = "healthy"
else if (ratio >= 1) status = "warning"
else status = "critical"
if (seedingCount === 0 && status === "healthy") status = "warning"
return status
}
function getHealthBadgeVariant(status: TrackerHealth): BadgeVariant {
return HEALTH_META[status].badge
}
function getHealthLabel(status: TrackerHealth): string {
return HEALTH_META[status].label
}
function getHealthDescription(status: TrackerHealth): string {
return HEALTH_META[status].description
}
function getHealthPulseDot(status: TrackerHealth): PulseDotStatus {
return HEALTH_META[status].pulseDot
}
export type { TrackerHealth }
export {
getHealthBadgeVariant,
getHealthDescription,
getHealthLabel,
getHealthPulseDot,
getTrackerHealth,
}