-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
457 lines (413 loc) · 13.4 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
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
/**
* This script aims to generate a stream of actions in the xAPI or ActivityStreams formats
* and feed it to Elasticsearch for Kibana visualisation.
*
* This script is used on the LASI 2016 workshop in Bilbao to demonstrate basics of
* interactive data analysis with Elasticsearch and Kibana
*
* Created by Andrii Vozniuk on 24.06.2016
*
* MIT License
*/
'use strict'
/**
* Set up key parameters
*/
var esHostUrl = 'https://admin:[email protected]:9243';
var usersCount = 200;
var objectsCount = 500;
var locationsCount = 20;
var locationsPerUser = 4;
var verbsPerUser = 7;
var objectsPerUser = 7;
var usersPerUser = 5;
var maxWaitBetweenActions = 1000;
/**
* Require packages
*/
var _ = require('lodash');
var winston = require('winston');
var elasticsearch = require('elasticsearch');
var Chance = require('chance');
var chance = new Chance();
/**
* Set up sets of values to randomly select from
*/
winston.info('Starting generation of random users, locations, verbs and objects');
// xAPI verbs copied from:
// https://github.com/adlnet/xAPIVerbs/blob/master/verbs.js
var xAPIVerbsObj = {
"abandoned" : {
"id" : "https://w3id.org/xapi/adl/verbs/abandoned",
"display" : {"en-US" : "abandoned",
"fr-FR" : "a abandonné"}
},
"answered" : {
"id" : "http://adlnet.gov/expapi/verbs/answered",
"display" : {"de-DE" : "beantwortete",
"en-US" : "answered",
"fr-FR" : "a répondu",
"es-ES" : "contestó"}
},
"asked" : {
"id" : "http://adlnet.gov/expapi/verbs/asked",
"display" : {"de-DE" : "fragte",
"en-US" : "asked",
"fr-FR" : "a demandé",
"es-ES" : "preguntó"}
},
"attempted" : {
"id" : "http://adlnet.gov/expapi/verbs/attempted",
"display" : {"de-DE" : "versuchte",
"en-US" : "attempted",
"fr-FR" : "a essayé",
"es-ES" : "intentó"}
},
"attended" : {
"id" : "http://adlnet.gov/expapi/verbs/attended",
"display" : {"de-DE" : "nahm teil an",
"en-US" : "attended",
"fr-FR" : "a suivi",
"es-ES" : "asistió"}
},
"commented" : {
"id" : "http://adlnet.gov/expapi/verbs/commented",
"display" : {"de-DE" : "kommentierte",
"en-US" : "commented",
"fr-FR" : "a commenté",
"es-ES" : "comentó"}
},
"completed" : {
"id" : "http://adlnet.gov/expapi/verbs/completed",
"display" : {"de-DE" : "beendete",
"en-US" : "completed",
"fr-FR" : "a terminé",
"es-ES" : "completó"}
},
"exited" : {
"id" : "http://adlnet.gov/expapi/verbs/exited",
"display" : {"de-DE" : "verließ",
"en-US" : "exited",
"fr-FR" : "a quitté",
"es-ES" : "salió"}
},
"experienced" : {
"id" : "http://adlnet.gov/expapi/verbs/experienced",
"display" : {"de-DE" : "erlebte",
"en-US" : "experienced",
"fr-FR" : "a éprouvé",
"es-ES" : "experimentó"}
},
"failed" : {
"id" : "http://adlnet.gov/expapi/verbs/failed",
"display" : {"de-DE" : "verfehlte",
"en-US" : "failed",
"fr-FR" : "a échoué",
"es-ES" : "fracasó"}
},
"imported" : {
"id" : "http://adlnet.gov/expapi/verbs/imported",
"display" : {"de-DE" : "importierte",
"en-US" : "imported",
"fr-FR" : "a importé",
"es-ES" : "importó"}
},
"initialized" : {
"id" : "http://adlnet.gov/expapi/verbs/initialized",
"display" : {"de-DE" : "initialisierte",
"en-US" : "initialized",
"fr-FR" : "a initialisé",
"es-ES" : "inicializó"}
},
"interacted" : {
"id" : "http://adlnet.gov/expapi/verbs/interacted",
"display" : {"de-DE" : "interagierte",
"en-US" : "interacted",
"fr-FR" : "a interagi",
"es-ES" : "interactuó"}
},
"launched" : {
"id" : "http://adlnet.gov/expapi/verbs/launched",
"display" : {"de-DE" : "startete",
"en-US" : "launched",
"fr-FR" : "a lancé",
"es-ES" : "lanzó"}
},
"mastered" : {
"id" : "http://adlnet.gov/expapi/verbs/mastered",
"display" : {"de-DE" : "meisterte",
"en-US" : "mastered",
"fr-FR" : "a maîtrisé",
"es-ES" : "dominó"}
},
"passed" : {
"id" : "http://adlnet.gov/expapi/verbs/passed",
"display" : {"de-DE" : "bestand",
"en-US" : "passed",
"fr-FR" : "a réussi",
"es-ES" : "aprobó"}
},
"preferred" : {
"id" : "http://adlnet.gov/expapi/verbs/preferred",
"display" : {"de-DE" : "bevorzugte",
"en-US" : "preferred",
"fr-FR" : "a préféré",
"es-ES" : "prefirió"}
},
"progressed" : {
"id" : "http://adlnet.gov/expapi/verbs/progressed",
"display" : {"de-DE" : "machte Fortschritt mit",
"en-US" : "progressed",
"fr-FR" : "a progressé",
"es-ES" : "progresó"}
},
"registered" : {
"id" : "http://adlnet.gov/expapi/verbs/registered",
"display" : {"de-DE" : "registrierte",
"en-US" : "registered",
"fr-FR" : "a enregistré",
"es-ES" : "registró"}
},
"responded" : {
"id" : "http://adlnet.gov/expapi/verbs/responded",
"display" : {"de-DE" : "reagierte",
"en-US" : "responded",
"fr-FR" : "a répondu",
"es-ES" : "respondió"}
},
"resumed" : {
"id" : "http://adlnet.gov/expapi/verbs/resumed",
"display" : {"de-DE" : "setzte fort",
"en-US" : "resumed",
"fr-FR" : "a repris",
"es-ES" : "continuó"}
},
"satisfied" : {
"id" : "https://w3id.org/xapi/adl/verbs/satisfied",
"display" : {"en-US" : "satisfied"}
},
"scored" : {
"id" : "http://adlnet.gov/expapi/verbs/scored",
"display" : {"de-DE" : "erreichte",
"en-US" : "scored",
"fr-FR" : "a marqué",
"es-ES" : "anotó"}
},
"shared" : {
"id" : "http://adlnet.gov/expapi/verbs/shared",
"display" : {"de-DE" : "teilte",
"en-US" : "shared",
"fr-FR" : "a partagé",
"es-ES" : "compartió"}
},
"suspended" : {
"id" : "http://adlnet.gov/expapi/verbs/suspended",
"display" : {"de-DE" : "pausierte",
"en-US" : "suspended",
"fr-FR" : "a suspendu",
"es-ES" : "aplazó"}
},
"terminated" : {
"id" : "http://adlnet.gov/expapi/verbs/terminated",
"display" : {"de-DE" : "beendete",
"en-US" : "terminated",
"fr-FR" : "a terminé",
"es-ES" : "terminó"}
},
"voided" : {
"id" : "http://adlnet.gov/expapi/verbs/voided",
"display" : {"de-DE" : "entwertete",
"en-US" : "voided",
"fr-FR" : "a annulé",
"es-ES" : "anuló"}
},
"waived" : {
"id" : "https://w3id.org/xapi/adl/verbs/waived",
"display" : {"en-US" : "waived"}
},
// Two verbs made up by me to demonstrate user-user interaction analysis
"messaged" : {
"id" : "https://vozniuk.com/xapi/adl/verbs/messaged",
"display" : {"en-US" : "messaged"}
},
// "invited" : {
// "id" : "https://vozniuk.com/xapi/adl/verbs/invited",
// "display" : {"en-US" : "invited"}
// },
"mentioned" : {
"id" : "https://vozniuk.com/xapi/adl/verbs/mentioned",
"display" : {"en-US" : "mentioned"}
}
};
var xAPIVerbsArray = _.keys(xAPIVerbsObj); // we need an array for predefined order
// Build an array of realistic locations and IPs
var locations = [];
for (var i = 0; i < locationsCount; i++) {
var newLocation = {
ipAddress : chance.ip(),
location : {
lon: chance.longitude(),
lat: chance.latitude()
},
city : chance.city(),
countryCode : chance.country(),
countryName : chance.country({ full: true })
}
locations.push(newLocation);
}
// Generate an array of objects (maybe using existing items as a basis)
var objects = [];
for (var i = 0; i < objectsCount; i++) {
var newObj = {
id: chance.hash(),
definition: {
name: {
'en-US': chance.file()
},
description: {
'en-US': chance.paragraph()
}
},
objectType: "Media"
}
objects.push(newObj);
}
// Generate an array of users
var users = [];
for (var i = 0; i < usersCount; i++) {
var newUser = {
id: chance.hash(),
name: chance.name(),
mbox: chance.email(),
locations: [],
verbs: [],
objects: [],
users: []
}
// We skew the random distributions below by doing Math.pow()
// to make the data more "interesting" for exploration
// Assign to each user locations (with IPs) she would typically interact from
for (var j = 0; j < locationsPerUser; j++) {
var rndLocationNmb = Math.floor(Math.pow(Math.random(), 2)*locations.length);
newUser.locations.push(rndLocationNmb);
}
// Assign to each user types of interaction she would typically do
for (var j = 0; j < verbsPerUser; j++) {
var rndVerbNmb = Math.floor(Math.pow(Math.random(), 2)*xAPIVerbsArray.length);
newUser.verbs.push(rndVerbNmb);
}
// Assign to each user objects she would typically interact with
for (var j = 0; j < objectsPerUser; j++) {
var rndObjNmb = Math.floor((1-Math.pow(Math.random(), 2))*objects.length);
newUser.objects.push(rndObjNmb);
}
// Assign to each user other users she interacts with
for (var j = 0; j < usersPerUser; j++) {
var rndObjNmb = Math.floor((1-Math.pow(Math.random(), 2))*users.length);
newUser.users.push(rndObjNmb);
}
users.push(newUser);
}
// At this point we have users, verbs, objects and locations for the context
/**
* Randomly generate event
*/
// A sample xAPI statement
// {
// "actor": {
// "mbox": "mailto:[email protected]",
// "name": "Example Learner",
// "objectType": "Agent"
// },
// "verb": {
// "id": "http://adlnet.gov/expapi/verbs/answered",
// "display": {
// "en-US": "answered"
// }
// },
// "object": {
// "id": "http://adlnet.gov/expapi/activities/example",
// "definition": {
// "name": {
// "en-US": "Example Activity"
// },
// "description": {
// "en-US": "Example activity description"
// }
// },
// "objectType": "Activity"
// }
// }
var getGeneratedAction = function () {
// Pick a random user
var userNmb = Math.floor(Math.pow(Math.random(), 2)*users.length);
var curActorObj = users[userNmb];
// Pick a random verb of user verbs
var verbNmb = Math.floor(Math.pow(Math.random(), 2)*curActorObj.verbs.length);
var curVerb = xAPIVerbsArray[curActorObj.verbs[verbNmb]];
var curVerbObj = xAPIVerbsObj[curVerb];
// Pick a random object or user objects
if (curVerb === 'messaged' || curVerb === 'mentioned') {
var objNmb = Math.floor(Math.pow(Math.random(), 2)*curActorObj.users.length);
var curUserObj = users[curActorObj.users[objNmb]]
var curObject = {
id: curUserObj.id,
name: curUserObj.name,
objectType: 'User'
};
} else {
var objNmb = Math.floor(Math.pow(Math.random(), 2)*curActorObj.objects.length);
var curObject = objects[curActorObj.objects[objNmb]];
}
// Pick a random context (ip location etc)
var locNmb = Math.floor(Math.pow(Math.random(), 2)*curActorObj.locations.length);
var curContext = locations[curActorObj.locations[locNmb]];
var actionJson = {
timestamp: new Date(),
actor: {
id: curActorObj.id,
name: curActorObj.name,
mbox: curActorObj.mbox
},
verb: curVerbObj,
object: curObject,
context: curContext
}
return actionJson;
}
/**
* Establish an Elasticsearch connection
*/
var generateAndSendActions = function () {
var actionJson = getGeneratedAction();
winston.info('Putting object into Elasticsearch: ', JSON.stringify(actionJson));
esClient.create({
index: 'actions',
type: 'action',
body: actionJson
}).then(function (resp) {
winston.info('Object was successfully put into Elasticsearch with response: ', resp);
// schedule the next action
setTimeout(generateAndSendActions, Math.random()*maxWaitBetweenActions);
}, function (err) {
return winston.error('An error occurred when creating an action in Elasticsearch: ', err);
});
}
var esClient = new elasticsearch.Client({
host: esHostUrl
});
// TODO: Use async series for the steps
// ping the client to see if it is up
esClient.ping({
requestTimeout: 10000,
// undocumented params are appended to the query string
hello: 'elasticsearch!'
}, function (err) {
if (err) {
return winston.error('Elasticsearch connection cannot be established. ' + err);
}
winston.info('Elasticsearch connection to ' + esHostUrl +
' was established successfully');
generateAndSendActions();
});