Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
482 lines (408 loc) · 14.7 KB
/
Copy pathapp.js
File metadata and controls
482 lines (408 loc) · 14.7 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
const DATA_FILES = {
current: "data/current-status.json",
incidentsIndex: "data/incidents/index.json",
};
const REFRESH_MS = 60_000;
const HOUR_MS = 60 * 60 * 1000;
const DAY_MS = 24 * HOUR_MS;
const ui = {
statusHeadline: document.getElementById("statusHeadline"),
overallPill: document.getElementById("overallPill"),
lastRefreshed: document.getElementById("lastRefreshed"),
uptime24h: document.getElementById("uptime24h"),
uptime30d: document.getElementById("uptime30d"),
uptime90d: document.getElementById("uptime90d"),
uptimeChart: document.getElementById("uptimeChart"),
dayStrip: document.getElementById("dayStrip"),
incidentTimeline: document.getElementById("incidentTimeline"),
};
function cacheBust(url) {
return `${url}?t=${Date.now()}`;
}
async function fetchJson(url) {
const response = await fetch(cacheBust(url), { cache: "no-store" });
if (!response.ok) throw new Error(`Failed to fetch ${url}`);
return response.json();
}
function mergeIncidentSeries(seriesFiles) {
const items = [];
for (const file of seriesFiles) {
const fileItems = Array.isArray(file?.items)
? file.items
: file?.incident && file.incident.id
? [file.incident]
: file?.id
? [file]
: [];
items.push(...fileItems.filter(Boolean));
}
return {
items: items.sort((a, b) => new Date(incidentSortTimestamp(b)) - new Date(incidentSortTimestamp(a))),
};
}
async function fetchIncidentSeries(current) {
const currentPaths = (current.incident_file_paths || [])
.map((entry) => (typeof entry === "string" ? entry : entry?.path))
.filter(Boolean);
let manifestPaths = [];
try {
const manifest = await fetchJson(DATA_FILES.incidentsIndex);
manifestPaths = (manifest.files || [])
.map((entry) => (typeof entry === "string" ? entry : entry?.path))
.filter(Boolean);
} catch {
// Optional legacy manifest; current-status append-only file list is preferred.
}
const files = Array.from(new Set([...currentPaths, ...manifestPaths]));
if (!files.length) return { items: [] };
const loaded = await Promise.all(
files.map(async (path) => {
try {
return await fetchJson(path);
} catch (error) {
console.warn(`Failed to load incident file: ${path}`, error);
return null;
}
})
);
return mergeIncidentSeries(loaded.filter(Boolean));
}
function formatLocal(iso, options = {}) {
if (!iso) return "--";
return new Date(iso).toLocaleString([], {
month: "short",
day: "numeric",
hour: "numeric",
minute: "2-digit",
...options,
});
}
function statusLabel(status) {
const map = {
operational: "Operational",
minor: "Minor Delays",
degraded: "Degraded Service",
major: "Major Disruption",
critical: "Service Outage",
};
return map[status] || "Unknown";
}
function pillClass(status) {
const normalized = ["operational", "minor", "degraded", "major", "critical"].includes(status)
? status
: "loading";
return `status-pill status-pill--${normalized}`;
}
function percent(value) {
return `${Number(value).toFixed(2)}%`;
}
function uptimeBandForPercent(value) {
const n = Number(value);
if (n >= 98) return "operational";
if (n >= 95) return "degraded";
return "major";
}
function parseDate(value) {
const date = new Date(value);
return Number.isNaN(date.getTime()) ? null : date;
}
function isSameLocalDay(a, b) {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
);
}
function incidentSortTimestamp(incident) {
return (
incident?.version_created_at ||
incident?.updated_at ||
incident?.updates?.at?.(-1)?.timestamp ||
incident?.started_at ||
""
);
}
function normalizeSeverity(value) {
if (!value) return "operational";
if (value === "degraded") return "degraded";
if (value === "minor") return "minor";
if (value === "major") return "major";
if (value === "critical") return "critical";
if (value === "identified" || value === "investigating" || value === "monitoring") return "degraded";
if (value === "resolved") return "operational";
return "degraded";
}
function parseDelayMinutes(text) {
if (!text) return null;
const match = String(text).match(/(\d+)\s*(?:min|mins|minute|minutes)\b/i);
return match ? Number(match[1]) : null;
}
function severityForIncident(incident) {
const title = String(incident?.title || "").toLowerCase();
const summary = String(incident?.summary || "").toLowerCase();
const updateMessages = (incident?.updates || [])
.map((update) => String(update?.message || "").toLowerCase())
.join(" ");
const haystack = `${title} ${summary} ${updateMessages}`;
if (/\bcancel(?:ed|led|lation)?\b/i.test(haystack)) return "critical";
if (haystack.includes("platform change")) return "operational";
const delayMinutes =
parseDelayMinutes(title) ?? parseDelayMinutes(summary) ?? parseDelayMinutes(updateMessages);
if (delayMinutes !== null) {
if (delayMinutes >= 30) return "major";
return "minor";
}
return normalizeSeverity(incident?.severity);
}
function buildOutageSegments(incidents, referenceNow) {
const segments = [];
for (const incident of incidents.items || []) {
const incidentSeverity = severityForIncident(incident);
if (incidentSeverity === "operational") continue;
const startedAt = parseDate(incident.started_at);
if (!startedAt) continue;
const resolvedAt = parseDate(incident.resolved_at);
const incidentEnd = resolvedAt || referenceNow;
if (incidentEnd <= startedAt) continue;
segments.push({ start: startedAt, end: incidentEnd, severity: incidentSeverity });
}
return segments;
}
function clippedSegments(segments, windowStart, windowEnd) {
return segments
.map((segment) => {
const start = Math.max(segment.start.getTime(), windowStart.getTime());
const end = Math.min(segment.end.getTime(), windowEnd.getTime());
if (end <= start) return null;
return { start, end, severity: segment.severity };
})
.filter(Boolean)
.sort((a, b) => a.start - b.start);
}
function mergedDowntimeBuckets(segments, windowStart, windowEnd) {
const clipped = clippedSegments(segments, windowStart, windowEnd);
if (!clipped.length) return 0;
const occupiedBuckets = new Set();
for (const segment of clipped) {
const startBucket = Math.floor(segment.start / 60_000);
const endExclusiveBucket = Math.ceil(segment.end / 60_000);
for (let bucket = startBucket; bucket < endExclusiveBucket; bucket += 1) {
occupiedBuckets.add(bucket);
}
}
return occupiedBuckets.size;
}
function uptimeForWindow(segments, windowStart, windowEnd) {
const denominator =
Math.ceil(windowEnd.getTime() / 60_000) - Math.floor(windowStart.getTime() / 60_000);
if (denominator <= 0) return 100;
const downtime = mergedDowntimeBuckets(segments, windowStart, windowEnd);
return Math.max(0, Math.min(100, ((denominator - downtime) / denominator) * 100));
}
function startOfDay(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}
function startOfMonth(date) {
return new Date(date.getFullYear(), date.getMonth(), 1);
}
function addDays(date, days) {
return new Date(date.getTime() + days * DAY_MS);
}
function addMonths(date, months) {
return new Date(date.getFullYear(), date.getMonth() + months, 1);
}
function formatMonthYearShort(date) {
const month = date.toLocaleString([], { month: "short" });
const year = String(date.getFullYear()).slice(-2);
return `${month} '${year}`;
}
function computeUptimeFromIncidents(incidents, current) {
const referenceNow = new Date();
const segments = buildOutageSegments(incidents, referenceNow);
const completedDayBoundary = startOfDay(referenceNow);
const summary = {
last24h: uptimeForWindow(segments, new Date(referenceNow.getTime() - DAY_MS), referenceNow),
last30d: uptimeForWindow(segments, new Date(referenceNow.getTime() - 30 * DAY_MS), referenceNow),
last90d: uptimeForWindow(segments, new Date(referenceNow.getTime() - 90 * DAY_MS), referenceNow),
};
const monthly = [];
const currentMonthStart = startOfMonth(referenceNow);
for (let offset = 0; offset >= -11; offset -= 1) {
const monthStart = addMonths(currentMonthStart, offset);
const monthEnd = addMonths(monthStart, 1);
const effectiveMonthEnd = monthEnd > completedDayBoundary ? completedDayBoundary : monthEnd;
monthly.push({
month: formatMonthYearShort(monthStart),
uptime_percent: uptimeForWindow(segments, monthStart, effectiveMonthEnd),
});
}
const recent_days = [];
for (let offset = -29; offset <= 0; offset += 1) {
const dayStart = addDays(completedDayBoundary, offset);
const dayEnd = addDays(dayStart, 1);
const dayUptime = uptimeForWindow(segments, dayStart, dayEnd);
recent_days.push({
date: dayStart.toISOString().slice(0, 10),
uptime_percent: dayUptime,
uptime_band: uptimeBandForPercent(dayUptime),
});
}
return {
generated_at: referenceNow.toISOString(),
summary,
monthly,
recent_days,
};
}
function renderCurrentStatus(current) {
ui.statusHeadline.classList.remove("loading");
ui.statusHeadline.textContent = current.status_message || "No status message";
ui.overallPill.className = pillClass(current.overall_status);
ui.overallPill.textContent = statusLabel(current.overall_status);
}
function renderUptime(uptime) {
ui.uptime24h.textContent = percent(uptime.summary.last24h);
ui.uptime30d.textContent = percent(uptime.summary.last30d);
ui.uptime90d.textContent = percent(uptime.summary.last90d);
ui.uptimeChart.classList.remove("loading-chart");
ui.uptimeChart.innerHTML = "";
uptime.monthly.forEach((row) => {
const wrapper = document.createElement("div");
wrapper.className = "chart-row";
const label = document.createElement("span");
label.className = "chart-label mono";
label.textContent = row.month;
const track = document.createElement("div");
track.className = "chart-track";
const fill = document.createElement("div");
fill.className = `chart-fill chart-fill--${uptimeBandForPercent(row.uptime_percent)}`;
fill.style.width = `${Math.max(0, Math.min(100, Number(row.uptime_percent)))}%`;
fill.title = `${row.month}: ${percent(row.uptime_percent)}`;
track.appendChild(fill);
const value = document.createElement("span");
value.className = "chart-value mono";
value.textContent = percent(row.uptime_percent);
wrapper.append(label, track, value);
ui.uptimeChart.appendChild(wrapper);
});
ui.dayStrip.innerHTML = "";
uptime.recent_days.forEach((day) => {
const cell = document.createElement("button");
cell.type = "button";
cell.className = `day-cell day-${day.uptime_band}`;
cell.setAttribute(
"data-label",
`${day.date}: ${percent(day.uptime_percent)}`
);
cell.setAttribute(
"aria-label",
`${day.date}: ${percent(day.uptime_percent)}`
);
ui.dayStrip.appendChild(cell);
});
}
function timelineIncidentsForDisplay(incidents, current) {
const referenceDate = parseDate(current?.as_of) || new Date();
const base = (incidents.items || [])
.filter((incident) => {
const timestamp = parseDate(incidentSortTimestamp(incident)) || parseDate(incident?.started_at);
return timestamp ? isSameLocalDay(timestamp, referenceDate) : false;
})
.slice()
.sort((a, b) => new Date(incidentSortTimestamp(b)) - new Date(incidentSortTimestamp(a)));
const activeIds = Array.isArray(current?.active_incident_ids) ? current.active_incident_ids : [];
if (!activeIds.length) return base;
const byId = new Map();
for (const incident of base) {
if (!incident?.id) continue;
if (!byId.has(incident.id)) byId.set(incident.id, []);
byId.get(incident.id).push(incident);
}
const latestById = new Map();
for (const incident of base) {
if (!incident?.id) continue;
if (!latestById.has(incident.id)) latestById.set(incident.id, incident);
}
const expanded = [];
for (const id of activeIds) {
const queue = byId.get(id);
if (queue?.length) {
expanded.push(queue.shift());
continue;
}
const fallback = latestById.get(id);
if (fallback) expanded.push(fallback);
}
return (expanded.length ? expanded : base).sort(
(a, b) => new Date(incidentSortTimestamp(b)) - new Date(incidentSortTimestamp(a))
);
}
function renderTimeline(incidents, current) {
ui.incidentTimeline.innerHTML = "";
const sorted = timelineIncidentsForDisplay(incidents, current);
sorted.forEach((incident) => {
const item = document.createElement("li");
item.className = "timeline-item";
const displaySeverity = severityForIncident(incident);
const incidentStatus = String(incident.status || "").trim().toLowerCase();
const resolvedText = incident.resolved_at
? `Resolved ${formatLocal(incident.resolved_at)}`
: `Ongoing since ${formatLocal(incident.started_at)}`;
const metaParts = [capitalize(displaySeverity)];
if (incidentStatus && incidentStatus !== "resolved") {
metaParts.push(escapeHtml(incident.status));
}
metaParts.push(resolvedText);
item.innerHTML = `
<span class="timeline-dot severity-${displaySeverity}" aria-hidden="true"></span>
<div>
<p class="timeline-title">${escapeHtml(incident.title)}</p>
<p class="timeline-meta">${metaParts.join(" · ")}</p>
<p class="timeline-desc">${escapeHtml(incident.summary)}</p>
</div>
`;
ui.incidentTimeline.appendChild(item);
});
}
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function capitalize(value) {
if (!value) return "";
return value.charAt(0).toUpperCase() + value.slice(1);
}
function renderRefreshTime() {
ui.lastRefreshed.textContent = `Last refresh: ${new Date().toLocaleTimeString([], {
hour: "numeric",
minute: "2-digit",
second: "2-digit",
})}`;
}
function renderError(error) {
console.error(error);
ui.statusHeadline.classList.remove("loading");
ui.statusHeadline.textContent =
"Unable to load status data. Check data files or local server configuration.";
ui.overallPill.className = "status-pill status-pill--major";
ui.overallPill.textContent = "Data Error";
}
async function loadAndRender() {
try {
const current = await fetchJson(DATA_FILES.current);
const incidents = await fetchIncidentSeries(current);
const uptime = computeUptimeFromIncidents(incidents, current);
renderCurrentStatus(current);
renderUptime(uptime);
renderTimeline(incidents, current);
renderRefreshTime();
} catch (error) {
renderError(error);
}
}
loadAndRender();
setInterval(loadAndRender, REFRESH_MS);