forked from mozilla/testpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.js
129 lines (102 loc) · 3.5 KB
/
metrics.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
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the 'License'). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
/* global TelemetryController */
const {Cu} = require('chrome');
Cu.import('resource://gre/modules/TelemetryController.jsm');
const { setTimeout } = require('sdk/timers');
const Events = require('sdk/system/events');
const store = require('sdk/simple-storage').storage;
const PrefsService = require('sdk/preferences/service');
// Event type for receiving pings from experiments
const EVENT_SEND_METRIC = 'testpilot::send-metric';
// Minimum interval of time between main Telemetry pings in ms
const PING_INTERVAL = 24 * 60 * 60 * 1000;
// Interval between Telemetry ping time checks, more frequent than the actual
// ping so we can play catch-up if the computer was asleep at the ping time.
const PING_CHECK_INTERVAL = 10 * 60 * 1000;
// List of preferences we'll override on install & restore on uninstall
const PREFERENCE_OVERRIDES = {
'toolkit.telemetry.enabled': true,
'datareporting.healthreport.uploadEnabled': true
};
module.exports = {
init: function() {
if (!store.telemetryPing) {
store.lastTelemetryPingTimestamp = Date.now();
store.telemetryPingPayload = {
version: 1,
tests: {}
};
}
this.maybePingTelemetry();
Events.on(EVENT_SEND_METRIC, this.onExperimentPing);
},
onEnable: function() {
// Backup existing preference settings and then override.
store.metricsPrefsBackup = {};
Object.keys(PREFERENCE_OVERRIDES).forEach(name => {
store.metricsPrefsBackup[name] = PrefsService.get(name);
PrefsService.set(name, PREFERENCE_OVERRIDES[name]);
});
},
onDisable: function() {
// Restore previous preference settings before override.
if (store.metricsPrefsBackup) {
Object.keys(PREFERENCE_OVERRIDES).forEach(name => {
PrefsService.set(name, store.metricsPrefsBackup[name]);
});
}
},
destroy: function() {
Events.off(EVENT_SEND_METRIC, this.onExperimentPing);
},
maybePingTelemetry: function() {
const now = Date.now();
const elapsed = now - store.lastTelemetryPingTimestamp;
if (elapsed > PING_INTERVAL) {
store.lastTelemetryPingTimestamp = now;
TelemetryController.submitExternalPing(
'testpilot',
store.telemetryPingPayload,
{ addClientId: true, addEnvironment: true }
);
}
this.pingTimer = setTimeout(
() => this.maybePingTelemetry(),
PING_CHECK_INTERVAL
);
},
updateExperiment: function(addonId, data) {
store.telemetryPingPayload.tests[addonId] = Object.assign(
store.telemetryPingPayload.tests[addonId] || {features: {}},
data
);
},
experimentEnabled: function(addonId) {
this.updateExperiment(addonId, {last_enabled: Date.now()});
},
experimentDisabled: function(addonId) {
this.updateExperiment(addonId, {last_disabled: Date.now()});
},
experimentFeaturesChanged: function(addonId, features) {
this.updateExperiment(addonId, {features: features});
},
onExperimentPing: function(ev) {
const { subject, data } = ev;
const dataParsed = JSON.parse(data);
// TODO: Map add-on ID (subject) to other pingTypes as necessary
const pingType = 'testpilottest';
const payload = {
version: 1,
test: subject,
payload: dataParsed
};
TelemetryController.submitExternalPing(
pingType, payload,
{ addClientId: true, addEnvironment: true }
);
}
};