|
| 1 | +import { deepClone } from "@mui/x-data-grid/internals"; |
| 2 | +import { EXCLUDED_METRICS } from "lib/benchmark/llms/common"; |
| 3 | +import { queryClickhouseSaved } from "lib/clickhouse"; |
| 4 | +import { BenchmarkGroupRequest } from "lib/datamodels/benchmark_group_request"; |
| 5 | +import { formatZodError } from "lib/datamodels/zodUtils"; |
| 6 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 7 | + |
| 8 | +const DEFAULT_TABLE_GROUP = [ |
| 9 | + "device", |
| 10 | + "backend", |
| 11 | + "model", |
| 12 | + "dtype", |
| 13 | + "backend", |
| 14 | + "arch", |
| 15 | +]; |
| 16 | +const DEFAULT_ROW_GROUP = ["workflow_id", "job_id", "granularity_bucket"]; |
| 17 | +const BENCNMARK_TABLE_NAME = "oss_ci_benchmark_llms"; |
| 18 | +export default async function handler( |
| 19 | + req: NextApiRequest, |
| 20 | + res: NextApiResponse |
| 21 | +) { |
| 22 | + const request = BenchmarkGroupRequest.safeParse(req.query); |
| 23 | + |
| 24 | + if (!request.success) { |
| 25 | + return res.status(400).json({ |
| 26 | + error: "Invalid query parameters", |
| 27 | + details: formatZodError(request.error), |
| 28 | + }); |
| 29 | + } |
| 30 | + const qp = request.data; |
| 31 | + const groupTableByFields = |
| 32 | + qp.group_table_by_fields || deepClone(DEFAULT_TABLE_GROUP); |
| 33 | + const groupRowByFields = qp.groupRowByFields || deepClone(DEFAULT_ROW_GROUP); |
| 34 | + |
| 35 | + const params = { |
| 36 | + excludedMetrics: EXCLUDED_METRICS, |
| 37 | + benchmarks: [qp.benchmark_name], |
| 38 | + granularity: "hour", |
| 39 | + repo: qp.repo, |
| 40 | + startTime: qp.start_time, |
| 41 | + stopTime: qp.end_time, |
| 42 | + models: [], |
| 43 | + device: "", |
| 44 | + dtypes: [], |
| 45 | + backends: [], |
| 46 | + commits: [], |
| 47 | + branches: [], |
| 48 | + arch: "", |
| 49 | + }; |
| 50 | + |
| 51 | + console.log("inputs", params); |
| 52 | + const response = await queryClickhouseSaved(BENCNMARK_TABLE_NAME, params); |
| 53 | + const tableGroups = new Map(); |
| 54 | + |
| 55 | + response.forEach((row: any) => { |
| 56 | + // Build table-level key |
| 57 | + const tableKey = groupTableByFields |
| 58 | + .map((f: any) => (row[f] ? `${row[f]}` : "")) |
| 59 | + .join("|"); |
| 60 | + |
| 61 | + // Build row-level key |
| 62 | + const rowKey = groupRowByFields.map((f: any) => row[f] ?? "").join("|"); |
| 63 | + |
| 64 | + if (!tableGroups.has(tableKey)) { |
| 65 | + tableGroups.set(tableKey, new Map()); |
| 66 | + } |
| 67 | + |
| 68 | + const tableMap = tableGroups.get(tableKey)!; |
| 69 | + if (!tableMap.has(rowKey)) { |
| 70 | + tableMap.set(rowKey, []); |
| 71 | + tableMap.set("group_data", new Map()); |
| 72 | + groupRowByFields.forEach((f: any) => { |
| 73 | + tableMap.get("group_data").set(f, row[f]); |
| 74 | + }); |
| 75 | + } |
| 76 | + tableMap.get(rowKey)!.push(row); |
| 77 | + }); |
| 78 | + |
| 79 | + const result: { |
| 80 | + groupInfo: Record<string, string>; |
| 81 | + rows: Record<string, any[]>; |
| 82 | + }[] = []; |
| 83 | + |
| 84 | + for (const [tableKey, rowMap] of tableGroups.entries()) { |
| 85 | + const groupValues = tableKey.split("|"); |
| 86 | + |
| 87 | + const groupInfo = Object.fromEntries( |
| 88 | + groupTableByFields.map((field: any, i: number) => [field, groupValues[i]]) |
| 89 | + ); |
| 90 | + |
| 91 | + const rows: Record<string, any[]> = Object.fromEntries(rowMap.entries()); |
| 92 | + result.push({ groupInfo, rows }); |
| 93 | + } |
| 94 | + |
| 95 | + const rowGroupFields = ["workflow_id", "job_id", "granularity_bucket"]; |
| 96 | + |
| 97 | + const finalTables: { |
| 98 | + groupInfo: Record<string, string>; |
| 99 | + rows: Record<string, any>[]; |
| 100 | + }[] = []; |
| 101 | + |
| 102 | + for (const group of result) { |
| 103 | + const pivotedRows: Record<string, any>[] = []; |
| 104 | + |
| 105 | + for (const rowList of Object.values(group.rows)) { |
| 106 | + if (!Array.isArray(rowList) || rowList.length === 0) continue; // ✅ Skip empty |
| 107 | + |
| 108 | + const rowObj: Record<string, any> = {}; |
| 109 | + |
| 110 | + // Set row group fields from the first item |
| 111 | + rowGroupFields.forEach((field) => { |
| 112 | + rowObj[field] = rowList[0][field]; |
| 113 | + }); |
| 114 | + |
| 115 | + // Add each metric -> actual value |
| 116 | + for (const record of rowList) { |
| 117 | + // Only include valid metric and actual values |
| 118 | + if (record.metric && record.actual !== undefined) { |
| 119 | + rowObj[record.metric] = record.actual; |
| 120 | + } |
| 121 | + } |
| 122 | + pivotedRows.push(rowObj); |
| 123 | + } |
| 124 | + |
| 125 | + const info = deepClone(group.groupInfo); |
| 126 | + info["total_rows"] = pivotedRows.length; |
| 127 | + finalTables.push({ |
| 128 | + groupInfo: info, |
| 129 | + rows: pivotedRows, |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + console.log(JSON.stringify(finalTables[0], null, 2)); |
| 134 | + |
| 135 | + res.status(200).json(finalTables); |
| 136 | +} |
0 commit comments