95 lines · 3.6 KB
import { createServerFn } from "@tanstack/react-start";
import { z } from "zod";
import { viewerMiddleware } from "./middleware";
import { repoDb } from "~/server/scoped-db";
import { accountsForEmails } from "~/server/commit-authors";
import { limitBrowse } from "~/server/limits";
import { requireRepoAccess } from "~/server/repos";
import { repoInsights } from "~/server/insights/repo-insights-do";
import { repoRef } from "~/lib/validation";

const DAY_MS = 24 * 60 * 60 * 1000;
const PERIODS = { week: 7, month: 30 } as const;

type PulseCounts = {
  issuesOpened: number;
  issuesClosed: number;
  pullsOpened: number;
  pullsMerged: number;
  activeAuthors: number;
};

async function pulse(repoId: string, since: number): Promise<PulseCounts> {
  const row = await repoDb(repoId).first<PulseCounts>(
    `SELECT
       (SELECT COUNT(*) FROM issues WHERE repo_id = ?1 AND is_pull = 0 AND created_at >= ?2) AS issuesOpened,
       (SELECT COUNT(*) FROM issues WHERE repo_id = ?1 AND is_pull = 0 AND closed_at >= ?2) AS issuesClosed,
       (SELECT COUNT(*) FROM issues WHERE repo_id = ?1 AND is_pull = 1 AND created_at >= ?2) AS pullsOpened,
       (SELECT COUNT(*) FROM pull_requests WHERE repo_id = ?1 AND merged_at >= ?2) AS pullsMerged,
       (SELECT COUNT(DISTINCT author_id) FROM issues WHERE repo_id = ?1 AND created_at >= ?2) AS activeAuthors`,
    repoId,
    since,
  );
  return row ?? { issuesOpened: 0, issuesClosed: 0, pullsOpened: 0, pullsMerged: 0, activeAuthors: 0 };
}

type Contributor = {
  name: string;
  account: string | null;
  commits: number;
  first: number;