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
|
export {
DEFAULT_WINDOW_DAYS,
VALID_WINDOWS,
parseWindow,
} from "./response-time";
import { DEFAULT_WINDOW_DAYS } from "./response-time";
export const PR_SIZE_CLASSES = [
{ key: "xs", label: "XS", max: 10, description: "≤ 10 lines" },
{ key: "s", label: "S", max: 50, description: "11 – 50 lines" },
{ key: "m", label: "M", max: 250, description: "51 – 250 lines" },
{ key: "l", label: "L", max: 1000, description: "251 – 1000 lines" },
{
key: "xl",
label: "XL",
max: Number.POSITIVE_INFINITY,
description: "> 1000 lines",
},
] as const;
export type PrSizeClassKey = (typeof PR_SIZE_CLASSES)[number]["key"];
export const DEFAULT_TOP_N = 10;
export interface PrSizeInput {
id: string;
number: number;
title: string;
state: string;
isDraft?: boolean;
createdAt: Date | string;
mergedAt?: Date | string | null;
closedAt?: Date | string | null;
additions: number;
deletions: number;
files: number;
}
export interface PrSizeStat extends PrSizeInput {
linesChanged: number;
sizeClass: PrSizeClassKey;
}
export interface PrSizeSummary {
total: number;
merged: number;
open: number;
medianLines: number;
meanLines: number;
p90Lines: number;
largestLines: number;
smallestLines: number;
smallPrRatio: number;
}
export interface PrSizeBucket {
key: PrSizeClassKey;
label: string;
description: string;
count: number;
bytes: number;
}
export interface PrSizeReport {
windowDays: number;
now: number;
perPr: PrSizeStat[];
summary: PrSizeSummary;
buckets: PrSizeBucket[];
largest: PrSizeStat[];
}
function toTime(value: Date | string | null | undefined): number | null {
if (!value) return null;
const t = value instanceof Date ? value.getTime() : Date.parse(value);
return Number.isFinite(t) ? t : null;
}
export function classifyPrSize(linesChanged: number): PrSizeClassKey {
if (!Number.isFinite(linesChanged) || linesChanged < 0) return "xs";
for (const c of PR_SIZE_CLASSES) {
if (linesChanged <= c.max) return c.key;
}
return "xl";
}
function safeLines(additions: number, deletions: number): number {
const a = Number.isFinite(additions) && additions > 0 ? additions : 0;
const d = Number.isFinite(deletions) && deletions > 0 ? deletions : 0;
return a + d;
}
function anchorTime(pr: PrSizeInput): number | null {
if (pr.state === "merged" && pr.mergedAt) {
return toTime(pr.mergedAt);
}
return toTime(pr.createdAt);
}
export function computePrSizeStats(
prs: readonly PrSizeInput[],
windowDays: number,
now: number = Date.now()
): PrSizeStat[] {
const cutoff = windowDays > 0 ? now - windowDays * 24 * 60 * 60 * 1000 : null;
const out: PrSizeStat[] = [];
for (const pr of prs) {
const anchor = anchorTime(pr);
if (anchor === null) continue;
if (cutoff !== null && anchor < cutoff) continue;
const linesChanged = safeLines(pr.additions, pr.deletions);
out.push({
...pr,
linesChanged,
sizeClass: classifyPrSize(linesChanged),
});
}
return out;
}
function percentile(sortedAsc: readonly number[], p: number): number {
const n = sortedAsc.length;
if (n === 0) return 0;
if (n === 1) return sortedAsc[0]!;
const rank = (p / 100) * (n - 1);
const lo = Math.floor(rank);
const hi = Math.ceil(rank);
if (lo === hi) return sortedAsc[lo]!;
const frac = rank - lo;
return Math.round(sortedAsc[lo]! + frac * (sortedAsc[hi]! - sortedAsc[lo]!));
}
export function summarisePrSizes(stats: readonly PrSizeStat[]): PrSizeSummary {
const sizes = stats.map((s) => s.linesChanged).sort((a, b) => a - b);
const n = sizes.length;
const total = sizes.reduce((acc, v) => acc + v, 0);
const merged = stats.filter((s) => s.state === "merged").length;
const open = stats.filter(
(s) => s.state === "open" && !s.isDraft
).length;
const smallCount = stats.filter(
(s) => s.sizeClass === "xs" || s.sizeClass === "s"
).length;
return {
total: n,
merged,
open,
medianLines: n === 0 ? 0 : percentile(sizes, 50),
meanLines: n === 0 ? 0 : Math.round(total / n),
p90Lines: n === 0 ? 0 : percentile(sizes, 90),
largestLines: n === 0 ? 0 : sizes[n - 1]!,
smallestLines: n === 0 ? 0 : sizes[0]!,
smallPrRatio: n === 0 ? 0 : Math.round((smallCount / n) * 1000) / 10,
};
}
export function bucketPrSizes(stats: readonly PrSizeStat[]): PrSizeBucket[] {
const out: PrSizeBucket[] = PR_SIZE_CLASSES.map((c) => ({
key: c.key,
label: c.label,
description: c.description,
count: 0,
bytes: 0,
}));
for (const s of stats) {
const b = out.find((x) => x.key === s.sizeClass)!;
b.count++;
b.bytes += s.linesChanged;
}
return out;
}
export function topLargestPrs(
stats: readonly PrSizeStat[],
limit: number = DEFAULT_TOP_N
): PrSizeStat[] {
const n =
Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : DEFAULT_TOP_N;
return stats
.slice()
.sort((a, b) => {
if (a.linesChanged !== b.linesChanged) {
return b.linesChanged - a.linesChanged;
}
return b.number - a.number;
})
.slice(0, n);
}
export interface BuildPrSizeReportOptions {
prs: readonly PrSizeInput[];
windowDays?: number;
now?: number;
topN?: number;
}
export function buildPrSizeReport(
opts: BuildPrSizeReportOptions
): PrSizeReport {
const now = opts.now ?? Date.now();
const windowDays = opts.windowDays ?? DEFAULT_WINDOW_DAYS;
const perPr = computePrSizeStats(opts.prs, windowDays, now);
return {
windowDays,
now,
perPr,
summary: summarisePrSizes(perPr),
buckets: bucketPrSizes(perPr),
largest: topLargestPrs(perPr, opts.topN ?? DEFAULT_TOP_N),
};
}
export const __internal = {
PR_SIZE_CLASSES,
DEFAULT_TOP_N,
classifyPrSize,
computePrSizeStats,
summarisePrSizes,
bucketPrSizes,
topLargestPrs,
buildPrSizeReport,
toTime,
anchorTime,
percentile,
safeLines,
};
|