Files
openclaw-market/scripts/clear-mock-data.ts
2026-03-16 22:00:47 +08:00

103 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 清理模拟数据脚本
* 删除所有 claws 数据(以及关联的 heartbeats, tasks, token_usage
*
* 使用方法: npx tsx scripts/clear-mock-data.ts
*/
import { db } from "@/lib/db";
import { claws, heartbeats, tasks, tokenUsage, geoCache } from "@/lib/db/schema";
import { sql } from "drizzle-orm";
import { redis } from "@/lib/redis";
async function clearMockData() {
console.log("🧹 开始清理数据...\n");
try {
// 1. 清理 token_usage
console.log("🗑️ 清理 token_usage...");
const tokenResult = await db.delete(tokenUsage);
console.log(` ✅ 已删除所有 token_usage 记录\n`);
// 2. 清理 tasks
console.log("🗑️ 清理 tasks...");
await db.delete(tasks);
console.log(` ✅ 已删除所有 tasks 记录\n`);
// 3. 清理 heartbeats
console.log("🗑️ 清理 heartbeats...");
await db.delete(heartbeats);
console.log(` ✅ 已删除所有 heartbeats 记录\n`);
// 4. 清理 claws
console.log("🗑️ 清理 claws...");
await db.delete(claws);
console.log(` ✅ 已删除所有 claws 记录\n`);
// 5. 清理 geo_cache可选
console.log("🗑️ 清理 geo_cache...");
await db.delete(geoCache);
console.log(` ✅ 已删除所有 geo_cache 记录\n`);
// 6. 清理 Redis 数据
console.log("🗑️ 清理 Redis 数据...");
// 获取所有相关的 Redis keys
const keysToDelete: string[] = [];
// 在线状态
const onlineKeys = await redis.keys("claw:online:*");
keysToDelete.push(...onlineKeys);
// 活跃 claws
const activeKeys = await redis.keys("active_claws*");
keysToDelete.push(...activeKeys);
// 地区统计
const regionKeys = await redis.keys("region:*");
keysToDelete.push(...regionKeys);
// 全局统计
const globalKeys = await redis.keys("global:*");
keysToDelete.push(...globalKeys);
// 每小时活动
const hourlyKeys = await redis.keys("hourly:*");
keysToDelete.push(...hourlyKeys);
// 热力图缓存
const heatmapKeys = await redis.keys("heatmap:*");
keysToDelete.push(...heatmapKeys);
// Token 排行榜缓存
const leaderboardKeys = await redis.keys("token_leaderboard:*");
keysToDelete.push(...leaderboardKeys);
// Token 统计缓存
const tokenStatsKeys = await redis.keys("token_stats:*");
keysToDelete.push(...tokenStatsKeys);
if (keysToDelete.length > 0) {
await redis.del(...keysToDelete);
console.log(` ✅ 已删除 ${keysToDelete.length} 个 Redis keys\n`);
} else {
console.log(` 没有 Redis keys 需要删除\n`);
}
console.log("=" .repeat(50));
console.log("✨ 数据清理完成!\n");
} catch (error) {
console.error("❌ 清理失败:", error);
throw error;
}
}
// 运行
clearMockData()
.then(() => process.exit(0))
.catch((err) => {
console.error("❌ 错误:", err);
process.exit(1);
});