import m, {MithrilComponent} from '@garage44/mithril' import {Button} from '@garage44/mithril/components' import type {StatsDayApps, StatsOverview, StatsPlaces, StatsWeb, StatsWorld} from '@/types' import {local} from '@/lib/reactive' import { change, CLIENT_LABELS, count, dayLabel, downloadCsv, EVENT_LABELS, loadOverview, loadPlaces, loadWeb, loadWorld, placeName, } from '@/lib/stats' import {Card, Cohorts, compared, Funnel, LineDays, Period, Ranked, type Series, Spark, StackedDays, step, Tiles, WeekHours} from './charts' /** * Admin › Stats (`docs/mockups/stats/`, v1): how the world is used and who * visits the sites. Four pages, each one filter row and the numbers for it; * the server leaves out anything about fewer than 5 people, so a gap here is * that rule, not a bug. */ const APPS: Series[] = [ {key: 'android', label: 'Android app', slot: 's1'}, {key: 'web', label: 'App in a browser', slot: 's2'}, {key: 'desktop', label: 'Desktop app', slot: 's3'}, ] /** Different accounts per day: the three apps, with the web chat and unnamed apps folded into "other". */ function appsWithOther(rows: StatsDayApps[]) { return rows.map((r) => ({...r, other: Math.max(0, r.all - r.android - r.web - r.desktop)})) } const COUNTRY_NAMES = new Intl.DisplayNames(['en'], {type: 'region'}) /** A two-letter code as the country's name; `unknown` as itself. */ function countryName(code: string) { if (code.length !== 2) return code === 'unknown' ? 'Not known' : code try { return COUNTRY_NAMES.of(code) ?? code } catch { return code } } /** A busy square's place link: World opens above it. */ function placeLink(lat: number, lon: number) { return `/go?place=${lat.toFixed(5)},${lon.toFixed(5)},400m,-60.0,0.0` } /** A page that loads its numbers for a period, and again when the period changes. */ abstract class StatsPage extends MithrilComponent { s = local({days: 30, data: null as null | T, loading: true}) abstract fetch(days: number): Promise oninit() { void this.load() } setDays = (days: number) => { this.s.days = days void this.load() } async load() { this.s.loading = true const got = await this.fetch(this.s.days) if (got) this.s.data = got as never this.s.loading = false m.redraw() } loadingView() { return

{this.s.loading ? 'Loading…' : 'No numbers to show.'}

} } export class StatsOverviewPage extends StatsPage { fetch = loadOverview view() { const o = this.s.data if (!o) return
{this.loadingView()}
const f = o.funnel return (
{Period(this.s.days, this.setDays)} {Tiles([ {label: 'In the world now', note: `most today: ${o.peak_today}`, value: count(o.online_now)}, {label: 'Active today', note: 'so far', value: count(o.active_today)}, o.active_period === null ? {label: `Active in ${o.days} days`, note: 'sessions are kept 90 days', value: '—'} : { change: change(o.active_period, o.active_prev), label: `Active in ${o.days} days`, note: `of ${count(o.accounts_total)} accounts`, value: count(o.active_period), }, compared('New accounts', o.new_accounts, o.new_accounts_prev), compared('Web visitors', o.visitors, o.visitors_prev), ])} {Card( 'People in the world each day', 'different accounts signed in, by the app they used', StackedDays(appsWithOther(o.daily), [...APPS, {key: 'other', label: 'Web chat and older apps', slot: 'q3'}]), )}
{Card('Visitors to the sites', 'per day', LineDays(o.visitors_daily, 170, 300))} {Card( 'From a visit to a regular', `${o.days} days`, Funnel([ ['Visited a site', f.visitors], ['Opened World', f.opened_world], ['Made an account', f.accounts], ['Signed in to the world', f.signed_in], ['Back within a week', f.came_back], ]), )} {Card( 'Busiest places', 'hours in the world', Ranked( o.places.map((p) => ({ label: {placeName(p.lat, p.lon)}, n: Math.round(p.minutes / 60), })), undefined, 'No square has had 5 people in it yet', ), )}
) } } export class StatsWorldPage extends StatsPage { fetch = loadWorld csv(w: StatsWorld) { downloadCsv( `world-${w.days}-days.csv`, ['what', 'this period', 'period before'], w.events.map((e) => [EVENT_LABELS[e.kind] ?? e.kind, e.n, e.prev]), ) } view() { const w = this.s.data if (!w) return
{this.loadingView()}
const peakAt = w.peak_hour === null ? '' : new Date(w.peak_hour * 3_600_000).toLocaleString(undefined, {day: 'numeric', hour: '2-digit', minute: '2-digit', month: 'short', weekday: 'short'}) return (
{Period(this.s.days, this.setDays, )} {Tiles([ compared('Sessions', w.sessions, w.sessions_prev), {label: 'Typical session', note: `median · longest ${w.longest_minutes} min`, value: `${w.median_minutes} min`}, {label: 'Most at once', note: peakAt, value: count(w.peak)}, compared( 'Messages relayed', w.events.filter((e) => e.kind.endsWith('_message')).reduce((s, e) => s + e.n, 0), w.events.filter((e) => e.kind.endsWith('_message')).reduce((s, e) => s + e.prev, 0), 'content never seen', ), compared('Call minutes', w.events.find((e) => e.kind === 'call_minutes')?.n ?? 0, w.events.find((e) => e.kind === 'call_minutes')?.prev ?? 0), ])}
{Card('When people are in the world', 'average signed in, your time zone', WeekHours(w.hours))} {Card("Who comes back", "share of each week's new accounts seen again", Cohorts(w.cohorts))}
{Card( 'What people did', '', {w.events.length === 0 && } {w.events.map((e) => ( ))}
WhatThis periodBeforeChange
Nothing counted yet
{EVENT_LABELS[e.kind] ?? e.kind} {count(e.n)} {count(e.prev)} {change(e.n, e.prev) ?? ''} {Spark(e.daily)}
, )} {Card( 'Apps in use', 'people, last 7 days', {w.apps.map((a) => ( ))} {w.refused.map((r) => ( ))}
AppBuildPeople
{CLIENT_LABELS[a.client] ?? a.client} {a.build || '—'} {count(a.people)}
Turned away · {CLIENT_LABELS[r.client] ?? r.client} {r.reason === 'protocol' ? `protocol ${r.protocol}` : 'sign-in refused'} {count(r.devices)} ({count(r.attempts)} tries)
,

An app turned away for its protocol is older or newer than this server: the app itself only says "connection reset".

, )}
) } } const SITES: [null | string, string][] = [ [null, 'All sites'], ['web', 'cartopolis.org'], ['viewer', 'World'], ['about', 'about.cartopolis.org'], ] export class StatsWebPage extends StatsPage { site: null | string = null fetch = (days: number) => loadWeb(days, this.site) setSite(site: null | string) { this.site = site void this.load() } view() { const w = this.s.data if (!w) return
{this.loadingView()}
const sites = (
{SITES.map(([id, label]) => ( ))}
) const rank = (rows: {value: string; visitors: number}[]) => rows.map((r) => ({label: r.value, n: r.visitors})) return (
{Period(this.s.days, this.setDays, sites)} {Tiles([ compared('Visitors', w.visitors, w.visitors_prev), {label: 'Pages shown', note: w.visitors ? `${(w.views / w.visitors).toFixed(1)} per visitor` : '', value: count(w.views)}, {label: 'Opened World', note: w.visitors ? `${Math.round((w.opened_world / Math.max(1, w.visitors)) * 100)}% of visitors` : '', value: count(w.opened_world)}, compared('APK downloads', w.apk_downloads, w.apk_downloads_prev), {label: 'Crawlers filtered', note: 'never counted as visitors', value: count(w.bots)}, ])}
{Card('Visitors per day', 'one browser on one site on one day', LineDays(w.daily.map((d) => ({day: d.day, n: d.visitors})), 190))} {Card( 'Devices', 'by window width', Ranked(rank(w.devices)),

Languages

, Ranked(rank(w.languages)), )}
{Card('Pages', '', Ranked(rank(w.pages), ['Page', 'Visitors']))} {Card('Where they came from', '', Ranked(rank(w.sources), ['Site', 'Visitors']))} {Card( 'Countries', '', Ranked(w.countries.map((r) => ({label: countryName(r.value), n: r.visitors})), ['Country', 'Visitors']),

Read from the address at the moment of the visit, which is then dropped. IP Geolocation by DB-IP.

, )}

Each page sends one request when it is shown: the site, the page with its ids removed, the site the visitor came from and the window width. A visitor is a hash of a secret that is thrown away at midnight, the address and the browser — no cookie, and no address is kept. APK downloads are counted by the web server, whole downloads only.

) } } export class StatsPlacesPage extends StatsPage { fetch = loadPlaces view() { const p = this.s.data if (!p) return
{this.loadingView()}
const sq = p.squares const max = Math.max(1, ...sq.map((s) => s.minutes)) // Every square, unless they spread too far to read: then the 15 km // around the busiest one. const MAX = 60 const busiest = sq[0] ?? {sx: 0, sy: 0} const near = sq.filter((s) => Math.abs(s.sx - busiest.sx) <= MAX / 2 && Math.abs(s.sy - busiest.sy) <= MAX / 2) const xs = near.map((s) => s.sx) const ys = near.map((s) => s.sy) const [minX, maxX, minY, maxY] = [Math.min(...xs) - 1, Math.max(...xs) + 1, Math.min(...ys) - 1, Math.max(...ys) + 1] const cell = 22 return (
{Period(this.s.days, this.setDays)}
{Card( 'Hours spent in each 250 m square', `${sq.length} squares`, sq.length === 0 ? (

No square has had 5 different people in it yet.

) : ( {sq .filter((s) => s.sx >= minX && s.sx <= maxX && s.sy >= minY && s.sy <= maxY) .map((s) => ( {`${s.lat.toFixed(4)}, ${s.lon.toFixed(4)} · ${Math.round(s.minutes / 60)} h · ${s.people} person-days`} ))} ),

North is up. A square appears only once 5 different people have been in it on one day; positions are never stored, only each square's minutes. Click a square to open World above it.

, )} {Card( 'Busiest squares', 'hours', Ranked( sq.slice(0, 12).map((s) => ({ label: {placeName(s.lat, s.lon)}, n: Math.round(s.minutes / 60), })), ), )}
) } }