Files
lislgosms/tools/local/replay-signature-retirement.mjs

58 lines
2.8 KiB
JavaScript

import { createRequire } from 'node:module';
import { PrismaPg } from '../../api/node_modules/@prisma/adapter-pg/dist/index.js';
import { PrismaClient } from '../../api/node_modules/@prisma/client/index.js';
const require = createRequire(import.meta.url);
const { SignatureRetirementService } = require('../../api/dist/signature-retirement/signature-retirement.service.js');
const prisma = new PrismaClient({
adapter: new PrismaPg(process.env.DATABASE_URL ?? 'postgresql://cmpp:cmpp_password@localhost:5432/cmpp_platform?schema=public'),
});
const service = new SignatureRetirementService(prisma);
const todayKey = dateKey(new Date());
const results = [];
try {
for (let offset = 30; offset >= 0; offset -= 1) {
const detectionDate = dateKey(new Date(Date.now() - offset * 86_400_000));
results.push(await service.runDetection(detectionDate));
}
const notifications = [];
for (let offset = 4; offset >= 0; offset -= 1) {
const notificationDate = dateKey(new Date(Date.now() - offset * 86_400_000));
const notification = await service.publishNotifications(notificationDate);
const detections = await prisma.signatureRetirementDetection.findMany({
where: { detectionDate: new Date(`${notificationDate}T00:00:00.000Z`), signatureId: { startsWith: 'qa-retirement-' } },
select: { id: true },
});
await prisma.signatureRetirementMessage.updateMany({
where: { detectionId: { in: detections.map((item) => item.id) } },
data: { createdAt: new Date(`${notificationDate}T08:00:00+08:00`) },
});
notifications.push({ notificationDate, ...notification });
}
const unreported = await service.unreportedSignatures({ date: todayKey, page: 1, pageSize: 10 });
const todayMessages = await service.listMessages({ dateFrom: todayKey, dateTo: todayKey, page: 1, pageSize: 10 });
const filteredHistory = await service.listMessages({ dateFrom: dateKey(new Date(Date.now() - 4 * 86_400_000)), dateTo: todayKey, tenantId: 'qa-retirement-tenant-b', signatureKeyword: '跨通道', page: 1, pageSize: 10 });
console.log(JSON.stringify({
todayKey,
detectionDays: results.length,
detectionRows: results.reduce((sum, item) => sum + item.alerted + item.healthy, 0),
today: results.at(-1),
notifications,
unreported,
todayMessages: { total: todayMessages.total, page: todayMessages.page, pageSize: todayMessages.pageSize, items: todayMessages.items.length },
filteredHistory: { total: filteredHistory.total, items: filteredHistory.items.length, applicationNames: [...new Set(filteredHistory.items.map((item) => item.applicationName))] },
}));
} finally {
await prisma.$disconnect();
}
function dateKey(value) {
return new Intl.DateTimeFormat('en-CA', {
timeZone: 'Asia/Shanghai',
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).format(value);
}