|
| 1 | +import { Button, Typography } from "@mui/material"; |
| 2 | +import { Box } from "@mui/system"; |
| 3 | +import { |
| 4 | + DataGrid, |
| 5 | + GridColDef, |
| 6 | + GridRenderCellParams, |
| 7 | + useGridApiRef, |
| 8 | +} from "@mui/x-data-grid"; |
| 9 | +import { RenderRawContent } from "components/benchmark_v3/components/common/RawContentDialog"; |
| 10 | +import Link from "next/link"; |
| 11 | +import { useMemo } from "react"; |
| 12 | +import { |
| 13 | + fmtFixed2, |
| 14 | + formatHeaderName, |
| 15 | + getBenchmarkTimeSeriesComparisionTableRenderingConfig, |
| 16 | + renderBasedOnUnitConifg, |
| 17 | +} from "../helper"; |
| 18 | +import { groupKeyAndLabel } from "./BenchmarkTimeSeriesComparisonSection/BenchmarkTimeSeriesComparisonTable/ComparisonTableHelpers"; |
| 19 | + |
| 20 | +export default function BenchmarkRawDataTable({ |
| 21 | + config, |
| 22 | + data, |
| 23 | + title, |
| 24 | + isDebug = false, |
| 25 | +}: { |
| 26 | + config: any; |
| 27 | + data: any; |
| 28 | + title?: { |
| 29 | + text: string; |
| 30 | + description?: string; |
| 31 | + }; |
| 32 | + isDebug?: boolean; |
| 33 | +}) { |
| 34 | + const apiRef = useGridApiRef(); |
| 35 | + |
| 36 | + const rows: any[] = useMemo(() => { |
| 37 | + return ToRawTableRow(config, data); |
| 38 | + }, [data]); |
| 39 | + |
| 40 | + const allColumns = useMemo(() => { |
| 41 | + const s = new Set<string>(); |
| 42 | + rows.forEach((r) => |
| 43 | + r.rowItem.forEach((i: any) => { |
| 44 | + Object.keys(i ?? {}).forEach((k) => { |
| 45 | + const groupItem = i[k]; |
| 46 | + // helps debuging if the group item has more than one data item |
| 47 | + if (groupItem?.data?.length > 1) { |
| 48 | + groupItem.data.forEach((d: any, idx: number) => { |
| 49 | + s.add(`${k}||${idx}`); |
| 50 | + }); |
| 51 | + } else { |
| 52 | + s.add(k); |
| 53 | + } |
| 54 | + }); |
| 55 | + }) |
| 56 | + ); |
| 57 | + const auto = Array.from(s).sort(); |
| 58 | + return auto; |
| 59 | + }, [rows]); |
| 60 | + |
| 61 | + const columns: GridColDef[] = useMemo( |
| 62 | + () => getTableConlumnRendering(config, allColumns), |
| 63 | + [allColumns, config] |
| 64 | + ); |
| 65 | + |
| 66 | + return ( |
| 67 | + <Box> |
| 68 | + <Typography variant="h6">{title?.text}</Typography> |
| 69 | + {title?.description && ( |
| 70 | + <Typography variant="body2">{title.description}</Typography> |
| 71 | + )} |
| 72 | + {isDebug && ( |
| 73 | + <RenderRawContent |
| 74 | + data={rows} |
| 75 | + title="Report Raw Json" |
| 76 | + buttonName="View Full Raw Data" |
| 77 | + type="json" |
| 78 | + /> |
| 79 | + )} |
| 80 | + <Button |
| 81 | + onClick={() => |
| 82 | + apiRef?.current?.exportDataAsCsv({ |
| 83 | + allColumns: true, |
| 84 | + utf8WithBom: true, |
| 85 | + fileName: "benchmark_raw_data", |
| 86 | + }) |
| 87 | + } |
| 88 | + > |
| 89 | + Download CSV |
| 90 | + </Button> |
| 91 | + <DataGrid |
| 92 | + density="compact" |
| 93 | + apiRef={apiRef} |
| 94 | + rows={rows} |
| 95 | + columns={columns} |
| 96 | + pageSizeOptions={[25, 50, 100]} |
| 97 | + initialState={{ |
| 98 | + sorting: { |
| 99 | + sortModel: [{ field: "timestamp", sort: "asc" }], |
| 100 | + }, |
| 101 | + pagination: { |
| 102 | + paginationModel: { pageSize: 25 }, |
| 103 | + }, |
| 104 | + }} |
| 105 | + sx={{ |
| 106 | + "& .MuiDataGrid-virtualScroller": { scrollbarGutter: "stable" }, |
| 107 | + "& .MuiDataGrid-cell": { |
| 108 | + py: 0, // less vertical padding |
| 109 | + fontSize: "0.75rem", |
| 110 | + }, |
| 111 | + "& .MuiDataGrid-columnHeaders": { |
| 112 | + minHeight: 32, |
| 113 | + lineHeight: "32px", |
| 114 | + fontSize: "0.75rem", |
| 115 | + }, |
| 116 | + "& .MuiDataGrid-row": { |
| 117 | + minHeight: 32, |
| 118 | + }, |
| 119 | + }} |
| 120 | + /> |
| 121 | + </Box> |
| 122 | + ); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * function to get the table column rendering logics |
| 127 | + * |
| 128 | + * @param config |
| 129 | + * @param metricFields |
| 130 | + * @returns |
| 131 | + */ |
| 132 | +function getTableConlumnRendering( |
| 133 | + config: any, |
| 134 | + metricFields: string[] = [] |
| 135 | +): GridColDef[] { |
| 136 | + const metadataColumns: any[] = [ |
| 137 | + { |
| 138 | + field: "workflow_run", |
| 139 | + headerName: "Workflow Run", |
| 140 | + minWidth: 140, |
| 141 | + valueGetter: (_value: any, row: any) => { |
| 142 | + const wf = row?.workflow_id ?? ""; |
| 143 | + const job = row?.job_id ?? ""; |
| 144 | + return `${wf}/${job}`; |
| 145 | + }, |
| 146 | + valueFormatter: (value: any, row: any) => { |
| 147 | + return value ?? ""; |
| 148 | + }, |
| 149 | + renderCell: (params: GridRenderCellParams<any>) => ( |
| 150 | + <Link href={params.row.job_url} target="_blank" rel="noopener"> |
| 151 | + {params.value} |
| 152 | + </Link> |
| 153 | + ), |
| 154 | + }, |
| 155 | + { |
| 156 | + field: "commit", |
| 157 | + headerName: "Commit", |
| 158 | + renderCell: (params: GridRenderCellParams<any>) => ( |
| 159 | + <Link href={params.row.commit_url} target="_blank" rel="noopener"> |
| 160 | + <span |
| 161 | + style={{ |
| 162 | + fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace", |
| 163 | + }} |
| 164 | + > |
| 165 | + {String(params.value).slice(0, 8)} |
| 166 | + </span> |
| 167 | + </Link> |
| 168 | + ), |
| 169 | + }, |
| 170 | + { |
| 171 | + field: "timestamp", |
| 172 | + headerName: "Timestamp", |
| 173 | + }, |
| 174 | + ]; |
| 175 | + |
| 176 | + const metadata = config?.extraMetadata ?? []; |
| 177 | + const metadataCols: GridColDef[] = metadata |
| 178 | + .filter((k: any) => !!k.field) // skip fields that are not defined |
| 179 | + .map((k: any) => ({ |
| 180 | + field: k.field, |
| 181 | + headerName: k.displayName ?? k.field, |
| 182 | + flex: 0.5, |
| 183 | + renderCell: (p: any) => ( |
| 184 | + <Typography variant="body2">{p.row[k.field]}</Typography> |
| 185 | + ), |
| 186 | + })); |
| 187 | + |
| 188 | + const metricCols: GridColDef[] = metricFields.map((field) => ({ |
| 189 | + field, |
| 190 | + headerName: formatHeaderName( |
| 191 | + field, |
| 192 | + config?.renderOptions?.tableRenderingBook |
| 193 | + ), |
| 194 | + sortable: false, |
| 195 | + filterable: false, |
| 196 | + valueGetter: (_value: any, row: any) => { |
| 197 | + const data = row.rowItem; |
| 198 | + if (data.length == 0) { |
| 199 | + return undefined; |
| 200 | + } |
| 201 | + let fieldName = field; |
| 202 | + let idx = 0; |
| 203 | + if (field.split("||").length > 1) { |
| 204 | + idx = Number(field.split("||")[1]); |
| 205 | + fieldName = field.split("||")[0]; |
| 206 | + } |
| 207 | + const value = data[0][fieldName]?.data[idx]?.value; |
| 208 | + return value; |
| 209 | + }, |
| 210 | + valueFormatter: (value: any, row: any) => { |
| 211 | + let fieldName = field; |
| 212 | + const rc = getBenchmarkTimeSeriesComparisionTableRenderingConfig( |
| 213 | + fieldName, |
| 214 | + config |
| 215 | + ); |
| 216 | + return renderBasedOnUnitConifg(fmtFixed2(value), rc?.unit); |
| 217 | + }, |
| 218 | + renderCell: (params: GridRenderCellParams<any>) => { |
| 219 | + return <Box>{params.formattedValue ?? ""}</Box>; |
| 220 | + }, |
| 221 | + })); |
| 222 | + |
| 223 | + return [...metadataColumns, ...metadataCols, ...metricCols]; |
| 224 | +} |
| 225 | + |
| 226 | +/** |
| 227 | + * Transform the data into a table row item |
| 228 | + * @param config |
| 229 | + * @param data |
| 230 | + * @returns |
| 231 | + */ |
| 232 | +export function ToRawTableRow(config: any, data: any) { |
| 233 | + const m = new Map<string, any>(); |
| 234 | + for (const d of data ?? []) { |
| 235 | + const i = d.group_info; |
| 236 | + const wf = String(i?.workflow_id ?? ""); |
| 237 | + const jobId = String(i?.job_id ?? ""); |
| 238 | + const sourceRepo = i?.repo ?? ""; |
| 239 | + const repoUrl = `https://github.com/${sourceRepo}`; |
| 240 | + const commitUrl = `${repoUrl}/commit/${i?.commit ?? ""}`; |
| 241 | + const jobUrl = `${repoUrl}/actions/runs/${wf}/job/${jobId}`; |
| 242 | + const rawData = d.data ?? []; |
| 243 | + const { key } = groupKeyAndLabel(i); |
| 244 | + if (!m.has(key)) { |
| 245 | + m.set(key, { |
| 246 | + ...i, |
| 247 | + job_id: jobId, |
| 248 | + workflow_id: wf, |
| 249 | + commit: i?.commit ?? "", |
| 250 | + commit_url: commitUrl, |
| 251 | + job_url: jobUrl, |
| 252 | + repo: String(i?.repo ?? ""), |
| 253 | + timestamp: i?.granularity_bucket ?? "", |
| 254 | + id: key, |
| 255 | + rowItem: [], |
| 256 | + }); |
| 257 | + } |
| 258 | + m.get(key)!.rowItem.push(rawData); |
| 259 | + } |
| 260 | + return Array.from(m.values()); |
| 261 | +} |
0 commit comments