/ concept-collection / benchcompress
Sign in
concept-collection / benchcompress
49 lines · 1.6 KBBlameHistoryRaw
1import { BenchmarkResult } from "../../../types";
2import { formatNumber, formatSize } from "../utils/formatters";
3import { columns } from "../table/columns";
5export const exportToCsv = (
6 data: BenchmarkResult[],
7 selectedDataset: string,
8) => {
9 // Convert data to CSV
10 const headers = columns.map((col) => col.header).join(",");
11 const rows = data
12 .map((row) =>
13 columns
14 .map((col) => {
15 const value = row[col.accessorKey as keyof BenchmarkResult];
16 // Format numbers according to their display format
17 if (col.accessorKey === "compression_ratio") {
18 return `${formatNumber(value as number)}x`;
19 } else if (
20 col.accessorKey === "encode_time" ||
21 col.accessorKey === "decode_time"
22 ) {
23 return formatNumber(value as number, 4);
24 } else if (
25 col.accessorKey === "original_size" ||
26 col.accessorKey === "compressed_size"
27 ) {
28 return formatSize(value as number);
29 } else if (typeof value === "number") {
30 return formatNumber(value);
31 }
32 return value;
33 })
34 .join(","),
35 )
36 .join("\n");
37 const csv = `${headers}\n${rows}`;
39 // Create and trigger download
40 const blob = new Blob([csv], { type: "text/csv" });
41 const url = window.URL.createObjectURL(blob);
42 const a = document.createElement("a");
43 a.href = url;
44 a.download = `benchmark-results${selectedDataset ? `-${selectedDataset}` : ""}.csv`;
45 document.body.appendChild(a);
46 a.click();
47 document.body.removeChild(a);
48 window.URL.revokeObjectURL(url);
49};
moveopenescclose