CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
Blame · Line-by-line history
health-score.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 74d8c4d | 1 | import { db } from "../db"; |
| 2 | import { gateRuns, pullRequests, issues, repoAdvisoryAlerts } from "../db/schema"; | |
| 3 | import { eq, and, gte, sql, count } from "drizzle-orm"; | |
| 4 | ||
| 5 | export interface HealthComponent { | |
| 6 | score: number; | |
| 7 | max: number; | |
| 8 | label: string; | |
| 9 | hint: string; | |
| 10 | } | |
| 11 | ||
| 12 | export interface HealthScore { | |
| 13 | total: number; | |
| 14 | grade: "elite" | "strong" | "improving" | "needs-attention"; | |
| 15 | components: { | |
| 16 | security: HealthComponent; | |
| 17 | greenGates: HealthComponent; | |
| 18 | velocity: HealthComponent; | |
| 19 | maintenance: HealthComponent; | |
| 20 | }; | |
| 21 | } | |
| 22 | ||
| 23 | function formatDuration(hours: number): string { | |
| 24 | if (hours < 1) return `${Math.round(hours * 60)}m`; | |
| 25 | if (hours < 24) return `${hours.toFixed(1)}h`; | |
| 26 | return `${(hours / 24).toFixed(1)}d`; | |
| 27 | } | |
| 28 | ||
| 29 | export async function computeHealthScore(repoId: string): Promise<HealthScore> { | |
| 30 | const since30d = new Date(Date.now() - 30 * 86400e3); | |
| 31 | const since90d = new Date(Date.now() - 90 * 86400e3); | |
| 32 | ||
| 33 | const [advisoryCount, gateResult, prResult, issueResult] = await Promise.all([ | |
| 34 | db | |
| 35 | .select({ n: count() }) | |
| 36 | .from(repoAdvisoryAlerts) | |
| 37 | .where(and(eq(repoAdvisoryAlerts.repositoryId, repoId), eq(repoAdvisoryAlerts.status, "open"))) | |
| 38 | .then(r => Number(r[0]?.n ?? 0)) | |
| 39 | .catch(() => 0), | |
| 40 | ||
| 41 | db | |
| 42 | .select({ | |
| 43 | total: count(), | |
| 44 | passed: sql<number>`count(*) filter (where ${gateRuns.status} in ('passed','repaired'))`, | |
| 45 | }) | |
| 46 | .from(gateRuns) | |
| 47 | .where(and(eq(gateRuns.repositoryId, repoId), gte(gateRuns.createdAt, since30d))) | |
| 48 | .then(r => r[0] ?? { total: 0, passed: 0 }) | |
| 49 | .catch(() => ({ total: 0, passed: 0 })), | |
| 50 | ||
| 51 | db | |
| 52 | .select({ | |
| 53 | avgMinutes: sql<number>`avg(extract(epoch from (${pullRequests.mergedAt} - ${pullRequests.createdAt})) / 60)`, | |
| 54 | }) | |
| 55 | .from(pullRequests) | |
| 56 | .where(and(eq(pullRequests.repositoryId, repoId), eq(pullRequests.state, "merged"), gte(pullRequests.mergedAt, since90d))) | |
| 57 | .then(r => r[0]?.avgMinutes ?? null) | |
| 58 | .catch(() => null), | |
| 59 | ||
| 60 | db | |
| 61 | .select({ | |
| 62 | avgDays: sql<number>`avg(extract(epoch from (now() - ${issues.createdAt})) / 86400)`, | |
| 63 | }) | |
| 64 | .from(issues) | |
| 65 | .where(and(eq(issues.repositoryId, repoId), eq(issues.state, "open"))) | |
| 66 | .then(r => r[0]?.avgDays ?? null) | |
| 67 | .catch(() => null), | |
| 68 | ]); | |
| 69 | ||
| 70 | // Security (0-30) | |
| 71 | const secScore = advisoryCount === 0 ? 30 : advisoryCount === 1 ? 20 : advisoryCount <= 2 ? 15 : advisoryCount <= 4 ? 8 : 0; | |
| 72 | const secHint = advisoryCount === 0 ? "No open advisories" : `${advisoryCount} open advisor${advisoryCount === 1 ? "y" : "ies"}`; | |
| 73 | ||
| 74 | // Green gates (0-25) | |
| 75 | const total = Number(gateResult.total); | |
| 76 | const passed = Number(gateResult.passed); | |
| 77 | const rate = total > 0 ? passed / total : 1; | |
| 78 | const gateScore = total > 0 ? Math.round(rate * 25) : 12; | |
| 79 | const gateHint = total > 0 ? `${Math.round(rate * 100)}% passed (${total} runs, 30d)` : "No gate runs yet"; | |
| 80 | ||
| 81 | // Velocity (0-25) | |
| 82 | const avgHours = prResult !== null ? Number(prResult) / 60 : null; | |
| 83 | let velScore = 12; | |
| 84 | let velHint = "No merged PRs (90d)"; | |
| 85 | if (avgHours !== null) { | |
| 86 | if (avgHours <= 4) { velScore = 25; velHint = `Avg TTM ${formatDuration(avgHours)} — Elite`; } | |
| 87 | else if (avgHours <= 24) { velScore = 20; velHint = `Avg TTM ${formatDuration(avgHours)}`; } | |
| 88 | else if (avgHours <= 72) { velScore = 14; velHint = `Avg TTM ${formatDuration(avgHours)}`; } | |
| 89 | else if (avgHours <= 168) { velScore = 7; velHint = `Avg TTM ${formatDuration(avgHours)}`; } | |
| 90 | else { velScore = 0; velHint = `Avg TTM ${formatDuration(avgHours)} — PRs sit too long`; } | |
| 91 | } | |
| 92 | ||
| 93 | // Maintenance (0-20) | |
| 94 | const avgDays = issueResult !== null ? Number(issueResult) : null; | |
| 95 | let maintScore = 20; | |
| 96 | let maintHint = "Issue backlog healthy"; | |
| 97 | if (avgDays !== null) { | |
| 98 | if (avgDays > 90) { maintScore = 0; maintHint = `Avg open issue age ${Math.round(avgDays)}d — stale backlog`; } | |
| 99 | else if (avgDays > 30) { maintScore = 8; maintHint = `Avg open issue age ${Math.round(avgDays)}d`; } | |
| 100 | else if (avgDays > 14) { maintScore = 14; maintHint = `Avg open issue age ${Math.round(avgDays)}d`; } | |
| 101 | else { maintScore = 20; maintHint = `Avg open issue age ${Math.round(avgDays)}d`; } | |
| 102 | } | |
| 103 | ||
| 104 | const total2 = secScore + gateScore + velScore + maintScore; | |
| 105 | return { | |
| 106 | total: total2, | |
| 107 | grade: total2 >= 85 ? "elite" : total2 >= 70 ? "strong" : total2 >= 50 ? "improving" : "needs-attention", | |
| 108 | components: { | |
| 109 | security: { score: secScore, max: 30, label: "Security", hint: secHint }, | |
| 110 | greenGates: { score: gateScore, max: 25, label: "Green Gates", hint: gateHint }, | |
| 111 | velocity: { score: velScore, max: 25, label: "Velocity", hint: velHint }, | |
| 112 | maintenance: { score: maintScore, max: 20, label: "Maintenance", hint: maintHint }, | |
| 113 | }, | |
| 114 | }; | |
| 115 | } |