Refactor logging timestamps to use formatUtcSqlDateTime function across proxy routes and services; add storedTimestampRepairService for fixing createdAt values in database

This commit is contained in:
cita
2026-03-06 11:57:30 +08:00
parent 7fda94f27d
commit acdc846b13
19 changed files with 580 additions and 95 deletions
+5 -2
View File
@@ -3,6 +3,7 @@ import { eq } from 'drizzle-orm';
import { sendNotification } from './notifyService.js';
import { setAccountRuntimeHealth } from './accountHealthService.js';
import { appendSessionTokenRebindHint } from './alertRules.js';
import { formatUtcSqlDateTime } from './localTimeService.js';
export async function reportTokenExpired(params: {
accountId: number;
@@ -14,6 +15,7 @@ export async function reportTokenExpired(params: {
const siteLabel = params.siteName || 'unknown-site';
const detailText = params.detail ? appendSessionTokenRebindHint(params.detail) : '';
const detail = detailText ? ` (${detailText})` : '';
const createdAt = formatUtcSqlDateTime(new Date());
await db.insert(schema.events).values({
type: 'token',
@@ -22,7 +24,7 @@ export async function reportTokenExpired(params: {
level: 'error',
relatedId: params.accountId,
relatedType: 'account',
createdAt: new Date().toISOString(),
createdAt,
}).run();
await db.update(schema.accounts).set({
@@ -44,13 +46,14 @@ export async function reportTokenExpired(params: {
}
export async function reportProxyAllFailed(params: { model: string; reason: string }) {
const createdAt = formatUtcSqlDateTime(new Date());
await db.insert(schema.events).values({
type: 'proxy',
title: '代理全部失败',
message: `模型=${params.model}, 原因=${params.reason}`,
level: 'error',
relatedType: 'route',
createdAt: new Date().toISOString(),
createdAt,
}).run();
await sendNotification(
+7 -4
View File
@@ -15,6 +15,7 @@ import {
} from './accountExtraConfig.js';
import { decryptAccountPassword } from './accountCredentialService.js';
import { setAccountRuntimeHealth } from './accountHealthService.js';
import { formatUtcSqlDateTime } from './localTimeService.js';
type CheckinExecutionStatus = 'success' | 'failed' | 'skipped';
@@ -128,6 +129,7 @@ export async function checkinAccount(accountId: number, options?: { skipEvent?:
const site = rows[0].sites;
if (isSiteDisabled(site.status)) {
const createdAt = formatUtcSqlDateTime(new Date());
setAccountRuntimeHealth(account.id, {
state: 'disabled',
reason: '\u7ad9\u70b9\u5df2\u7981\u7528',
@@ -137,7 +139,7 @@ export async function checkinAccount(accountId: number, options?: { skipEvent?:
accountId: account.id,
status: 'skipped',
message: 'site disabled',
createdAt: new Date().toISOString(),
createdAt,
}).run();
if (!options?.skipEvent) {
@@ -148,7 +150,7 @@ export async function checkinAccount(accountId: number, options?: { skipEvent?:
level: 'info',
relatedId: accountId,
relatedType: 'account',
createdAt: new Date().toISOString(),
createdAt,
}).run();
}
@@ -242,12 +244,13 @@ export async function checkinAccount(accountId: number, options?: { skipEvent?:
}
}
const createdAt = formatUtcSqlDateTime(new Date());
await db.insert(schema.checkinLogs).values({
accountId: account.id,
status: normalizedStatus,
message: logMessage,
reward: logReward,
createdAt: new Date().toISOString(),
createdAt,
}).run();
if (!options?.skipEvent) {
@@ -260,7 +263,7 @@ export async function checkinAccount(accountId: number, options?: { skipEvent?:
level: effectiveSuccess ? 'info' : 'error',
relatedId: accountId,
relatedType: 'account',
createdAt: new Date().toISOString(),
createdAt,
}).run();
}
@@ -0,0 +1,37 @@
import { eq, isNull, like, or, sql } from 'drizzle-orm';
import { db, schema } from '../db/index.js';
import { formatUtcSqlDateTime } from './localTimeService.js';
function normalizedCreatedAtSql(column: any) {
return sql<string>`replace(substr(${column}, 1, 19), 'T', ' ')`;
}
export async function repairStoredCreatedAtValues(now = new Date()): Promise<void> {
const repairedAt = formatUtcSqlDateTime(now);
await db.update(schema.events)
.set({ createdAt: repairedAt })
.where(or(isNull(schema.events.createdAt), eq(schema.events.createdAt, '')))
.run();
await db.update(schema.proxyLogs)
.set({ createdAt: repairedAt })
.where(or(isNull(schema.proxyLogs.createdAt), eq(schema.proxyLogs.createdAt, '')))
.run();
await db.update(schema.checkinLogs)
.set({ createdAt: repairedAt })
.where(or(isNull(schema.checkinLogs.createdAt), eq(schema.checkinLogs.createdAt, '')))
.run();
await db.update(schema.events)
.set({ createdAt: normalizedCreatedAtSql(schema.events.createdAt) })
.where(like(schema.events.createdAt, '%T%'))
.run();
await db.update(schema.proxyLogs)
.set({ createdAt: normalizedCreatedAtSql(schema.proxyLogs.createdAt) })
.where(like(schema.proxyLogs.createdAt, '%T%'))
.run();
await db.update(schema.checkinLogs)
.set({ createdAt: normalizedCreatedAtSql(schema.checkinLogs.createdAt) })
.where(like(schema.checkinLogs.createdAt, '%T%'))
.run();
}