Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions backend/internal/service/model_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"fmt"
"math"
"strings"
"time"

"github.com/new-api-tools/backend/internal/cache"
Expand Down Expand Up @@ -134,7 +135,7 @@ func (s *ModelStatusService) GetModelStatus(modelName, window string) (map[strin
COUNT(*) as total,
SUM(CASE WHEN type = 2 AND completion_tokens > 0 THEN 1 ELSE 0 END) as success,
SUM(CASE WHEN type = 5 THEN 1 ELSE 0 END) as failure,
SUM(CASE WHEN type = 2 AND completion_tokens = 0 THEN 1 ELSE 0 END) as empty
SUM(CASE WHEN type = 2 AND completion_tokens = 0 THEN 1 ELSE 0 END) as empty_count
FROM logs
WHERE model_name = ?
AND created_at >= ? AND created_at < ?
Expand All @@ -143,7 +144,10 @@ func (s *ModelStatusService) GetModelStatus(modelName, window string) (map[strin
startTime, slotSeconds,
startTime, slotSeconds))

rows, _ := s.db.Query(slotQuery, modelName, startTime, now)
rows, err := s.db.Query(slotQuery, modelName, startTime, now)
if err != nil {
return nil, err
}

// Initialize all slots with zeros
type slotInfo struct {
Expand All @@ -163,7 +167,7 @@ func (s *ModelStatusService) GetModelStatus(modelName, window string) (map[strin
total: toInt64(row["total"]),
success: toInt64(row["success"]),
failure: toInt64(row["failure"]),
empty: toInt64(row["empty"]),
empty: toInt64(row["empty_count"]),
}
}
}
Expand Down Expand Up @@ -240,13 +244,18 @@ func (s *ModelStatusService) GetModelStatus(modelName, window string) (map[strin
// GetMultipleModelsStatus returns status for multiple models
func (s *ModelStatusService) GetMultipleModelsStatus(modelNames []string, window string) ([]map[string]interface{}, error) {
results := make([]map[string]interface{}, 0, len(modelNames))
failedModels := make([]string, 0)
for _, name := range modelNames {
status, err := s.GetModelStatus(name, window)
if err != nil {
failedModels = append(failedModels, fmt.Sprintf("%s: %v", name, err))
continue
}
results = append(results, status)
}
if len(modelNames) > 0 && len(results) == 0 && len(failedModels) > 0 {
return nil, fmt.Errorf("all model status queries failed: %s", strings.Join(failedModels, "; "))
}
return results, nil
}

Expand Down