// Publication stats: audience, output and the shape of what a publication // writes. The numbers are public, so every reader gets them. // // The aggregation is pure and testable: the card supplies the records and the // two counts, and this file decides what the numbers mean. The recent-document // list is deliberately absent: the article card and the publication's own // pages already carry that, and a second list only made the popup taller. import { articleDetails } from './article' import type { DocumentRecord } from './types' /** How many documents the stats read for the reading-time average. */ export const DESK_SCAN = 100 export interface DeskData { /** Constellation's subscriber total, or null when it could not be read. */ subscribers: number | null /** Constellation's document count, or null when it could not be read. */ posts: number | null /** How many records this scan actually read. */ scanned: number /** The scan hit its cap, so `posts` may be the better total. */ truncated: boolean averageMinutes?: number } function title(value: unknown): string | undefined { if (typeof value !== 'string') return undefined const trimmed = value.trim() return trimmed || undefined } /** * What the stats show, from the records they read and the two counts they were * given. `subscribers` and `posts` stay null when their lookups failed: the * card draws a dash rather than a zero it does not believe. */ export function buildDesk( records: { uri: string; value: DocumentRecord }[], counts: { subscribers: number | null; posts: number | null }, ): DeskData { const timed = records .filter((record) => !!title(record.value.title)) .map((record) => articleDetails(record.value).readingMinutes) .filter((minutes): minutes is number => typeof minutes === 'number' && minutes > 0) const averageMinutes = timed.length > 0 ? Math.max(1, Math.round(timed.reduce((sum, minutes) => sum + minutes, 0) / timed.length)) : undefined return { subscribers: counts.subscribers, posts: counts.posts, scanned: records.length, truncated: records.length >= DESK_SCAN, ...(averageMinutes ? { averageMinutes } : {}), } }