-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateCSV.js
More file actions
108 lines (87 loc) · 4.29 KB
/
generateCSV.js
File metadata and controls
108 lines (87 loc) · 4.29 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import fs from "fs/promises";
import path from "path";
const csvHeader = "problem_id,user_handler,rating,topics,attempts,solved,topicsSkill,creationTime\n";
const handlersPath = path.join("./data/handlers.json");
const progressPath = "./data/progress.json";
//const tempDataPath = "./data/tempData.json";
const csvPath = "./ml/dataset.csv";
try {
await fs.access(csvPath); // Check if file exists
} catch (error) {
await fs.writeFile(csvPath, csvHeader, "utf-8"); // Only write header if file doesn't exist
}
function formatCSV(problem) {
return `${problem.problem_id},${problem.user_handler},${problem.rating},"${problem.topics.join(";")}",${problem.attempts},${problem.solved},${problem.topicsSkill},${problem.creationTime}\n`;
}
// Read data files
const [handlers, progressFile,/* tempDataFile*/] = await Promise.all([
fs.readFile(handlersPath, "utf-8"),
fs.readFile(progressPath, "utf-8"),
// fs.readFile(tempDataPath, "utf-8"),
]);
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const handlersList = JSON.parse(handlers);
const progress = JSON.parse(progressFile);
//const tempData = JSON.parse(tempDataFile);
// Batch processing
const batchSize = 5; // Adjust this based on your needs
// Process handlers in batches
for (let i = 0; i < handlersList.length; i += batchSize) {
const batch = handlersList.slice(i, i + batchSize);
await Promise.all(
batch.map(async (placeHolder) => {
const problemAttempts = {};
console.log("Sending handler:", placeHolder);
progress.topicsSolved = {};
try {
const response = await fetch("http://localhost:3000/userDetails", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ handler: placeHolder }),
});
const problems = await response.json();
for (let j = 0; j < problems.length; j++) {
let currentProblem = problems[j];
let problemId = currentProblem.id;
let skillPoints = 0;
let topicsCount = 0;
if (!problemAttempts[problemId]) {
problemAttempts[problemId] = {
problem_id: problemId,
user_handler: placeHolder,
rating: currentProblem.rating,
topics: currentProblem.topics,
attempts: 0,
solved: false,
};
}
problemAttempts[problemId].attempts++; // Count each submission
if (currentProblem.solved) {
problemAttempts[problemId].solved = true; // Keep only final result
problemAttempts[problemId].topics.forEach(topic => {
skillPoints += (progress.topicsSolved[topic] || 0);
topicsCount++;
progress.topicsSolved[topic] = (progress.topicsSolved[topic] || 0) + 1;
});
}
// Compute average skill points for this problem
problemAttempts[problemId].topicsSkill = topicsCount > 0 ? skillPoints / topicsCount : 0;
problemAttempts[problemId].creationTime = currentProblem.creationTime;
if (!problemAttempts[problemId].rating) problemAttempts[problemId].rating = 0;
if (!problemAttempts[problemId].topics || problemAttempts[problemId].topics.length === 0)
problemAttempts[problemId].topics = ["none"];
}
const csvRows = Object.values(problemAttempts).map(formatCSV).join("");
await fs.appendFile(csvPath, csvRows, "utf-8"); // Append new rows
// Push the final processed data
//tempData.push(...Object.values(problemAttempts));
} catch (error) {
console.error("Error fetching data:", error);
}
})
);
//await fs.writeFile(tempDataPath, JSON.stringify(tempData, null, 2));
await delay(500); // ⏳ Wait before starting the next batch
}