//! 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) std.Io.Writer.Error![]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) {
try w.print("{s}", .{glyphs[rand.uintLessThan(usize, glyphs.len)]});
}
return w.buffered();
}
fn writeCommas(w: *std.Io.Writer, n: u64) std.Io.Writer.Error!void {
if (n >= 1000) {
try writeCommas(w, n / 1000);
try w.print(",{d:0>3}", .{n % 1000});
} else {
try w.print("{d}", .{n});
}
}
fn writeUptime(w: *std.Io.Writer, s: i64) std.Io.Writer.Error!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) {
try w.print("{d}d {d}h", .{ d, h });
} else if (h > 0) {
try w.print("{d}h {d}m", .{ h, m });
} else {
try w.print("{d}m", .{m});
}
}
/// the plain-text page (also the
body of the html page)
pub fn render(buf: []u8, args: Args) std.Io.Writer.Error![]const u8 {
var w: std.Io.Writer = .fixed(buf);
try w.print(
\\
\\ ~ s t r e a m ~
\\
\\ the atproto firehose, live and archived.
\\
\\
, .{});
try w.print(" \u{03df} \u{00b0} \u{00b7} \u{02da} \u{00b7} \u{03df} \u{00b0} \u{00b7} \u{02da}\n", .{});
var row: u64 = 0;
while (row < wave_rows) : (row += 1) {
var wbuf: [wave_width * 3]u8 = undefined;
try w.print(" {s}\n", .{try waveRow(&wbuf, args.seq, row)});
}
try w.print(
\\
\\ "No one ever steps in the same river twice."
\\ — Heraclitus
\\
\\
, .{});
if (args.serving) {
try w.print(" Following the network at sequence ", .{});
try writeCommas(&w, if (args.seq > 0) @intCast(args.seq) else 0);
try w.print(".\n\n", .{});
} else {
try w.print(
\\ Backfilling network history.
\\ Live subscriptions open when the archive catches up.
\\
, .{});
}
try w.print("\n{s}\n\n", .{endpoints.text_block});
try w.print(" up ", .{});
try writeUptime(&w, args.uptime_s);
try w.print(" \u{00b7} ", .{});
try writeCommas(&w, args.events_total);
try w.print(" live events this run \u{00b7} {d} listening\n", .{args.subscribers});
try w.print(" build {s} \u{00b7} source: tangled.org/zat.dev/stream\n\n", .{build_options.git_sha});
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) std.Io.Writer.Error![]const u8 {
var w: std.Io.Writer = .fixed(buf);
try w.writeAll(
\\
\\
\\stream · the atproto river