-
Notifications
You must be signed in to change notification settings - Fork 0
/
an.js
347 lines (328 loc) · 9.24 KB
/
an.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
var fs = require('fs'),
NodePromise = require('node-promise'),
Promise = NodePromise.Promise;
var usersToSkip = [
'a@f.com',
'aaa@aaa.com',
'allie@brandingpays.com',
'allie@readz.com',
'allieferr@gmail.com',
'allieferraiolo@hotmail.com',
'apeller@us.ibm.com',
'b@b.com',
'bee@bee.com',
'bogus1@bogus1.com',
'brandingpays@gmail.com',
'carol@thepassionateolive.com',
'carolanglin@comcast.net',
'clairechang3@gmail.com',
'd@d.com',
'etaverner@yahoo.com',
'example@example.com',
'example1@example.com',
'example100@example.com',
'example200@example.com',
'frog@frog.com',
'genentech@example.com',
'joby@qburst.com',
'jon@brandingpays.com',
'jon@ferraiolo.com',
'jonferraiolo@gmail.com',
'karen@kang.com',
'karen.kang@brandingpays.com',
'karen.kang@me.com',
'leaf@leaf.com',
'login@login.com',
'meta@crawfordgroup.com',
'meta@metamehling.com',
'metamehlingassoc@gmail.com',
'pinky@brandingpays.com',
'q@q.com',
'r@s.com',
'reshiva@yahoo.com',
'star@star.com',
'steven.kang@brandingpays.com',
'sun@sun.com',
'test@abc.com',
'test@brandingpays.com',
'test@atest.com',
'test@test.com',
'tester@bp.com',
'test10@test.com',
'test123@test.com',
'test@test.com',
'test2@test.com',
'test200@test.com',
'test3@test.com',
'test4@test.com',
'test5@test.com',
'testengineer80@gmail.com',
'testTM@test.com',
'testTM2@test.com',
'testTM3@test.com',
'testuser1@haas.com',
'tree@tree.com',
'userhaas@bp.com',
'x@y.com',
'z@z.com'
];
var defaultUsersusersFolderPath = '/Applications/XAMPP/xamppfiles/users',
usersFolderPath = defaultUsersusersFolderPath,
defaultUsersFileName = 'users.txt',
usersFileName = defaultUsersFileName,
logBadUsers = false,
users = [];
var printCommandLineOptions = function() {
console.log();
console.log('--help');
console.log('--usersfolder=<path-to-users-folder> (default: '+defaultUsersusersFolderPath+')');
console.log('--usersfilename=<users-index-file> (default: '+defaultUsersFileName+')');
console.log('--logbadusers (default: do not list bad user accounts');
};
// Get command line parameters
process.argv.forEach(function (val, index, array) {
if (val.substr(0,2) == '--') {
var tokens = val.split('=');
if (tokens[0] == '--help') {
printCommandLineOptions();
process.exit(1);
} else if (tokens[0] == '--usersfolder') {
usersFolderPath = tokens[1];
} else if (tokens[0] == '--usersfilename') {
usersFileName = tokens[1];
} else if (tokens[0] == '--logbadusers') {
logBadUsers = true;
} else {
printCommandLineOptions();
process.exit(1);
}
}
});
var usersTxtFilePath = usersFolderPath + '/' + usersFileName;
var isEmptyObject = function(obj) {
for(var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true;
};
// Prevent running out of file descriptors
var maxFileOps = 100;
var activeFileOps = 0;
var readDirPending = [];
var readFilePending = [];
var readDirWrapper = function(pathToFolder, callback) {
if (activeFileOps >= maxFileOps) {
readDirPending.push({ pathToFolder:pathToFolder, callback:callback });
} else {
var doReadDir = function(pathToFolder, callback) {
activeFileOps++;
fs.readdir(pathToFolder, function(err, data) {
callback(err, data);
activeFileOps--;
if (readDirPending.length > 0) {
var pending = readDirPending.shift();
doReadDir(pending.pathToFolder, pending.callback);
}
});
};
doReadDir(pathToFolder, callback);
}
};
var readFileWrapper = function(pathToFile, callback) {
if (activeFileOps >= maxFileOps) {
readFilePending.push({ pathToFile:pathToFile, callback:callback });
} else {
var doReadFile = function(pathToFile, callback) {
activeFileOps++;
fs.readFile(pathToFile, function(err, data) {
callback(err, data);
activeFileOps--;
if (readFilePending.length > 0) {
var pending = readFilePending.shift();
doReadFile(pending.pathToFile, pending.callback);
}
});
};
doReadFile(pathToFile, callback);
}
};
/**
* Read the user's latest data file
* @param {string} userFolderPath Path to user's data folder
* @param {function} callback
* {null|undefined|string} Error string if any errors found
* {string} Contents of latest data file
*/
var readLatestData = function(userFolderPath, callback) {
var latestPath = userFolderPath + '/latest';
readFileWrapper(latestPath, function(err, latestContents) {
if (err) {
callback('Cannot read latestPath='+latestPath+', err='+err);
} else {
var dataPath = userFolderPath + '/' + latestContents.toString().trim();
readFileWrapper(dataPath, function(err, dataContents) {
if (err) {
callback('Cannot read dataPath='+dataPath);
} else {
callback(null, dataContents.toString());
}
});
}
});
};
/**
* Verify that the given user has a proper folder
*/
var getUserData = function(email, callback) {
var userFolderPath = usersFolderPath + '/' + email;
readDirWrapper(userFolderPath, function(err, files) {
if (err) {
callback('Cannot read folder for email: '+email);
} else {
if (files.indexOf('latest') < 0) {
callback('No latest file for email: '+email);
} else {
readLatestData(userFolderPath, function(err, dataContents) {
callback(err, dataContents);
});
}
}
});
};
/**
* Find all users.
*
* @param {function} callback Callback function, with params:
* {string|null|undefined} error (null means no error)
* {array[string]} users Array of user names (email addresses)
*/
var findUsers = function(callback) {
readFileWrapper(usersTxtFilePath, function(err, usersTxtContents) {
if (err) {
callback(err);
}
// replace CRLF and CR with LF
var usersTxt = usersTxtContents.toString().replace(/(?:\r\n|\r)/g, '\n');
var userLines = usersTxt.split('\n');
var userCount = 0;
userLines.forEach(function(userLine) {
var tokens = userLine.split(':');
var email = tokens[0];
if (email.indexOf('@') >= 0 && usersToSkip.indexOf(email) < 0) {
// Verify that this user has a proper folder
getUserData(email, function(err, userData) {
if (err) {
if (logBadUsers) {
console.log(err);
}
} else {
users.push({email:email, data:userData});
}
userCount++;
if (userCount >= userLines.length) {
callback(null, users);
}
});
} else {
userCount++;
}
});
});
};
findUsers(function(err, users) {
if (err) {
console.error(err);
} else {
var fields = {};
var emails = {};
users.forEach(function(user) {
var dataNormalized = user.data.replace(/(?:\r\n|\r)/g, '\n');
var lines = dataNormalized.split('\n');
var U;
for(var i=0;i<lines.length;i++){
var line=lines[i];
if(line.indexOf('{')>=0 && line.indexOf('}')>=0){
var obj = null;
var unescaped_line = unescape(line);
try {
obj = JSON.parse(unescaped_line);
} catch (e) {
console.log('JSON.parse failed for email: '+user.email+', line:'+line);
}
data_version = obj._version;
if(!obj._version){
console.log("Error: no version number from server data.");
}
// If the current line is a full update, then reset U from the data in this line
if(obj._updateType==="full"){
U = obj;
// Else if the current line is a single-field incremental update,
// then just update that one field in U
}else if(obj._updateType==="incremental"){
if(obj.field){
// Ensure that obj.field actually exists.
// Necessary to make sure U.UI.NavTab doesn't
// reference through null object
var tokens=obj.field.split('.');
var grandParentObj=null;
var parentObj={U:U};
for(var j=0;j<tokens.length;j++){
var propname=tokens[j];
if(!parentObj[propname]){
parentObj[propname]={};
}
grandParentObj=parentObj;
parentObj=parentObj[propname];
}
if(typeof obj.value !== "undefined" && obj.value !== null){
grandParentObj[propname]=obj.value;
}
}else{
console.log("Error: missing field property for user: '+user.email+' on line:"+line);
}
}else{
console.log("Error: incorrect value for obj._updateType:"+obj._updateType+' for user: '+user.email+' on line:'+line);
}
}else{
console.log("Error: no curly braces for user: '+user.email+' on line:"+line);
}
} //for loop ends here
var recurseU = function(obj, baseString) {
if ((typeof obj == 'object' && isEmptyObject(obj)) || (typeof obj == 'string' && obj.length == 0)) {
return;
} else if (typeof obj == 'string') {
emails[user.email] = true;
if (!fields[baseString]) {
fields[baseString] = { count:0, totalChars:0 };
}
fields[baseString].count++;
fields[baseString].totalChars += obj.length;
} else if (typeof obj == 'object') {
for(var prop in obj) {
if (prop[0] != '_') {
recurseU(obj[prop], baseString+'.'+prop);
}
}
}
};
recurseU(U, 'U');
});
console.log('total #users='+users.length);
var usersWithDataCount = 0;
for (var i in emails) {
usersWithDataCount++;
}
console.log('total #users with data='+usersWithDataCount);
/*
console.log('users without data:');
users.forEach(function(user) {
if (!emails[user.email]) {
console.log(user.email);
}
});
*/
console.dir(fields);
}
});