Skip to content

feat: 实现自动日志清理功能的 API 接口#7

Merged
ding113 merged 2 commits into
mainfrom
claude/issue-6-20251027-1555
Oct 27, 2025
Merged

feat: 实现自动日志清理功能的 API 接口#7
ding113 merged 2 commits into
mainfrom
claude/issue-6-20251027-1555

Conversation

@ding113

@ding113 ding113 commented Oct 27, 2025

Copy link
Copy Markdown
Owner

摘要

修复 ##6 - 实现自动日志清理功能的缺失 API 接口。

修改内容

  • ✅ 新增 /api/admin/system-config 端点 (GET/POST)
  • ✅ 支持自动清理配置的读取和更新
  • ✅ 扩展验证 Schema 支持日志清理参数
  • ✅ 更新 Server Actions 支持完整配置

测试计划

  • 测试启用/禁用自动清理开关
  • 测试修改保留天数、执行时间、批量大小
  • 测试配置持久化
  • 验证数据验证规则

🤖 Generated with Claude Code

- 新增 /api/admin/system-config 端点 (GET/POST)
- 支持自动清理配置的读取和更新
- 扩展验证 Schema 支持日志清理参数
- 更新 Server Actions 支持完整配置

修复 #6

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Ding <ding113@users.noreply.github.com>
@claude

claude Bot commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Claude finished @ding113's task —— View job


代码审查完成 ✅

任务清单:

  • 读取并分析修改的文件
  • 审查 API 路由实现
  • 审查验证 Schema
  • 审查 Server Actions 修改
  • 提供代码审查反馈
  • 添加适当的 PR 标签

代码审查结果

✅ 整体评价

代码实现质量良好,成功补充了自动日志清理功能的 API 接口。实现符合项目架构规范,认证、验证、错误处理都比较完善。


📋 详细审查

1. API 路由实现 (src/app/api/admin/system-config/route.ts)

优点:

  • 认证检查完善(管理员权限验证)
  • 错误处理全面(Zod 验证错误、通用错误)
  • 日志记录完整(包含用户 ID 和变更内容)
  • HTTP 状态码使用正确

⚠️ 发现 2 个问题(已添加行内评论):

问题 1:Schema 重复定义 (第 51-59 行)

  • API 路由中重新定义了验证 Schema,但 schemas.ts 中已有 UpdateSystemSettingsSchema
  • 两处定义存在差异:API 路由中 currencyDisplay 使用 z.string().optional(),而 schemas.ts 使用 z.enum() 验证货币类型
  • 影响: 绕过了货币类型的枚举验证,可能接受无效的货币代码
  • 建议: 删除重复定义,直接导入使用 UpdateSystemSettingsSchema

问题 2:不安全的类型断言 (第 67 行)

  • 使用 as any 绕过类型检查
  • 建议: 使用统一的 Schema 后,此问题会自动解决

2. 验证 Schema (src/lib/validation/schemas.ts)

优点:

  • 验证规则完整且合理
  • 错误消息清晰(中文提示)
  • 使用 z.coerce 处理数字类型转换
  • 范围限制合理(保留天数 1-365 天,批量大小 1000-100000)

无问题


3. Server Actions (src/actions/system-config.ts)

优点:

  • 正确使用 UpdateSystemSettingsSchema 进行验证
  • 权限检查完善
  • 错误处理全面
  • 使用 revalidatePath 刷新缓存

无问题


🔍 其他观察

架构一致性:

  • Server Actions 和 API 路由功能重复是合理的设计
  • 支持不同的使用场景(Next.js 表单 vs REST API)

类型安全:

  • UpdateSystemSettingsInput 类型定义完整
  • Repository 层正确处理可选字段

📝 修改建议

高优先级:

  1. ✅ 删除 API 路由中的重复 Schema 定义,改用 UpdateSystemSettingsSchema
  2. ✅ 移除 as any 类型断言

低优先级:

  • 考虑在 API 路由中也添加 revalidatePath(如果前端会直接调用 API)

✅ 测试建议

PR 描述中的测试计划很完整,建议重点测试:

  1. ✅ 货币类型验证(确保只接受有效的货币代码)
  2. ✅ 边界值测试(保留天数:0, 1, 365, 366)
  3. ✅ 批量大小边界值(999, 1000, 100000, 100001)
  4. ✅ 权限验证(非管理员用户访问)

🏷️ 已添加标签

  • enhancement - 功能增强

| View PR

@claude claude Bot added the enhancement New feature or request label Oct 27, 2025
Comment on lines +51 to +59
const schema = z.object({
siteTitle: z.string().min(1, "站点标题不能为空").max(128, "站点标题不能超过128个字符"),
allowGlobalUsageView: z.boolean(),
currencyDisplay: z.string().optional(),
enableAutoCleanup: z.boolean().optional(),
cleanupRetentionDays: z.number().int().min(1).max(365).optional(),
cleanupSchedule: z.string().min(1).optional(),
cleanupBatchSize: z.number().int().min(1000).max(100000).optional(),
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题:Schema 重复定义

这里重新定义了验证 Schema,但 src/lib/validation/schemas.ts 中已经有 UpdateSystemSettingsSchema

存在的差异:

  • 这里:currencyDisplay: z.string().optional()
  • schemas.ts:使用 z.enum() 验证 CURRENCY_CONFIG 的键

影响: 绕过了货币类型的枚举验证,可能接受无效的货币代码。

建议修改:

import { UpdateSystemSettingsSchema } from "@/lib/validation/schemas";

// 删除第 51-59 行的 schema 定义
// 直接使用:
const validated = UpdateSystemSettingsSchema.parse(body);

const updated = await updateSystemSettings({
siteTitle: validated.siteTitle.trim(),
allowGlobalUsageView: validated.allowGlobalUsageView,
currencyDisplay: validated.currencyDisplay as any,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题:不安全的类型断言

使用 as any 绕过了 TypeScript 类型检查。

如果使用 UpdateSystemSettingsSchema(已在 schemas.ts 中定义),这个类型断言就不需要了,因为验证后的类型会自动匹配 UpdateSystemSettingsInput

✅ 移除 API 路由中的重复 Schema 定义
✅ 导入并使用 UpdateSystemSettingsSchema
✅ 移除不安全的 as any 类型断言
✅ 修复 ZodError.errors → ZodError.issues
✅ 确保货币类型枚举验证正确执行

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@claude

claude Bot commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Claude finished @ding113's task —— View job


代码审查完成 ✅


任务清单:

  • 读取修改后的文件
  • 检查之前的问题是否解决
  • 全面审查代码质量
  • 提供审查反馈
  • 添加适当的标签

审查结果

✅ 之前的问题已全部解决

问题 1:Schema 重复定义 ✅ 已修复

  • API 路由现在正确导入并使用 UpdateSystemSettingsSchema(第 4 行)
  • 移除了重复的 Schema 定义
  • 货币类型验证现在正确使用 CURRENCY_CONFIG 枚举

问题 2:不安全的类型断言 ✅ 已修复

  • 移除了 as any 类型断言
  • 验证后的数据类型自动匹配 UpdateSystemSettingsInput

📋 代码质量评估

1. API 路由实现 (src/app/api/admin/system-config/route.ts)

✅ 优点:

  • 认证检查完善(管理员权限验证)
  • 正确使用统一的验证 Schema
  • 错误处理全面(Zod 验证错误、通用错误)
  • 日志记录完整(包含用户 ID 和变更内容)
  • HTTP 状态码使用正确(401/400/500)
  • 类型安全,无类型断言

无问题发现


2. 验证 Schema (src/lib/validation/schemas.ts)

✅ 优点:

  • 验证规则完整且合理
  • 货币类型使用 z.enum() 正确验证 CURRENCY_CONFIG 的键
  • 错误消息清晰(中文提示)
  • 使用 z.coerce 处理数字类型转换
  • 范围限制合理:
    • 保留天数:1-365 天
    • 批量大小:1000-100000
    • 执行时间:非空字符串

无问题发现


3. Server Actions (src/actions/system-config.ts)

✅ 优点:

  • 正确使用 UpdateSystemSettingsSchema 进行验证
  • 权限检查完善
  • 错误处理全面(包含 Zod 验证错误)
  • 使用 revalidatePath 刷新多个相关路径的缓存
  • 日志记录完整

无问题发现


4. 类型定义和数据层

✅ 架构一致性:

  • UpdateSystemSettingsInput 类型定义完整
  • Repository 层正确处理可选字段(使用 undefined 检查)
  • Server Actions 和 API 路由功能重复是合理的设计(支持不同使用场景)

🎯 总体评价

代码质量:优秀 ⭐⭐⭐⭐⭐

  1. ✅ 成功修复了之前审查中发现的所有问题
  2. ✅ 代码实现符合项目架构规范
  3. ✅ 类型安全,无类型断言
  4. ✅ 验证规则完整且合理
  5. ✅ 错误处理全面
  6. ✅ 日志记录完整
  7. ✅ 认证和权限检查完善

无新问题发现,建议合并。 🚀


📝 测试建议

PR 描述中的测试计划很完整,建议重点测试:

  1. ✅ 启用/禁用自动清理开关
  2. ✅ 修改保留天数、执行时间、批量大小
  3. ✅ 配置持久化(重启后配置保持)
  4. ✅ 数据验证规则:
    • 边界值测试(保留天数:0, 1, 365, 366)
    • 批量大小边界值(999, 1000, 100000, 100001)
    • 货币类型验证(有效/无效的货币代码)
  5. ✅ 权限验证(非管理员用户访问)

🏷️ 已添加标签

  • enhancement - 功能增强

@ding113
ding113 merged commit bc2cdbf into main Oct 27, 2025
2 checks passed
@ding113 ding113 linked an issue Oct 27, 2025 that may be closed by this pull request
@ding113
ding113 deleted the claude/issue-6-20251027-1555 branch October 28, 2025 01:28
ding113 added a commit that referenced this pull request Feb 23, 2026
- Extract QUERY_CLIENT_DEFAULTS from AppProviders so test imports the
  actual config instead of duplicating it (bugbot #5)
- Store shared-timer state on globalThis for HMR safety, matching the
  pattern already used in session-cache (bugbot #6)
- Rename clearAllSessionsCache -> clearAllSessionsQueryCache and
  clearAllSessionCache -> clearAllCaches to eliminate single-char
  naming confusion (bugbot #7)
ding113 added a commit that referenced this pull request Feb 23, 2026
…and timers (#815)

* fix(dashboard): prevent memory leaks from unbounded polling, caches, and timers

- Configure QueryClient with gcTime=2min, staleTime=30s, and
  refetchIntervalInBackground=false to stop all 11 polling queries
  from firing in hidden browser tabs
- Cap useInfiniteQuery to maxPages=5 (250 entries) to prevent
  unbounded page accumulation in the logs table
- Replace per-instance setInterval in RelativeTime with a shared
  10s tick emitter (O(1) timers instead of O(n))
- Migrate SessionCache from unbounded Map to TTLMap with LRU
  eviction (activeSessionsCache maxSize=100, sessionDetailsCache
  maxSize=10000)
- Hoist formatResponseTime to module scope to avoid per-render
  closure allocation
- Add refreshWhenHidden=false to big-screen SWR configs

* fix: address PR #815 review comments

- Move misplaced JSDoc from formatResponseTime to calcPercentageChange
- Log errors in shared-timer catch block instead of silently swallowing
- Use @/ path alias for ttl-map import in session-cache

* fix: address remaining PR #815 review comments

- Extract QUERY_CLIENT_DEFAULTS from AppProviders so test imports the
  actual config instead of duplicating it (bugbot #5)
- Store shared-timer state on globalThis for HMR safety, matching the
  pattern already used in session-cache (bugbot #6)
- Rename clearAllSessionsCache -> clearAllSessionsQueryCache and
  clearAllSessionCache -> clearAllCaches to eliminate single-char
  naming confusion (bugbot #7)
@github-actions github-actions Bot mentioned this pull request Apr 19, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

自动日志清理功能接口 404

1 participant