mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
21 lines
638 B
TypeScript
21 lines
638 B
TypeScript
export type Stat = { n: number; p50: number; p95: number; max: number }
|
|
|
|
function nearestRank(sorted: number[], quantile: number): number {
|
|
return sorted[Math.min(sorted.length - 1, Math.max(0, Math.ceil(quantile * sorted.length) - 1))]!
|
|
}
|
|
|
|
export function stat(values: (number | undefined)[]): Stat | null {
|
|
const sorted = values
|
|
.filter((value): value is number => typeof value === 'number' && Number.isFinite(value))
|
|
.sort((a, b) => a - b)
|
|
if (sorted.length === 0) {
|
|
return null
|
|
}
|
|
return {
|
|
n: sorted.length,
|
|
p50: nearestRank(sorted, 0.5),
|
|
p95: nearestRank(sorted, 0.95),
|
|
max: sorted.at(-1)!
|
|
}
|
|
}
|