-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
250 lines (171 loc) · 5.5 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
var express = require("express");
var app = express();
var async = require("async");
var db = require("mongojs")("mongodb://localhost/datamining", ["tuples", "classifier"]);
var util = require('util');
var hbs = require('hbs');
var logger = require("./lib/logger.js").Logger('app.js', false);
var defaultResponse = {
hostname: process.env.HOSTNAME || "0.0.0.0",
port : process.env.PORT || null
};
logger.info("Starting...");
//set view manager to Handlebar.js
app.set('view engine', 'hbs');
hbs.registerPartials(__dirname + '/views/partials');
hbs.registerHelper('json', function (data) {
return JSON.stringify(data);
});
//Login for each request.
app.use(require('morgan')('dev'));
//set up public directory to hold assets.
app.use('/public', require('serve-static')('public'));
//Expects attributes to be given in the query field of the request.
app.get("/", function (req, res) {
res.render('index', defaultResponse);
});
app.get("/index", function (req, res) {
res.render('index', defaultResponse);
});
app.get("/minedata", function (req, res) {
res.render('minedata', defaultResponse);
});
app.get("/student", function (req, res) {
res.render('student', defaultResponse);
});
app.get("/student_done", function (req, res) {
res.render('student_done', defaultResponse);
});
app.get("/instructor", function (req, res) {
res.render('instructor', defaultResponse);
});
app.get("/maintenance", function (req, res) {
res.render('maintenance', {});
});
app.route("/classifier/:id").post(function (req, res) {
//1. Check that this classifier doesn't exists....
// - Not currently implemented.
//2. Push the entry into the database.
logger.warn("POST: /classifier/" + req.params.id);
db.classifier.find(
{
"classifier": req.params.id
},
function (err, data) {
if (err) {
logger.error(err);
res.status(500);
return res.send("ERROR!");
}
if (data.length > 0) {
res.status(403);
return res.send("CLASSIFIER EXISTS");
}
db.classifier.insert(
{
"classifier": req.params.id,
"attributes": req.query.attr
},
function (err, data) {
if (err) {
logger.error(err);
res.status(500);
return res.send("ERROR");
}
res.status(200);
return res.send("OK");
}
);
}
);
});
app.get("/data/:id", function (req, res) {
logger.warn("GET: /data/" + req.params.id);
var result = [];
var flag = false;
setTimeout(function () {
if (!flag) {
res.status(408);
res.send("TIMEOUT!");
}
}, 3000);
async.each(
[req.params.id],
function (item, callback) {
db.tuples.find({"classifier": item}, function (err, data) {
if (data.length <= 0) {
//logger.info(util.inspect(data, false, null));
result.push([]);
return callback(204); //respond with no-data
} else {
result.push(data);
return callback(); //indicate we are done to the async library.
}
});
},
function (err) {
if (err) {
flag = true;
if (err === 204) {
res.status(204);
return res.send("NO DATA");
} else {
res.status(500);
return res.send("ERROR");
}
}
res.status(200);
flag = true;
return res.send(JSON.stringify(result));
}
);
});
/*
* Takes in a classifier_id (via url REST) and then a
* sequence of values via the "query" of the req.
*
* 1. Push the data in the into Mongo.tuples using the keys
* of the query as the keys into Mongo.
* 2. Craft response message.
*/
app.post("/data/:id", function (req, res) {
logger.warn("POST: /data/" + req.params.id);
var fromClient = "";
req.on('data', function (d) {
fromClient += d;
});
req.on('end', function () {
try {
fromClient = JSON.parse(fromClient);
async.each(
fromClient,
function (item, cb) {
db.tuples.insert(
{
"classifier": req.params.id,
"data" : item,
},
function (err, data) {
if (err) {
cb();
}
;
cb();
}
);
}, function (err) {
res.status(200);
return res.send();
}
);
} catch (e) {
res.status(400);
return res.send("COULD NOT PARSE INPUT");
}
});
});
var server = app.listen(process.env.PORT || 3000, '0.0.0.0', function () {
var host = server.address().address;
var port = server.address().port;
logger.info("App running at: http://" + host + ":" + port);
});