koishi-plugin-chat-analyse 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Collector.d.ts +7 -0
- package/lib/index.js +33 -6
- package/package.json +1 -1
package/lib/Collector.d.ts
CHANGED
|
@@ -18,6 +18,11 @@ declare module 'koishi' {
|
|
|
18
18
|
analyse_msg: {
|
|
19
19
|
uid: number;
|
|
20
20
|
type: string;
|
|
21
|
+
count: number;
|
|
22
|
+
timestamp: Date;
|
|
23
|
+
};
|
|
24
|
+
analyse_rank: {
|
|
25
|
+
uid: number;
|
|
21
26
|
hour: Date;
|
|
22
27
|
count: number;
|
|
23
28
|
timestamp: Date;
|
|
@@ -29,6 +34,7 @@ declare module 'koishi' {
|
|
|
29
34
|
timestamp: Date;
|
|
30
35
|
};
|
|
31
36
|
analyse_at: {
|
|
37
|
+
id: number;
|
|
32
38
|
uid: number;
|
|
33
39
|
target: string;
|
|
34
40
|
content: string;
|
|
@@ -48,6 +54,7 @@ export declare class Collector {
|
|
|
48
54
|
/** @const {number} BUFFER_THRESHOLD - 内存缓存区触发自动刷新的消息数量阈值。 */
|
|
49
55
|
private static readonly BUFFER_THRESHOLD;
|
|
50
56
|
private msgStatBuffer;
|
|
57
|
+
private rankStatBuffer;
|
|
51
58
|
private cmdStatBuffer;
|
|
52
59
|
private oriCacheBuffer;
|
|
53
60
|
private whoAtBuffer;
|
package/lib/index.js
CHANGED
|
@@ -57,6 +57,7 @@ var Collector = class _Collector {
|
|
|
57
57
|
static BUFFER_THRESHOLD = 100;
|
|
58
58
|
// 统一的缓冲区
|
|
59
59
|
msgStatBuffer = /* @__PURE__ */ new Map();
|
|
60
|
+
rankStatBuffer = /* @__PURE__ */ new Map();
|
|
60
61
|
cmdStatBuffer = /* @__PURE__ */ new Map();
|
|
61
62
|
oriCacheBuffer = [];
|
|
62
63
|
whoAtBuffer = [];
|
|
@@ -86,10 +87,15 @@ var Collector = class _Collector {
|
|
|
86
87
|
this.ctx.model.extend("analyse_msg", {
|
|
87
88
|
uid: "unsigned",
|
|
88
89
|
type: "string",
|
|
90
|
+
count: "unsigned",
|
|
91
|
+
timestamp: "timestamp"
|
|
92
|
+
}, { primary: ["uid", "type"] });
|
|
93
|
+
this.ctx.model.extend("analyse_rank", {
|
|
94
|
+
uid: "unsigned",
|
|
89
95
|
hour: "timestamp",
|
|
90
96
|
count: "unsigned",
|
|
91
97
|
timestamp: "timestamp"
|
|
92
|
-
}, { primary: ["uid", "
|
|
98
|
+
}, { primary: ["uid", "hour"] });
|
|
93
99
|
if (this.config.enableOriRecord) {
|
|
94
100
|
this.ctx.model.extend("analyse_cache", {
|
|
95
101
|
id: "unsigned",
|
|
@@ -100,11 +106,12 @@ var Collector = class _Collector {
|
|
|
100
106
|
}
|
|
101
107
|
if (this.config.enableWhoAt) {
|
|
102
108
|
this.ctx.model.extend("analyse_at", {
|
|
109
|
+
id: "unsigned",
|
|
103
110
|
uid: "unsigned",
|
|
104
111
|
target: "string",
|
|
105
112
|
content: "text",
|
|
106
113
|
timestamp: "timestamp"
|
|
107
|
-
}, { indexes: ["target", "uid"] });
|
|
114
|
+
}, { primary: "id", autoInc: true, indexes: ["target", "uid"] });
|
|
108
115
|
}
|
|
109
116
|
}
|
|
110
117
|
/**
|
|
@@ -134,15 +141,23 @@ var Collector = class _Collector {
|
|
|
134
141
|
}
|
|
135
142
|
}
|
|
136
143
|
const hourStart = new Date(messageTime.getFullYear(), messageTime.getMonth(), messageTime.getDate(), messageTime.getHours());
|
|
144
|
+
const rankKey = `${uid}:${hourStart.toISOString()}`;
|
|
145
|
+
const existingRank = this.rankStatBuffer.get(rankKey);
|
|
146
|
+
if (existingRank) {
|
|
147
|
+
existingRank.count++;
|
|
148
|
+
existingRank.timestamp = messageTime;
|
|
149
|
+
} else {
|
|
150
|
+
this.rankStatBuffer.set(rankKey, { uid, hour: hourStart, count: 1, timestamp: messageTime });
|
|
151
|
+
}
|
|
137
152
|
const uniqueElementTypes = new Set(elements.map((e) => e.type));
|
|
138
153
|
for (const type of uniqueElementTypes) {
|
|
139
|
-
const key = `${uid}:${type}
|
|
154
|
+
const key = `${uid}:${type}`;
|
|
140
155
|
const existing = this.msgStatBuffer.get(key);
|
|
141
156
|
if (existing) {
|
|
142
157
|
existing.count++;
|
|
143
158
|
existing.timestamp = messageTime;
|
|
144
159
|
} else {
|
|
145
|
-
this.msgStatBuffer.set(key, { uid, type,
|
|
160
|
+
this.msgStatBuffer.set(key, { uid, type, count: 1, timestamp: messageTime });
|
|
146
161
|
}
|
|
147
162
|
}
|
|
148
163
|
if (this.config.enableWhoAt) {
|
|
@@ -244,10 +259,12 @@ var Collector = class _Collector {
|
|
|
244
259
|
async flushBuffers() {
|
|
245
260
|
const cmdBufferToFlush = Array.from(this.cmdStatBuffer.values());
|
|
246
261
|
const msgBufferToFlush = Array.from(this.msgStatBuffer.values());
|
|
262
|
+
const rankBufferToFlush = Array.from(this.rankStatBuffer.values());
|
|
247
263
|
const oriCacheBufferToFlush = this.oriCacheBuffer;
|
|
248
264
|
const whoAtBufferToFlush = this.whoAtBuffer;
|
|
249
265
|
this.cmdStatBuffer.clear();
|
|
250
266
|
this.msgStatBuffer.clear();
|
|
267
|
+
this.rankStatBuffer.clear();
|
|
251
268
|
this.oriCacheBuffer = [];
|
|
252
269
|
this.whoAtBuffer = [];
|
|
253
270
|
try {
|
|
@@ -268,6 +285,16 @@ var Collector = class _Collector {
|
|
|
268
285
|
(row) => msgBufferToFlush.map((item) => ({
|
|
269
286
|
uid: item.uid,
|
|
270
287
|
type: item.type,
|
|
288
|
+
count: import_koishi.$.add(import_koishi.$.ifNull(row.count, 0), item.count),
|
|
289
|
+
timestamp: item.timestamp
|
|
290
|
+
}))
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
if (rankBufferToFlush.length > 0) {
|
|
294
|
+
await this.ctx.database.upsert(
|
|
295
|
+
"analyse_rank",
|
|
296
|
+
(row) => rankBufferToFlush.map((item) => ({
|
|
297
|
+
uid: item.uid,
|
|
271
298
|
hour: item.hour,
|
|
272
299
|
count: import_koishi.$.add(import_koishi.$.ifNull(row.count, 0), item.count),
|
|
273
300
|
timestamp: item.timestamp
|
|
@@ -753,13 +780,13 @@ var Stat = class {
|
|
|
753
780
|
if (usersInGuild.length === 0) return "暂无统计数据";
|
|
754
781
|
const uids = usersInGuild.map((u) => u.uid);
|
|
755
782
|
const userNameMap = new Map(usersInGuild.map((u) => [u.uid, u.userName]));
|
|
756
|
-
const stats = await this.ctx.database.select("
|
|
783
|
+
const stats = await this.ctx.database.select("analyse_rank").where({ uid: { $in: uids }, hour: { $gte: since } }).groupBy("uid", { count: /* @__PURE__ */ __name((row) => import_koishi3.$.sum(row.count), "count") }).orderBy("count", "desc").limit(100).execute();
|
|
757
784
|
if (stats.length === 0) return "暂无统计数据";
|
|
758
785
|
const total = stats.reduce((sum, record) => sum + record.count, 0);
|
|
759
786
|
const list = stats.map((item) => [userNameMap.get(item.uid) || `UID ${item.uid}`, item.count]);
|
|
760
787
|
return { list, total };
|
|
761
788
|
} else {
|
|
762
|
-
const msgStats = await this.ctx.database.select("
|
|
789
|
+
const msgStats = await this.ctx.database.select("analyse_rank").where({ hour: { $gte: since } }).project(["uid", "count"]).execute();
|
|
763
790
|
if (msgStats.length === 0) return "暂无统计数据";
|
|
764
791
|
const allUsers = await this.ctx.database.get("analyse_user", {}, ["uid", "userId", "userName"]);
|
|
765
792
|
const uidToUserMap = new Map(allUsers.map((u) => [u.uid, { userId: u.userId, userName: u.userName }]));
|