CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | /**
* Block J18 — Repository pulse / activity summary.
*
* Pure rollups for the `/:owner/:repo/pulse` page. Takes already-fetched
* commits + PR + issue rows and buckets them into a time window. No I/O —
* the route handler is responsible for querying git + Drizzle.
*
* A "pulse" is a recent-activity snapshot over a rolling window (1d / 7d /
* 30d / 90d). It answers: "who's been pushing, what's moving, what's new
* and what's closing?"
*/
export const PULSE_WINDOWS = ["1d", "7d", "30d", "90d"] as const;
export type PulseWindow = (typeof PULSE_WINDOWS)[number];
export const DEFAULT_WINDOW: PulseWindow = "7d";
const WINDOW_DAYS: Record<PulseWindow, number> = {
"1d": 1,
"7d": 7,
"30d": 30,
"90d": 90,
};
/** Pure: validate a raw string as a supported pulse window, else fall back. */
export function parseWindow(raw: unknown): PulseWindow {
if (typeof raw === "string" && (PULSE_WINDOWS as readonly string[]).includes(raw)) {
return raw as PulseWindow;
}
return DEFAULT_WINDOW;
}
/** Pure: return the Date at the start of the window relative to `now`. */
export function windowStart(now: Date, w: PulseWindow): Date {
const days = WINDOW_DAYS[w];
const d = new Date(now.getTime());
d.setUTCDate(d.getUTCDate() - days);
return d;
}
/** Pure: number of days represented by a given pulse window. */
export function windowDays(w: PulseWindow): number {
return WINDOW_DAYS[w];
}
function toMs(d: string | Date | null | undefined): number | null {
if (d === null || d === undefined) return null;
if (d instanceof Date) {
const t = d.getTime();
return Number.isFinite(t) ? t : null;
}
const t = Date.parse(d);
return Number.isFinite(t) ? t : null;
}
function inWindow(t: number | null, start: Date, end: Date): boolean {
if (t === null) return false;
return t >= start.getTime() && t <= end.getTime();
}
// ---------------------------------------------------------------------------
// Commits
// ---------------------------------------------------------------------------
export interface PulseCommit {
sha: string;
author: string;
authorEmail: string;
date: string;
message?: string;
}
export interface ContributorCount {
author: string;
email: string;
count: number;
}
export interface CommitPulse {
total: number;
byAuthor: ContributorCount[];
firstSha: string | null;
lastSha: string | null;
}
/**
* Pure: count commits inside [start, end] and group by author email.
* `commits` is the newest-first list returned by `listCommits`.
*/
export function summariseCommits(
commits: PulseCommit[],
start: Date,
end: Date
): CommitPulse {
const inRange = commits.filter((c) => inWindow(toMs(c.date), start, end));
const counts = new Map<string, ContributorCount>();
for (const c of inRange) {
const emailKey = (c.authorEmail || "").toLowerCase().trim();
const nameKey = (c.author || "").toLowerCase().trim();
const key = emailKey || nameKey || "(unknown)";
const prev = counts.get(key);
if (prev) {
prev.count += 1;
} else {
counts.set(key, {
author: c.author || "(unknown)",
email: c.authorEmail || "",
count: 1,
});
}
}
const byAuthor = Array.from(counts.values()).sort(
(a, b) => b.count - a.count || a.author.localeCompare(b.author)
);
return {
total: inRange.length,
byAuthor,
firstSha: inRange.length ? inRange[inRange.length - 1].sha : null,
lastSha: inRange.length ? inRange[0].sha : null,
};
}
// ---------------------------------------------------------------------------
// Pull requests
// ---------------------------------------------------------------------------
export interface PulsePr {
id?: string;
number: number;
title: string;
state: string; // "open" | "closed" | "merged"
isDraft?: boolean;
authorName?: string;
createdAt: string | Date;
updatedAt: string | Date;
closedAt: string | Date | null;
mergedAt: string | Date | null;
}
export interface PrPulse {
opened: number;
mergedCount: number;
closed: number;
active: number;
openedList: PulsePr[];
mergedList: PulsePr[];
}
/**
* Pure: bucket PRs by what changed in-window.
* - `opened`: createdAt in window
* - `mergedCount`: mergedAt in window (mutually exclusive with closed)
* - `closed`: closedAt in window AND not merged in window
* - `active`: state='open' and updatedAt in window
*/
export function summarisePrs(prs: PulsePr[], start: Date, end: Date): PrPulse {
let opened = 0,
mergedCount = 0,
closed = 0,
active = 0;
const openedList: PulsePr[] = [];
const mergedList: PulsePr[] = [];
for (const p of prs) {
const created = toMs(p.createdAt);
const closedMs = toMs(p.closedAt);
const mergedMs = toMs(p.mergedAt);
const updated = toMs(p.updatedAt);
const createdIn = inWindow(created, start, end);
const mergedIn = inWindow(mergedMs, start, end);
const closedIn = inWindow(closedMs, start, end);
if (createdIn) {
opened++;
openedList.push(p);
}
if (mergedIn) {
mergedCount++;
mergedList.push(p);
} else if (closedIn) {
closed++;
}
if (p.state === "open" && inWindow(updated, start, end)) active++;
}
return { opened, mergedCount, closed, active, openedList, mergedList };
}
// ---------------------------------------------------------------------------
// Issues
// ---------------------------------------------------------------------------
export interface PulseIssue {
id?: string;
number: number;
title: string;
state: string; // "open" | "closed"
authorName?: string;
createdAt: string | Date;
updatedAt: string | Date;
closedAt: string | Date | null;
}
export interface IssuePulse {
opened: number;
closed: number;
active: number;
openedList: PulseIssue[];
closedList: PulseIssue[];
}
/**
* Pure: bucket issues into opened/closed/active counts over the window.
* - `opened`: createdAt in window
* - `closed`: closedAt in window
* - `active`: state='open' AND updatedAt in window
*/
export function summariseIssues(
issues: PulseIssue[],
start: Date,
end: Date
): IssuePulse {
let opened = 0,
closed = 0,
active = 0;
const openedList: PulseIssue[] = [];
const closedList: PulseIssue[] = [];
for (const i of issues) {
const created = toMs(i.createdAt);
const closedMs = toMs(i.closedAt);
const updated = toMs(i.updatedAt);
if (inWindow(created, start, end)) {
opened++;
openedList.push(i);
}
if (inWindow(closedMs, start, end)) {
closed++;
closedList.push(i);
}
if (i.state === "open" && inWindow(updated, start, end)) active++;
}
return { opened, closed, active, openedList, closedList };
}
// ---------------------------------------------------------------------------
// One-shot builder
// ---------------------------------------------------------------------------
export interface PulseReport {
window: PulseWindow;
days: number;
start: string;
end: string;
commits: CommitPulse;
prs: PrPulse;
issues: IssuePulse;
}
export function buildPulseReport(opts: {
window: PulseWindow;
now: Date;
commits: PulseCommit[];
prs: PulsePr[];
issues: PulseIssue[];
}): PulseReport {
const start = windowStart(opts.now, opts.window);
const end = opts.now;
return {
window: opts.window,
days: windowDays(opts.window),
start: start.toISOString(),
end: end.toISOString(),
commits: summariseCommits(opts.commits, start, end),
prs: summarisePrs(opts.prs, start, end),
issues: summariseIssues(opts.issues, start, end),
};
}
export const __internal = {
PULSE_WINDOWS,
DEFAULT_WINDOW,
WINDOW_DAYS,
parseWindow,
windowStart,
windowDays,
summariseCommits,
summarisePrs,
summariseIssues,
buildPulseReport,
};
|