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
+3 -1
View File
@@ -37,6 +37,7 @@ import {
} from './downstreamPolicy.js';
import { composeProxyLogMessage } from './logPathMeta.js';
import { executeEndpointFlow, withUpstreamPath } from './endpointFlow.js';
import { formatUtcSqlDateTime } from '../../services/localTimeService.js';
const MAX_RETRIES = 2;
const CLAUDE_SSE_EVENT_NAMES = new Set([
@@ -719,6 +720,7 @@ async function logProxy(
upstreamPath: string | null = null,
) {
try {
const createdAt = formatUtcSqlDateTime(new Date());
const normalizedErrorMessage = composeProxyLogMessage({
downstreamPath,
upstreamPath,
@@ -739,7 +741,7 @@ async function logProxy(
estimatedCost,
errorMessage: normalizedErrorMessage,
retryCount,
createdAt: new Date().toISOString(),
createdAt,
}).run();
} catch (error) {
console.warn('[proxy/chat] failed to write proxy log', error);
+3 -1
View File
@@ -12,6 +12,7 @@ import { mergeProxyUsage, parseProxyUsage, pullSseDataEvents } from '../../servi
import { ensureModelAllowedForDownstreamKey, getDownstreamRoutingPolicy, recordDownstreamCostUsage } from './downstreamPolicy.js';
import { withExplicitProxyRequestInit } from '../../services/siteProxy.js';
import { composeProxyLogMessage } from './logPathMeta.js';
import { formatUtcSqlDateTime } from '../../services/localTimeService.js';
const MAX_RETRIES = 2;
@@ -242,6 +243,7 @@ async function logProxy(
estimatedCost = 0,
) {
try {
const createdAt = formatUtcSqlDateTime(new Date());
const normalizedErrorMessage = composeProxyLogMessage({
downstreamPath: '/v1/completions',
errorMessage,
@@ -261,7 +263,7 @@ async function logProxy(
estimatedCost,
errorMessage: normalizedErrorMessage,
retryCount,
createdAt: new Date().toISOString(),
createdAt,
}).run();
} catch (error) {
console.warn('[proxy/completions] failed to write proxy log', error);
+3 -1
View File
@@ -12,6 +12,7 @@ import { parseProxyUsage } from '../../services/proxyUsageParser.js';
import { ensureModelAllowedForDownstreamKey, getDownstreamRoutingPolicy, recordDownstreamCostUsage } from './downstreamPolicy.js';
import { withExplicitProxyRequestInit } from '../../services/siteProxy.js';
import { composeProxyLogMessage } from './logPathMeta.js';
import { formatUtcSqlDateTime } from '../../services/localTimeService.js';
const MAX_RETRIES = 2;
@@ -157,6 +158,7 @@ async function logProxy(
estimatedCost = 0,
) {
try {
const createdAt = formatUtcSqlDateTime(new Date());
const normalizedErrorMessage = composeProxyLogMessage({
downstreamPath: '/v1/embeddings',
errorMessage,
@@ -176,7 +178,7 @@ async function logProxy(
estimatedCost,
errorMessage: normalizedErrorMessage,
retryCount,
createdAt: new Date().toISOString(),
createdAt,
}).run();
} catch (error) {
console.warn('[proxy/embeddings] failed to write proxy log', error);
+3 -1
View File
@@ -10,6 +10,7 @@ import { shouldRetryProxyRequest } from '../../services/proxyRetryPolicy.js';
import { ensureModelAllowedForDownstreamKey, getDownstreamRoutingPolicy, recordDownstreamCostUsage } from './downstreamPolicy.js';
import { withExplicitProxyRequestInit } from '../../services/siteProxy.js';
import { composeProxyLogMessage } from './logPathMeta.js';
import { formatUtcSqlDateTime } from '../../services/localTimeService.js';
const MAX_RETRIES = 2;
@@ -127,6 +128,7 @@ async function logProxy(
estimatedCost = 0,
) {
try {
const createdAt = formatUtcSqlDateTime(new Date());
const normalizedErrorMessage = composeProxyLogMessage({
downstreamPath: '/v1/images/generations',
errorMessage,
@@ -146,7 +148,7 @@ async function logProxy(
estimatedCost,
errorMessage: normalizedErrorMessage,
retryCount,
createdAt: new Date().toISOString(),
createdAt,
}).run();
} catch (error) {
console.warn('[proxy/images] failed to write proxy log', error);
+3 -1
View File
@@ -28,6 +28,7 @@ import {
import { ensureModelAllowedForDownstreamKey, getDownstreamRoutingPolicy, recordDownstreamCostUsage } from './downstreamPolicy.js';
import { composeProxyLogMessage } from './logPathMeta.js';
import { executeEndpointFlow, withUpstreamPath } from './endpointFlow.js';
import { formatUtcSqlDateTime } from '../../services/localTimeService.js';
const MAX_RETRIES = 2;
@@ -1577,6 +1578,7 @@ async function logProxy(
upstreamPath: string | null = null,
) {
try {
const createdAt = formatUtcSqlDateTime(new Date());
const normalizedErrorMessage = composeProxyLogMessage({
downstreamPath,
upstreamPath,
@@ -1597,7 +1599,7 @@ async function logProxy(
estimatedCost,
errorMessage: normalizedErrorMessage,
retryCount,
createdAt: new Date().toISOString(),
createdAt,
}).run();
} catch (error) {
console.warn('[proxy/responses] failed to write proxy log', error);