feat(ops): add usage_logs partition status to ops dashboard

Add partition management integration to the smart ops system:
- Backend: Add GetUsageLogsPartitionStatus endpoint in OpsHandler
- Backend: Add partition query methods in OpsRepository
- Backend: Add UsageLogsPartitionStatus type in OpsService
- Frontend: Add OpsPartitionStatusCard component
- Frontend: Add partition status display in OpsDashboard
- i18n: Add Chinese and English translations

The partition status card shows:
- Whether usage_logs is partitioned
- Current row count vs threshold (100K)
- Partition count (if partitioned)
- Warning message when partitioning is recommended

This allows administrators to monitor partition status directly
from the ops dashboard without checking server logs.
This commit is contained in:
User
2026-04-16 23:16:17 +08:00
parent eb5adbbae5
commit 60d15d2ba4
10 changed files with 409 additions and 1 deletions

View File

@@ -923,3 +923,22 @@ func parseOpsDuration(v string) (time.Duration, bool) {
return 0, false
}
}
// ==================== Usage Logs Partition Management ====================
// GetUsageLogsPartitionStatus returns partition status of usage_logs table.
// GET /api/v1/admin/ops/partition-status
func (h *OpsHandler) GetUsageLogsPartitionStatus(c *gin.Context) {
if h.opsService == nil {
response.Error(c, http.StatusServiceUnavailable, "Ops service not available")
return
}
status, err := h.opsService.GetUsageLogsPartitionStatus(c.Request.Context())
if err != nil {
response.ErrorFrom(c, err)
return
}
response.Success(c, status)
}

View File

@@ -1526,3 +1526,78 @@ func opsNullInt16(v *int16) any {
}
return sql.NullInt64{Int64: int64(*v), Valid: true}
}
// ==================== Usage Logs Partition Management ====================
// IsUsageLogsPartitioned checks if usage_logs table is partitioned.
func (r *opsRepository) IsUsageLogsPartitioned(ctx context.Context) (bool, error) {
if r == nil || r.db == nil {
return false, fmt.Errorf("nil ops repository")
}
var isPartitioned bool
err := r.db.QueryRowContext(ctx, `
SELECT EXISTS(
SELECT 1
FROM pg_partitioned_table pt
JOIN pg_class c ON c.oid = pt.partrelid
WHERE c.relname = 'usage_logs'
)
`).Scan(&isPartitioned)
if err != nil {
return false, fmt.Errorf("check usage_logs partitioned: %w", err)
}
return isPartitioned, nil
}
// GetUsageLogsRowCount returns the approximate row count of usage_logs table.
func (r *opsRepository) GetUsageLogsRowCount(ctx context.Context) (int64, error) {
if r == nil || r.db == nil {
return 0, fmt.Errorf("nil ops repository")
}
var rowCount int64
// Use pg_class.reltuples for fast approximate count (avoid slow COUNT(*) on large tables)
err := r.db.QueryRowContext(ctx, `
SELECT COALESCE(
(SELECT reltuples::bigint FROM pg_class WHERE relname = 'usage_logs'),
0
)
`).Scan(&rowCount)
if err != nil {
// Fallback to actual COUNT if pg_class estimate fails
err = r.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM usage_logs`).Scan(&rowCount)
if err != nil {
return 0, fmt.Errorf("get usage_logs row count: %w", err)
}
}
return rowCount, nil
}
// GetUsageLogsPartitionCount returns the number of partitions for usage_logs table.
func (r *opsRepository) GetUsageLogsPartitionCount(ctx context.Context) (int, error) {
if r == nil || r.db == nil {
return 0, fmt.Errorf("nil ops repository")
}
var count int
err := r.db.QueryRowContext(ctx, `
SELECT COUNT(*)
FROM pg_class c
WHERE c.relkind = 'r'
AND c.relname LIKE 'usage_logs_%'
AND EXISTS (
SELECT 1 FROM pg_inherits i
WHERE i.inhrelid = c.oid
AND EXISTS (
SELECT 1 FROM pg_class parent
WHERE parent.oid = i.inhparent
AND parent.relname = 'usage_logs'
)
)
`).Scan(&count)
if err != nil {
return 0, fmt.Errorf("get usage_logs partition count: %w", err)
}
return count, nil
}

View File

@@ -202,6 +202,9 @@ func registerOpsRoutes(admin *gin.RouterGroup, h *handler.Handlers) {
ops.GET("/dashboard/error-trend", h.Admin.Ops.GetDashboardErrorTrend)
ops.GET("/dashboard/error-distribution", h.Admin.Ops.GetDashboardErrorDistribution)
ops.GET("/dashboard/openai-token-stats", h.Admin.Ops.GetDashboardOpenAITokenStats)
// Usage logs partition management
ops.GET("/partition-status", h.Admin.Ops.GetUsageLogsPartitionStatus)
}
}

View File

@@ -63,6 +63,11 @@ type OpsRepository interface {
UpsertDailyMetrics(ctx context.Context, startTime, endTime time.Time) error
GetLatestHourlyBucketStart(ctx context.Context) (time.Time, bool, error)
GetLatestDailyBucketDate(ctx context.Context) (time.Time, bool, error)
// Usage logs partition management
IsUsageLogsPartitioned(ctx context.Context) (bool, error)
GetUsageLogsRowCount(ctx context.Context) (int64, error)
GetUsageLogsPartitionCount(ctx context.Context) (int, error)
}
type OpsInsertErrorLogInput struct {

View File

@@ -724,3 +724,72 @@ func sanitizeErrorBodyForStorage(raw string, maxBytes int) (sanitized string, tr
}
return raw, false
}
// ==================== Usage Logs Partition Management ====================
// UsageLogsPartitionStatus represents the partition status of usage_logs table.
type UsageLogsPartitionStatus struct {
IsPartitioned bool `json:"is_partitioned"`
RowCount int64 `json:"row_count"`
PartitionCount int `json:"partition_count"`
ThresholdRows int64 `json:"threshold_rows"` // 100000
NeedsPartitioning bool `json:"needs_partitioning"` // rowCount >= threshold && !isPartitioned
WarningLevel string `json:"warning_level"` // "none", "info", "warning"
LastCheckedAt string `json:"last_checked_at"`
}
// GetUsageLogsPartitionStatus returns the current partition status of usage_logs table.
func (s *OpsService) GetUsageLogsPartitionStatus(ctx context.Context) (*UsageLogsPartitionStatus, error) {
if s.opsRepo == nil {
return nil, errors.New("ops repository not available")
}
status := &UsageLogsPartitionStatus{
ThresholdRows: 100000,
LastCheckedAt: time.Now().UTC().Format(time.RFC3339),
}
// Check if usage_logs is partitioned
isPartitioned, err := s.opsRepo.IsUsageLogsPartitioned(ctx)
if err != nil {
log.Printf("[Ops] GetUsageLogsPartitionStatus check partitioned failed: %v", err)
return nil, err
}
status.IsPartitioned = isPartitioned
// Get row count
rowCount, err := s.opsRepo.GetUsageLogsRowCount(ctx)
if err != nil {
log.Printf("[Ops] GetUsageLogsPartitionStatus get row count failed: %v", err)
return nil, err
}
status.RowCount = rowCount
// Determine partition count if partitioned
if isPartitioned {
count, err := s.opsRepo.GetUsageLogsPartitionCount(ctx)
if err != nil {
log.Printf("[Ops] GetUsageLogsPartitionStatus get partition count failed: %v", err)
// Non-critical, continue
} else {
status.PartitionCount = count
}
}
// Determine warning level and needs_partitioning
if isPartitioned {
status.WarningLevel = "none"
status.NeedsPartitioning = false
} else if rowCount >= status.ThresholdRows {
status.WarningLevel = "warning"
status.NeedsPartitioning = true
} else if rowCount >= 50000 {
status.WarningLevel = "info"
status.NeedsPartitioning = false
} else {
status.WarningLevel = "none"
status.NeedsPartitioning = false
}
return status, nil
}