-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_data_visualization_single_endpoint_switch_case.js
108 lines (95 loc) · 3.15 KB
/
server_data_visualization_single_endpoint_switch_case.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
const MongoClient = require('mongodb').MongoClient
const assert = require('assert')
const urlMongo = 'mongodb://localhost:27017/bigchain'
var express = require('express');
var app = express();
var str = "";
var queryMongo = function(queryMatch, querySortByCount, callback) {
MongoClient.connect(urlMongo, queryMatch, querySortByCount, function(err, db) {
if (err) throw err;
console.log("Connected successfully to server")
executeAggregateQuery(queryMatch, querySortByCount, db, function(err, data) {
callback(err, data);
db.close();
});
})
}
var executeAggregateQuery = function(queryMatch, querySortByCount, db, callback) {
const client = db.db();
const collection = client.collection('assets');
collection.aggregate([queryMatch, querySortByCount]).toArray(function(err, docs) {
callback(err, docs);
});
};
//CORS middleware
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
//routes
app.get('/', function(req, res, next) {
console.log("Someone connected.")
var queryMatch = req.query.queryMatch;
console.log(req.query.queryMatch);
var querySortByCount = req.query.querySortByCount;
console.log(req.query.querySortByCount);
console.log(querySortByCount);
switch(queryMatch){
case 'visitors':
queryMatch = {$match: {'data.Survey Type' : 'visitors' }};
break;
case 'residents':
queryMatch = {$match: {'data.Survey Type' : 'residents' }};
break;
case 'enterprises':
queryMatch = {$match: {'data.Survey Type' : 'enterprises' }};
break;
case 'municipalities':
queryMatch = {$match: {'data.Survey Type' : 'municipalities' }};
break;
}
switch(querySortByCount){
//ETIS
case 'queryDisabilityConsiderations':
querySortByCount = {$sortByCount:"$data.Disability considerations"};
break;
case 'querySatisfaction':
querySortByCount = {$sortByCount:"$data.Overall Satisfaction"};
break;
//socio demographic
case 'queryCity':
querySortByCount = {$sortByCount :"$data.City"};
break;
case 'queryAge':
querySortByCount = {$sortByCount :"$data.Age"};
break;
case 'queryGender':
querySortByCount = {$sortByCount :"$data.Gender"};
break;
case 'queryHighestDegree':
querySortByCount = {$sortByCount :"$data.Highest Degree"};
break;
case 'queryProfessionalStatus':
querySortByCount = {$sortByCount :"$data.Professional Status"};
break;
case 'queryAnnualHousehold':
querySortByCount = {$sortByCount :"$data.Annual Household"};
break;
//contributions
case 'queryPurpose':
querySortByCount = {$sortByCount :"$data.Purpose"};
break;
case 'queryInterestingFeatures':
querySortByCount = {$sortByCount :"$data.Interesting Features"};
break;
}
console.log(JSON.stringify(querySortByCount));
queryMongo(queryMatch, querySortByCount, function(err, data) {
if(err)
res.status(500).json({error: err});
else
res.json(data);
})
})
var server = app.listen(3000, function() {});