Skip to content

Fix/model status mysql query#22

Open
0401lucky wants to merge 2 commits into
james-6-23:mainfrom
0401lucky:fix/model-status-mysql-query
Open

Fix/model status mysql query#22
0401lucky wants to merge 2 commits into
james-6-23:mainfrom
0401lucky:fix/model-status-mysql-query

Conversation

@0401lucky
Copy link
Copy Markdown

@0401lucky 0401lucky commented Apr 27, 2026

Summary by CodeRabbit

  • New Features

    • Added ability to bypass caching for model status queries via query parameter.
  • Bug Fixes

    • Fixed timestamp unit mismatches in model status calculations.
    • Improved error handling when querying multiple models.
  • Tests

    • Added comprehensive test coverage for model status queries and timestamp handling.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

📝 Walkthrough

Walkthrough

The PR adds cache-aware status retrieval methods to the model status service while introducing SQL timestamp normalization to handle millisecond/second unit mismatches in logs. It moves shared type conversion helpers to a new utility module and includes comprehensive test coverage for SQL generation patterns.

Changes

Cohort / File(s) Summary
Handler Cache Routing
backend/internal/handler/model_status.go
Adds no_cache query parameter parsing with isNoCacheRequest helper to determine cache usage, routing requests to new *WithCache service methods.
Service Cache Methods & SQL Normalization
backend/internal/service/model_status.go
Introduces GetModelStatusWithCache, GetMultipleModelsStatusWithCache, and GetAllModelsStatusWithCache methods; adds timestamp normalization via CASE/FLOOR for logs.created_at unit mismatches; implements runtime schema detection for completion_tokens column; updates result parsing to use new empty_count SQL alias; adds error aggregation for multi-model queries.
Test Suite for SQL Generation
backend/internal/service/model_status_test.go
Adds tests validating normalized timestamp expressions, available-models queries with time windowing, and model-status slot queries under completion_tokens enabled/disabled modes.
Type Conversion Refactoring
backend/internal/service/type_conversions.go (new), backend/internal/service/user_management.go
Extracts toInt64 and toString helper functions into shared type_conversions.go module; removes duplicates from user_management.go.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Cache paths now branch where once they were straight,
Timestamps normalized, no more time debate!
Helpers extracted, shared code runs free,
SQL queries aligned in perfect harmony.
Tests verify each logical flow—hops gleefully 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix/model status mysql query' refers to bug fixes in model status MySQL queries, which aligns with the primary changes refactoring cache-aware methods and normalizing timestamp handling in SQL generation across multiple service files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.11.4)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


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

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
backend/internal/service/type_conversions.go (1)

6-32: Consider handling unsigned integer types for broader database driver compatibility.

Some database drivers may return unsigned integer types (uint, uint32, uint64) for COUNT or similar aggregate results. Currently these would fall through to the default case and return 0.

♻️ Optional: Add unsigned integer support
 	case float32:
 		return int64(val)
+	case uint64:
+		return int64(val)
+	case uint:
+		return int64(val)
+	case uint32:
+		return int64(val)
 	case string:
 		var n int64
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/internal/service/type_conversions.go` around lines 6 - 32, toInt64
currently ignores unsigned integer types returned by some DB drivers; update the
function to handle uint, uint8, uint16, uint32, uint64 (and optionally uintptr)
by adding cases for those types in the type switch (e.g., case uint64: if val >
math.MaxInt64 { return math.MaxInt64 } else { return int64(val) }) and similarly
cast other uint* types to int64; remember to import math for MaxInt64 to safely
clamp uint64 values that would overflow int64.
backend/internal/service/model_status.go (1)

138-143: Global state may complicate testing and hot schema changes.

The sync.Once pattern caches the column existence check for the process lifetime. This is efficient but means:

  1. Schema migrations adding completion_tokens won't be detected until service restart
  2. Tests sharing this global state may have interdependencies

This is likely acceptable given schema changes typically require deployment, but worth noting.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/internal/service/model_status.go` around lines 138 - 143, The
package-level caching via logsCompletionTokensOnce and
logsHasCompletionTokensCol in logsHaveCompletionTokens() prevents detecting
schema changes and interferes with tests; refactor by moving the cache into
ModelStatusService (add a logsCompletionTokensOnce sync.Once and
logsHasCompletionTokensCol bool as fields) or provide a test-only reset (e.g.,
ResetLogsCompletionTokensCache) and change logsHaveCompletionTokens() to call
s.db.ColumnExists("logs","completion_tokens") inside that instance-level once so
s.db.ColumnExists is invoked per service instance and test suites can reset the
cache as needed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@backend/internal/service/model_status.go`:
- Around line 138-143: The package-level caching via logsCompletionTokensOnce
and logsHasCompletionTokensCol in logsHaveCompletionTokens() prevents detecting
schema changes and interferes with tests; refactor by moving the cache into
ModelStatusService (add a logsCompletionTokensOnce sync.Once and
logsHasCompletionTokensCol bool as fields) or provide a test-only reset (e.g.,
ResetLogsCompletionTokensCache) and change logsHaveCompletionTokens() to call
s.db.ColumnExists("logs","completion_tokens") inside that instance-level once so
s.db.ColumnExists is invoked per service instance and test suites can reset the
cache as needed.

In `@backend/internal/service/type_conversions.go`:
- Around line 6-32: toInt64 currently ignores unsigned integer types returned by
some DB drivers; update the function to handle uint, uint8, uint16, uint32,
uint64 (and optionally uintptr) by adding cases for those types in the type
switch (e.g., case uint64: if val > math.MaxInt64 { return math.MaxInt64 } else
{ return int64(val) }) and similarly cast other uint* types to int64; remember
to import math for MaxInt64 to safely clamp uint64 values that would overflow
int64.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a44edf39-78da-4d20-a625-1e48b1dec6a0

📥 Commits

Reviewing files that changed from the base of the PR and between 62c818e and 3cc6a73.

📒 Files selected for processing (5)
  • backend/internal/handler/model_status.go
  • backend/internal/service/model_status.go
  • backend/internal/service/model_status_test.go
  • backend/internal/service/type_conversions.go
  • backend/internal/service/user_management.go
💤 Files with no reviewable changes (1)
  • backend/internal/service/user_management.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant