This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglobal_analytics.js
64 lines (51 loc) · 1.55 KB
/
global_analytics.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
const _ = require("lodash");
const EventEmitter = require("events").EventEmitter;
const timeline = [];
const _emitter = new EventEmitter();
module.exports = {
// Push a global (i.e. not testrun related) analytics event to the timeline.
// This will cause any reporters that listen to global analytics events to
// receive the event. A markers list will be started with the current time.
//
// NOTE: name must be unique if non-colliding markers are desired.
push: (eventName, metadata, startMarkerName) => {
startMarkerName = startMarkerName ? startMarkerName : "start";
const ev = {
type: "analytics-event",
data: {
name: eventName,
markers: [{
name: startMarkerName,
t: Date.now()
}],
metadata
}
};
_emitter.emit("message", ev);
timeline.push(ev);
},
// Mark an event's timeline with a marker named markerName at current time.
// Default to a marker name of "end" if one isn't supplied.
mark: (eventName, markerName) => {
markerName = markerName ? markerName : "end";
const ev = {
type: "analytics-event-mark",
eventName,
data: {
name: markerName,
t: Date.now()
}
};
_emitter.emit("message", ev);
timeline.push(ev);
},
// Return a copy of the existing timeline so that late-arriving reporters
// can synchronize to global analytics messages that emitted before they
// were initialized.
sync: () => {
return _.cloneDeep(timeline);
},
getEmitter: () => {
return _emitter;
}
};