-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.js
64 lines (56 loc) · 2.31 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
/**
* Records metrics about avoided clicks and mouse actions.
* @param {{clicks: number, drags: number, keyStrokes: number}} metrics - The metrics to record.
*/
let metricsQueue = [];
let isProcessingQueue = false;
function recordMetrics(metrics) {
let today = new Date().toISOString().split('T')[0]; // Get today's date in YYYY-MM-DD format
metrics.date = today; // Add the date to the metrics
metricsQueue.push(metrics);
if (!isProcessingQueue) {
processQueue();
}
}
function processQueue() {
if (metricsQueue.length === 0) {
isProcessingQueue = false;
return;
}
isProcessingQueue = true;
let metrics = metricsQueue.shift();
let date = metrics.date;
delete metrics.date; // Remove the date from the metrics
let key = 'metrics-' + date;
chrome.storage.local.get(key, function(result) {
// If the metrics for the date are not defined, default to { clicks: 0, drags: 0, keyStrokes: 0 }
let dailyMetrics = result[key] || { clicks: 0, drags: 0, keyStrokes: 0 };
// Add the new metrics to the existing ones
dailyMetrics.clicks += metrics.clicks || 0;
dailyMetrics.drags += metrics.drags || 0;
dailyMetrics.keyStrokes += metrics.keyStrokes || 0;
let updatedMetrics = {};
updatedMetrics[key] = dailyMetrics;
chrome.storage.local.set(updatedMetrics, processQueue);
console.log('Metrics updated for ' + date + ':', dailyMetrics);
});
// Remove data older than 1 year
let oneYearAgo = new Date();
oneYearAgo.setFullYear(oneYearAgo.getFullYear() - 1);
let oneYearAgoStr = 'metrics-' + oneYearAgo.toISOString().split('T')[0];
chrome.storage.local.get(null, function(items) {
for (let key in items) {
if (key.startsWith('metrics-') && key < oneYearAgoStr) {
chrome.storage.local.remove(key);
}
}
});
// Update global metrics
chrome.storage.local.get(['globalMetrics'], function(result) {
let globalMetrics = result.globalMetrics || { clicks: 0, drags: 0, keyStrokes: 0 };
globalMetrics.clicks += metrics.clicks || 0;
globalMetrics.drags += metrics.drags || 0;
globalMetrics.keyStrokes += metrics.keyStrokes || 0;
chrome.storage.local.set({ globalMetrics: globalMetrics });
});
}