-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (64 loc) · 1.83 KB
/
index.js
File metadata and controls
77 lines (64 loc) · 1.83 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
const fs = require("fs");
const JSONStream = require("JSONStream");
const es = require("event-stream");
const jsonProcessor = require("./objectProcessor");
const userJsonPath = "./users.json";
const votesJsonPath = "./votes.json";
function processJson(filePath, jsonMap, processorFunction) {
return new Promise((resolve, reject) => {
var inputStream = fs.createReadStream(filePath, {
flags: "r",
encoding: "utf-8",
});
inputStream
.pipe(JSONStream.parse("*"))
.pipe(
es.through(function (data) {
this.pause();
processorFunction(data, jsonMap, this);
return data;
})
)
.on("end", () => {
console.log("stream reading ended");
resolve();
});
});
}
function printOutput(query_type, usersDataObj, userVotesCount) {
const locations = [...new Set(usersDataObj.get(query_type))];
let ans = [];
locations.forEach((location) => {
const userList = usersDataObj.get(location);
const voteSentiment = userList.reduce((acc, curr) => {
return acc + userVotesCount.get(curr);
}, 0);
ans.push({
Segment: location,
"Sentiment Score": voteSentiment,
"Participation Percentage":
(userList.length / usersDataObj.get("totalUsers")) * 100,
});
});
console.table(ans);
}
async function main(query) {
const usersDataObj = new Map();
const userVotesCount = new Map();
usersDataObj.set("totalUsers", 0);
const promise1 = processJson(
userJsonPath,
usersDataObj,
jsonProcessor.processOneUser
);
const promise2 = processJson(
votesJsonPath,
userVotesCount,
jsonProcessor.processOneVote
);
await Promise.all([promise1, promise2]);
printOutput(query, usersDataObj, userVotesCount);
}
const query_type = process.argv.slice(2)[0];
// Main Function
main(query_type);