From c27ba7208356bf3b6e74bad89abf68d31bb1b714 Mon Sep 17 00:00:00 2001
From: deep1401 <gandhi0869@gmail.com>
Date: Fri, 28 Feb 2025 14:28:17 -0800
Subject: [PATCH] Download CSV

---
 .../Experiment/Eval/ViewCSVModal.tsx          | 40 ++++++++++++++++++-
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/renderer/components/Experiment/Eval/ViewCSVModal.tsx b/src/renderer/components/Experiment/Eval/ViewCSVModal.tsx
index bfd604f4..b6bccb7e 100644
--- a/src/renderer/components/Experiment/Eval/ViewCSVModal.tsx
+++ b/src/renderer/components/Experiment/Eval/ViewCSVModal.tsx
@@ -166,6 +166,28 @@ function formatScore(score) {
   }
 }
 
+const convertReportToCSV = (report: { header: any[]; body: any[] }) => {
+  if (!report?.header || !report?.body) return '';
+  const csvRows = [];
+  csvRows.push(report.header.join(','));
+  report.body.forEach((row) => {
+      const csvRow = row
+          .map((cell) => {
+              let cellText = '';
+              if (typeof cell === 'object') {
+                  // Convert objects to a JSON string and escape inner quotes
+                  cellText = JSON.stringify(cell).replace(/"/g, '""');
+              } else {
+                  cellText = cell;
+              }
+              return `"${cellText}"`;
+          })
+          .join(',');
+      csvRows.push(csvRow);
+  });
+  return csvRows.join('\n');
+};
+
 const ViewCSVModal = ({ open, onClose, jobId, fetchCSV, compareData = null }) => {
   const [report, setReport] = useState({});
 
@@ -186,7 +208,6 @@ const ViewCSVModal = ({ open, onClose, jobId, fetchCSV, compareData = null }) =>
 
   } else {
     try {
-      console.log('compareData', compareData);
       setReport(formatEvalData(compareData, true));
     } catch (e) {
       setReport({ header: ['Error'], body: [[compareData]] });
@@ -198,6 +219,8 @@ const ViewCSVModal = ({ open, onClose, jobId, fetchCSV, compareData = null }) =>
 
 
   const handleDownload = async () => {
+
+    if (!compareData) {
     const response = await fetch(
       chatAPI.Endpoints.Experiment.GetAdditionalDetails(jobId, 'download')
     );
@@ -210,7 +233,20 @@ const ViewCSVModal = ({ open, onClose, jobId, fetchCSV, compareData = null }) =>
     link.click();
     document.body.removeChild(link);
     URL.revokeObjectURL(url);
-  };
+  } else {
+    const csvContent = convertReportToCSV(report);
+    const blob = new Blob([csvContent], { type: 'text/csv' });
+    const url = URL.createObjectURL(blob);
+    const link = document.createElement('a');
+    link.href = url;
+    link.download = `detailed_report.csv`; // Adjust extension if necessary
+    document.body.appendChild(link);
+    link.click();
+    document.body.removeChild(link);
+    URL.revokeObjectURL(url);
+  }
+
+};
 
   return (
     <Modal open={open} onClose={onClose}>
-- 
GitLab