All files / lib/evolution/exporters report.ts

16.12% Statements 5/31
100% Branches 0/0
0% Functions 0/3
16.12% Lines 5/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x     1x     1x                               1x       1x                        
/**
 * @file report.ts
 * تحويل اللوجات إلى CSV/JSON + تنزيل المتصفح
 */
import { EvolutionLog } from '@/lib/evolution/history.collector';
 
export function toCSV(rows: EvolutionLog[]): string {
  const header = ['timestamp', 'trend', 'confidence', 'changeRate', 'notes'].join(',');
  const body = rows
    .map((r) =>
      [
        r.timestamp,
        r.trend,
        (r.confidence * 100).toFixed(1) + '%',
        r.changeRate.toFixed(2) + '%',
        r.notes ? `"${r.notes.replace(/"/g, '""')}"` : '',
      ].join(',')
    )
    .join('\n');
  return header + '\n' + body;
}
 
export function toJSON(rows: EvolutionLog[]): string {
  return JSON.stringify(rows, null, 2);
}
 
export function download(filename: string, content: string, type = 'text/plain') {
  if (typeof document === 'undefined') return;
  const blob = new Blob([content], { type });
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  a.remove();
  setTimeout(() => URL.revokeObjectURL(url), 500);
}