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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 5x 5x 5x 34x 34x 34x 25x 101x 100x 100x 11x 10x 10x 10x 10x 1x 10x 857x 28x 28x 364x 28x 24x 37x 37x 37x 37x 481x 481x 386x 381x 37x 37x 32x 37x 32x 28x 20x 16x 5x | /**
* Feed Health Tracker Service
*
* Tracks the outcome of feed tool invocations to provide
* cached health status without making upstream API calls.
*
* The tracker is a module-level singleton that records success/error
* outcomes for each feed tool call. The `get_server_health` tool
* reads from this tracker to report feed availability.
*
* ISMS Policy: MO-001 (Monitoring and Alerting), PE-001 (Performance Standards)
*
* @module services/FeedHealthTracker
*/
// ── Public types ──────────────────────────────────────────────────
/**
* Per-feed health status.
*
* | Value | Meaning |
* |-----------|---------|
* | `ok` | Last invocation succeeded |
* | `error` | Last invocation failed |
* | `unknown` | Feed has not been invoked yet |
*/
export interface FeedStatus {
/** Current feed health verdict */
status: 'ok' | 'error' | 'unknown';
/** ISO-8601 timestamp of the last successful invocation */
lastSuccess?: string;
/** Error message from the last failed invocation */
lastError?: string;
/** ISO-8601 timestamp of the last invocation attempt */
lastAttempt?: string;
}
/**
* Overall feed availability level.
*
* | Level | Condition | Description |
* |--------------|------------------------------------------|-------------|
* | Full | ≥10/13 ok | Normal operation |
* | Degraded | 5–9/13 ok | Reduced data quality expected |
* | Sparse | 1–4/13 ok | Minimal data, analysis-only mode likely |
* | Unavailable | 0/13 ok AND ≥1 probed feed errored | All probed feeds failing |
* | Unknown | 0/13 ok AND 0 errors (no probes yet) | Cache is empty — status cannot be determined without a live probe |
*
* `Unknown` is distinct from `Unavailable` to prevent consumers from
* interpreting "no cached data" as "feeds are down". See issue #1.
*/
export type AvailabilityLevel = 'Full' | 'Degraded' | 'Sparse' | 'Unavailable' | 'Unknown';
/** Summary of feed availability. */
export interface FeedAvailability {
/** Feeds with `status === 'ok'` in the cache. */
operationalFeeds: number;
/** Feeds with `status === 'error'` in the cache. */
errorFeeds: number;
/** Feeds that have never been probed (`status === 'unknown'`). */
unknownFeeds: number;
totalFeeds: number;
level: AvailabilityLevel;
}
// ── Constants ─────────────────────────────────────────────────────
/** All tracked feed tool names. */
export const FEED_TOOL_NAMES = [
'get_meps_feed',
'get_events_feed',
'get_procedures_feed',
'get_adopted_texts_feed',
'get_mep_declarations_feed',
'get_documents_feed',
'get_plenary_documents_feed',
'get_committee_documents_feed',
'get_plenary_session_documents_feed',
'get_external_documents_feed',
'get_parliamentary_questions_feed',
'get_corporate_bodies_feed',
'get_controlled_vocabularies_feed',
] as const;
/** Threshold boundaries for availability levels. */
const FULL_THRESHOLD = 10;
const DEGRADED_THRESHOLD = 5;
// ── FeedHealthTracker implementation ──────────────────────────────
/**
* Singleton service that records feed tool invocation outcomes
* and derives per-feed and overall availability health.
*
* Does not make network calls — all data comes from tool dispatch hooks.
*/
export class FeedHealthTracker {
private readonly statuses = new Map<string, FeedStatus>();
private readonly startTime: number;
private readonly feedToolSet: ReadonlySet<string>;
constructor() {
this.startTime = Date.now();
this.feedToolSet = new Set<string>(FEED_TOOL_NAMES);
}
/** Returns `true` when the tool name identifies a tracked feed tool. */
isFeedTool(name: string): boolean {
return this.feedToolSet.has(name);
}
/** Record a successful feed invocation. Silently ignores unknown feed names. */
recordSuccess(feedName: string): void {
if (!this.feedToolSet.has(feedName)) return;
const now = new Date().toISOString();
this.statuses.set(feedName, {
status: 'ok',
lastSuccess: now,
lastAttempt: now,
});
}
/** Record a failed feed invocation, preserving the last success timestamp. Silently ignores unknown feed names. */
recordError(feedName: string, errorMessage: string): void {
if (!this.feedToolSet.has(feedName)) return;
const now = new Date().toISOString();
const existing = this.statuses.get(feedName);
const entry: FeedStatus = {
status: 'error',
lastError: errorMessage,
lastAttempt: now,
};
if (existing?.lastSuccess !== undefined) {
entry.lastSuccess = existing.lastSuccess;
}
this.statuses.set(feedName, entry);
}
/** Get the current status of a single feed (defaults to `unknown`). */
getStatus(feedName: string): FeedStatus {
return this.statuses.get(feedName) ?? { status: 'unknown' };
}
/** Get the statuses of all tracked feeds keyed by tool name. */
getAllStatuses(): Record<string, FeedStatus> {
const result: Record<string, FeedStatus> = {};
for (const name of FEED_TOOL_NAMES) {
result[name] = this.getStatus(name);
}
return result;
}
/** Server uptime in whole seconds since tracker creation. */
getUptimeSeconds(): number {
return Math.floor((Date.now() - this.startTime) / 1000);
}
/** Derive the overall availability level from current feed statuses. */
getAvailability(): FeedAvailability {
let operational = 0;
let errored = 0;
let unknown = 0;
for (const name of FEED_TOOL_NAMES) {
const { status } = this.getStatus(name);
if (status === 'ok') operational++;
else if (status === 'error') errored++;
else unknown++;
}
const total = FEED_TOOL_NAMES.length;
return {
operationalFeeds: operational,
errorFeeds: errored,
unknownFeeds: unknown,
totalFeeds: total,
level: deriveLevel(operational, errored),
};
}
/**
* Reset all tracked statuses.
* Intended for testing only.
* @internal
*/
reset(): void {
this.statuses.clear();
}
}
// ── Helpers ───────────────────────────────────────────────────────
/**
* Map operational feed count to an availability level.
*
* When `operational === 0`, we distinguish two cases:
* - If at least one probed feed has errored, report `Unavailable` (feeds are down).
* - Otherwise (cache is empty, no probes yet) report `Unknown` — consumers
* must not treat "no data" as "feeds are down". See issue #1.
*/
function deriveLevel(operational: number, errored: number): AvailabilityLevel {
if (operational >= FULL_THRESHOLD) return 'Full';
if (operational >= DEGRADED_THRESHOLD) return 'Degraded';
if (operational >= 1) return 'Sparse';
if (errored >= 1) return 'Unavailable';
return 'Unknown';
}
// ── Module-level singleton ────────────────────────────────────────
/** Global feed health tracker instance used by tool dispatch and the health tool. */
export const feedHealthTracker = new FeedHealthTracker();
|