mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-04 15:49:24 +08:00
fix: 练习正确率只按已作答记录计算
未作答(not_attempted)和无结果的记录不再计入正确率分母;接口新增 attempted 字段,界面显示"答对数 / 已作答数"。月度练习量仍统计全部练习。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -123,7 +123,7 @@ Mistake statistics show total mistakes, mastered count, mastery rate, subject di
|
||||
|
||||

|
||||
|
||||
Practice statistics show total practice, accuracy and active months, with distribution and trends by subject and difficulty.
|
||||
Practice statistics show total practice, accuracy (correct / attempted) and active months, with distribution and trends by subject and difficulty.
|
||||
|
||||

|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@
|
||||
|
||||

|
||||
|
||||
练习统计单独展示累计练习、正确率和活跃月份,并按学科、难度观察练习分布与趋势。
|
||||
练习统计单独展示累计练习、正确率(答对数 / 已作答数)和活跃月份,并按学科、难度观察练习分布与趋势。
|
||||
|
||||

|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ describe('/api/stats', () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date('2026-09-30T16:30:00Z'));
|
||||
mocks.mockPrismaPracticeRecord.groupBy.mockResolvedValue([]);
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([{ createdAt: new Date('2026-09-30T16:01:00Z'), isCorrect: true, difficulty: 'medium' }]);
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([{ createdAt: new Date('2026-09-30T16:01:00Z'), result: 'correct', isCorrect: true, difficulty: 'medium' }]);
|
||||
try {
|
||||
const response = await GET_PRACTICE_STATS();
|
||||
expect(response.status).toBe(200);
|
||||
@@ -80,10 +80,11 @@ describe('/api/stats', () => {
|
||||
{ difficulty: 'medium', _count: { id: 5 } },
|
||||
]);
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([
|
||||
{ createdAt: new Date(), isCorrect: true, difficulty: 'medium' },
|
||||
{ createdAt: new Date(), result: 'correct', isCorrect: true, difficulty: 'medium' },
|
||||
]);
|
||||
mocks.mockPrismaPracticeRecord.count
|
||||
.mockResolvedValueOnce(8)
|
||||
.mockResolvedValueOnce(5)
|
||||
.mockResolvedValueOnce(5);
|
||||
|
||||
const response = await GET_PRACTICE_STATS();
|
||||
@@ -95,7 +96,7 @@ describe('/api/stats', () => {
|
||||
expect(call[0].where.source).toEqual({ not: 'duplicate_import' });
|
||||
}
|
||||
expect(mocks.mockPrismaPracticeRecord.findMany.mock.calls[0][0].where.source).toEqual({ not: 'duplicate_import' });
|
||||
expect(mocks.mockPrismaPracticeRecord.count).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.mockPrismaPracticeRecord.count).toHaveBeenCalledTimes(3);
|
||||
for (const call of mocks.mockPrismaPracticeRecord.count.mock.calls) {
|
||||
expect(call[0].where.source).toEqual({ not: 'duplicate_import' });
|
||||
}
|
||||
@@ -116,14 +117,15 @@ describe('/api/stats', () => {
|
||||
|
||||
// Mock activity stats
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([
|
||||
{ createdAt: new Date(), isCorrect: true, difficulty: 'medium' },
|
||||
{ createdAt: new Date(), isCorrect: false, difficulty: 'hard' },
|
||||
{ createdAt: new Date(), result: 'correct', isCorrect: true, difficulty: 'medium' },
|
||||
{ createdAt: new Date(), result: 'wrong', isCorrect: false, difficulty: 'hard' },
|
||||
]);
|
||||
|
||||
// Mock total and correct counts
|
||||
mocks.mockPrismaPracticeRecord.count
|
||||
.mockResolvedValueOnce(15)
|
||||
.mockResolvedValueOnce(10);
|
||||
.mockResolvedValueOnce(10)
|
||||
.mockResolvedValueOnce(12);
|
||||
|
||||
const response = await GET_PRACTICE_STATS();
|
||||
const data = await response.json();
|
||||
@@ -157,7 +159,8 @@ describe('/api/stats', () => {
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([]);
|
||||
mocks.mockPrismaPracticeRecord.count
|
||||
.mockResolvedValueOnce(100) // total
|
||||
.mockResolvedValueOnce(75); // correct
|
||||
.mockResolvedValueOnce(75) // correct
|
||||
.mockResolvedValueOnce(80); // attempted
|
||||
|
||||
const response = await GET_PRACTICE_STATS();
|
||||
const data = await response.json();
|
||||
@@ -165,7 +168,8 @@ describe('/api/stats', () => {
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.overallStats.total).toBe(100);
|
||||
expect(data.overallStats.correct).toBe(75);
|
||||
expect(data.overallStats.rate).toBe('75.0');
|
||||
expect(data.overallStats.attempted).toBe(80);
|
||||
expect(data.overallStats.rate).toBe('93.8');
|
||||
});
|
||||
|
||||
it('应该处理零记录情况', async () => {
|
||||
@@ -181,6 +185,50 @@ describe('/api/stats', () => {
|
||||
expect(data.overallStats.rate).toBe(0);
|
||||
});
|
||||
|
||||
it('应该排除 not_attempted 记录,不将其计入已作答', async () => {
|
||||
mocks.mockPrismaPracticeRecord.groupBy.mockResolvedValue([]);
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([]);
|
||||
mocks.mockPrismaPracticeRecord.count
|
||||
.mockResolvedValueOnce(10) // total
|
||||
.mockResolvedValueOnce(6) // correct
|
||||
.mockResolvedValueOnce(8); // attempted(排除了 2 条 not_attempted)
|
||||
|
||||
const response = await GET_PRACTICE_STATS();
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.overallStats.total).toBe(10);
|
||||
expect(data.overallStats.correct).toBe(6);
|
||||
expect(data.overallStats.attempted).toBe(8);
|
||||
expect(data.overallStats.rate).toBe('75.0');
|
||||
});
|
||||
|
||||
it('应该将 result 为 null 且 isCorrect 非空的旧记录视为已作答', async () => {
|
||||
mocks.mockPrismaPracticeRecord.groupBy.mockResolvedValue([]);
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([
|
||||
{ createdAt: new Date(), result: null, isCorrect: true, difficulty: 'easy' },
|
||||
{ createdAt: new Date(), result: null, isCorrect: false, difficulty: 'hard' },
|
||||
]);
|
||||
mocks.mockPrismaPracticeRecord.count
|
||||
.mockResolvedValueOnce(10) // total
|
||||
.mockResolvedValueOnce(6) // correct
|
||||
.mockResolvedValueOnce(8); // attempted(包含 2 条 result=null 的旧记录)
|
||||
|
||||
const response = await GET_PRACTICE_STATS();
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.overallStats.total).toBe(10);
|
||||
expect(data.overallStats.correct).toBe(6);
|
||||
expect(data.overallStats.attempted).toBe(8);
|
||||
expect(data.overallStats.rate).toBe('75.0');
|
||||
// 月度趋势:result=null 且 isCorrect=true 计入 correct
|
||||
const currentMonth = new Date().toISOString().slice(0, 7);
|
||||
const monthData = data.activityStats.find((m: { date: string }) => m.date === currentMonth);
|
||||
expect(monthData.total).toBe(2);
|
||||
expect(monthData.correct).toBe(1);
|
||||
});
|
||||
|
||||
it('应该拒绝未登录用户', async () => {
|
||||
vi.mocked(getServerSession).mockResolvedValue(null);
|
||||
|
||||
|
||||
@@ -13,6 +13,22 @@ const logger = createLogger('api:stats:practice');
|
||||
/** 重复导入产生的记录不计入练习统计 */
|
||||
const EXCLUDE_DUPLICATE_IMPORT = { source: { not: 'duplicate_import' } } satisfies Prisma.PracticeRecordWhereInput;
|
||||
|
||||
/** 已作答:result 为 correct 或 wrong;result 为 null 的旧记录以 isCorrect 是否非 null 判断 */
|
||||
const ATTEMPTED_CONDITION = {
|
||||
OR: [
|
||||
{ result: { in: ['correct', 'wrong'] } },
|
||||
{ AND: [{ result: null }, { isCorrect: { not: null } }] }
|
||||
]
|
||||
} satisfies Prisma.PracticeRecordWhereInput;
|
||||
|
||||
/** 答对:result 为 correct,或 result 为 null 且 isCorrect 为 true */
|
||||
const CORRECT_CONDITION = {
|
||||
OR: [
|
||||
{ result: 'correct' },
|
||||
{ AND: [{ result: null }, { isCorrect: true }] }
|
||||
]
|
||||
} satisfies Prisma.PracticeRecordWhereInput;
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
@@ -26,7 +42,7 @@ export async function GET() {
|
||||
const now = new Date();
|
||||
const timeZone = getAppTimeZone();
|
||||
|
||||
const [subjectGroups, activityStats, difficultyGroups, totalRecords, correctRecords] = await Promise.all([
|
||||
const [subjectGroups, activityStats, difficultyGroups, totalRecords, correctRecords, attemptedRecords] = await Promise.all([
|
||||
prisma.practiceRecord.groupBy({
|
||||
by: ['subject'],
|
||||
where: { userId, ...EXCLUDE_DUPLICATE_IMPORT },
|
||||
@@ -42,6 +58,7 @@ export async function GET() {
|
||||
},
|
||||
select: {
|
||||
createdAt: true,
|
||||
result: true,
|
||||
isCorrect: true,
|
||||
difficulty: true
|
||||
}
|
||||
@@ -52,7 +69,8 @@ export async function GET() {
|
||||
_count: { id: true }
|
||||
}),
|
||||
prisma.practiceRecord.count({ where: { userId, ...EXCLUDE_DUPLICATE_IMPORT } }),
|
||||
prisma.practiceRecord.count({ where: { userId, isCorrect: true, ...EXCLUDE_DUPLICATE_IMPORT } }),
|
||||
prisma.practiceRecord.count({ where: { userId, ...CORRECT_CONDITION, ...EXCLUDE_DUPLICATE_IMPORT } }),
|
||||
prisma.practiceRecord.count({ where: { userId, ...ATTEMPTED_CONDITION, ...EXCLUDE_DUPLICATE_IMPORT } }),
|
||||
]);
|
||||
|
||||
// Process activity stats into monthly counts
|
||||
@@ -68,7 +86,8 @@ export async function GET() {
|
||||
const date = formatDateTime(record.createdAt, 'yyyy-MM', timeZone);
|
||||
if (monthlyActivity[date]) {
|
||||
monthlyActivity[date].total++;
|
||||
if (record.isCorrect) {
|
||||
const isCorrect = record.result === 'correct' || (record.result === null && record.isCorrect === true);
|
||||
if (isCorrect) {
|
||||
monthlyActivity[date].correct++;
|
||||
}
|
||||
|
||||
@@ -89,7 +108,8 @@ export async function GET() {
|
||||
overallStats: {
|
||||
total: totalRecords,
|
||||
correct: correctRecords,
|
||||
rate: totalRecords > 0 ? (correctRecords / totalRecords * 100).toFixed(1) : 0
|
||||
attempted: attemptedRecords,
|
||||
rate: attemptedRecords > 0 ? (correctRecords / attemptedRecords * 100).toFixed(1) : 0
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ export function PracticeStats() {
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{stats.overallStats.rate}%</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{stats.overallStats.correct} / {stats.overallStats.total} {t.stats?.correct || "Correct"}
|
||||
{stats.overallStats.correct} / {stats.overallStats.attempted} {t.stats?.attempted || "Attempted"}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -545,6 +545,7 @@ export const translations = {
|
||||
weeklyTrend: "Monthly Trend",
|
||||
total: "Total",
|
||||
correct: "Correct",
|
||||
attempted: "Attempted",
|
||||
},
|
||||
settings: {
|
||||
title: "Settings",
|
||||
@@ -2233,6 +2234,7 @@ export const translations = {
|
||||
weeklyTrend: "月度趋势",
|
||||
total: "总题数",
|
||||
correct: "正确",
|
||||
attempted: "已作答",
|
||||
},
|
||||
settings: {
|
||||
title: "设置",
|
||||
|
||||
@@ -229,7 +229,7 @@ export interface PracticeStatsData {
|
||||
subjectStats: { name: string; value: number }[];
|
||||
activityStats: { date: string; total: number; correct: number;[key: string]: number | string }[];
|
||||
difficultyStats: { name: string; value: number }[];
|
||||
overallStats: { total: number; correct: number; rate: string };
|
||||
overallStats: { total: number; correct: number; attempted: number; rate: string };
|
||||
}
|
||||
|
||||
export interface TagStats {
|
||||
|
||||
Reference in New Issue
Block a user