-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatsRecorderModule.js
More file actions
137 lines (117 loc) · 3.35 KB
/
statsRecorderModule.js
File metadata and controls
137 lines (117 loc) · 3.35 KB
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
/**
* 统计记录模块 - 内容脚本版本
* 供 content.js 直接使用
*/
// ============== statsStorage.js ==============
class StatsStorage {
static STORAGE_KEY = 'linuxDoStats';
static DATA_VERSION = 1;
static RETENTION_DAYS = 30;
static async get() {
return new Promise((resolve) => {
if (!chrome?.storage?.local) {
resolve(this._createEmpty());
return;
}
try {
chrome.storage.local.get(this.STORAGE_KEY, (result) => {
const lastError = chrome.runtime?.lastError;
if (lastError) {
resolve(this._createEmpty());
return;
}
const data = result[this.STORAGE_KEY] || this._createEmpty();
resolve(data);
});
} catch (error) {
resolve(this._createEmpty());
}
});
}
static async set(data) {
return new Promise((resolve) => {
if (!chrome?.storage?.local) {
resolve();
return;
}
try {
chrome.storage.local.set({ [this.STORAGE_KEY]: data }, () => {
resolve();
});
} catch (error) {
resolve();
}
});
}
static _createEmpty() {
return {
version: this.DATA_VERSION,
days: {},
lastPruneAt: Date.now()
};
}
static async record(event) {
const data = await this.get();
const now = new Date();
const dateKey = this._getDateKey(now);
const hourKey = now.getHours().toString();
if (!data.days[dateKey]) {
data.days[dateKey] = {
totals: { posts: 0, durationMs: 0, errors: 0 },
hours: {},
updatedAt: now.getTime()
};
}
const day = data.days[dateKey];
if (!day.hours[hourKey]) {
day.hours[hourKey] = { posts: 0, durationMs: 0, errors: 0 };
}
day.hours[hourKey].posts += event.posts;
day.hours[hourKey].durationMs += event.durationMs;
if (event.hasError) {
day.hours[hourKey].errors += 1;
}
day.totals.posts += event.posts;
day.totals.durationMs += event.durationMs;
if (event.hasError) {
day.totals.errors += 1;
}
day.updatedAt = now.getTime();
const shouldPrune = !data.lastPruneAt || (now.getTime() - data.lastPruneAt > 86400000);
if (shouldPrune) {
this._pruneOldDays(data);
data.lastPruneAt = now.getTime();
}
await this.set(data);
}
static _pruneOldDays(data) {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - this.RETENTION_DAYS);
const cutoffKey = this._getDateKey(cutoffDate);
for (const dateKey in data.days) {
if (dateKey < cutoffKey) {
delete data.days[dateKey];
}
}
}
static _getDateKey(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
}
// ============== statsRecorder.js ==============
class StatsRecorder {
static async record(options = {}) {
const { posts = 1, durationMs = 0, hasError = false } = options;
await StatsStorage.record({ posts, durationMs, hasError });
}
static async recordPost(durationMs = 0) {
await this.record({ posts: 1, durationMs, hasError: false });
}
static async recordError() {
await this.record({ posts: 0, durationMs: 0, hasError: true });
}
}
window.StatsRecorder = StatsRecorder;