koishi-plugin-chat-analyse 1.6.6 → 1.6.8
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/index.js +36 -21
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -2132,7 +2132,7 @@ var Data = class {
|
|
|
2132
2132
|
this.ctx = ctx;
|
|
2133
2133
|
this.config = config;
|
|
2134
2134
|
this.dataDir = path.join(this.ctx.baseDir, "data", "chat-analyse");
|
|
2135
|
-
if (this.config.enableAutoBackup) this.ctx.cron("0
|
|
2135
|
+
if (this.config.enableAutoBackup) this.ctx.cron("0 2 * * *", () => {
|
|
2136
2136
|
this.backupCache();
|
|
2137
2137
|
});
|
|
2138
2138
|
}
|
|
@@ -2148,16 +2148,21 @@ var Data = class {
|
|
|
2148
2148
|
async backupCache() {
|
|
2149
2149
|
try {
|
|
2150
2150
|
await fs.mkdir(this.dataDir, { recursive: true });
|
|
2151
|
-
const now = /* @__PURE__ */ new Date();
|
|
2152
|
-
const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
|
|
2153
|
-
const year = lastMonth.getFullYear();
|
|
2154
|
-
const month = (lastMonth.getMonth() + 1).toString().padStart(2, "0");
|
|
2155
|
-
const filename = `analyse_cache_${year}-${month}.json`;
|
|
2156
|
-
const filepath = path.join(this.dataDir, filename);
|
|
2157
2151
|
const allUsers = await this.ctx.database.get("analyse_user", {});
|
|
2158
2152
|
if (allUsers.length === 0) return;
|
|
2159
2153
|
const uidToUserInfoMap = new Map(allUsers.map((u) => [u.uid, u]));
|
|
2160
|
-
const
|
|
2154
|
+
const targetDate = /* @__PURE__ */ new Date();
|
|
2155
|
+
targetDate.setMonth(targetDate.getMonth() - 1);
|
|
2156
|
+
const year = targetDate.getFullYear();
|
|
2157
|
+
const monthIndex = targetDate.getMonth();
|
|
2158
|
+
const day = targetDate.getDate();
|
|
2159
|
+
const monthString = (monthIndex + 1).toString().padStart(2, "0");
|
|
2160
|
+
const dayString = day.toString().padStart(2, "0");
|
|
2161
|
+
const filename = `analyse_cache_${year}-${monthString}-${dayString}.json`;
|
|
2162
|
+
const filepath = path.join(this.dataDir, filename);
|
|
2163
|
+
const startDate = new Date(year, monthIndex, day);
|
|
2164
|
+
const endDate = new Date(year, monthIndex, day + 1);
|
|
2165
|
+
const records = await this.ctx.database.get("analyse_cache", { timestamp: { $gte: startDate, $lt: endDate } });
|
|
2161
2166
|
if (records.length === 0) return;
|
|
2162
2167
|
const dataToExport = records.map((record) => {
|
|
2163
2168
|
const userInfo = uidToUserInfoMap.get(record.uid);
|
|
@@ -2165,7 +2170,7 @@ var Data = class {
|
|
|
2165
2170
|
const { id, uid, ...restOfRecord } = record;
|
|
2166
2171
|
return { userId: userInfo.userId, channelId: userInfo.channelId, ...restOfRecord };
|
|
2167
2172
|
}).filter(Boolean);
|
|
2168
|
-
await fs.writeFile(filepath, JSON.stringify(dataToExport, null, 2));
|
|
2173
|
+
if (dataToExport.length > 0) await fs.writeFile(filepath, JSON.stringify(dataToExport, null, 2));
|
|
2169
2174
|
} catch (error) {
|
|
2170
2175
|
this.ctx.logger.error("原始记录备份失败:", error);
|
|
2171
2176
|
}
|
|
@@ -2185,19 +2190,29 @@ var Data = class {
|
|
|
2185
2190
|
const uidToUserInfoMap = new Map(allUsers.map((u) => [u.uid, u]));
|
|
2186
2191
|
for (const tableName of tablesToProcess) {
|
|
2187
2192
|
const filepath = path.join(this.dataDir, `${tableName}.json`);
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2193
|
+
const allDataToExport = [];
|
|
2194
|
+
let offset = 0;
|
|
2195
|
+
while (true) {
|
|
2196
|
+
const batchRecords = await this.ctx.database.get(tableName, {}, {
|
|
2197
|
+
limit: BATCH_SIZE,
|
|
2198
|
+
offset
|
|
2199
|
+
});
|
|
2200
|
+
if (batchRecords.length === 0) break;
|
|
2201
|
+
let processedBatch;
|
|
2202
|
+
if (tableName === "analyse_user") {
|
|
2203
|
+
processedBatch = batchRecords.map(({ uid, ...rest }) => rest);
|
|
2204
|
+
} else {
|
|
2205
|
+
processedBatch = batchRecords.map((record) => {
|
|
2206
|
+
const userInfo = uidToUserInfoMap.get(record.uid);
|
|
2207
|
+
if (!userInfo) return null;
|
|
2208
|
+
const { uid, ...restOfRecord } = record;
|
|
2209
|
+
return { userId: userInfo.userId, channelId: userInfo.channelId, ...restOfRecord };
|
|
2210
|
+
}).filter(Boolean);
|
|
2211
|
+
}
|
|
2212
|
+
allDataToExport.push(...processedBatch);
|
|
2213
|
+
offset += BATCH_SIZE;
|
|
2199
2214
|
}
|
|
2200
|
-
await fs.writeFile(filepath, JSON.stringify(
|
|
2215
|
+
await fs.writeFile(filepath, JSON.stringify(allDataToExport, null, 2));
|
|
2201
2216
|
}
|
|
2202
2217
|
return "数据备份成功";
|
|
2203
2218
|
} catch (error) {
|