Skip to content

Comments

# feat(leaderboard): 添加排行榜排序和筛选功能#448

Merged
ding113 merged 5 commits intoding113:devfrom
YewFence:feature/leaderboard-sort-filter
Dec 25, 2025
Merged

# feat(leaderboard): 添加排行榜排序和筛选功能#448
ding113 merged 5 commits intoding113:devfrom
YewFence:feature/leaderboard-sort-filter

Conversation

@YewFence
Copy link
Contributor

@YewFence YewFence commented Dec 25, 2025

Summary

Add client-side sorting, type filtering, and visual enhancements to the Leaderboard feature, improving data analysis experience.

Closes #442 - Implements column sorting and extends provider type filter to the provider leaderboard.


变更摘要

为排行榜(Leaderboard)添加了客户端排序、类型筛选和视觉增强功能,提升数据分析体验。

主要改动

  • 表头排序功能:所有排行榜(用户、供应商、模型、缓存命中率)现在支持点击表头进行列排序

    • 支持三态切换:升序 → 降序 → 无排序
    • 排序列加粗显示:当前排序列的表头和单元格自动加粗,提升视觉识别度
  • 成本列默认加粗:成本/消耗金额列在无排序时默认加粗显示

    • 突出显示关键财务数据
    • 排序其他列时自动取消加粗,避免视觉干扰
  • 类型筛选器扩展:将 ProviderTypeFilter 组件从缓存命中率排行榜扩展到供应商排行榜

    • API 层支持 providerType 参数
    • 缓存层和数据库查询层同步更新

涉及文件

文件 改动说明
src/app/[locale]/dashboard/leaderboard/_components/leaderboard-table.tsx 添加排序状态管理、UI 和加粗逻辑
src/app/[locale]/dashboard/leaderboard/_components/leaderboard-view.tsx 扩展列定义,添加 sortKey/getValue/defaultBold
src/app/api/leaderboard/route.ts API 支持 provider scope 的 providerType 参数
src/lib/redis/leaderboard-cache.ts 缓存层传递 providerType
src/repository/leaderboard.ts 数据库查询支持 providerType 过滤

技术细节

排序列加粗实现

  • 新增 defaultBold 属性到 ColumnDef 接口
  • 加粗逻辑:shouldBold = isActiveSortColumn || (col.defaultBold && noSorting)
  • 成本列配置 defaultBold: true,在无排序时保持加粗

测试验证

  • bun run lint 通过
  • bun run typecheck 通过
  • 手动测试排序功能正常
  • 手动测试类型筛选功能正常
  • 手动测试加粗样式切换正常

截图/演示

排序功能

image

筛选功能

image

Checklist

  • 目标分支为 dev
  • 所有状态检查已通过
  • main 无直接冲突
  • 引用相关 Issue

Description enhanced by Claude AI

YewFence and others added 3 commits December 25, 2025 11:13
- 为供应商排行榜和缓存命中率排行榜添加客户端列排序功能
- 点击表头可切换升序/降序/无排序
- 为供应商排行榜新增 ProviderTypeFilter 类型筛选器
- 扩展 API 和缓存层支持供应商排行榜的 providerType 参数

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

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

fix(leaderboard): 修复 TableRow key 警告

将 key 计算提取到变量中,避免类型转换问题

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

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

feat(leaderboard): 为所有排行榜添加列排序功能

- 用户排行榜支持按用户名、请求数、Token数、消费金额排序
- 模型排行榜支持按模型名、请求数、Token数、消费金额、成功率排序
- 供应商排行榜和缓存命中率排行榜新增供应商名称排序
- 移除前三名字体加粗逻辑,避免排序后视觉混淆

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

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

refactor(leaderboard): 排序时保持前三名高亮样式

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

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

fix(leaderboard): 修复 TableRow key 为 undefined 的警告

当 getRowKey 返回 undefined 时 fallback 到 index
- 添加 defaultBold 属性支持列默认加粗
- 排序生效时对应列字体加粗
- 成本列在无排序时默认加粗,排序其他列时取消加粗
- 移除成本列固定的 font-semibold 样式

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @YewFence, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在通过引入客户端排序、扩展供应商类型筛选功能以及优化视觉呈现,显著提升排行榜页面的用户体验和数据分析能力。这些改进使用户能够更灵活地探索和理解排行榜数据,从而更高效地进行决策。

Highlights

  • 排行榜排序功能: 为所有排行榜(用户、供应商、模型、缓存命中率)添加了客户端排序功能,支持点击表头进行升序、降序和无排序的三态切换。
  • 视觉增强: 排序时自动取消前三名高亮,并加粗显示当前排序列的表头和单元格。成本/消耗金额列在无排序时默认加粗。
  • 供应商类型筛选扩展: 将 "ProviderTypeFilter" 组件从缓存命中率排行榜扩展到供应商排行榜,允许用户按供应商类型筛选数据。
  • 后端支持: API、缓存层和数据库查询层已同步更新,以支持 "providerType" 参数进行供应商排行榜的筛选。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 为排行榜添加了客户端排序和供应商类型筛选功能,整体实现良好,代码结构清晰。主要改动涉及前端表格组件、API 路由、缓存层和数据库查询,覆盖了完整的业务链路。

在前端 leaderboard-table.tsx 中,客户端排序逻辑实现正确,但发现一个关于“前三名高亮”的显示 bug,即排序后高亮未按预期取消。此外,在 leaderboard-table.tsxleaderboard-view.tsx 中存在少量可优化的重复代码,已在具体评论中提出重构建议。

后端的改动,包括 API、缓存和数据库层对 providerType 过滤的支持,实现得非常出色,特别是 repository/leaderboard.ts 中动态构建 SQL 查询的方式值得称赞。

总体而言,这是一次高质量的功能增强。在修复提到的 bug 和进行少量代码优化后,即可合并。

Comment on lines 178 to 180
const isActiveSortColumn = sortKey === col.sortKey && sortDirection !== null;
const noSorting = sortKey === null;
const shouldBold = isActiveSortColumn || (col.defaultBold && noSorting);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

计算列是否应加粗的逻辑 (shouldBold) 在 TableHeader (此处) 和 TableBody (L208-L210) 中被重复。为了提高代码的可维护性和减少重复,建议将此逻辑提取到一个独立的辅助函数中,例如:

const getShouldBold = (col: ColumnDef<T>) => {
  const isActiveSortColumn = sortKey === col.sortKey && sortDirection !== null;
  const noSorting = sortKey === null;
  return isActiveSortColumn || (col.defaultBold && noSorting);
};

然后在两处都调用 getShouldBold(col)

@github-actions github-actions bot added enhancement New feature or request area:UI area:statistics size/S Small PR (< 200 lines) labels Dec 25, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

No significant issues identified in this PR. The implementation is well-structured and follows the existing patterns in the codebase.

PR Size: S

  • Lines changed: 251 (194 additions + 57 deletions)
  • Files changed: 5

Review Analysis

Changes reviewed:

  1. leaderboard-table.tsx: Added client-side sorting with proper state management
  2. leaderboard-view.tsx: Extended column definitions with sortKey/getValue/defaultBold
  3. route.ts: API now accepts providerType for provider scope
  4. leaderboard-cache.ts: Cache layer properly passes providerType to queries
  5. leaderboard.ts: Database queries correctly filter by providerType

Validation performed:

  • Sorting logic correctly handles tri-state cycling (asc → desc → null)
  • Type definitions properly extended with optional properties
  • API parameter validation is consistent with existing patterns
  • Cache keys correctly incorporate providerType to avoid cache collisions
  • Database queries properly filter with conditional where clauses

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Existing patterns followed (no dedicated tests for UI sorting is acceptable for this scope)
  • Code clarity - Good

Automated review by Claude AI

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

No significant issues identified in this PR. The implementation is well-structured and follows the existing patterns in the codebase.

PR Size: S

  • Lines changed: 251 (194 additions + 57 deletions)
  • Files changed: 5

Review Analysis

Changes reviewed:

  1. leaderboard-table.tsx: Added client-side sorting with proper state management
  2. leaderboard-view.tsx: Extended column definitions with sortKey/getValue/defaultBold
  3. route.ts: API now accepts providerType for provider scope
  4. leaderboard-cache.ts: Cache layer properly passes providerType to queries
  5. leaderboard.ts: Database queries correctly filter by providerType

Validation performed:

  • Sorting logic correctly handles tri-state cycling (asc → desc → null)
  • Type definitions properly extended with optional properties
  • API parameter validation is consistent with existing patterns
  • Cache keys correctly incorporate providerType to avoid cache collisions
  • Database queries properly filter with conditional where clauses

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Existing patterns followed (no dedicated tests for UI sorting is acceptable for this scope)
  • Code clarity - Good

Automated review by Claude AI

YewFence and others added 2 commits December 25, 2025 13:22
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@ding113 ding113 merged commit a8e120f into ding113:dev Dec 25, 2025
4 of 6 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 25, 2025
@github-actions github-actions bot mentioned this pull request Dec 29, 2025
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:statistics area:UI enhancement New feature or request size/S Small PR (< 200 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants