pager/docs/scripts/screenshots.mjs
2026-11-16 20:32:39 +01:00

81 lines
4.5 KiB
JavaScript

// Seeds a running Pager (BASE, default http://localhost:8080, fresh database) with demo data and takes the doc screenshots.
import { chromium } from 'playwright'
import { mkdirSync } from 'node:fs'
const BASE = process.env.BASE || 'http://localhost:8080'
const OUT = new URL('../static/img/screenshots/', import.meta.url).pathname
mkdirSync(OUT, { recursive: true })
const post = async (ctx, path, body) => {
const r = await ctx.request.post(BASE + '/api' + path, { data: body })
if (!r.ok()) throw new Error(`${path}: ${r.status()} ${await r.text()}`)
}
const browser = await chromium.launch()
const ctx = await browser.newContext({ viewport: { width: 1180, height: 760 }, baseURL: BASE })
const page = await ctx.newPage()
// Register through the UI
await page.goto('/register')
await page.fill('input[placeholder="Organisation name"]', 'Acme Ops')
await page.fill('input[placeholder="Your name"]', 'Ann Admin')
await page.fill('input[type=email]', 'ann@acme.example')
await page.fill('input[type=password]', 'correct-horse-battery')
await page.screenshot({ path: OUT + 'register.png' })
await page.click('button.primary')
await page.waitForSelector('text=Nothing here')
// Seed alerts through both inbound transports
const { token } = await (await ctx.request.get(BASE + '/api/tenant')).json()
const am = (name, prio, summary, desc, fp, extra = {}, status = 'firing') => fetch(`${BASE}/api/ingest/${token}/alertmanager`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ alerts: [{ status, fingerprint: fp, labels: { alertname: name, priority: prio, instance: 'web-1:9100', job: 'node' }, annotations: { summary, description: desc, ...extra }, generatorURL: 'http://prometheus.example/graph' }] }),
})
await am('HighCPU', 'P1', 'CPU above 90% on web-1', 'CPU usage is 91%', 'cpu1')
for (const v of [93, 95, 97, 97]) await am('HighCPU', 'P1', 'CPU above 90% on web-1', `CPU usage is ${v}%`, 'cpu1', { comment: 'Runbook: https://wiki.example/cpu' })
await am('DiskAlmostFull', 'P2', 'Disk /var 92% full on db-1', '/var has 8 GB left', 'disk1')
await am('DiskAlmostFull', 'P2', 'Disk /var 92% full on db-1', '/var has 7 GB left', 'disk1')
await am('CertExpiry', 'P4', 'Certificate expires in 10 days', 'api.acme.example', 'cert1')
await am('OldAlert', 'P3', 'Queue was backed up', 'resolved soon', 'q1')
await am('OldAlert', 'P3', 'Queue was backed up', 'resolved soon', 'q1', {}, 'resolved')
await fetch(`${BASE}/api/ingest/${token}/webhook`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ alias: 'nightly-backup', message: 'Nightly backup failed', description: 'exit code 2', priority: 'P3' }),
})
// Team and triage
await post(ctx, '/users', { name: 'Bob Builder', email: 'bob@acme.example', password: 'correct-horse-battery', role: 'admin' })
await post(ctx, '/users', { name: 'Cara Coder', email: 'cara@acme.example', password: 'correct-horse-battery', role: 'member' })
await post(ctx, '/alerts/1/assign', { user_id: 2 }) // HighCPU -> Bob
await post(ctx, '/alerts/1/comments', { text: 'Looking at it, a deploy is running.' })
await post(ctx, '/alerts/2/ack')
// Make the seeded alerts look lived-in (needs the sqlite file of the backend; skipped otherwise)
if (process.env.SQLITE_FILE) {
const { DatabaseSync } = await import('node:sqlite')
const db = new DatabaseSync(process.env.SQLITE_FILE)
const ago = (min) => new Date(Date.now() - min * 60000).toISOString()
const aged = { cpu1: [200, 2], disk1: [55, 9], cert1: [14 * 60, 14 * 60], 'wh-': [2 * 1440 + 300, 2 * 1440 + 300] }
for (const [fp, [first, last]] of Object.entries(aged))
db.prepare('UPDATE alerts SET first_seen_at = ?, last_seen_at = ? WHERE fingerprint LIKE ?').run(ago(first), ago(last), fp + '%')
db.exec('UPDATE alert_events SET created_at = (SELECT first_seen_at FROM alerts WHERE alerts.id = alert_events.alert_id)')
db.close()
}
const shot = async (p, name, wait) => {
await p.goto(name.path)
await p.waitForSelector(wait)
await p.waitForTimeout(300)
await p.screenshot({ path: OUT + name.file + '.png' })
}
await shot(page, { path: '/', file: 'alerts' }, '.row')
await shot(page, { path: '/alerts/1', file: 'alert-detail' }, '.timeline li')
await shot(page, { path: '/team', file: 'team' }, 'table')
// Phone view
const mctx = await browser.newContext({ viewport: { width: 390, height: 780 }, deviceScaleFactor: 2, storageState: await ctx.storageState(), baseURL: BASE })
const m = await mctx.newPage()
await shot(m, { path: '/', file: 'mobile-alerts' }, '.row')
await browser.close()
console.log('screenshots written to', OUT)