-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
414 lines (353 loc) · 14 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
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
const http = require('http');
const express = require('express');
const path = require('path');
const yaml = require('js-yaml');
const fs = require('fs');
var crypto = require('crypto');
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
// Read configuration file
const config = yaml.load(fs.readFileSync('././config/settings.yaml', 'utf8'));
// prepare configuration options
let uri = config['mongodb_atlas']['endpoint'];
const username = config['mongodb_atlas']['username'];
const password = config['mongodb_atlas']['password'];
uri = uri.replace(/\<username\>/g, username).replace(/\<password\>/g, password);
const datasetFolder = config['dataset']['folder'];
const datasetFileName = config['dataset']['filename'];
const toolVersion = config['version'];
const nQuestionsPerExperiment = config['n_questions_per_experiment'];
const guidelineTask = config['guideline']['task_function_naming'];
const tools = require('./my_tools');
const database = require('./utils/database');
const port = process.env.PORT || 3000
const javascript_client = config['javascript_client'];
// Handy toolbox and helper functions
const app = express();
const sessionStore = new MongoDBStore({
uri: uri,
collection: 'sessions',
expires: 1000 * 60 * 60 * 24 * 2 // 2 days
});
// Initialize session
app.use(
session({
secret: 'my-secret',
resave: false,
saveUninitialized: false,
store: sessionStore
})
);
// Register template engine
app.set('view engine', 'ejs');
app.set('views', 'views');
// Make static pages available together with css and javascript
app.use(express.static(path.join(__dirname, 'public')));
// BODY Parser functions
app.post('/result-receiver', bodyParser.json({ extended: true, limit: '10mb' }));
// handle submission of mouse traces logs and clicked tokens
app.post('/result-receiver', function (req, res, next) {
console.log("post received: mouse trace");
// save the log file use as name:
// current time + id of the file
let correctAnswers = 0;
let wrongAnswers = 0;
// Check how many correct asnwers
if (req.session.correctAnswers) {
correctAnswers = req.session.correctAnswers;
}
// Check how many wrong asnwers
if (req.session.wrongAnswers) {
wrongAnswers = req.session.wrongAnswers;
}
if ("id" in req.body && "mousetrace" in req.body) {
// enrich the data with nickname before storing the human log
req.body.nickname = req.session.nickname;
req.body.randomcode = req.session.randomCode;
req.body.experimentset = req.session.experiment_set;
req.body.version = toolVersion;
// check if the answer was right
if (req.body.functionnamebyuser == req.session.expectedCorrectAnswer) {
correctAnswers += 1;
req.session.correctAnswers = correctAnswers;
} else {
wrongAnswers += 1;
req.session.wrongAnswers = wrongAnswers;
}
//tools.saveHumanAttentionLog(fileId = req.body.id, content = 'Marco');
tools.saveHumanAttentionLogToMongo(req.body);
}
console.log("Raw Body: ")
console.log(req.body);
// respond with a successful message
res.setHeader('Content-Type', 'text');
res.write("Saved Successfully.");
res.end();
});
// READ THE NICKNAME FIELD
app.post("/login", bodyParser.urlencoded({ extended: true }));
app.post("/login", (req, res, next) => {
const userInputNickName = req.body.nickname;
console.log("Nickname: " + userInputNickName);
const randomCodeNumber = Math.floor(Math.random() * 1000000000000000);
const to_hash = userInputNickName + randomCodeNumber;
let hashed_string = crypto.createHash('md5').update(to_hash).digest('hex');
// md5 output 32 characters
req.session.nickname = userInputNickName;
req.session.randomCode = hashed_string;
req.session.correctAnswers = 0;
req.session.wrongAnswers = 0;
res.redirect("/task-description");
});
// TASK DESCRIPTION PAGE
app.use("/task-description", (req, res, next) => {
// Choose the experiment dataset to propose
// Aka: ask MongoDB to have a list of available datasets
// Upload available experiment sets to MongoDB
fs.readdir(datasetFolder, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
let filtered_files = files.filter(function (file) {
return file.startsWith('experiment_');
});
console.log('filtered_files')
console.log(filtered_files)
let minutesToBeReproposed = 120;
database.mongoConnectDelivery(collection => {
// we serve either brand new experiment sets
// or those that have been delivered more than x minutes ago
// and we still do not have a completion
let cursor = collection.find(
{ $or: [
{ delivered_to: 0 },
{ $and: [
{ delivered_to: { $gte: 1 }},
{ completed_by: { $exists: false}},
{ most_recent_time_when_served: { $lt: Date.now() - (1000 * 60 * minutesToBeReproposed)}}
]}
]}
);
//let cursor = collection.find({ delivered_to: 0 });
cursor.toArray( function(err, docs) {
let servableDocuments = docs.map(doc => doc.filename);
// let namesDelivered = docs.map(doc => doc.filename);
// console.log('filtered_files: ' + filtered_files);
// console.log('namesDelivered: ' + namesDelivered);
// const neverDelivered =
// filtered_files.filter(file => !namesDelivered.includes(file));
// if (neverDelivered.length == 0) {
// res.write('No more experiments set available, ask Matteo to deliver new ones.');
// res.end();
// }
// // Select one randomly
// let selected_filename = neverDelivered[0];
servableDocuments.sort();
console.log(servableDocuments)
let selected_filename = servableDocuments[0];
if (!filtered_files.includes(selected_filename)) {
console.log('Unavailable selected document: ' + selected_filename);
res.write('Doc not found: ' + selected_filename);
res.end();
}
console.log(selected_filename);
// Save the experiment filename as
// session.delivered_dataset for the user
req.session.experiment_set = selected_filename;
console.log(selected_filename + ' delivered to ' + req.session.nickname);
// update mongo (increment)
// Tell MongoDB we are delivering it
// Aka: create {'dataset': 'experiment_13', 'delivered': +1}
collection.updateOne({ filename: selected_filename },
{ $inc: { delivered_to: 1 },
$max: { most_recent_time_when_served: Date.now()},
$push: { users: { by: req.session.nickname,
code: req.session.randomCode,
timestamp: Date.now()}
}
});
// serve the page
console.log("Task description displayed...")
res.render('task-description',{
toolVersion: toolVersion,
guideline: guidelineTask
});
})
})
});
});
// TASK DISCLAIMER PAGE
app.use("/task-disclaimer", (req, res, next) => {
console.log("Task disclaimer displayed...");
res.render('task-disclaimer',{
toolVersion: toolVersion
});
});
// TASK RATE PAGE
app.use("/rate-difficulty", bodyParser.urlencoded({ extended: true }));
app.post("/rate-difficulty", (req, res, next) => {
console.log(req.body);
if(req.body != 'undefined'
&& 'fileindex' in req.body
&& 'rating' in req.body) {
const fileIndex = req.body.fileindex;
const rating = req.body.rating;
console.log('Received rating from: ' + req.session.randomCode +
' - at position: ' + fileIndex +
' - rating: ' + rating);
req.body.nickname = req.session.nickname;
req.body.randomcode = req.session.randomCode;
req.body.experimentset = req.session.experiment_set;
req.body.version = toolVersion;
tools.saveRatingToMongo(req.body);
if (req.session.nickname) {
console.log('Next page...');
const nextIndex = parseInt(fileIndex) + 1
res.redirect("/experiment?positionOfFile=" + (parseInt(nextIndex)).toString());
res.end();
return;
}
}
res.end();
});
app.use("/rate-difficulty", (req, res, next) => {
// Check if the user inserted a user name
if (!req.session.nickname) {
res.redirect("/login");
res.end();
return;
}
let positionOfFile = 0;
if (req.query != 'undefined' && 'positionOfFile' in req.query) {
positionOfFile = parseInt(req.query.positionOfFile);
console.log('Showing rater for file at position: ' + positionOfFile);
}
console.log("Rate question...");
res.render('rate-difficulty',{
toolVersion: toolVersion,
positionOfFile: positionOfFile
});
});
// START/LOGIN PAGE
app.use("/login", (req, res, next) => {
console.log("Login displayed...")
res.render('login', {
toolVersion: toolVersion
});
});
// END PAGE
app.use("/end-experiment", (req, res, next) => {
console.log("End experiment...");
let correctAnswers = 0;
let wrongAnswers = 0;
// Check how many correct answers
if (req.session.correctAnswers) {
correctAnswers = req.session.correctAnswers;
}
// Check how many wrong answers
if (req.session.wrongAnswers) {
wrongAnswers = req.session.wrongAnswers;
}
if ((correctAnswers + wrongAnswers) < nQuestionsPerExperiment) {
res.write('Cheater? You have not completed 20 questions, go back and submit all of them.');
res.end();
return;
}
// inform the server that this experiment set has been Completed
database.mongoConnectDelivery(collection => {
// update mongo (increment) completed pages
collection.updateOne({ filename: req.session.experiment_set },
{ $inc: { completed_by: 1 },
$push: { completed_users: { by: req.session.nickname,
timestamp: Date.now()}
}
});
// serve the page
console.log("Experiment Completed: " + req.session.experiment_set);
console.log("By: " + req.session.nickname);
res.render('end-experiment-view', {
randomCode: req.session.randomCode,
correctAnswers: correctAnswers,
wrongAnswers: wrongAnswers,
totalAnswers: correctAnswers + wrongAnswers,
toolVersion: toolVersion
});
res.end();
})
});
app.use("/experiment", bodyParser.urlencoded({ extended: true }));
// Slideshow-view of code as tokenized (v02 of the App)
app.use("/experiment", (req, res, next) => {
// Check if the user inserted a user name
if (!req.session.nickname) {
res.redirect("/login");
res.end();
return;
}
console.log("Nickname: " + req.session.nickname +
" - RandomCode: " + req.session.randomCode);
let correctAnswers = 0;
let wrongAnswers = 0;
// Check how many correct asnwers
if (req.session.correctAnswers) {
correctAnswers = req.session.correctAnswers;
}
// Check how many wrong asnwers
if (req.session.wrongAnswers) {
wrongAnswers = req.session.wrongAnswers;
}
let positionOfFile = 0;
if (req.query != 'undefined' && 'positionOfFile' in req.query) {
positionOfFile = parseInt(req.query.positionOfFile);
console.log('Serving file at position: ' + positionOfFile);
}
const jsonFilePath = datasetFolder + req.session.experiment_set;
var fs = require('fs');
var allFunctionsData;
fs.readFile(jsonFilePath, 'utf8', function (err, data) {
if (err) throw err;
// console.log(data);
allFunctionsData = JSON.parse(data);
fileInfo = allFunctionsData[positionOfFile];
if (typeof fileInfo === 'undefined') {
res.redirect('/end-experiment')
//res.send("This index is not available");
res.end();
return;
}
// tokens = tools.prepareCodeBoxText(fileInfo.tokens, tokenClass='token');
tokens = fileInfo.tokens_in_code
console.log(tokens)
let optionsFunctionNames = fileInfo.options;
console.log(optionsFunctionNames);
progressPercentage =
parseInt((positionOfFile / allFunctionsData.length) * 100);
//fileName: fileInfo.file_name,
//functionName: fileInfo.function_name,
req.session.expectedCorrectAnswer = fileInfo.option_correct;
res.render('single-code-processed',
{
javascript_client: javascript_client,
positionOfFile: positionOfFile,
uuid: fileInfo.uuid,
optionsFunctionNames: optionsFunctionNames,
tokens: tokens,
progress: progressPercentage,
tokenClass: "token",
nickname: req.session.nickname,
correctAnswers: correctAnswers,
wrongAnswers: wrongAnswers,
totalAnswers: correctAnswers + wrongAnswers,
guideline: guidelineTask
});
});
});
// HOME PAGE
app.use("/", (req, res, next) => {
res.redirect('/login');
});
// Event Driven Architecture
const server = http.createServer(app);
server.listen(port);