75 lines
2.1 KiB
JavaScript
75 lines
2.1 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const content = `// server/utils/runtimeState.ts - 服务启动时间内存状态 + 问答记录 DB 读写
|
|
import { answerCache } from "~~/server/utils/cache";
|
|
import { prisma } from "~~/server/utils/db";
|
|
import { serverEnv } from "~~/server/utils/env";
|
|
|
|
const startTime = Date.now();
|
|
export const SERVICE_VERSION = "1.1.0";
|
|
|
|
export const formatLocalDateTime = (date: Date) => {
|
|
const pad = (value: number) => value.toString().padStart(2, "0");
|
|
return \`\${date.getFullYear()}-\${pad(date.getMonth() + 1)}-\${pad(date.getDate())} \${pad(date.getHours())}:\${pad(date.getMinutes())}:\${pad(date.getSeconds())}\`;
|
|
};
|
|
|
|
export const addQaRecord = async (record: {
|
|
userId: string;
|
|
question: string;
|
|
type: string;
|
|
options: string;
|
|
answer: string;
|
|
}) => {
|
|
await prisma.qaRecord.create({
|
|
data: {
|
|
userId: record.userId,
|
|
question: record.question,
|
|
type: record.type,
|
|
options: record.options || null,
|
|
answer: record.answer || null
|
|
}
|
|
});
|
|
};
|
|
|
|
export const getQaRecords = async (
|
|
userId: string,
|
|
options: { page: number; size: number }
|
|
) => {
|
|
const { page, size } = options;
|
|
const skip = (page - 1) * size;
|
|
const [total, rows] = await Promise.all([
|
|
prisma.qaRecord.count({ where: { userId } }),
|
|
prisma.qaRecord.findMany({
|
|
where: { userId },
|
|
orderBy: { createdAt: "desc" },
|
|
skip,
|
|
take: size,
|
|
select: { question: true, type: true, options: true, answer: true, createdAt: true }
|
|
})
|
|
]);
|
|
const records = rows.map((r) => ({
|
|
time: formatLocalDateTime(r.createdAt),
|
|
timestamp: r.createdAt.toISOString(),
|
|
question: r.question,
|
|
type: r.type,
|
|
options: r.options ?? "",
|
|
answer: r.answer ?? ""
|
|
}));
|
|
return { records, total };
|
|
};
|
|
|
|
export const getRuntimeStats = () => {
|
|
return {
|
|
version: SERVICE_VERSION,
|
|
uptime: (Date.now() - startTime) / 1000,
|
|
model: serverEnv.openAiModel,
|
|
cache_enabled: serverEnv.enableCache,
|
|
cache_size: answerCache?.size() ?? 0
|
|
};
|
|
};
|
|
`;
|
|
|
|
fs.writeFileSync(path.join(__dirname, 'server', 'utils', 'runtimeState.ts'), content, 'utf8');
|
|
console.log('Written successfully');
|