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