dc3879eca6
Whoever is on call gets paged on a phone, and until now the only ways to
act on a page were the notification's Acknowledge button or a terminal.
Tapping the notification itself opened /api/incidents/{id}, which a
browser can only answer with a 401 in JSON. The server now serves a web
UI at / covering the incident queue, each incident's alerts and timeline
with every action on it, who is on call, the alert feed, and changing
your own password. The notification link now points at /incidents/{id}
in that UI.
It is embedded in the binary and has no build step: plain HTML, CSS and
ES modules under internal/web/static, served with an ETag per file and a
CSP that allows nothing from any other origin. That is how rd-web is
built. It avoids adding a node toolchain to the Dockerfile and the
pipeline for a page this size, and it keeps the page on the same origin
as the API, so no CORS is needed and nothing else has to be deployed.
Paths without a file extension fall back to index.html, so a deep link
survives a reload. An unknown path under /api/ still gets a JSON 404
rather than the page.
Signing in uses a username and password, because pasting a 64-character
API key into a phone at 3am is not a sign-in flow. Users have no
password until one is set through PUT /api/users/{id}/password, or
optionally at bootstrap. A user without a password is exactly where they
were before this commit and can only use API keys. A login sets an
HttpOnly, SameSite=Lax session cookie. It lasts 30 days and slides
forward while in use, so an on-call phone does not sign itself out.
Only the token's hash is stored, as for API keys.
The cookie needs a CSRF guard where a bearer header does not, because
browsers attach cookies to requests other sites make. So cookie-
authenticated requests go through Go 1.25's http.CrossOriginProtection,
and bearer requests do not. A request carrying an Authorization header
is judged on that header alone and never falls back to the cookie.
Changing a password ends every other session of that user. Changing
your own requires the current password, so a phone left signed in
cannot be used to take the account over.
Failed logins are counted per username and per client address. Ten
failures for one username in 15 minutes refuse that username for the
rest of the window, even with the right password. That makes locking
somebody out possible for anyone who knows their username. It was
accepted because the alternative is unlimited guessing, and during a
lockout the notification's Acknowledge button and API keys keep
working. The address limit reads the first X-Forwarded-For hop, since
behind the gateway RemoteAddr is Envoy. It is looser, because a whole
office behind one NAT shares it.
The Secure flag follows TERDUT_PUBLIC_URL, since TLS terminates at the
gateway and the server itself only ever sees plain HTTP. The chart
already defaults that variable to https://<hostname>.
Schedule editing, statistics and user management stay in terdut-tui for
now. The API they use is unchanged, and bearer authentication behaves
exactly as before.
142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
// On-call: who is on duty now, the week around it, and your own next shifts.
|
||
// Read-only for now; the TUI edits the schedule.
|
||
|
||
import * as api from './api.js';
|
||
import { h, clear, icon, spinner } from './ui.js';
|
||
import { isoDate, mondayOf, addDays, isoWeek, initial } from './format.js';
|
||
import { myID } from './state.js';
|
||
|
||
const view = () => document.getElementById('view-oncall');
|
||
|
||
let weekStart = mondayOf(new Date());
|
||
let data = null;
|
||
let error = null;
|
||
|
||
const dayName = new Intl.DateTimeFormat(undefined, { weekday: 'short' });
|
||
const dayDate = new Intl.DateTimeFormat(undefined, { day: 'numeric', month: 'short' });
|
||
|
||
export function show() {
|
||
if (!data) clear(view(), spinner());
|
||
refresh();
|
||
}
|
||
|
||
export async function refresh() {
|
||
const start = weekStart;
|
||
const today = new Date();
|
||
try {
|
||
const [now, week, upcoming] = await Promise.all([
|
||
api.onCallNow(),
|
||
api.schedule(isoDate(start), isoDate(addDays(start, 6))),
|
||
api.schedule(isoDate(today), isoDate(addDays(today, 60))),
|
||
]);
|
||
if (start !== weekStart) return;
|
||
data = { now, week, upcoming };
|
||
error = null;
|
||
} catch (err) {
|
||
error = err.message;
|
||
}
|
||
render();
|
||
}
|
||
|
||
function shiftWeek(n) {
|
||
weekStart = addDays(weekStart, 7 * n);
|
||
refresh();
|
||
}
|
||
|
||
function render() {
|
||
if (!data) {
|
||
clear(view(), error ? h('div', { class: 'load-error', text: error }) : spinner());
|
||
return;
|
||
}
|
||
clear(view(),
|
||
error && h('div', { class: 'load-error', text: `Showing older data: ${error}` }),
|
||
nowCard(),
|
||
weekCard(),
|
||
myShifts(),
|
||
);
|
||
}
|
||
|
||
function you(userID) {
|
||
return userID === myID() ? h('span', { class: 'you', text: 'you' }) : null;
|
||
}
|
||
|
||
function nowCard() {
|
||
const n = data.now;
|
||
return h('div', { class: 'card now-card' },
|
||
h('div', { class: `avatar ${n ? '' : 'none'}`, text: n ? initial(n.username) : '–' }),
|
||
h('div', {},
|
||
h('div', { class: 'now-label', text: 'On call now' }),
|
||
h('div', { class: 'now-name' }, n ? n.username : 'Nobody', n && you(n.user_id)),
|
||
),
|
||
);
|
||
}
|
||
|
||
function weekCard() {
|
||
const byDate = new Map(data.week.map((e) => [e.date, e]));
|
||
const today = isoDate(new Date());
|
||
const days = [];
|
||
for (let i = 0; i < 7; i++) {
|
||
const d = addDays(weekStart, i);
|
||
const key = isoDate(d);
|
||
const e = byDate.get(key);
|
||
days.push(h('li', { class: `day ${key === today ? 'today' : ''} ${key < today ? 'past' : ''}` },
|
||
h('span', { class: 'day-name', text: dayName.format(d) }),
|
||
h('span', { class: 'day-date', text: dayDate.format(d) }),
|
||
h('span', { class: `day-who ${e ? '' : 'nobody'}` }, e ? e.username : 'nobody', e && you(e.user_id)),
|
||
));
|
||
}
|
||
const thisWeek = isoDate(weekStart) === isoDate(mondayOf(new Date()));
|
||
return [
|
||
h('div', { class: 'page-head' },
|
||
h('h2', { text: thisWeek ? 'This week' : 'Week' }),
|
||
h('div', { class: 'week-nav' },
|
||
h('button', { class: 'btn btn-ghost btn-icon', type: 'button', 'aria-label': 'Previous week', onclick: () => shiftWeek(-1) },
|
||
icon('chevronLeft')),
|
||
h('button', {
|
||
class: 'btn btn-ghost label',
|
||
type: 'button',
|
||
title: 'Back to this week',
|
||
onclick: () => { weekStart = mondayOf(new Date()); refresh(); },
|
||
text: `Week ${isoWeek(weekStart)}`,
|
||
}),
|
||
h('button', { class: 'btn btn-ghost btn-icon', type: 'button', 'aria-label': 'Next week', onclick: () => shiftWeek(1) },
|
||
icon('chevronRight')),
|
||
),
|
||
),
|
||
h('ul', { class: 'card days' }, days),
|
||
];
|
||
}
|
||
|
||
// myShifts groups your upcoming dates into runs of consecutive days.
|
||
function myShifts() {
|
||
const mine = data.upcoming.filter((e) => e.user_id === myID()).map((e) => e.date).sort();
|
||
const runs = [];
|
||
for (const date of mine) {
|
||
const last = runs[runs.length - 1];
|
||
if (last && isoDate(addDays(parse(last.to), 1)) === date) last.to = date;
|
||
else runs.push({ from: date, to: date });
|
||
}
|
||
const fmt = (s) => `${dayName.format(parse(s))} ${dayDate.format(parse(s))}`;
|
||
return [
|
||
h('div', { class: 'page-head' }, h('h2', { text: 'Your next shifts' })),
|
||
h('div', { class: 'card' },
|
||
runs.length
|
||
? h('ul', { class: 'shift-list' }, runs.slice(0, 8).map((r) =>
|
||
h('li', {},
|
||
h('span', { text: r.from === r.to ? fmt(r.from) : `${fmt(r.from)} – ${fmt(r.to)}` }),
|
||
h('span', { class: 'muted', text: days(r) })),
|
||
))
|
||
: h('div', { class: 'empty', text: 'Nothing scheduled in the next 60 days.' })),
|
||
];
|
||
}
|
||
|
||
function parse(s) {
|
||
const [y, m, d] = s.split('-').map(Number);
|
||
return new Date(y, m - 1, d);
|
||
}
|
||
|
||
function days(r) {
|
||
const n = Math.round((parse(r.to) - parse(r.from)) / 86400000) + 1;
|
||
return n === 1 ? '1 day' : `${n} days`;
|
||
}
|