Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions common/src/util/__tests__/db-health-alerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,34 @@ describe('evaluateBusyBackendRank', () => {
expect(evaluateBusyBackendRank([])).toEqual({ breach: false, top: null })
})
})

describe('evaluateStatCoverage percentage formatting', () => {
it('handles zero whole gracefully', () => {
const cov = evaluateStatCoverage(
coverageRow({ pgss_rows: 0, pgss_with_text: 0 }),
)
expect(cov.summary).toContain('0/0')
expect(cov.summary).toContain('0%')
})

it('formats normal percentages correctly', () => {
const cov = evaluateStatCoverage(
coverageRow({ pgss_rows: 200, pgss_with_text: 50 }),
)
expect(cov.summary).toContain('25%')
})

it('rounds percentages correctly', () => {
const cov = evaluateStatCoverage(
coverageRow({ pgss_rows: 300, pgss_with_text: 100 }),
)
expect(cov.summary).toContain('33%')
})

it('handles 100% correctly', () => {
const cov = evaluateStatCoverage(
coverageRow({ pgss_rows: 100, pgss_with_text: 100 }),
)
expect(cov.summary).toContain('100%')
})
})
6 changes: 4 additions & 2 deletions common/src/util/db-health-alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,10 @@ export function evaluateStatCoverage(row: StatCoverageRow): StatCoverage {
// may break the alert that does not consult it.
const statementsBlind = statementRows === 0
const activityBlind = activityVisible === 0
const pct = (part: number, whole: number) =>
whole > 0 ? Math.round((part / whole) * 100) : 0
const pct = (part: number, whole: number) => {
if (!Number.isFinite(part) || !Number.isFinite(whole) || whole <= 0) return 0
return Math.round((part / whole) * 100)
}
const summary = row.has_read_all_stats
? `role ${row.role} has pg_read_all_stats: full fleet visibility`
: `role ${row.role} lacks pg_read_all_stats — ${statementsWithText}/${statementRows} ` +
Expand Down
Loading