forked from kanghj/wordnews_chrome_extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventLogger.js
57 lines (51 loc) · 1.98 KB
/
eventLogger.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
var currentEvent = "";
var eventLogCont = {};
//This function will create a event log
function createEventLog (id, userid, activity, eventName) {
eventLogCont[id] = { user_id: userid,
activity: activity,
time_elapsed: 0,
currentEvent: eventName,
currentStartTime: performance.now(),
detail: {
pattern: []
}
};
}
//A generic function which can be used for
function newEvent (id, eventName) {
console.log("New event: " + eventName);
//Cache the event log
var eventLog = eventLogCont[id];
//Create a new event
var newEventDetail = {};
//Cache new start time
var newCurrentStartTime = performance.now();
//Compute the time elapsed
newEventDetail[eventLog.currentEvent] = newCurrentStartTime - eventLog.currentStartTime;
//Set the new start time
eventLog.currentStartTime = newCurrentStartTime;
eventLog.currentEvent = eventName;
eventLog.detail.pattern.push(newEventDetail);
}
function addDetail (id, detailTitle, detail) {
eventLogCont[id].detail[detailTitle] = detail;
}
function getEventLog(id) {
var eventLog = eventLogCont[id];
var newEventDetail = {};
var newCurrentStartTime = performance.now();
//Compute the time elapsed
newEventDetail[eventLog.currentEvent] = newCurrentStartTime - eventLog.currentStartTime;
eventLog.detail.pattern.push(newEventDetail);
//Remove the unnecessary properties
delete eventLog["currentEvent"];
delete eventLog["currentStartTime"];
//Compute the total time elapsed
for (var i = 0; i < eventLog.detail.pattern.length; ++i) {
for (var key in eventLog.detail.pattern[i]) {
eventLog.time_elapsed += eventLog.detail.pattern[i][key];
}
}
return eventLog;
}