koishi-plugin-chat-analyse 1.6.7 → 1.6.9

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.
Files changed (2) hide show
  1. package/lib/index.js +42 -34
  2. 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 1 1 * *", () => {
2135
+ if (this.config.enableAutoBackup) this.ctx.cron("0 0 * * *", () => {
2136
2136
  this.backupCache();
2137
2137
  });
2138
2138
  }
@@ -2151,28 +2151,26 @@ var Data = class {
2151
2151
  const allUsers = await this.ctx.database.get("analyse_user", {});
2152
2152
  if (allUsers.length === 0) return;
2153
2153
  const uidToUserInfoMap = new Map(allUsers.map((u) => [u.uid, u]));
2154
- const now = /* @__PURE__ */ new Date();
2155
- const lastMonthDate = new Date(now.getFullYear(), now.getMonth() - 1, 1);
2156
- const year = lastMonthDate.getFullYear();
2157
- const monthIndex = lastMonthDate.getMonth();
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();
2158
2159
  const monthString = (monthIndex + 1).toString().padStart(2, "0");
2159
- const daysInMonth = new Date(year, monthIndex + 1, 0).getDate();
2160
- for (let day = 1; day <= daysInMonth; day++) {
2161
- const dayString = day.toString().padStart(2, "0");
2162
- const filename = `analyse_cache_${year}-${monthString}-${dayString}.json`;
2163
- const filepath = path.join(this.dataDir, filename);
2164
- const startDate = new Date(year, monthIndex, day);
2165
- const endDate = new Date(year, monthIndex, day + 1);
2166
- const records = await this.ctx.database.get("analyse_cache", { timestamp: { $gte: startDate, $lt: endDate } });
2167
- if (records.length === 0) continue;
2168
- const dataToExport = records.map((record) => {
2169
- const userInfo = uidToUserInfoMap.get(record.uid);
2170
- if (!userInfo) return null;
2171
- const { id, uid, ...restOfRecord } = record;
2172
- return { userId: userInfo.userId, channelId: userInfo.channelId, ...restOfRecord };
2173
- }).filter(Boolean);
2174
- await fs.writeFile(filepath, JSON.stringify(dataToExport, null, 2));
2175
- }
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 } });
2166
+ if (records.length === 0) return;
2167
+ const dataToExport = records.map((record) => {
2168
+ const userInfo = uidToUserInfoMap.get(record.uid);
2169
+ if (!userInfo) return null;
2170
+ const { id, uid, ...restOfRecord } = record;
2171
+ return { userId: userInfo.userId, channelId: userInfo.channelId, ...restOfRecord };
2172
+ }).filter(Boolean);
2173
+ if (dataToExport.length > 0) await fs.writeFile(filepath, JSON.stringify(dataToExport, null, 2));
2176
2174
  } catch (error) {
2177
2175
  this.ctx.logger.error("原始记录备份失败:", error);
2178
2176
  }
@@ -2192,19 +2190,29 @@ var Data = class {
2192
2190
  const uidToUserInfoMap = new Map(allUsers.map((u) => [u.uid, u]));
2193
2191
  for (const tableName of tablesToProcess) {
2194
2192
  const filepath = path.join(this.dataDir, `${tableName}.json`);
2195
- let dataToExport;
2196
- if (tableName === "analyse_user") {
2197
- dataToExport = allUsers.map(({ uid, ...rest }) => rest);
2198
- } else {
2199
- const records = await this.ctx.database.get(tableName, {});
2200
- dataToExport = records.map((record) => {
2201
- const userInfo = uidToUserInfoMap.get(record.uid);
2202
- if (!userInfo) return null;
2203
- const { uid, ...restOfRecord } = record;
2204
- return { userId: userInfo.userId, channelId: userInfo.channelId, ...restOfRecord };
2205
- }).filter(Boolean);
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;
2206
2214
  }
2207
- await fs.writeFile(filepath, JSON.stringify(dataToExport, null, 2));
2215
+ await fs.writeFile(filepath, JSON.stringify(allDataToExport, null, 2));
2208
2216
  }
2209
2217
  return "数据备份成功";
2210
2218
  } catch (error) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chat-analyse",
3
3
  "description": "强大而全面的聊天数据分析插件。支持多维度统计(命令、发言、消息类型、活跃度),可生成发言排行、词云图,并提供完善的数据管理。",
4
- "version": "1.6.7",
4
+ "version": "1.6.9",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],