-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata.js
78 lines (72 loc) · 2.1 KB
/
data.js
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
const csv = require("csv-parser");
const fs = require("fs");
const results = [];
const resultsWithRank = [];
const quests = "# of Quests Completed",
skillBadges = "# of Skill Badges Completed";
fs.createReadStream("data/data.csv")
.pipe(csv())
.on("data", (data) => results.push(data))
.on("end", () => {
let id = 0;
let institute = results[0]["Institution"];
results.forEach((result) => {
delete result["Enrolment Date & Time"];
delete result["Google Cloud Skills Boost Profile URL"];
delete result["Institution"];
delete result["Student Email"];
result.id = id++;
});
results.sort(
(a, b) =>
Number(b[quests]) +
Number(b[skillBadges]) -
(Number(a[quests]) + Number(a[skillBadges])) ||
a["Student Name"] - b["Student Name"]
);
let rank = 1;
results[0]["Rank"] = rank;
for (let pointer = 1; pointer < results.length; pointer++) {
let totalNumberOfQuestsCompletedByPreviousRank =
Number(results[pointer - 1][quests]) +
Number(results[pointer - 1][skillBadges]);
let totalNumberOfQuestsCompleted =
Number(results[pointer][quests]) + Number(results[pointer][skillBadges]);
if (totalNumberOfQuestsCompleted === 0) {
rank++;
results[pointer]["Rank"] = rank;
}
else if (
totalNumberOfQuestsCompletedByPreviousRank ===
totalNumberOfQuestsCompleted
) {
results[pointer]["Rank"] = results[pointer - 1]["Rank"];
} else {
rank++;
results[pointer]["Rank"] = rank;
}
}
results.forEach((result) => {
let obj = {
Rank: result["Rank"],
"Student Name": result["Student Name"],
"Enrolment Status": result["Enrolment Status"],
"# of Quests Completed": result[quests],
"# of Skill Badges Completed": result[skillBadges],
id: result["id"],
};
resultsWithRank.push(obj);
});
fs.writeFile(
"data/data.json",
JSON.stringify({
resultsWithRank,
buildDate: new Date(Date.now()).toDateString(),
institute,
}),
(err) => {
if (err) throw err;
console.log("Data file has been saved!");
}
);
});