Files
community-rule/lib/server/ruleLaunchCounts.ts
T
adilallo d9c475a967 Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
2026-11-10 18:32:33 -07:00

69 lines
1.8 KiB
TypeScript

export const RULE_COUNT_FOLD_THRESHOLD = 5;
export type RuleCountRow = {
orgType: string | null;
place: string | null;
};
export type RuleCountCell = {
orgType: string | null;
place: string | null;
count: number;
folded: boolean;
};
export type RuleLaunchCounts = {
finished: number;
cells: RuleCountCell[];
};
function normalizePlace(raw: string | null): string {
return (raw ?? "").trim().replace(/\s+/g, " ").toLowerCase();
}
function cellKey(orgType: string | null, placeKey: string): string {
return `${orgType ?? ""}\u0000${placeKey}`;
}
/**
* Finished-Rule counts by group type and place. Any cell under five is folded
* so a single group cannot be picked out.
*/
export function aggregateRuleLaunchCounts(rows: RuleCountRow[]): RuleLaunchCounts {
const grouped = new Map<
string,
{ orgType: string | null; place: string | null; count: number }
>();
for (const row of rows) {
const orgType = row.orgType && row.orgType.trim().length > 0 ? row.orgType : null;
const placeRaw = row.place && row.place.trim().length > 0 ? row.place.trim() : null;
const key = cellKey(orgType, normalizePlace(placeRaw));
const existing = grouped.get(key);
if (existing) {
existing.count += 1;
} else {
grouped.set(key, { orgType, place: placeRaw, count: 1 });
}
}
const shown: RuleCountCell[] = [];
let foldedCount = 0;
for (const cell of grouped.values()) {
if (cell.count < RULE_COUNT_FOLD_THRESHOLD) {
foldedCount += cell.count;
continue;
}
shown.push({ ...cell, folded: false });
}
shown.sort((a, b) => b.count - a.count || (a.orgType ?? "").localeCompare(b.orgType ?? ""));
if (foldedCount > 0) {
shown.push({
orgType: null,
place: null,
count: foldedCount,
folded: true,
});
}
return { finished: rows.length, cells: shown };
}