-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·254 lines (203 loc) · 8 KB
/
index.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
var express = require('express');
var cov = require('compute-covariance');
var Correlation = require('node-correlation');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var firebase = require('firebase');
var firApp = firebase.initializeApp({
apiKey: "AIzaSyCDVUYROx4SJurgy01twBlRM9LZZuhGLpQ",
authDomain: "umapit-io.firebaseapp.com",
databaseURL: "https://umapit-io.firebaseio.com",
storageBucket: "umapit-io.appspot.com",
messagingSenderId: "349495637518"
});
var router = require('./app/routes');
app.use('/', router);
app.use(express.static(__dirname + '/public'));
http.listen(port, function () {
console.log('listening on port', port);
});
io.on('connection', function (socket) {
socket.on('getGlobalGeoJSON', (data) => {
var globalData = require('./geojsonFiles/global.geojson.json');
socket.emit('setGlobalGeoJSON', {
globalData: globalData
});
});
//Gets a list of the datasets that the user has associated with them
socket.on('getListOfUserProjects', (data) => {
let uid = data.uid;
if (uid) {
let ref = firebase.database();
ref.ref('users/' + uid + '/projects').on('value', function (snapshot) {
let projects = [];
let projectIDs = [];
let x = snapshot.val();
for (let item in x) {
projectIDs.push(item);
}
for (let projectID of projectIDs) {
ref.ref('projects/' + projectID).on('value', (snapshot) => {
let result = snapshot.val();
result['id'] = snapshot.key;
if (projects.length === projectIDs.length) {
let index = projectIDs.indexOf(result.id);
if (index !== -1){
projects[index] = result;
}
} else {
projects.push(result);
}
if (projects.length === projectIDs.length) {
socket.emit('listOfUserProjects', {projects: projects});
}
})
}
socket.emit('listOfUserProjects', {projects: projects});
});
}
});
//Gets a list of the datasets that the user has associated with them
socket.on('getListOfUserDatasets', (data) => {
let uid = data.uid;
if (uid) {
let ref = firebase.database();
let datasetIDs = [];
let datasetDetials = [];
ref.ref('users/' + uid + '/datasets').on('value', function (snapshot) {
let x = snapshot.val();
for (let item in x) {
datasetIDs.push(item);
}
for (i in datasetIDs) {
let datasetID = datasetIDs[i];
ref.ref('datasets-metadata/' + datasetID).once('value').then(function (snapshot) {
let result = snapshot.val();
result["id"] = snapshot.key;
datasetDetials.push(result);
if (datasetDetials.length === datasetIDs.length) {
socket.emit('listOfUserDatasets', {dataset: datasetDetials});
}
});
}
});
}
});
socket.on('getGlobalGeoJSON', () => {
var globaljson = require('./geojsonFiles/global.geojson.json');
socket.emit('globalGeoJSON', globaljson);
});
socket.on('getDatasetWithID', (data) => {
if (data) {
let ref = firebase.database();
ref.ref('datasets/' + data.datasetid).once('value').then(function (snapshot) {
let dataset = snapshot.val();
let maxVal = dataset.data[0].value;
let minVal = dataset.data[0].value;
for (item of dataset.data){
if (item.value < minVal){
minVal = item.value;
}
if (item.value > maxVal){
maxVal = item.value;
}
}
dataset.minVal = minVal;
dataset.maxVal = maxVal;
if (data.viewOnly){
socket.emit('setDatasetViewOnly', {datasetid: data, data: dataset});
} else {
socket.emit('setDataset', {datasetid: data, data: dataset});
}
});
}
});
socket.on('getProjectWithId', (id) => {
if (id) {
let ref = firebase.database();
ref.ref('projects/' + id).once('value').then(function (snapshot) {
let project = snapshot.val();
if (project){
project["id"] = id;
}
socket.emit('setProjectWithId', project);
}, function (error) {
// An error happened.
console.log("ERROR");
});
}
});
socket.on('saveProjectDetailsInDB', (project) => {
if (project) {
let ref = firebase.database();
ref.ref('projects').once('value').then(function (snapshot) {
if (snapshot.hasChild(project.id)) {
let updates = {};
let projectPath = '/projects/' + project.id;
updates[projectPath + '/title'] = project.title;
updates[projectPath + '/datasetIDs'] = project.datasetIDs;
updates[projectPath + '/dataset1ID'] = project.dataset1ID;
updates[projectPath + '/dataset2ID'] = project.dataset2ID;
updates[projectPath + '/isPublic'] = project.isPublic;
updates[projectPath + '/visibleDataset'] = project.visibleDataset;
updates[projectPath + '/projectScreenshotURL'] = project.projectScreenshotURL;
updates[projectPath + '/ds1Color'] = project.ds1Color;
updates[projectPath + '/ds2Color'] = project.ds2Color;
firebase.database().ref().update(updates);
}
});
}
});
socket.on('computeDatasetRatio', (data) => {
let ref = firebase.database();
let ds1 = null;
let ds2 = null;
ref.ref('datasets/'+data.dataset1id).once('value').then(function (snapshot1) {
ds1 = snapshot1.val().data;
if (ds1 && ds2){
let dsCombined = computeRatioSet(ds1,ds2);
let dataToSend = {
data: {
data: dsCombined,
minVal: -1.0,
maxVal: 1.0
}
}
socket.emit('plotCorrelation', dataToSend);
}
});
ref.ref('datasets/'+data.dataset2id).once('value').then(function (snapshot2) {
ds2 = snapshot2.val().data;
if (ds1 && ds2){
let dsCombined = computeRatioSet(ds1,ds2);
let dataToSend = {
data: {
data: dsCombined,
minVal: -1.0,
maxVal: 1.0
}
}
socket.emit('plotCorrelation', dataToSend);
}
});
});
});
function computeRatioSet(ds1, ds2) {
let dsCombined = [];
let ds1Sum = 0.0;
let ds2Sum = 0.0;
for (i in ds1){
ds1Sum += ds1[i].value;
}
for (i in ds2){
ds2Sum += ds2[i].value;
}
for (i in ds1){
let newVal = Object.assign({}, ds1[i]);
newVal.value = ((ds1[i].value/ds1Sum).toPrecision(4)-(ds2[i].value/ds2Sum).toPrecision(4)).toPrecision(4);
dsCombined.push(newVal);
}
return dsCombined;
}