//! homepage — the ascii river at GET /
//!
//! the waves are seeded from the current upstream seq, so each observed
//! network position has its own river. curl gets plain text; browsers get
//! the same art in a dark
with slowly drifting waves.
const std = @import("std");
const build_options = @import("build_options");
const endpoints = @import("endpoints.zig");
const wave_width = 58;
const wave_rows = 3;
pub const Args = struct {
seq: i64,
events_total: u64,
subscribers: u32,
uptime_s: i64,
serving: bool,
};
pub const StatusArgs = struct {
upstream_seq: i64,
durable_seq: u64,
process_events: u64,
subscribers: u32,
uptime_s: i64,
serving: bool,
bootstrap_enabled: bool,
compaction_enabled: bool,
retry_enabled: bool,
verify_valid: u64,
verify_invalid: u64,
/// Durable lifecycle facts, read from the metadata store rather than from
/// process-local counters: an operator asking "how long has this been
/// bootstrapping" needs an answer that survives a restart. Null means the
/// key is genuinely absent (a fresh data dir), never that a read failed.
phase: ?[]const u8 = null,
phase_age_s: ?i64 = null,
backfill_duration_us: ?i64 = null,
/// Repository rollup, upstream's BackfillStats. Null while no repository
/// state exists at all, which is not the same as every counter being zero.
repos_total: ?u32 = null,
repos_complete: u32 = 0,
repos_pending: u32 = 0,
repos_failed: u32 = 0,
repos_unavailable: u32 = 0,
/// Live cursors, upstream's LiveStats.
next_seq: u64 = 0,
/// Retention, upstream's CursorLookbackStats. Zero lookback means cursor
/// replay is disabled, which is reported rather than shown as no retention.
lookback_s: u64 = 0,
oldest_retained_seq: ?u64 = null,
sealed_segments: ?usize = null,
/// Timestamp import, upstream's ImportInfo. Null when import never ran.
import_job: ?[]const u8 = null,
};
/// deterministic wave row: same seq → same river, next seq → a new one.
/// `buf` needs 3x wave_width bytes (glyphs are 1-3 bytes each).
fn waveRow(buf: []u8, seq: i64, row: u64) []const u8 {
var prng = std.Random.DefaultPrng.init(@as(u64, @bitCast(seq)) *% 0x9e3779b97f4a7c15 +% row);
const rand = prng.random();
// mostly water, some chop, a little foam
const glyphs = [_][]const u8{ "~", "~", "~", "~", "\u{2248}", "\u{2248}", "\u{00b7}" };
var w: std.Io.Writer = .fixed(buf);
var i: usize = 0;
while (i < wave_width) : (i += 1) {
w.print("{s}", .{glyphs[rand.uintLessThan(usize, glyphs.len)]}) catch break;
}
return w.buffered();
}
fn writeCommas(w: *std.Io.Writer, n: u64) void {
if (n >= 1000) {
writeCommas(w, n / 1000);
w.print(",{d:0>3}", .{n % 1000}) catch {};
} else {
w.print("{d}", .{n}) catch {};
}
}
fn writeUptime(w: *std.Io.Writer, s: i64) void {
const secs: u64 = if (s > 0) @intCast(s) else 0;
const d = secs / 86_400;
const h = (secs % 86_400) / 3600;
const m = (secs % 3600) / 60;
if (d > 0) {
w.print("{d}d {d}h", .{ d, h }) catch {};
} else if (h > 0) {
w.print("{d}h {d}m", .{ h, m }) catch {};
} else {
w.print("{d}m", .{m}) catch {};
}
}
/// the plain-text page (also the
body of the html page)
pub fn render(buf: []u8, args: Args) []const u8 {
var w: std.Io.Writer = .fixed(buf);
w.print(
\\
\\ ~ s t r e a m ~
\\
\\ every event on the atproto network, as it
\\ happens, archived forever
\\
\\
, .{}) catch {};
w.print(" \u{03df} \u{00b0} \u{00b7} \u{02da} \u{00b7} \u{03df} \u{00b0} \u{00b7} \u{02da}\n", .{}) catch {};
var row: u64 = 0;
while (row < wave_rows) : (row += 1) {
var wbuf: [wave_width * 3]u8 = undefined;
w.print(" {s}\n", .{waveRow(&wbuf, args.seq, row)}) catch {};
}
w.print(
\\
\\ "No one ever steps in the same river twice."
\\ — Heraclitus
\\
\\
, .{}) catch {};
if (args.serving) {
w.print(" Following the network at sequence ", .{}) catch {};
writeCommas(&w, if (args.seq > 0) @intCast(args.seq) else 0);
w.print(".\n\n", .{}) catch {};
} else {
w.print(
\\ Backfilling network history.
\\ Live subscriptions open when the archive catches up.
\\
, .{}) catch {};
}
w.print("\n{s}\n\n", .{endpoints.text_block}) catch {};
w.print(" up ", .{}) catch {};
writeUptime(&w, args.uptime_s);
w.print(" \u{00b7} ", .{}) catch {};
writeCommas(&w, args.events_total);
w.print(" live events this run \u{00b7} {d} listening\n", .{args.subscribers}) catch {};
w.print(" build {s} \u{00b7} source: tangled.org/zat.dev/stream\n\n", .{build_options.git_sha}) catch {};
return w.buffered();
}
// Browser rendering lives below as a structured live instrument. The text
// representation above remains intentionally useful to curl and terminals.
/// The river is a live instrument: every displayed runtime value and the wave
/// pattern refresh from the public status report without a page reload.
pub fn renderHtml(buf: []u8, args: Args) []const u8 {
var w: std.Io.Writer = .fixed(buf);
w.writeAll(
\\
\\
\\stream · the atproto river