-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.js
204 lines (167 loc) · 4.46 KB
/
process.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
/*
* Processing functions
*/
var request = require('request'),
zlib = require('zlib'),
libfs = require('fs'),
libpath = require('path'),
liburl = require('url'),
config = require('./config.js'),
XAPICollection = require('./xapicollection.js').CollectionSync;
var digest = {};
function intervalRefresh(done)
{
var i = 0;
function genCb(){
if(i<config.refreshOrder.length)
refresh(config.refreshOrder[i++], null, null, genCb);
else if(done)
done();
}
genCb();
}
function refresh(collectId, req,res,next)
{
// create data directory if it doesn't exist
try {
libfs.mkdirSync( libpath.join(__dirname, 'data') );
console.log('Creating data directory');
}
catch(e){}
console.log('Refreshing', collectId);
var collectConfig = config.collections[collectId];
// load cached statements
var json = '';
var cacheId = collectConfig.sharesDataWith ? collectConfig.sharesDataWith : collectId;
var gzCache = libfs.createReadStream( libpath.join(__dirname, 'data', cacheId+'.json.gz') );
gzCache.on('error', function(err){
console.log('Error opening file');
handleFileJsonData();
});
var cache = gzCache.pipe(zlib.createGunzip());
cache.setEncoding('utf8');
cache.on('error', function(){
console.log('Error unzipping file');
handleFileJsonData();
})
.on('data', function(chunk){
json += chunk;
})
.on('end', handleFileJsonData);
var statements = null;
function handleFileJsonData()
{
// parse cached statements if present
try {
statements = JSON.parse(json);
}
catch(e){
console.log('No previous data for '+collectId+', starting fresh.');
statements = [];
}
if( collectConfig.initialQuery )
{
// get timestamp of last known statement
var since;
if( statements.length > 0 )
since = statements[statements.length-1].stored;
// build request options
var qs = {'since': since, 'ascending': true};
for(var i in collectConfig.initialQuery){
qs[i] = collectConfig.initialQuery[i];
}
var options = {
'method': 'GET',
'url': liburl.resolve(config.lrs.endpoint, 'statements'),
'headers': {'X-Experience-API-Version': '1.0.1'},
'qs': qs
};
if(config.lrs.username && config.lrs.password)
options.auth = {'user': config.lrs.username, 'pass': config.lrs.password};
// make the request to the LRS
request(options, requestCb);
}
else {
processStatements();
}
}
function requestCb(err,resp,body)
{
// fail on error
if(err){
console.log('Error retrieving from LRS', err);
processStatements();
}
else {
// parse response, save out statements
var newData = JSON.parse(body);
console.log('Statements retrieved from LRS:', newData.statements.length);
Array.prototype.push.apply(statements, newData.statements);
// get next page of statements if available
if(newData.more){
request.get(
liburl.resolve(config.lrs.endpoint, newData.more),
{'headers': {'X-Experience-API-Version':'1.0.1'}},
requestCb
);
}
else {
saveNewStatements();
}
}
}
function saveNewStatements()
{
// compress and save new statement cache
var output = zlib.createGzip();
output.on('error', function(err){
console.log('Error compressing cache', err);
});
var gzOutput = libfs.createWriteStream( libpath.join(__dirname, 'data', collectId+'.json.gz') );
gzOutput.on('error', function(err){
console.log('Error writing cache to file', err);
});
output.pipe(gzOutput);
output.write(JSON.stringify(statements), 'utf8', function(){
output.end()
processStatements();
});
}
function processStatements()
{
// process statements using the xAPI Collection class
var collection = new XAPICollection(statements);
// loop over commands in config file and apply
var commands = config.collections[collectId].commands;
try
{
if( typeof(commands) === 'function' ){
collection = commands(collection);
}
else {
for( var i=0; i<commands.length; i++ ){
collection = collection[commands[i][0]].apply(collection, commands[i].slice(1));
}
}
digest[collectId] = collection.contents;
}
catch(e){
console.log('Error while processing statements', e);
}
// respond with processed statements and we're done!
if(res)
res.status(200).send();
else if(next)
next();
}
}
function serveResults(collectId, req,res)
{
if(digest[collectId])
res.send(digest[collectId]);
else
res.status(500).send();
}
exports.intervalRefresh = intervalRefresh;
exports.refresh = refresh;
exports.serveResults = serveResults;