/tools/peepless/stats/Folio 7 of 7StatsDaybook of peepless 1.0.0-rc.2Days kept in UTC
7.0

This site's own books

Up to the last 30 days of mattblack.space/tools/peepless, counted by peepless. The reference stats page: one small endpoint on readCounts and summarize, and this page reading it.

Reading Reading the numbers…

7.1
PageviewsVisitsHumans
7.2

Sources

Countries

Pages

Events

7.3

How this folio is made

The site's own endpoint, api/stats.ts, included here as it is deployed. It answers without a token because it only ever reads this one project. It leaves out today until the day is over, so the numbers move once a day and never say when a visit happened, and it folds any country with fewer than 10 visits in the 30 days into "others". Your own stats endpoint should use createStatsHandler and a token unless you mean it to be public.

// The site's own numbers, public: the last 30 finished UTC days as a Summary, built with
// readCounts and summarize. No token, because it only ever answers this one project's totals.
// Today is left out until it ends, so the numbers change once a day and never show when a visit
// happened; a country seen on fewer than MIN_COUNTRY visits in the 30 days is folded into "others".
// It starts no earlier than COUNTED.since, the day counting began, so it never shows empty days
// from before then. Cached at the edge until the next UTC midnight.
import { readCounts, summarize, supabaseStore } from 'peepless/server'
import { COUNTED } from '../src/counted.ts'

const MIN_COUNTRY = 10
const DAY = 86_400_000

const json = (body: unknown, status: number, cache: string) =>
  new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json', 'Cache-Control': cache } })

export async function GET(): Promise<Response> {
  const url = process.env.SUPABASE_URL
  const key = process.env.SUPABASE_SERVICE_ROLE_KEY
  if (!url || !key) return json({ error: 'not counting yet' }, 503, 'no-store')
  const now = Date.now()
  const midnight = Math.ceil(now / DAY) * DAY
  const until = new Date(midnight - 2 * DAY).toISOString().slice(0, 10)
  const back = new Date(midnight - 31 * DAY).toISOString().slice(0, 10)
  const since = back < COUNTED.since ? COUNTED.since : back
  const fresh = Math.max(60, Math.round((midnight - now) / 1000))
  const cache = `public, s-maxage=${fresh}`
  if (since > until) {
    const empty = { pageviews: 0, visits: 0, humans: 0, sources: {}, campaigns: {}, countries: {}, pages: {}, events: {}, days: [] }
    return json({ ...empty, since, until }, 200, cache)
  }
  try {
    const rows = await readCounts(supabaseStore({ url, key }), { project: COUNTED.project, since, until })
    const s = summarize(rows, { since, until })
    const countries: Record<string, number> = {}
    let others = 0
    for (const [cc, n] of Object.entries(s.countries)) {
      if (n >= MIN_COUNTRY && cc !== 'xx') countries[cc] = n
      else others += n
    }
    if (others) countries.others = others
    return json({ ...s, countries, since, until }, 200, cache)
  } catch (e) {
    console.error('[peepless-site]', e instanceof Error ? e.message : e)
    return json({ error: 'could not read' }, 500, 'no-store')
  }
}

The range runs 30 days ago to today, in UTC.