File size: 1,893 Bytes
b09ec4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { resolve } from 'path';
import { readdirSync, readFileSync } from 'fs';
import ExcelJS from 'exceljs';
import { format } from 'date-fns';

async function combineJSONToExcel() {
  const logsDirectory = resolve("/logs");
  const jsonFiles = readdirSync(logsDirectory).filter((file) => file.endsWith(".json"));
  if (jsonFiles.length === 0) {
    console.log("No JSON files found.");
    return;
  }
  const workbook = new ExcelJS.Workbook();
  const worksheet = workbook.addWorksheet("Combined Data");
  worksheet.addRow(["Prompt", "Template", "Search Results", "LLM Response", "User Score", "User Comment", "Timestamp"]);
  for (const jsonFile of jsonFiles) {
    const filePath = resolve(logsDirectory, jsonFile);
    const jsonContent = readFileSync(filePath, "utf-8");
    const jsonData = JSON.parse(jsonContent);
    worksheet.addRow([
      jsonData.prompt,
      jsonData.template,
      jsonData.search_results,
      jsonData.llm_response,
      jsonData.user_score,
      jsonData.user_comment,
      jsonFile.replace(".json", "")
    ]);
  }
  const buffer = await workbook.xlsx.writeBuffer();
  return buffer;
}
const GET = async ({ locals, request }) => {
  try {
    const excelBuffer = await combineJSONToExcel();
    const currentDate = /* @__PURE__ */ new Date();
    const formattedDate = format(currentDate, "yyyy-MM-dd");
    const filename = `user-responses-${formattedDate}.xlsx`;
    return new Response(excelBuffer, {
      headers: {
        "Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "Content-Disposition": "attachment; filename=" + filename
      }
    });
  } catch (e) {
    return new Response(
      JSON.stringify({ success: false, error: e.message }),
      {
        headers: { "Content-Type": "application/json" }
      }
    );
  }
};

export { GET };
//# sourceMappingURL=_server.ts-MZHTV5BU.js.map