-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
548 lines (477 loc) · 20.7 KB
/
server.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
var express = require('express'),
http = require('http'),
path = require('path'),
url = require('url'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
CollectionDriver = require('./collectionDriverGaia').CollectionDriver;
var natural = require('natural'),
tokenizer = new natural.TreebankWordTokenizer();
natural.PorterStemmer.attach();
var mongoHost = 'localHost'; // Mongo host by default
var mongoPort = 27017; // Mongo port by default
var PORT = 3000; // port of the server
var collectionDriver;
var app = express();
/********** CONFIG **************************/
app.set('port', PORT);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// after the app.set lines, but before any app.use or app.get lines:
// express now parses the incoming body data
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req,res,next){
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
var mongoClient = new MongoClient(new Server(mongoHost, mongoPort));
mongoClient.open(function(err, mongoClient) {
if (!mongoClient) {
console.error("Error! Exiting... Must start MongoDB first");
process.exit(1); //D
}
var db = mongoClient.db("MyDatabase");
collectionDriver = new CollectionDriver(db);
});
app.use(express.static(path.join(__dirname, 'public')));
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
/************** End of config *******************/
function checkGaiaDB(collectionName) {
//return (collectionName == 'gaiadb');
return true; // accept all collection names
}
/*************************** GET ***************************/
// Adds the index to collection
// Call this after there's a few element
app.get('/:collection/add2dsphereIndex', function(req, res) {
var collection = req.params.collection;
if (!checkGaiaDB(collection)) {
res.send(400, {error: 'no such collection'});
}
collectionDriver.addLocation2dsphereIndex(collection, function(error, objs) {
if (error) {
res.send(400, error);
} else {
console.log(objs);
res.send(200, objs);
}});
});
// Get the entire collection (Table OR json)
app.get('/:collection', function(req, res) {
var params = req.params;
var collection = req.params.collection;
var url_parts = url.parse(req.url, true);
var params = url_parts.query;
if (!checkGaiaDB(collection)) {
res.send(400, {error: 'no such collection'});
}
collectionDriver.findAll(req.params.collection, function(error, objs) {
if (error) {
res.send(400, error);
} else {
if (params.format == 'json') {
res.send(200, objs);
} else if (params.format == 'table') {
if (req.accepts('html')) {
res.render('data',{objects: objs, collection: req.params.collection});
} else {
res.set('Content-Type','application/json');
res.send(200, objs);
}
}
}
});
});
// Get specific item. This returns everything
app.get('/:collection/getitem', function(req, res) {
var params = req.params;
var collection = params.collection;
var url_parts = url.parse(req.url, true);
var entityid = url_parts.query.id;
console.log('Get item with id: ' + entityid);
if (!checkGaiaDB(collection)) {
res.send(400, {error: 'no such collection'});
}
if (entityid) {
collectionDriver.get(collection, entityid, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
res.send(400, {error: 'bad url', url: req.url});
}
});
var filterFloat = function (value) {
if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
.test(value))
return Number(value);
return NaN;
}
// gets a subset of the collection by filtering on location coordinates
app.get('/:collection/filter/:method', function(req, res) {
var params = req.params;
var collection = params.collection;
var method = params.method;
var url_parts = url.parse(req.url, true);
var params = url_parts.query;
console.log("filter: " + req.url);
// find a square
if (!checkGaiaDB(collection)) {
res.send(400, {error: 'no such collection'});
}
if (method == 'circle') {
var center_lon = filterFloat(params.center_lon);
var center_lat = filterFloat(params.center_lat);
var min_dist = parseInt(params.min_dist); // meters in integer
var max_dist = parseInt(params.max_dist);
collectionDriver.findInCircle(req.params.collection, center_lon, center_lat, min_dist, max_dist, function(error, objs) {
if (error) {
res.send(400, error);
} else {
res.set('Content-Type','application/json');
res.send(200, objs);
}
});
} else if (method == 'box') {
var minlon = filterFloat(params.minlon);
var maxlon = filterFloat(params.maxlon);
var minlat = filterFloat(params.minlat);
var maxlat = filterFloat(params.maxlat);
var category = params.category;
console.log("search in box -- params: " + minlon + " " + minlat + " " + maxlon + " " + maxlat);
if (category) {
collectionDriver.findInBoxGivenCategory(req.params.collection, minlon, maxlon, minlat, maxlat, category, function(error, objs) {
if (error) {
res.send(400, error);
} else {
res.set('Content-Type','application/json');
res.send(200, objs);
}
});
} else {
collectionDriver.findInBox(req.params.collection, minlon, maxlon, minlat, maxlat, function(error, objs) {
if (error) {
res.send(400, error);
} else {
res.set('Content-Type','application/json');
res.send(200, objs);
}
});
}
} else if (method == 'top') {
var minlon = filterFloat(params.minlon);
var maxlon = filterFloat(params.maxlon);
var minlat = filterFloat(params.minlat);
var maxlat = filterFloat(params.maxlat);
var number = parseInt(params.num);
var category = params.category;
console.log("search in top -- params: " + minlon + " " + minlat + " " + maxlon + " " + maxlat);
if (!category) {
res.send(400, {"error" : "no category specified"});
return;
}
collectionDriver.getTop(req.params.collection, minlon, maxlon, minlat, maxlat, category, number, function(error, objs) {
if (error) {
res.send(400, error);
} else {
// res.set('Content-Type','application/json');
res.send(200, objs);
}
});
}
});
/*************************** POST ***************************/
// Add item/items to collection
// Takes in the following:
// {longitude: ,
// latitude: ,
// title: <place name> ,
// category: [coffee, cafe...],
// source: <source>, (optional)
// media: [...], (optional)
// location_id: <id> (optional)}
app.post('/:collection', function(req, res) {
var obj = req.body;
var length = obj.length;
var collection = req.params.collection;
var db_insert_array = [];
var count = 0;
var error_message = null;
var array;
console.log("POST item(s)");
if (!length) { // if it's an element, put it into an array
length = 1;
array = [];
array.push(obj);
} else {
array = obj;
}
// for checking duplicates
var distance_threshold = 0.7;
var lon_gap = 0.001
var lat_gap = 0.001
// In order to do for loops synchronously
var index = 0;
process(index);
function process(index) {
if (index >= length) {
console.log("The cycle ended");
res.send(201, {"success": "successfully inserted an array of " + length + " items"});
} else {
var client_object = array[index];
var db_object = {};
// filter and only put the qualified ones into the array
if (client_object.title && client_object.category && client_object.longitude && client_object.latitude) { // need category??!!
var longitude = client_object.longitude;
var latitude = client_object.latitude;
var title = client_object.title;
var minlon = filterFloat(longitude) - lon_gap;
var maxlon = filterFloat(longitude) + lon_gap;
var minlat = filterFloat(latitude) - lat_gap;
var maxlat = filterFloat(latitude) + lat_gap;
console.log("I: " + index + " findDuplicate() -- longitude: " + longitude + " latitude: " + latitude);
if (!client_object.category.length) {
res.send(400, {'error': 'category should be an array'});
return;
}
// First checks if there's duplicates in the DB, if there is, cluster it.
collectionDriver.findInBox(req.params.collection, minlon, maxlon, minlat, maxlat, function(error, objs) {
if (error) {
res.send(400, error);
// console.log("error");
} else {
var duplicate_exists = false;
console.log("there is " + objs.length + " items in the radius. ");
if (objs.length > 0) {
console.log("objs " + objs);
var to_be_inserted_title = title; // .tokenizeAndStem().sort();
var max_similarity = {'distance' : 0.7, 'id' : ''};
for (var i = 0; i < objs.length; i++) {
var db_item_title = objs[i].title; // .tokenizeAndStem().sort()
console.log("DB item title -- " + db_item_title + "To-be-inserted item title -- " + to_be_inserted_title);
var distance = natural.JaroWinklerDistance(to_be_inserted_title, db_item_title);
console.log("distance: "+ distance);
if (distance > max_similarity['distance']) {
console.log("***** there's a duplicate *****");
console.log("{distance: " + distance + ", id: " + objs[i]._id);
max_similarity['distance'] = distance;
max_similarity['id'] = objs[i]._id.toString();
}
}
if (max_similarity['distance'] > 0.7) { // there's at least one item that's a fit, update to choose the max one
console.log("there is a duplicate with id " + max_similarity['id']);
duplicate_exists = true;
var category_array = client_object.category;
var media_source = client_object.source;
var media_array = client_object.media;
if (!media_array) {
// ERROR!!!!
console.log("ERROR: there should be a media array!!");
} else {
console.log(media_array);
collectionDriver.addMediaBulk(collection, media_array, media_source, max_similarity['id'], function(error, objs) {
collectionDriver.addCategoryBulk(collection, client_object.category, max_similarity['id'], function(error, objs) {
if (error) { /* res.send(400, error); */ }
else { /* res.send(201, objs); */ console.log("Added media and category for object_id " + max_similarity['id']); }
index = index + 1;
process(index);
return;
});
});
}
}
}
if (!duplicate_exists) { // there's no duplicate!!
db_object['title'] = client_object.title;
db_object['category'] = client_object.category; // this is an array
db_object['loc'] = {type: 'Point', coordinates: [client_object.longitude, client_object.latitude]};
db_object['rank'] = 0; // initialize rank to 0
db_object['media'] = {}; // empty arrays // "yelp": [], "google": [], "twitter": [], "facebook": [], "instagram": []
var media_array = [];
if (client_object.media) {
media_array = client_object.media;
}
db_object['media'][client_object.source] = media_array;
console.log("there's no duplciate. adding media_array: " + media_array);
collectionDriver.save(collection, db_object, function(err, docs) {
if (err) { /* res.send(400, err); */ }
else { /* res.send(201, docs); */ console.log("Added new location for object_id " + docs._id.toString());}
index = index + 1;
process(index);
return;
});
}
}
});
} else {
error_message = {'error' : 'requires object\'s title, longitude, latitude, category'};
}
}
}
// // for saving an array at once (if not checking duplicates)
// collectionDriver.saveBulk(collection, db_insert_array, function(err, docs) {
// if (err) { res.send(400, err); }
// else { res.send(201, docs); }
// });
// }
});
/*************************** PUT(update) ***************************/
function filterMedia(object) {
var newObj = {};
for (var key in object) {
if (key == 'location_id' | key == 'post_id' | key == 'text' | key == 'tags' | key == 'image_url' |
key == 'link' | key == 'rating') {
newObj[key] = object[key];
console.log(key + object[key]);
}
}
return newObj;
}
// Add a media item to the media array
// /:collection/addMedia?id= &source=
app.put('/:collection/addMedia', function(req, res) {
var object = req.body;
var length = object.length;
var collection = req.params.collection;
var url_parts = url.parse(req.url, true);
var params = url_parts.query;
var source = params.source;
var entityid = params.id;
console.log('Update media on item with id: ' + entityid + ' on source ' + source);
if (!checkGaiaDB(collection)) {
res.send(400, {error: 'no such collection'});
}
if (!length) { // an element
if (entityid && source) {
collectionDriver.addMedia(collection, object, source, entityid, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
var error = { error : 'No item _id or source given'};
res.send(400, error);
}
} else { // an array
if (entityid && source) {
collectionDriver.addMediaBulk(collection, object, source, entityid, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
var error = { error : 'No item _id or source given'};
res.send(400, error);
}
}
});
// Updates the rank by incrementing or decrementing by that value
// ?id= &incvalue=
app.put('/:collection/incrementRank', function(req, res) {
var collection = req.params.collection;
var url_parts = url.parse(req.url, true);
var params = url_parts.query;
var inc_value = params.incvalue;
var entityid = params.id;
collectionDriver.incrementRank(collection, inc_value, entityid, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
});
// Updates the rank by incrementing or decrementing by that value
// ?id= &value=
app.put('/:collection/updateRank', function(req, res) {
var collection = req.params.collection;
var url_parts = url.parse(req.url, true);
var params = url_parts.query;
var new_value = parseFloat(params.value);
var entityid = params.id;
console.log("updateRank " + new_value);
collectionDriver.updateRank(collection, new_value, entityid, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
});
// Update the media information given the entity id and the source
// ?id= &source=
// not really needed...
/*
app.put('/:collection/updateMedia', function(req, res) {
var object = req.body;
var params = req.params;
var collection = params.collection;
var url_parts = url.parse(req.url, true);
var entityid = url_parts.query.id;
var source = url_parts.query.source;
console.log('Update media on item with id: ' + entityid + ' on source ' + source);
if (!checkGaiaDB(collection)) {
res.send(400, {error: 'no such collection'});
}
if (entityid) {
collectionDriver.updateMedia(collection, object, entityid, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
var error = { error : 'No item _id given'};
res.send(400, error);
}
});
*/
// Update the item to a totally different item
app.put('/:collection/:entity', function(req, res) {
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.update(collection, req.body, entity, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
var error = { "message" : "Cannot PUT a whole collection" };
res.send(400, error);
}
});
/*************************** DELETE ***************************/
// Delete the item from collection
app.delete('/:collection/:entity', function(req, res) {
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.delete(collection, entity, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, "Item id: " + entity + " is successfully deleted\n"); }
});
} else {
var error = { "message" : "Cannot DELETE a whole collection" };
res.send(400, error);
}
});
/*************************** DELETE WHOLE COLLECTION (comment this out when its not needed) ***************************/
app.get('/:collection/removeCollection', function(req, res) {
var params = req.params;
var collection = params.collection;
collectionDriver.removeCollection(collection, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
});
/*************************** DEFAULT ***************************/
app.get('/', function (req, res) {
res.header("Content-Type", "text/html");
res.write('<html><body><h1>Hello Gaia</h1></body></html>');
res.end();
});
// default: no such url
app.use(function (req,res) {
res.render('404', {url:req.url});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'))});