forked from theodi/nodejs-oauth-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
716 lines (669 loc) · 24.2 KB
/
api.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
var ObjectId = require('mongodb').ObjectID;
const fetch = require('node-fetch');
const { Parser } = require('@json2csv/plainjs'); // Library to create CSV for output
const { Headers } = fetch;
resolved = false;
promises = [];
/*
* execQuery (query)
*
* Take a raw query and exec it against the Learning Locker Endpoint
* Writen to support the use of pagination using MORE
*
* REUSABLE
*/
const execQuery = async (query) => {
let myHeaders = new Headers();
myHeaders.append(
'Authorization',
'Basic ' + process.env.LEARNING_LOCKER_KEY
);
myHeaders.append('Content-Type', 'application/json');
myHeaders.append('X-Experience-API-Version', '1.0.0');
let requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow',
};
// get paramters from search param in url
let base = "https://theodi.learninglocker.net";
query = base + query;
const getJson = async (query) => {
try {
const res = await fetch(
query,
requestOptions
);
return await res.json();
}
// catch error and return 404 to user
catch (error) {
return {};
}
};
return await getJson(query);
}
/*
* getStatements
*
* Function to get the results of the first query
* Subsequenct calls to execQuery should come from the pagination (MORE) returned in the result
*
* REUSABLE
*/
const getStatements = async (activity, verb, since, until, related_activities) => {
let base = "/data/xAPI/statements?";
let args = [];
if (verb) { args.push("verb=" + verb); }
if (activity) { args.push("activity=" + encodeURIComponent(activity)); }
if (since) { args.push("since=" + since); }
if (until) { args.push("until=" + until); }
if (related_activities) { args.push("related_activities=true"); }
var query = base + args.join('&');
console.log(query);
return await execQuery(query);
}
/*
* simplifyOutput
*
* Takes the aggregated results from a set of results related to a question and creates an output suitable for rendering in ChartJS
*
* VERY SPECIFIC to aggregate queries about a question and to ChartJS
*/
function simplifyOutput(input) {
var array = [];
input.map((a) => {
array.push(a.Count);
});
return array;
}
/*
* NOT IN USE
* getCombinedProgress (existingProgress, newProgress)
*
* When handling a paginated set of statements, you may get multiple results about an actor contain the progress object and these need combining into one.
* This is particularly the case for sessionTime when the session will be launched, initialised etc etc etc lots of times.
* To work out total session time you need all of these statements.
*
* REUSABLE
*/
function getCombinedProgress(eProg,nProg) {
for (const [key, value] of Object.entries(eProg)) {
if (nProg[key]) {
eProg[key] = [...eProg[key],...nProg[key]];
}
}
return {...nProg,...eProg};
}
/*
* getNestedActors
*
* Another function to work with multiple pages of actor results to combine the data together
*
* REUSABLE when you have actors as the primary object
*/
const getNestedActors = (arr) => {
if (arr.length === 1) {
return arr[0].actors;
}
var combined = {};
for (var i=0; i<arr.length;i++) {
actors = arr[i].actors;
try {
for (const [key, value] of Object.entries(actors)) {
try {
if (combined[key]) {
//combined[key].progress = getCombinedProgress(combined[key].progress,actors[key].progress);
combined[key].progress = combined[key].progress.concat(actors[key].progress);
} else {
combined[key] = value;
}
} catch (err) {
console.log("Error combining data");
console.log(err);
}
}
} catch (err) {
console.log("Error getting nested actors, probably undefined");
console.log(err);
}
}
return combined;
}
/*
* getNestedObjects
*
* When working with multiple pages of results, we only want to return one set of objects.
* This function ensure there is one (and only one) copy of each object in the returned data.
*
* REUSABLE
*/
const getNestedObjects = (arr) => {
if (arr.length === 1) {
return arr[0].objects;
}
var combined = {};
for (var i=0; i<arr.length;i++) {
objects = arr[i].objects;
if (!objects) {
console.log("Empty data object");
} else {
for (const [key, value] of Object.entries(objects)) {
if (combined[key]) {
// IGNORE and continue;
} else {
combined[key] = value;
}
}
}
}
return combined;
}
/*
* getSessionTime (sortedList)
*
* Takes a sorted list of session events and works out the total time spent on an activity
* If the session is currently open then this time won't be included.
*
* REUSABLE with catculateSessionTimes
*/
function getSessionTime(sortedList) {
var paused = false;
var lastTime = null;
var timeSpent = 0;
for (var i=0;i<sorted.length;i++) {
verb = sorted[i].verb;
timestamp = sorted[i].timestamp;
if (verb == "http://adlnet.gov/expapi/verbs/launched" || verb == "http://adlnet.gov/expapi/verbs/resumed" || verb == "http://adlnet.gov/expapi/verbs/initialized") {
paused = false;
lastTime = timestamp;
}
if (verb == "http://adlnet.gov/expapi/verbs/suspended" || verb == "http://adlnet.gov/expapi/verbs/terminated") {
if (!paused && lastTime) {
timeSpent += new Date(timestamp) - new Date(lastTime);
lastTime = null;
}
}
}
return Math.round(timeSpent / 1000);
}
/*
* calculateSessionTimes(objects)
*
* Takes a set of actor objects (with progress) and adds a new key to the returned data (timeSpentSeconds) that represents the total time spent on the activity
*
* REUSABLE
*/
function calculateSessionTimes(objects) {
for (const [key, value] of Object.entries(objects)) {
var progress = value.progress;
var sessionTime = [];
for (const [pkey, pvalue] of Object.entries(progress)) {
if (pvalue.id == "act:sessionTime") {
sessionTime.push(pvalue);
}
}
if (!sessionTime)
{
return objects;
}
sorted = sessionTime.sort(
(objA, objB) => new Date(objA.timestamp) - new Date(objB.timestamp),
);
objects[key].timeSpentSeconds = getSessionTime(sorted);
}
return objects;
}
/*
* processReturn
*
* Outputs the data in the desired format
* NOTE: HTML should be removed from here and handled outside of the API handler
*
* REUSABLE
*/
function processReturn(req,res,filter,output,csvOutput) {
// Work out what the client asked for, the ".ext" specified always overrides content negotiation
ext = req.params["ext"] || filter.format;
// If there is no extension specified then manage it via content negoition, yay!
if (!ext) {
ext = req.accepts(['json', 'csv', 'html']);
}
// Return the data to the user in a format they asked for
// CSV, JSON or by default HTML (web page)
res.set('Access-Control-Allow-Origin', '*');
if (ext == "csv") {
const parser = new Parser({ header: true });
const csv = parser.parse(csvOutput);
res.set('Content-Type', 'text/csv');
res.send(csv);
} else if (ext == "json") {
res.set('Content-Type', 'application/json');
res.send(JSON.stringify(output, null, 4));
} else if (ext == "chartjs") {
res.set('Content-Type', 'text/plain');
res.send(JSON.stringify(simplifyOutput(csvOutput), null, 4));
} else {
res.set('Content-Type', 'application/json');
res.send(JSON.stringify(output, null, 4));
}
}
/*
* makeActivityDataCSVOutput (output)
*
* Takes the output from the getActivityData and creates an object that can be output as a CSV file
* Specific to the getActivityData API call
*
* NOT REUSABLE as each CSV has a different structure
*/
function makeActivityDataCSVOutput(output) {
var csvOutput = [];
for (const [actorid, data] of Object.entries(output.actors)) {
var item = {};
item.actor = actorid;
item.name = data.name || "";
item.mbox = data.mbox || "";
item.timeSpentSeconds = data.timeSpentSeconds || "";
for (const [key, progressdata] of Object.entries(data.progress)) {
if (progressdata.id != "act:sessionTime") {
item[progressdata.id] = progressdata.verb;
}
}
csvOutput.push(item);
}
return csvOutput;
}
/*
* processActivityDataObjects
*
* Specific function to build the data required for the getActivityData API call
* This function is required to call itself to process additional pages
*
* NOT REUSABLE
*/
function processActivityDataObjects(objects,activity,related_activities) {
//console.log(JSON.stringify(objects, null, 2));
var output = {};
console.log("Processing objects");
if (!objects) {
return {};
}
if (objects.more) {
console.log("Adding promise to the array");
promises.push(new Promise((resolve,reject) => {
execQuery(objects.more).then((objects) => {
resolve(processActivityDataObjects(objects,activity,related_activities));
});
}));
} else {
resolved = true;
}
var statements = objects.statements;
if (statements.length < 1 || !statements) {
return {};
}
if (!related_activities) {
output.object = statements[0].object;
} else {
output.objects = {};
}
output.actors = {};
try {
statements.map((a) => {
actorid = a.actor.account.name;
objectid = a.object.id;
if (related_activities && !output.objects[objectid]) {
output.objects[objectid] = a.object;
}
if (!output.actors[actorid]) {
output.actors[actorid] = a.actor;
progress = [];
} else {
progress = output.actors[actorid].progress;
}
verb = a.verb.id;
if(objectid == activity) {
if (verb == "http://adlnet.gov/expapi/verbs/passed" || verb == "http://adlnet.gov/expapi/verbs/failed") {
//console.log(verb);
objectid = "act:passed";
} else if (verb == "http://adlnet.gov/expapi/verbs/completed") {
objectid = "act:completed";
} else {
objectid = "act:sessionTime";
}
}
statement = {};
statement.id = objectid;
statement.verb = a.verb.id;
statement.timestamp = a.timestamp;
progress.push(statement);
output.actors[actorid].progress = progress;
});
} catch (error) {
console.log(error);
output.success = "unknown";
output.completion = "unknown";
}
return output;
}
/*
* combineQuestionSummaryResults
*
* When working with multiple pages of results, we only want to return one set of objects.
* This function adds up all the counts for the question summary results including completion and success measures
*
* NOT REUSABLE
*/
const combineQuestionSummaryResults = (arr) => {
var combined = {};
var output = {};
output.responses = [];
output.success = 0;
output.completion = 0;
for (var i=0; i<arr.length;i++) {
responses = arr[i].responses;
for (j=0;j<responses.length;j++) {
if (!combined[responses[j].id]) {
combined[responses[j].id] = 0;
}
combined[responses[j].id] += responses[j].count;
}
output.success += arr[i].success;
output.completion += arr[i].completion;
}
for (const [key, value] of Object.entries(combined)) {
var local = {};
local.id = key;
local.count = value;
output.responses.push(local);
}
return output;
}
/*
* makeQuestionDataCSVOutput (output)
*
* Takes the output from getQuestionSummaryData and creates an object that can be output as a CSV file
* Specific to the getQuestionSummaryData API call
* CSV has two columns:
* Answer: English text of the answer
* Count: Number of people who picked this response
*
* NOT REUSABLE as each CSV has a different structure
*/
function makeQuestionDataCSVOutput(output) {
var choices = output.object.definition.choices;
if (!choices) {
//FIXME A MATCHING COMPONENT
return [];
}
var rotated_choices = {};
var responses = output.responses;
var output = [];
for (i=0;i<choices.length;i++) {
rotated_choices[choices[i].id] = choices[i].description.en;
}
for (i=0;i<responses.length;i++) {
id = responses[i].id;
var local = {};
local.Answer = rotated_choices[id];
local.Count = responses[i].count;
output.push(local);
}
return output;
}
/*
* processQuestionDataObjects
*
* Specific function to build the data required for the getQuestionSummaryData API call
* This function is required to call itself to process additional pages
*
* NOT REUSABLE
*/
function processQuestionDataObjects(objects) {
var statements = objects.statements;
console.log("Processing objects");
if (!objects || !statements[0]) {
return {};
}
if (objects.more) {
console.log("Adding promise to the array");
promises.push(new Promise((resolve,reject) => {
execQuery(objects.more).then((objects) => {
resolve(processQuestionDataObjects(objects));
});
}));
} else {
resolved = true;
}
var output = {};
output.object = statements[0].object;
output.responses = [];
output.success = 0;
output.completion = 0;
var responseArray = [];
try {
statements.map((a) => {
result = a.result;
responses = result.response.split('[,]');
responses.map((response) => {
if (responseArray[response]) {
responseArray[response] += 1;
} else {
responseArray[response] = 1;
}
});
if (result.success) { output.success += 1; }
if (result.completion) { output.completion += 1; }
});
} catch (error) {
output.success = "unknown";
output.completion = "unknown";
}
try {
statements[0].object.definition.choices.map((a) => {
let jsonres = {};
jsonres.id = a.id;
jsonres.count = responseArray[a.id] || 0;
output.responses.push(jsonres);
});
} catch (error) {
// Do nothing
}
return output;
}
/*
* getActivityData
*
* Get actor level data about interactions with an activity an all it's related objects.
* You call this API with an activity ID which represents a page or module and it will return all related (e.g. child) activities.
* In simple terms the returned data will be in the form of one row per actor and one column per activity with some added extras including sessionTime.
*
* SPECIFIC API function.
* Example API call = http://localhost:3080/api/activityData?activity=https://theodi.stream.org/xapi/activities/learning-lockker-stand-alone-xapi-test-dt
*/
exports.getActivityData = function(req, res, dbo) {
var filter = req.query;
if (!filter.activity || filter.activity == null) {
res.statusMessage = "You need to define an activity e.g. http://url.com/?activity=http://....";
res.status(400).end();
res.send();
return;
}
var activity = filter.activity;
var verb = filter.verb || null;
var since = filter.since || null;
var until = filter.until || null;
var related_activities = filter.related_activities || true;
var format = filter.format;
console.log('');
console.log("Cache query");
console.log("Activity:" + activity);
console.log("Verb:" + verb);
console.log("since:" + since);
console.log("until:" + until);
console.log("related_activities:" + related_activities);
//Get any cached data as well as update since to be the last cache update
var key = null;
var dbConnect = dbo.getDb();
collection = "StatsCache"
dbConnect
.collection(collection)
.find({"activity": activity, "verb": verb, "since": since, "until": until, "related_activities": related_activities})
.toArray(function(err,items) {
cachedData = items[0];
if (cachedData) {
var newActors = {};
var newObjects = {};
for(var i=0;i<cachedData.actors.length;i++) {
try {
newActors[cachedData.actors[i].name] = cachedData.actors[i];
} catch(err) {
console.log(err);
console.log(cachedData.actors[i]);
}
}
for(var o=0;o<cachedData.objects.length;o++) {
try {
newObjects[cachedData.objects[o].id] = cachedData.objects[i];
} catch (err) {
console.log(err);
console.log(cachedData.objects[i]);
}
}
cachedData.actors = newActors;
cachedData.objects = newObjects;
}
updateFromLearningLocker(req,res,filter,cachedData,dbConnect);
});
}
function updateFromLearningLocker(req,res,filter,cachedData,dbConnect) {
console.log('updating from learning locker');
var activity = filter.activity;
var verb = filter.verb || null;
var since = filter.since || null;
var until = filter.until || null;
var related_activities = filter.related_activities || true;
var format = filter.format;
var collection = "StatsCache";
var key;
var since;
resolved = false;
promises = [];
if (cachedData) {
since = cachedData.lastUpdate;
key = cachedData._id;
console.log("Found cached data (" + key + "), updating since to " + since);
}
//Set the new date of cache to be now
const d = new Date();
var lastUpdate = d.toISOString();
//Do the query
getStatements(activity, verb, since, until, related_activities).then((objects) => {
console.log("Filter since = " + filter.since);
promises.push(new Promise((resolve,reject) => {
resolve(processActivityDataObjects(objects,activity,related_activities));
}));
var resolve = setInterval(() => {
if (resolved == true) {
clearInterval(resolve);
Promise.all(promises).then((values) =>{
if (Array.isArray(values) && values.length === 1 && typeof values[0] === 'object' && Object.keys(values[0]).length === 0) {
values = undefined;
}
if (!values && cachedData) {
values = [cachedData];
} else if (!values) {
return values;
} else if (cachedData) {
values.splice(0,0,cachedData);
}
var output = {};
output.activity = activity;
output.verb = verb;
output.since = filter.since;
output.until = until;
output.related_activities = related_activities;
output.lastUpdate = lastUpdate;
console.log("All promises returned");
output.actors = removeKeyURIs(calculateSessionTimes(getNestedActors(values)));
output.objects = removeKeyURIs(getNestedObjects(values));
console.log("done processing");
//Put/update <output> into cache database!
if (key) {
console.log("updating cache");
dbConnect
.collection(collection)
.updateOne({_id:new ObjectId(key)},{ $set: output },{upsert: true});
} else {
console.log("no cache, storing");
dbConnect
.collection(collection)
.insertOne(output, {check_keys: false});
}
//console.log(JSON.stringify(output, null, 2));
processReturn(req,res,filter,output,makeActivityDataCSVOutput(output));
});
}
},100);
});
}
function removeKeyURIs(objects) {
var output = [];
for (const key in objects) {
output.push(objects[key]);
}
console.log("done remove keys");
return output;
}
/*
* getQuestionSummaryData
*
* Get anonymous aggregated data about interactions with a single question.
* You call this API with an activity ID which represents a question which can be marked with the answered verb in XAPI.
* In simple terms the returned data will be in the form of the question object and how many people answered with each option.
* In the JSON format you also get how many completions and how many were successful as well as a defintion of the object itself.
*
* SPECIFIC API function.
* Example API call = http://localhost:3080/api/questionSummary?activity=activity=https://learning.theodi.org/xapi/activities/mit-moral-machine-test%23/id/630f81656b4097008b2afd6f_branching_0
*/
exports.getQuestionSummaryData = function(req, res, dbo) {
resolved = false;
promises = [];
var filter = req.query;
if (!filter.activity) {
res.statusMessage = "You need to define an activity e.g. http://url.com/?activity=http://....";
res.status(400).end();
res.send();
return;
}
var activity = filter.activity;
var verb = "http://adlnet.gov/expapi/verbs/answered";
var since = filter.since || null;
var until = filter.until || null;
var related_activities = filter.related_activities || false;
var format = filter.format;
getStatements(activity, verb, since, until, false).then((objects) => {
if (!objects) {
res.statusMessage = "Internal server error";
res.status(500).end();
res.send();
return;
}
promises.push(new Promise((resolve,reject) => {
resolve(processQuestionDataObjects(objects,activity,related_activities));
}));
var resolve = setInterval(() => {
if (resolved == true) {
clearInterval(resolve);
Promise.all(promises).then((values) =>{
var output = {};
console.log("All promises returned");
output = combineQuestionSummaryResults(values);
output.object = values[0].object;
console.log("done processing");
processReturn(req,res,filter,output,makeQuestionDataCSVOutput(output));
});
}
},100);
});
}