Skip to content

Conversation

@lll9p
Copy link

@lll9p lll9p commented Jan 28, 2026

恢复了key的last_used_at的使用,默认10000key以内会触发,超过设定的阈值不会触发,对性能没有影响;增加了相关的设置UI。

  • feat: Add keys_sort_max_count setting + group override
  • feat: Track api_keys.last_used_at from request logs
  • feat: Conditional keys sorting + UI toggle via last_used_enabled

关联 Issue / Related Issue

变更内容 / Change Content

  • Bug 修复 / Bug fix
  • 新功能 / New feature
  • 其他改动 / Other changes

自查清单 / Checklist

  • 我已在本地测试过我的变更。 / I have tested my changes locally.
  • 我已更新了必要的文档。 / I have updated the necessary documentation.

Summary by CodeRabbit

  • New Features
    • Added last-used time tracking for API keys with intelligent sorting that automatically adapts based on group size.
    • Introduced configurable threshold setting to enable/disable last-used sorting for better performance on large key lists.
    • API response now includes metadata indicating whether last-used sorting is active.
    • UI conditionally displays last-used time information based on configuration.

✏️ Tip: You can customize this high-level summary in your review settings.

lll9p added 3 commits January 27, 2026 21:48
* feat: Add keys_sort_max_count setting + group override
* feat: Track api_keys.last_used_at from request logs
* feat: Conditional keys sorting + UI toggle via last_used_enabled
@coderabbitai
Copy link

coderabbitai bot commented Jan 28, 2026

Walkthrough

The PR implements a configurable threshold-based mechanism for sorting API keys by last-used time. A probe query checks if the threshold is exceeded; if not, sorting by last-used is enabled. The system tracks key usage timestamps and updates them during request logging, with conditional UI display based on an enabled flag passed in API response metadata.

Changes

Cohort / File(s) Summary
Backend Handler Logic
internal/handler/key_handler.go
Captures Group object from findGroupByID; introduces probe query (threshold+1 row check) to avoid expensive COUNT(*); derives lastUsedEnabled flag; passes to ListKeysInGroupQuery; injects flag into paginated response Meta.
Data Models & Configuration
internal/models/types.go, internal/types/types.go
Adds LastUsedAt timestamp field to APIKey with GORM index; adds KeysSortMaxCount int field to GroupConfig and SystemSettings with validation (min=0, default=10000).
Query & Service Layer
internal/services/key_service.go, internal/services/request_log_service.go
Extends ListKeysInGroupQuery with sortByLastUsed boolean parameter to conditionally order by last_used_at; adds last_used_at update logic in request_log_service via CASE expression for safe timestamp tracking.
Pagination Response
internal/response/pagination.go
Adds optional Meta field (type any) to PaginatedResponse struct with omitempty tag.
Internationalization
internal/i18n/locales/en-US.go, internal/i18n/locales/ja-JP.go, internal/i18n/locales/zh-CN.go
Adds two new configuration translation keys per locale (config.keys_sort_max_count and config.keys_sort_max_count_desc) describing the threshold behavior in English, Japanese, and Simplified Chinese.
Frontend API & Types
web/src/api/keys.ts
Updates getGroupKeys response to use standardized Pagination type; adds optional meta field with last_used_enabled flag.
Frontend UI & Locales
web/src/components/keys/KeyTable.vue, web/src/locales/en-US.ts, web/src/locales/ja-JP.ts, web/src/locales/zh-CN.ts
Adds reactive lastUsedEnabled flag initialized from API meta; conditionally renders last-used badge based on flag; adds keysSortMaxCount translation keys in three locales.

Sequence Diagram(s)

sequenceDiagram
    participant Frontend as Frontend (KeyTable)
    participant Handler as Handler (ListKeysInGroup)
    participant DB as Database
    participant Service as KeyService
    
    Frontend->>Handler: GET /keys (group_id, page, page_size)
    Handler->>DB: SELECT group WHERE id = ?
    DB-->>Handler: Group object + Config
    
    Handler->>DB: Probe: SELECT * LIMIT ? OFFSET threshold
    DB-->>Handler: Row result (RowsAffected)
    Handler->>Handler: lastUsedEnabled = (RowsAffected == 0)
    
    Handler->>Service: ListKeysInGroupQuery(groupID, filters, lastUsedEnabled)
    Service->>Service: Build ORDER BY clause based on lastUsedEnabled
    alt lastUsedEnabled = true
        Service->>DB: ORDER BY last_used_at DESC, updated_at DESC
    else lastUsedEnabled = false
        Service->>DB: ORDER BY id DESC
    end
    
    DB-->>Service: Keys result set
    Service-->>Handler: Query result
    Handler->>Handler: Decrypt keys
    Handler->>Handler: Inject lastUsedEnabled into Meta
    Handler-->>Frontend: { items, pagination, meta: { last_used_enabled } }
    
    Frontend->>Frontend: Read meta.last_used_enabled
    Frontend->>Frontend: Conditionally render last-used badge
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • tbphp

Poem

🐰 A threshold to sort by time,
No costly counts—the probe works fine,
Last used tracked with every call,
Frontend knows to show it all!
Keys now ordered, not by ID—
Efficiency hops wild and free! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: Add key sorting threshold' accurately and concisely describes the main feature: introducing a configurable threshold for key sorting based on last_used_at, with fallback to ID ordering for performance.
Description check ✅ Passed The PR description covers the core feature (restoring last_used_at with threshold-based triggering), implementation details, and configuration additions, with checklist items completed. However, the description mixes languages and could be more structured.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai bot requested a review from tbphp January 28, 2026 00:58
@tbphp tbphp added the enhancement New feature or request label Jan 29, 2026
@tbphp
Copy link
Owner

tbphp commented Feb 9, 2026

感谢你的贡献,抱歉不能合并你的代码。我会用另外一种方式来恢复 last_used_at ,计划下个版本发布,详情可见 #383

@tbphp tbphp closed this Feb 9, 2026
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.

2 participants