-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
630 lines (482 loc) · 16.9 KB
/
app.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*jshint node:true*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
require('dotenv').load();
// This application uses express as it's web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
var https = require('https');
var JSON = require('JSON');
var geocoder = require('geocoder');
//var json2csv = require('json2csv');
// create a new express server
var app = express();
//using a mongoDB as the storage for the logger and session management
var mongoDBuri;
var initialMongoDBuri = function () {
var vcapServices;
if (process.env.VCAP_SERVICES)
vcapServices = JSON.parse(process.env.VCAP_SERVICES);
if (vcapServices && vcapServices.mongolab) {
mongoDBuri = vcapServices.mongolab[0].credentials.uri;
} else {
if (process.env.mongodb_uri)
mongoDBuri = process.env.mongodb_uri;
}
}
initialMongoDBuri();
var log4js = require('log4js');
var mongoAppender = require('log4js-node-mongodb');
var categories = ['index', 'signin', 'survey', 'represent', 'application'];
categories.forEach(function (category) {
log4js.addAppender(
mongoAppender.appender({
connectionString: mongoDBuri
}),
category);
});
app.locals.log4js = log4js;
var logger = log4js.getLogger('application');
//for the memory watch;
var memwatch = require('memwatch-next');
memwatch.on('leak', function (info) {
logger.error("leak:" + JSON.stringify(info));
});
memwatch.on('stats', function (stats) {
logger.info("stats:" + JSON.stringify(stats));
});
var async = require('async');
// routing
require('./routes/represent')(app);
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
var bodyParser = require('body-parser');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
app.locals.url = appEnv.url;
// start server on the specified port and binding host
app.listen(appEnv.port, appEnv.bind, function () {
// print a message when the server starts listening
logger.info('server starting on ' + appEnv.url);
});
//for session management
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret: "thisisasecret",
saveUninitialized: false,
resave: false,
store: new MongoStore({
url: mongoDBuri
}),
ttl: 5 * 24 * 60 * 60 //5 days expiration
}));
//set up logging for the request
var mongoMorgan = require('mongo-morgan');
app.use(mongoMorgan(mongoDBuri, 'combined', {
immediate: true,
collection: 'morganlogs'
}));
//DataCache
var datacache = require('./bluemix_datacache.js');
app.locals.datacache = datacache;
//using a different twitter library
var Twitter = require('twitter');
var twitterConfig = {
consumer_key: process.env.twitter_consumer_key,
consumer_secret: process.env.twitter_consumer_secret,
access_token_key: process.env.twitter_access_token_key,
access_token_secret: process.env.twitter_access_token_secret
};
app.locals.twitterConfig = twitterConfig;
var twitterHdl = new Twitter(twitterConfig);
//setting the timer for checking the memory usage with a period of 1 hour
//
var memStateTypes = {
init: {
key: "INIT",
range: [0]
},
normal: {
key: "NORMAL",
range: [0, 750]
},
warning: {
key: "WARNING",
range: [750, 950]
},
alarm1: {
key: "ALARM-1",
range: [950, 1050]
},
alarm2: {
key: "ALARM-2",
range: [1050, 1300]
}
};
//testing
/*
var memStateTypes={
init:{
key: "INIT",
range: [0]
},
normal:{
key: "NORMAL",
range: [0,140]
},
warning: {
key : "WARNING",
range: [140, 180]
},
alarm1:{
key : "ALARM-1",
range: [180, 220]
},
alarm2:{
key : "ALARM-2",
range: [220, 400]
}
}
*/
var memMonitor = {
state: memStateTypes.init,
messages: 0,
recordedusage: 0,
increasedtimes: 0
};
var updateMonitorState = function (previous, current, frequency) {
var message = null;
//progress the status first
//either a normal case or the case where the state has jumped
if (memMonitor.state === previous || memMonitor.state != current) {
message = current.key + ": the state of the monitor has been changed from " + memMonitor.state.key
memMonitor.state = current;
memMonitor.messages = frequency;
}
//send a message if the memory usage keeps increasing in the last 10 periods
if (memMonitor.messages == 0) {
message = memMonitor.state.key + ": memory has increaed " + memMonitor.increasedtimes + " times since the latest " + memMonitor.state.key + " message"
memMonitor.messages = frequency;
memMonitor.increasedtimes = 0;
}
if (message) {
message += ". The memory usage is around " + Math.ceil(memMonitor.recordedusage / 1000) / 1000 + "MB" +
" at " + appEnv.url;
twitterHdl.post('statuses/update', {
status: message
}, function (error, body, response) {
if (error)
logger.error("tweet message error + " + JSON.stringify(body));
else
logger.info("tweet message success " + JSON.stringify(body));
});
}
memMonitor.messages--;
}
if (memMonitor.state === memStateTypes.init) {
var initmem = process.memoryUsage();
memMonitor.recordedusage = initmem.rss;
updateMonitorState(memStateTypes.init, memStateTypes.normal, 1);
}
setInterval(function () {
var mem = process.memoryUsage();
//no need to handle the situation in the case
//that the memory usage is lower than 700MB
if (mem.rss <= memStateTypes.normal.range[1]) {
//test sending
return;
}
if (mem.rss > memMonitor.recordedusage) {
memMonitor.increasedtimes++;
} else
memMonitor.imcreasedtimes = 0;
memMonitor.recordedusage = mem.rss;
var memInt = Math.ceil(memMonitor.recordedusage / 1000 / 1000);
//if the memory usage is in the range of warning
//send a warning message through twitter @votesavvyrhok
if (memInt > memStateTypes.warning.range[0] &&
memInt <= memStateTypes.warning.range[1]) {
updateMonitorState(memStateTypes.normal, memStateTypes.warning, 10);
return;
}
//if the memory usage is in the range of alarm1
//send a alarm1 message through twitter @votesavvyrhok
if (memInt > memStateTypes.alarm1.range[0] &&
memInt <= memStateTypes.alarm1.range[1]) {
updateMonitorState(memStateTypes.warning, memStateTypes.alarm1, 5);
return;
}
//if the memory usage is in the range of alarm2
//send a alarm1 message through twitter @votesavvyrhok
if (memInt > memStateTypes.alarm2.range[0] &&
memInt <= memStateTypes.alarm2.range[1]) {
updateMonitorState(memStateTypes.alarm1, memStateTypes.alarm2, 2);
return;
}
}, 60 * 60 * 1000);
var dbCredentials = {
dbs: {
survey: {
name: "answers",
handler: null,
indexes: {}
},
signintwitters: {
name: "twitterusers",
handler: null
}
}
};
var cloudant;
function useDatabase(next) {
async.forEach(Object.keys(dbCredentials.dbs), function (db, callback) {
cloudant.db.create(dbCredentials.dbs[db].name, function (err, res) {
if (err) {
logger.warn('database ' + dbCredentials.dbs[db].name + ' already created');
} else {
logger.info('database ' + dbCredentials.dbs[db].name + ' is created');
}
dbCredentials.dbs[db].handler = cloudant.use(dbCredentials.dbs[db].name);
callback();
});
}, function (err) {
//create the index on answers db here,
//the index is upon the field of user_token;
var answersdb = dbCredentials.dbs.survey.handler;
var user = {
name: 'user',
type: 'json',
index: {
fields: [
{
"token": "desc"
},
{
"recordedat": "desc"
}
]
}
};
answersdb.index(user, function (err, response) {
if (err)
logger.warn("create index error" + JSON.stringify(err));
else
dbCredentials.dbs.survey.indexes.user = response;
logger.info('Index creation result: ', JSON.stringify(response));
});
next();
cloudant.db.create('latlong', function (err, res) {
if (err) {
logger.warn('latlong database already created');
} else {
logger.info('latlong database is created');
}
});
cloudant.db.list(function (err, all_dbs) {
logger.info('All my databases: %s', all_dbs);
});
// quickAnalysis(cloudant);
});
}
function quickAnalysis(feedback) {
logger.info('Quick Analysis ...');
var answers = cloudant.db.use('answers');
var selected = {};
var postcodes = [];
answers.list(function (err, body) {
if (err) {
logger.error(err);
} else {
var rows = body.rows;
var count = rows.length;
var combined = [];
var postcodecount = 0;
var gender = [];
logger.info("Number of responses: " + count);
rows.forEach(function (answer) {
answers.get(answer.id, {
revs_info: true
}, function (err, doc) {
if (doc && doc.formdata) {
var short = {};
// combined.push(doc.formdata);
short.gender = doc.formdata.personal.gender;
short.yob = doc.formdata.personal.yearOfBirth;
short.postcode = doc.formdata.personal.postalCode;
short.selected = doc.formdata.issues.selected;
short.timestamp = doc.formatedtime
logger.info(short);
combined.push(short);
if (!gender[doc.formdata.personal.gender]) {
gender[short.gender = doc.formdata.personal.gender] = 1;
} else {
gender[short.gender = doc.formdata.personal.gender] ++;
}
if (!selected[doc.formdata.issues.selected]) {
selected[doc.formdata.issues.selected] = 1;
} else {
selected[doc.formdata.issues.selected] ++;
}
if (doc.formdata.personal.postalCode != '') {
postcodes.push(doc.formdata.personal.postalCode);
postcodecount++;
}
} else {
logger.warn('Found an undefined answer document');
}
count--;
if (count === 0) {
var analysis = {
"count": rows.length,
"list": combined,
"postcodes": postcodes,
"postcodecount": postcodecount,
"selected": selected,
"gender": gender
}
console.log(analysis);
feedback(analysis);
}
});
});
}
});
}
function initializeDatabase(callback) {
if (process.env.VCAP_SERVICES) {
var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
if (vcapServices.cloudantNoSQLDB) {
var credentials = vcapServices.cloudantNoSQLDB[0].credentials;
dbCredentials.host = credentials.host;
dbCredentials.port = credentials.port;
dbCredentials.user = credentials.username;
dbCredentials.password = credentials.password;
dbCredentials.url = credentials.url;
}
cloudant = require('cloudant')(dbCredentials.url);
useDatabase(callback);
} else {
if (process.env.cloudant_hostname && process.env.cloudant_username && process.env.cloudant_password) {
dbCredentials.host = process.env.cloudant_hostname;
dbCredentials.user = process.env.cloudant_username;
dbCredentials.password = process.env.cloudant_password;
cloudant = require('cloudant')({
hostname: dbCredentials.host,
account: dbCredentials.user,
password: dbCredentials.password
});
useDatabase(callback);
}
}
}
function apiMapping() {
var apis = {
survey: {
name: "surveymanager.js",
db: dbCredentials.dbs.survey
},
signintwitters: {
name: "signintwitters.js",
db: dbCredentials.dbs.signintwitters
}
};
for (var api in apis) {
require('./routes/' + apis[api].name)(app, apis[api].db);
}
installErrorMiddleware();
}
var installErrorMiddleware = function () {
app.use(function (err, req, res, next) {
if (err.status == 403)
res.render('403.html', {
message: err.message
});
else
next(err);
});
}
require('./routes/index')(app);
initializeDatabase(apiMapping);
var latlong;
function unique(value, index, self) {
return self.indexOf(value) === index;
}
app.get('/analysis', function (request, response) {
response.setHeader('Content-Type', 'application/json');
quickAnalysis(function (data) {
response.end(JSON.stringify(data));
});
});
app.get('/geo', function (request, response) {
logger.info('Building geo data ...');
response.setHeader('Content-Type', 'application/json');
var answers = cloudant.db.use('answers');
var selected = [];
var postcodes = [];
answers.list(function (err, body) {
if (err) {
logger.error(err);
} else {
var rows = body.rows;
var count = rows.length;
var combined = [];
var postcodecount = 0;
var latlong = [];
logger.info("Number of responses: " + count);
rows.forEach(function (answer) {
answers.get(answer.id, {
revs_info: true
}, function (err, doc) {
if (doc.formdata) {
if (doc.formdata.personal.postalCode != '') {
postcodes.push(doc.formdata.personal.postalCode);
postcodecount++;
}
} else {
logger.warn('Found an undefined answer document');
}
count--;
if (count === 0) {
var uniquecodes = postcodes.filter(unique);
var uniquecount = uniquecodes.length;
logger.info('Unique postcodes: ' + uniquecodes.length);
uniquecodes.forEach(function (code) {
geocoder.geocode(code, function (err, data) {
if (data && data.status == 'OK' && data.results[0].geometry.bounds) {
latlong.push(data.results[0].geometry.bounds.northeast);
}
uniquecount--;
if (uniquecount === 0) {
console.log(latlong);
response.end(JSON.stringify(latlong));
}
});
});
}
});
});
}
});
});
var retrievingAdmin = function () {
if (process.env.admin) {
return process.env.admin.split(",");
}
return null;
}
app.locals.admin = retrievingAdmin();
logger.info('votesavvy application running');