221 lines · 8.2 KB
import { DurableObject, env } from "cloudflare:workers";
import { batch, stmt } from "../db";
import { branchSha, listRefs } from "../git/refs";
import { withStore } from "../git/store";
import { loadRepoById } from "../repos";
import { CommitIndex } from "./commit-index";
import type { ContributorRow, EmailDay } from "./commit-index";
import { fastForward, toIndexed } from "./fast-forward";

const STATE_KEY = "state";
const LOG_PAGE = 1000;
const PAGES_PER_ALARM = 5;
const MAX_INDEXED_COMMITS = 300_000;
const PUBLISH_CHUNK = 400;
const ROWS_PER_INSERT = 20;
const RETRY_ON_ERROR_MS = 60_000;
const WEEKS = 52;
const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
const MAX_CONTRIBUTORS = 100;
const CONTRIBUTOR_SPARKLINES = 25;
const MAX_DAY_COMMITS = 20;

type Rebuild = { target: string; gen: number; offset: number };

type State = {
  repoId: string;
  branch: string | null;
  head: string | null;
  gen: number;
  rebuild: Rebuild | null;
  truncated: boolean;
  /** Bumped when indexed data gains fields (e.g. co-authors); an older index is rebuilt once. */
  schema?: number;
};

const INDEX_SCHEMA = 2;

export type InsightsSummary = {
  status: "empty" | "indexing" | "ready";
  head: string | null;