* Sums one dimension over the range. * * On the `machines` metric: `daily_dim_counts.machines` is per day, so summing * it over a range gives **machine-days**, not distinct machines — a machine * seen on ten days counts ten times. A range-wide distinct count per dimension * value is not recover
(env: Env, url: URL, range: Range)
| 773 | * `max` is over a single row and the question does not arise. |
| 774 | */ |
| 775 | async function breakdown(env: Env, url: URL, range: Range): Promise<ApiResult> { |
| 776 | const dim = url.searchParams.get('dim') ?? ''; |
| 777 | const spec = DIMS[dim]; |
| 778 | if (!spec) return fail(`unknown dim — one of: ${Object.keys(DIMS).join(', ')}`); |
| 779 | |
| 780 | const requestedMetric = url.searchParams.get('metric'); |
| 781 | if (requestedMetric !== null && requestedMetric !== 'count' && requestedMetric !== 'machines') { |
| 782 | return fail('metric must be count or machines'); |
| 783 | } |
| 784 | const metric = requestedMetric ?? spec.metric; |
| 785 | |
| 786 | const rawLimit = url.searchParams.get('limit'); |
| 787 | const limit = rawLimit === null ? DEFAULT_BREAKDOWN_LIMIT : Number(rawLimit); |
| 788 | if (!Number.isInteger(limit) || limit < 1 || limit > MAX_BREAKDOWN_LIMIT) { |
| 789 | return fail(`limit must be an integer between 1 and ${MAX_BREAKDOWN_LIMIT}`); |
| 790 | } |
| 791 | |
| 792 | const event = url.searchParams.get('event') ?? spec.event ?? null; |
| 793 | if (event !== null && !/^[a-z_]{1,32}$/.test(event)) return fail('event must be a bare event name'); |
| 794 | |
| 795 | const binds: (string | number)[] = [dim, range.from, range.to]; |
| 796 | if (event !== null) binds.push(event); |
| 797 | |
| 798 | const { results } = await env.DB.prepare( |
| 799 | `SELECT value, sum(day_count) AS count, sum(day_machines) AS machines |
| 800 | FROM (SELECT day, value, sum(count) AS day_count, max(machines) AS day_machines |
| 801 | FROM daily_dim_counts |
| 802 | WHERE dim = ? AND day BETWEEN ? AND ?${event !== null ? ' AND event = ?' : ''} |
| 803 | GROUP BY day, value) |
| 804 | GROUP BY value`, |
| 805 | ) |
| 806 | .bind(...binds) |
| 807 | .all<BreakdownRow>(); |
| 808 | |
| 809 | const rows = results.map((r) => ({ |
| 810 | value: r.value, |
| 811 | count: r.count ?? 0, |
| 812 | machines: r.machines ?? 0, |
| 813 | })); |
| 814 | |
| 815 | const pick = (r: BreakdownRow): number => (metric === 'machines' ? r.machines : r.count); |
| 816 | |
| 817 | let ordered: BreakdownRow[]; |
| 818 | let truncated = false; |
| 819 | if (spec.order === 'bucket' && spec.buckets) { |
| 820 | // An ordered scale: the buckets keep their own order and all of them show, |
| 821 | // including empty ones, so the ordinal colour ramp always means the same thing. |
| 822 | const found = new Map(rows.map((r) => [r.value, r])); |
| 823 | const extra = rows.filter((r) => !spec.buckets?.includes(r.value)).sort((x, y) => pick(y) - pick(x)); |
| 824 | ordered = [ |
| 825 | ...spec.buckets.map((b) => found.get(b) ?? { value: b, count: 0, machines: 0 }), |
| 826 | ...extra, |
| 827 | ]; |
| 828 | } else { |
| 829 | const sorted = [...rows].sort( |
| 830 | spec.order === 'version_desc' |
| 831 | ? (x, y) => compareVersionsDesc(x.value, y.value) |
| 832 | : (x, y) => pick(y) - pick(x) || x.value.localeCompare(y.value), |