#!/usr/bin/env bun import { prisma } from '../lib/prisma' import { IGNORED_WINDOW_KEYWORDS } from '../lib/windowFilters' async function cleanupIgnoredWindows() { console.log(`开始清理数据库中的忽略窗口记录: ${IGNORED_WINDOW_KEYWORDS.join(', ')}`) const whereClause = { OR: [ ...IGNORED_WINDOW_KEYWORDS.flatMap(keyword => ([ { title: { contains: keyword, mode: 'insensitive' as const } }, { path: { contains: keyword, mode: 'insensitive' as const } } ])) ] } const [matchedCount, affectedHosts] = await Promise.all([ prisma.window.count({ where: whereClause }), prisma.window.findMany({ where: whereClause, select: { record: { select: { hostname: true } } }, distinct: ['recordId'] }) ]) if (matchedCount === 0) { console.log('没有找到需要清理的忽略窗口记录。') return } const hostnames = Array.from(new Set(affectedHosts.map(item => item.record.hostname))).sort() console.log(`匹配到 ${matchedCount} 条窗口记录,涉及 ${hostnames.length} 台主机。`) console.log(`主机列表: ${hostnames.join(', ')}`) const deleteResult = await prisma.window.deleteMany({ where: whereClause }) console.log(`已删除 ${deleteResult.count} 条忽略窗口记录。`) } cleanupIgnoredWindows() .catch((error) => { console.error('清理忽略窗口失败:', error) process.exitCode = 1 }) .finally(async () => { await prisma.$disconnect() })