-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
347 lines (316 loc) · 8.54 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
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
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
path = require('path'),
io = require('socket.io').listen(server),
mongo = require('mongodb'),
mongoose = require('mongoose'),
fs = require('fs');
//Input collection and database to use
mongoose.connect('mongodb://209.129.244.25/speech-00');
var collection = "HansTesting3";
server.listen(8080);
app.use("/", express.static(__dirname + '/'));
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("mongoDB connected");
});
var scheme = mongoose.Schema({
ANNOTATION: Array,
FILENAME: String,
USERID: String,
INTERACTION: String,
DATE: String,
TIME: String,
LATITUDE: String,
LONGITUDE: String,
TRANSCRIPT: String,
AUDIO: String,
STATUS: String
});
var items = mongoose.model('items', scheme, collection);
/**
* Get transcript content from Mongodb
* @param {Object} data condition for query
* @param {Function} cb return data back to client
* @return {none } none
*/
function getAudio(data, cb) {
var param = "AUDIO";
var object = {
_id: data
};
items.find(object, param).execFind(function(error, response) {
cb(response);
});
}
/**
* Update Annotation in MongoDb
* @param {Object} id condition for query
* @param {Integer} position position of word
* @param {String} content type of annotation
* @return {none} none
*/
function updateAnnotation(id, position, content) {
console.log("inside annotation");
var conditions = {
_id: id,
'ANNOTATION.position': parseInt(position)
};
var update = {
$set: {
'ANNOTATION.$.type': content
}
};
var options = {
upsert: true
};
var callback = function(error) {
console.log("success");
};
items.update(conditions, update, options, callback);
}
/**
* Update annotation type
* @param {Object} id condition for query
* @param {Integer} position position of the word in sentence
* @param {String} word annotated word
* @param {String} type type of annotation
* @return {none} none
*/
function updateType(id, position, word, type) {
var conditions = {
_id: id,
'ANNOTATION.position': parseInt(position)
};
items.find(conditions, function(error, data) {
console.log(data.length);
if (data.length) {
//Update Annotation if it exists
updateAnnotation(id, position, type);
} else {
var newType = {
name: word,
position: parseInt(position),
type: type
}
conditions = {
_id: id
};
var update = {
$addToSet: {
ANNOTATION: newType
}
};
var options = {
upsert: true
};
var callback = function(error) {
console.log("success");
};
items.update(conditions, update, options, callback);
}
});
}
/**
* Update status (pending, processing, verified)
* @param {Object} id condition for query
* @param {String} status status value to change
* @return {none} none
*/
function updateStatus(id, status) {
var conditions = {
_id: id
};
items.find(conditions, function(error, data) {
var conditions = {
_id: id
};
var update = {
$set: {
STATUS: status
}
};
var options = {
upsert: true
};
var callback = function(error) {
console.log("success");
};
items.update(conditions, update, options, callback);
});
}
/**
* Updates the transcript data in Mongodb
* @param {Object} id condition for query
* @param {[type]} position [description]
* @param {String} content transcript content
* @return {none} none
*/
function updateTranscript(id, position, content) {
var conditions = {
_id: id
};
items.find(conditions, function(error, data) {
if (content != data[0].TRANSCRIPT) {
var conditions = {
_id: id
};
var update = {
$set: {
TRANSCRIPT: content,
ANNOTATION: []
}
};
var options = {
upsert: true
};
var callback = function(error) {
console.log("success");
};
items.update(conditions, update, options, callback);
}
});
}
/**
* Get DialogIds to display in dropdown menu
* @param {String} userID condition for query
* @param {Function} cb return data back to client
* @return {none} none
*/
function getDialogList(userID, cb) {
var conditions = {
USERID: userID
};
var param = "-AUDIO";
var distinct = 'INTERACTION';
items.find(conditions, param).sort({
TIME: 1
}).distinct(distinct, function(error, items) {
console.log('items', items);
cb(items);
});
}
/**
* Get UserIds to display in dropdown menu
* @param {String} userID UserID
* @param {Function} cb Returns list back to client
* @return {none} none
*/
function getUserList(userID, cb) {
var conditions = {};
var param = "-AUDIO";
var distinct = 'USERID';
items.find(conditions, param).sort({
TIME: 1
}).distinct(distinct, function(error, items) {
console.log('items', items);
cb(items);
});
}
/**
* Get all data from DB
* @param {Function} cb returns data to client
* @return {none} none
*/
function getAllItems(cb) {
var param = "-AUDIO";
items.find({}, param).sort({
TIME: 1
}).execFind(function(error, items) {
cb(items);
});
}
/**
* Adds annotation to text in MongoDb
* @param {String} data_name word is being annotated
* @param {String} data_type setting the type
* @param {Integer} data_field position of the word in the sentence
*/
function addAnnotation(data_name, data_type, data_field) {
var info = {
name: data_name,
type: data_type,
position: data_field
}
var conditions = {
_id: "51dc9f908bbd955c2edd00c7"
};
var update = {
$set: {
ANNOTATION: []
}
};
var options = {
upsert: true
};
var callback = function(error) {
if (error)
console.log(error);
console.log("success");
};
items.update(conditions, update, options, callback);
}
/**
* Retreives requested data by user
* @param {Object} conditions specifies data types
* @param {Integer} order choose to sort by most recent
* @param {Function} cb returns data back to client
* @return {} none
*/
function getData(conditions, order, cb) {
var param = "-AUDIO";
console.log(conditions);
items.find(conditions, param).sort({
TIME: order
}).execFind(function(error, data) {
cb(data);
});
}
io.sockets.on('connection', function(socket) {
// Get all data when client starts
socket.on('getAllData', function() {
getAllItems(function(items) {
socket.emit('setMarkers', items);
});
});
// Get an audio file back to client
socket.on('getAudio', function(item, loop) {
getAudio(item, function(items) {
socket.emit('setAudios', items[0].AUDIO, loop);
});
});
socket.on('updateSub', function(id, position, content) {
updateAnnotation(id, position, content);
});
socket.on('setTypes', function(id, position, word, type) {
updateType(id, position, word, type);
});
socket.on('updateTranscript', function(id, position, content) {
updateTranscript(id, position, content);
});
socket.on('updateStatus', function(id, status) {
updateStatus(id, status);
});
socket.on('getDialogList', function(userID) {
getDialogList(userID, function(items) {
socket.emit('setDialogList', items);
});
});
socket.on('getUserList', function(userID) {
getUserList(userID, function(items) {
socket.emit('setUserList', items);
});
});
socket.on('getData', function(conditions, order) {
getData(conditions, order, function(data) {
socket.emit('setData', data);
});
});
socket.on('disconnect', function() {});
});