-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
167 lines (139 loc) · 4.35 KB
/
app.js
File metadata and controls
167 lines (139 loc) · 4.35 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
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
#! /usr/bin/env node
var path = require('path');
var fs = require('fs');
var http = require('http');
var Datastore = require('nedb'); // database
var mustache = require('mustache'); // template engine
var db = new Datastore({ filename: path.join(__dirname, 'vot.db'), autoload: true });
db.ensureIndex({ fieldName: 'full_name', unique: true }, function (err) {});
var file = path.join(__dirname, 'vot.dat');
var view = {};
var output;
function populate_db(callback) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return console.error(err);
}
var arr = data.split('\n');
var tasksToGo = arr.length;
arr.forEach(function (line) {
var date_raw = line.substring(8,14)
var date = Date.parse(date_raw);
var full_name = line.substring(15,50).trim();
var tmp_name_arr = full_name.split(" ");
var last_name = tmp_name_arr.splice(0, 1)[0];
var first_name = tmp_name_arr.join(" ");
var vote = line.substring(63,64);
// full_name included for unique constraint
var doc = { full_name: full_name,
first_name: first_name,
last_name: last_name,
vote: vote,
date: date };
db.insert(doc);
if (--tasksToGo === 0) {
callback();
}
});
});
}
function query_db(callback) {
// equivalent to sql: select vote from db
db.find({}, {vote: 1, _id: 0}, function (err, docs) {
var votes = {}
// count votes
var key;
for (var i = 0; i < docs.length; ++i) {
key = docs[i].vote;
if (key in votes) {
votes[key].count += 1;
}
else {
// initialize counter
votes[key] = {};
votes[key].vote = key;
votes[key].count = 1;
votes[key].last_voters = [];
}
}
// last voters
var tasksToGo = Object.keys(votes).length;
for (key in votes) {
// category last vote's date
db.findOne({vote: key}).sort({date: -1}).exec( function (err, doc) {
var last_date = doc.date;
var vote = doc.vote;
votes[vote].last_voting_date = last_date;
// voters on last day (might be more then one)
db.find({vote: vote, date: last_date}, function (err, docs) {
var vote;
var first_name;
for (var i = 0; i < docs.length; ++i) {
vote = docs[i].vote;
first_name = docs[i].first_name;
votes[vote].last_voters.push(first_name);
}
if (--tasksToGo === 0) {
// console.log(votes);
// { '1':
// { vote: '1',
// count: 11,
// last_voters: [ 'Daniel', 'Andrei', 'Bogdan Mihail' ],
// last_voting_date: 989528400000 },
// '2':
// { vote: '2',
// count: 21,
// last_voters: [ 'Daniela' ],
// last_voting_date: 989528400000 },
// '3':
// { vote: '3',
// count: 4,
// last_voters: [ 'Ionut', 'Simona' ],
// last_voting_date: 989442000000 },
// '4':
// { vote: '4',
// count: 7,
// last_voters: [ 'Eduard', 'Vlad Cristian' ],
// last_voting_date: 989528400000 },
// '#':
// { vote: '#',
// count: 5,
// last_voters: [ 'Florin' ],
// last_voting_date: 989614800000 } }
callback(votes);
}
})
});
}
});
}
function render_template(data) {
// populate view
view["votes"] = []
for (key in data) {
view["votes"].push(data[key]);
}
// scale function to exaggerate graph dimensions
view["scale"] = function() {return this * 30;};
// read template file
fs.readFile(path.join(__dirname, 'template.mustache.html'), 'utf8', function (err, template) {
if (err) {
return console.error(err);
}
// render template
output = mustache.render(template, view)
});
}
// main
populate_db( function () {
query_db(render_template)
});
// http server
var requestListener = function (req, res) {
res.writeHead(200);
res.end(output);
}
var server = http.createServer(requestListener);
server.listen(3000, function () {
console.log('Server listening on http://localhost:3000')
});