// An X search channel's walk, planned (pure: no I/O, so every step is testable // without a fetcher). A walk goes BACKWARDS in search windows from a `top` day // to a `floor`, newest first — the older walk's arithmetic // (social/olderBackfill.ts) over the channel's query. What finished walks have // searched end to end is [coveredSince, coveredUntil); each fetch walks ONE // stretch outside it, newest first: // - the first walk: from `until` (or tomorrow) down to `since` (or until a // year of windows in a row holds no match); // - the new days: from `until`/tomorrow down to the day before // `coveredUntil` (one day of overlap, deduped); // - the earlier days, when `since` was moved below `coveredSince`. // A walk cut short (budget, cancel, drain, a limit, an error) is resumed from // its saved position by the next fetch before anything else. import type { XSearchWalkState } from "../posts-server"; import { addUtcDays, addUtcMonths, OLDER_WINDOW_MONTHS, utcDayOf, } from "../../social/olderBackfill"; import { xSearchQueryKey, type XSearchConfig } from "./config"; export type XSearchWalk = NonNullable; export type XSearchWalkKind = "resume" | "first" | "new" | "earlier"; export type XSearchWalkPlan = | { kind: XSearchWalkKind; walk: XSearchWalk } | { kind: "none"; reason: string }; const later = (a: string, b: string | undefined): string => (b !== undefined && b > a ? b : a); // The first window of a walk from `top` down to `floor`. export function startXSearchWalk(top: string, floor: string | undefined, months: number): XSearchWalk { return { top, ...(floor !== undefined ? { floor } : {}), since: later(addUtcMonths(top, -months), floor), until: top, emptyWindows: 0, }; } // The state this config's walk continues from: the stored one when it belongs // to the same query, else a fresh one (a new query is a new search). export function xSearchStateFor( config: Pick, stored: XSearchWalkState | undefined, ): XSearchWalkState { const queryKey = xSearchQueryKey(config); return stored && stored.queryKey === queryKey ? stored : { queryKey }; } // What the next fetch walks. export function planXSearchWalk( config: XSearchConfig, state: XSearchWalkState, now: Date = new Date(), ): XSearchWalkPlan { const months = config.windowMonths ?? OLDER_WINDOW_MONTHS; if (state.walk) return { kind: "resume", walk: state.walk }; const top = config.until ?? addUtcDays(utcDayOf(now), 1); if (config.since !== undefined && config.since >= top) { return { kind: "none", reason: `nothing to search: ${config.since} is not before ${top}` }; } if (!state.coveredUntil) { return { kind: "first", walk: startXSearchWalk(top, config.since, months) }; } if (top > state.coveredUntil) { // One day of overlap below what was covered: a post made on the day the // last walk started is read again (and skipped) rather than missed. const floor = later(addUtcDays(state.coveredUntil, -1), config.since); return { kind: "new", walk: startXSearchWalk(top, floor, months) }; } if (config.since !== undefined && state.coveredSince !== undefined && config.since < state.coveredSince) { return { kind: "earlier", walk: startXSearchWalk(addUtcDays(state.coveredSince, 1), config.since, months), }; } return { kind: "none", reason: `already searched ${state.coveredSince ?? "?"} – ${state.coveredUntil}` + (config.until ? "" : "; the next fetch after today searches the new days"), }; } // The covered span once `walk` has been walked to its end, its last window // starting at `endSince`. export function coverAfterWalk( state: Pick, walk: Pick, endSince: string, ): { coveredSince: string; coveredUntil: string } { return { coveredSince: state.coveredSince !== undefined && state.coveredSince < endSince ? state.coveredSince : endSince, coveredUntil: state.coveredUntil !== undefined && state.coveredUntil > walk.top ? state.coveredUntil : walk.top, }; } // One line for the channel page and the ops read. export function describeXSearchWalk(state: XSearchWalkState | undefined): string { if (!state) return "not searched yet"; const parts: string[] = []; if (state.coveredUntil) parts.push(`searched ${state.coveredSince ?? "?"} – ${state.coveredUntil}`); if (state.walk) { parts.push( `walking ${state.walk.since} – ${state.walk.until}` + (state.walk.floor ? ` (down to ${state.walk.floor})` : "") + (state.walk.maxId ? ", resuming inside the window" : ""), ); } if (parts.length === 0) return "not searched yet"; return parts.join("; "); }