- 新增 ops 主仓库(ops_repo.go) - 实现告警数据访问(ops_repo_alerts.go) - 实现仪表板数据访问(ops_repo_dashboard.go) - 实现直方图数据访问(ops_repo_histograms.go) - 实现延迟直方图桶逻辑(ops_repo_latency_histogram_buckets.go) - 新增延迟直方图桶测试(ops_repo_latency_histogram_buckets_test.go) - 实现指标数据访问(ops_repo_metrics.go) - 实现预聚合数据访问(ops_repo_preagg.go) - 实现请求详情数据访问(ops_repo_request_details.go) - 实现趋势数据访问(ops_repo_trends.go) - 实现窗口统计数据访问(ops_repo_window_stats.go) - 更新并发缓存支持 ops 场景 - 注册 repository 依赖注入
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
)
|
|
|
|
func (r *opsRepository) GetWindowStats(ctx context.Context, filter *service.OpsDashboardFilter) (*service.OpsWindowStats, error) {
|
|
if r == nil || r.db == nil {
|
|
return nil, fmt.Errorf("nil ops repository")
|
|
}
|
|
if filter == nil {
|
|
return nil, fmt.Errorf("nil filter")
|
|
}
|
|
if filter.StartTime.IsZero() || filter.EndTime.IsZero() {
|
|
return nil, fmt.Errorf("start_time/end_time required")
|
|
}
|
|
|
|
start := filter.StartTime.UTC()
|
|
end := filter.EndTime.UTC()
|
|
if start.After(end) {
|
|
return nil, fmt.Errorf("start_time must be <= end_time")
|
|
}
|
|
// Bound excessively large windows to prevent accidental heavy queries.
|
|
if end.Sub(start) > 24*time.Hour {
|
|
return nil, fmt.Errorf("window too large")
|
|
}
|
|
|
|
successCount, tokenConsumed, err := r.queryUsageCounts(ctx, filter, start, end)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
errorTotal, _, _, _, _, _, err := r.queryErrorCounts(ctx, filter, start, end)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &service.OpsWindowStats{
|
|
StartTime: start,
|
|
EndTime: end,
|
|
|
|
SuccessCount: successCount,
|
|
ErrorCountTotal: errorTotal,
|
|
TokenConsumed: tokenConsumed,
|
|
}, nil
|
|
}
|