-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetTeams.js
158 lines (128 loc) · 3.89 KB
/
getTeams.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require('coffee-script');
require('coffee-script/register');
var fs = require('fs');
var csv = require('fast-csv');
var User = require('./entities/users/model');
var idColumn = "email";
//Total list of user ids
//Must match the form they are on the application
var totalList = [];
//The team being processed
var currentTeam = [];
//Array of team arrays
var totalTeams = [];
//People who haven't applied yet
var nonApplicants = [];
var sumTeamSize = 0.0;
//Add all not-deleted users to master list
User
.find({deleted: { $ne: true }})
.select(idColumn)
.exec(function(err, users) {
//Add all of the user ids to the list
users.forEach(function(user) {
totalList.push(user[idColumn]);
});
function writeToCSV() {
console.log("Writing to file");
var csvStream = csv
.createWriteStream({headers: true});
var writableStream = fs.createWriteStream("teams.csv");
writableStream.on("finish", function() {
console.log("Finished writing to file");
});
csvStream.pipe(writableStream);
//Put each of the teams into objects with headers
for(var teamIndex = 0; teamIndex < totalTeams.length; teamIndex++) {
var team = totalTeams[teamIndex];
var teamObject = {};
for(var i = 0; i < team.length; i++) {
teamObject["Teammember " + i] = team[i];
}
//Write each team object to the CSV
csvStream.write(teamObject);
}
csvStream.end();
}
function writeNonApplicantsCSV() {
console.log("Writing non-applicants");
var csvStream = csv
.createWriteStream({headers: true});
var writableStream = fs.createWriteStream("non-applicants.csv");
writableStream.on("finish", function() {
console.log("Finished writing non-applicants");
});
csvStream.pipe(writableStream);
for(var applicantIndex = 0; applicantIndex < nonApplicants.length; applicantIndex++) {
csvStream.write({email: nonApplicants[applicantIndex]});
}
csvStream.end();
}
//Completed all users
function finishedUsers() {
//Show statistics
console.log("Total Teams", totalTeams.length);
console.log("Average Team Size", sumTeamSize / totalTeams.length);
console.log("Total Non-Applicants", nonApplicants.length);
writeToCSV();
writeNonApplicantsCSV();
}
//Get the next user in the list
function nextUser() {
if(totalList.length === 0) {
return finishedUsers();
}
//Rewrite line
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write("Remaining Users: " + totalList.length);
//Process this user's team
processUser(totalList[0], [], function(newTeam) {
//Add the new team to the list
totalTeams.push(newTeam);
//Add to the averages
sumTeamSize += newTeam.length;
nextUser();
});
};
//Start the chain off
nextUser();
});
function processUser(id, team, callback) {
//The index of the user in the master list
var masterIndex = totalList.indexOf(id);
//Make a query for the user
var search = {};
search[idColumn] = id;
//User isn't in the database
if(masterIndex === -1) {
//If they don't exist, add their ID to the list of non-applicants
return User.findOne(search, function(err, user) {
if(err || user === null) {
nonApplicants.push(id);
}
callback(team);
});
}
team.push(id);
totalList.splice(masterIndex, 1);
//Get the information about the current user
User.findOne(search, function(err, user) {
//If that user doesn't exist
if(err || user === null) {
nonApplicants.push(id);
callback(team);
}
function recurseTeammate() {
if(user.teammates === undefined || user.teammates.length === 0) {
return callback(team);
}
//Remove the first roommate
processUser(user.teammates[0], team, function() {
user.teammates.splice(0, 1);
recurseTeammate();
});
}
recurseTeammate();
});
}