修改系统概览中统计信息的显示逻辑

This commit is contained in:
PingChuan
2025-05-30 16:14:28 +08:00
parent 01cea56a6a
commit 202d64efd6
+50 -38
View File
@@ -1,3 +1,4 @@
import { log } from "node:console";
import { postgrestGet, type PostgrestParams } from "../postgrest-client"; import { postgrestGet, type PostgrestParams } from "../postgrest-client";
import dayjs from 'dayjs'; import dayjs from 'dayjs';
@@ -20,8 +21,8 @@ function extractApiData<T>(responseData: unknown): T | null {
const code = (responseData as { code: number }).code; const code = (responseData as { code: number }).code;
// 如果有错误码且不是成功状态 // 如果有错误码且不是成功状态
if (code !== 0 && code !== 200) { if (code !== 0 && code !== 200) {
const errorMsg = 'msg' in responseData const errorMsg = 'msg' in responseData
? (responseData as { msg: string }).msg ? (responseData as { msg: string }).msg
: '未知错误'; : '未知错误';
console.error(`API响应错误: [${code}] ${errorMsg}`); console.error(`API响应错误: [${code}] ${errorMsg}`);
return null; return null;
@@ -58,22 +59,22 @@ function extractApiData<T>(responseData: unknown): T | null {
* 首页数据统计响应类型 * 首页数据统计响应类型
*/ */
interface HomeStatistics { interface HomeStatistics {
todayPendingFiles: number; todayPendingFiles: number;
monthlyReviewedFiles: number; monthlyReviewedFiles: number;
monthlyReviewGrowth: { monthlyReviewGrowth: {
value: number; value: number;
isUp: boolean; isUp: boolean;
}; };
monthlyPassRate: number; monthlyPassRate: number;
passRateGrowth: { passRateGrowth: {
value: number; value: number;
isUp: boolean; isUp: boolean;
}; };
issuesDetected: number; issuesDetected: number;
issuesGrowth: { issuesGrowth: {
value: number; value: number;
isUp: boolean; isUp: boolean;
}; };
} }
/** /**
@@ -91,13 +92,13 @@ export async function getHomeData(): Promise<HomeStatistics> {
// 通用API响应处理函数 // 通用API响应处理函数
const handleApiResponse = async <T>( const handleApiResponse = async <T>(
apiCall: Promise<{ apiCall: Promise<{
data?: unknown; data?: unknown;
headers?: Record<string, string>; headers?: Record<string, string>;
error?: string; error?: string;
status?: number status?: number
}>, }>,
errorMessage: string, errorMessage: string,
defaultValue: T defaultValue: T
): Promise<T> => { ): Promise<T> => {
try { try {
@@ -127,7 +128,7 @@ export async function getHomeData(): Promise<HomeStatistics> {
is_test_document: `eq.false` is_test_document: `eq.false`
} }
}; };
const todayPendingCount = await handleApiResponse<{count: number}[]>( const todayPendingCount = await handleApiResponse<{ count: number }[]>(
postgrestGet('documents', todayPendingParams), postgrestGet('documents', todayPendingParams),
'获取今日待审核文件数量失败', '获取今日待审核文件数量失败',
[] []
@@ -143,7 +144,7 @@ export async function getHomeData(): Promise<HomeStatistics> {
is_test_document: `eq.false` is_test_document: `eq.false`
} }
}; };
const thisMonthReviewedCount = await handleApiResponse<{count: number}[]>( const thisMonthReviewedCount = await handleApiResponse<{ count: number }[]>(
postgrestGet('documents', thisMonthReviewedParams), postgrestGet('documents', thisMonthReviewedParams),
'获取本月已审核文件数量失败', '获取本月已审核文件数量失败',
[] []
@@ -160,23 +161,24 @@ export async function getHomeData(): Promise<HomeStatistics> {
is_test_document: `eq.false` is_test_document: `eq.false`
} }
}; };
const lastMonthReviewedCount = await handleApiResponse<{count: number}[]>( const lastMonthReviewedCount = await handleApiResponse<{ count: number }[]>(
postgrestGet('documents', lastMonthReviewedParams), postgrestGet('documents', lastMonthReviewedParams),
'获取上月已审核文件数量失败', '获取上月已审核文件数量失败',
[] []
); );
// 上月已审核文件数量 // 上月已审核文件数量
const lastMonthReviewed = lastMonthReviewedCount[0]?.count || 0; const lastMonthReviewed = lastMonthReviewedCount[0]?.count || 0;
// 计算同比增长 // 计算同比增长
let reviewGrowthValue = 0; let reviewGrowthValue = 0;
let reviewGrowthIsUp = true; let reviewGrowthIsUp = true;
// console.log('lastMonthReviewed-------', lastMonthReviewed);
// console.log('monthlyReviewedFiles-------', monthlyReviewedFiles);
if (lastMonthReviewed > 0) { if (lastMonthReviewed > 0) {
const growthRate = ((monthlyReviewedFiles - lastMonthReviewed) / lastMonthReviewed) * 100; const growthRate = ((monthlyReviewedFiles - lastMonthReviewed) / lastMonthReviewed) * 100;
reviewGrowthValue = Math.abs(parseFloat(growthRate.toFixed(1))); reviewGrowthValue = Math.abs(parseFloat(growthRate.toFixed(1)));
reviewGrowthIsUp = growthRate >= 0; reviewGrowthIsUp = growthRate >= 0;
} else if (lastMonthReviewed == 0 && monthlyReviewedFiles > 0) {
reviewGrowthValue = 100;
reviewGrowthIsUp = true;
} }
// 3. 审核通过率 - 本月审核通过率 // 3. 审核通过率 - 本月审核通过率
@@ -188,7 +190,7 @@ export async function getHomeData(): Promise<HomeStatistics> {
is_test_document: `eq.false` is_test_document: `eq.false`
} }
}; };
const thisMonthTotalCount = await handleApiResponse<{count: number}[]>( const thisMonthTotalCount = await handleApiResponse<{ count: number }[]>(
postgrestGet('documents', thisMonthTotalParams), postgrestGet('documents', thisMonthTotalParams),
'获取本月审核通过数量失败', '获取本月审核通过数量失败',
[] []
@@ -198,7 +200,7 @@ export async function getHomeData(): Promise<HomeStatistics> {
// 本月审核通过率 // 本月审核通过率
const monthlyPassRate = (thisMonthPassTotal > 0 && monthlyReviewedFiles > 0) const monthlyPassRate = (thisMonthPassTotal > 0 && monthlyReviewedFiles > 0)
? parseFloat(((thisMonthPassTotal / monthlyReviewedFiles) * 100).toFixed(1)) ? parseFloat(((thisMonthPassTotal / monthlyReviewedFiles) * 100).toFixed(1))
: 0; : 0;
// 上月审核通过率 // 上月审核通过率
@@ -210,7 +212,7 @@ export async function getHomeData(): Promise<HomeStatistics> {
is_test_document: `eq.false` is_test_document: `eq.false`
} }
}; };
const lastMonthTotalCount = await handleApiResponse<{count: number}[]>( const lastMonthTotalCount = await handleApiResponse<{ count: number }[]>(
postgrestGet('documents', lastMonthTotalParams), postgrestGet('documents', lastMonthTotalParams),
'获取上月审核通过数量失败', '获取上月审核通过数量失败',
[] []
@@ -223,16 +225,26 @@ export async function getHomeData(): Promise<HomeStatistics> {
? parseFloat(((lastMonthTotal / lastMonthReviewed) * 100).toFixed(1)) ? parseFloat(((lastMonthTotal / lastMonthReviewed) * 100).toFixed(1))
: 0; : 0;
console.log('上个月-------', lastMonthPassRate);
// 计算通过率同比增长 // 计算通过率同比增长
let passRateGrowthValue = 0; let passRateGrowthValue = 0;
let passRateGrowthIsUp = true; let passRateGrowthIsUp = true;
if (lastMonthPassRate > 0) { if (lastMonthPassRate > 0) {
const passRateGrowth = ((monthlyPassRate - lastMonthPassRate) / lastMonthPassRate) * 100; const passRateGrowth = ((monthlyPassRate - lastMonthPassRate) / lastMonthPassRate) * 100;
passRateGrowthValue = Math.abs(parseFloat(passRateGrowth.toFixed(1))); passRateGrowthValue = Math.abs(parseFloat(passRateGrowth.toFixed(1)));
passRateGrowthIsUp = passRateGrowth >= 0; passRateGrowthIsUp = passRateGrowth >= 0;
} else if (lastMonthPassRate == 0 && monthlyPassRate > 0) {
passRateGrowthValue = 100;
passRateGrowthIsUp = true;
} }
console.log('上月通过率-------', lastMonthPassRate);
console.log('本月通过率-------', monthlyPassRate);
// 4. 检查出的问题总数(从评估结果表中统计) // 4. 检查出的问题总数(从评估结果表中统计)
const thisMonthIssuesParams: PostgrestParams = { const thisMonthIssuesParams: PostgrestParams = {
select: 'count', select: 'count',
@@ -241,7 +253,7 @@ export async function getHomeData(): Promise<HomeStatistics> {
'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段 'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段
} }
}; };
const thisMonthIssuesResponse = await handleApiResponse<{count: number}[]>( const thisMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
postgrestGet('evaluation_results', thisMonthIssuesParams), postgrestGet('evaluation_results', thisMonthIssuesParams),
'获取本月问题数据失败', '获取本月问题数据失败',
[] []
@@ -257,7 +269,7 @@ export async function getHomeData(): Promise<HomeStatistics> {
'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段 'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段
} }
}; };
const lastMonthIssuesResponse = await handleApiResponse<{count: number}[]>( const lastMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
postgrestGet('evaluation_results', lastMonthIssuesParams), postgrestGet('evaluation_results', lastMonthIssuesParams),
'获取上月问题数据失败', '获取上月问题数据失败',
[] []
@@ -268,13 +280,13 @@ export async function getHomeData(): Promise<HomeStatistics> {
// 计算问题数量同比增长 // 计算问题数量同比增长
let issuesGrowthValue = 0; let issuesGrowthValue = 0;
let issuesGrowthIsUp = true; let issuesGrowthIsUp = true;
if (lastMonthIssuesCount > 0) { if (lastMonthIssuesCount > 0) {
const issuesGrowth = ((thisMonthIssuesCount - lastMonthIssuesCount) / lastMonthIssuesCount) * 100; const issuesGrowth = ((thisMonthIssuesCount - lastMonthIssuesCount) / lastMonthIssuesCount) * 100;
issuesGrowthValue = Math.abs(parseFloat(issuesGrowth.toFixed(1))); issuesGrowthValue = Math.abs(parseFloat(issuesGrowth.toFixed(1)));
issuesGrowthIsUp = issuesGrowth >= 0; issuesGrowthIsUp = issuesGrowth >= 0;
} }
// 返回统计结果 // 返回统计结果
return { return {
todayPendingFiles, todayPendingFiles,