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