- 新增 ops 主服务(ops_service.go)和端口定义(ops_port.go) - 实现账号可用性检查服务(ops_account_availability.go) - 实现数据聚合服务(ops_aggregation_service.go) - 实现告警评估服务(ops_alert_evaluator_service.go) - 实现告警管理服务(ops_alerts.go) - 实现数据清理服务(ops_cleanup_service.go) - 实现并发控制服务(ops_concurrency.go) - 实现仪表板服务(ops_dashboard.go) - 实现错误处理服务(ops_errors.go) - 实现直方图服务(ops_histograms.go) - 实现指标采集服务(ops_metrics_collector.go) - 实现查询模式服务(ops_query_mode.go) - 实现实时监控服务(ops_realtime.go) - 实现请求详情服务(ops_request_details.go) - 实现重试机制服务(ops_retry.go) - 实现配置管理服务(ops_settings.go) - 实现趋势分析服务(ops_trends.go) - 实现窗口统计服务(ops_window_stats.go) - 添加 ops 相关领域常量 - 注册 service 依赖注入
158 lines
3.8 KiB
Go
158 lines
3.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// GetAccountAvailabilityStats returns current account availability stats.
|
|
//
|
|
// Query-level filtering is intentionally limited to platform/group to match the dashboard scope.
|
|
func (s *OpsService) GetAccountAvailabilityStats(ctx context.Context, platformFilter string, groupIDFilter *int64) (
|
|
map[string]*PlatformAvailability,
|
|
map[int64]*GroupAvailability,
|
|
map[int64]*AccountAvailability,
|
|
*time.Time,
|
|
error,
|
|
) {
|
|
if err := s.RequireMonitoringEnabled(ctx); err != nil {
|
|
return nil, nil, nil, nil, err
|
|
}
|
|
|
|
accounts, err := s.listAllAccountsForOps(ctx, platformFilter)
|
|
if err != nil {
|
|
return nil, nil, nil, nil, err
|
|
}
|
|
|
|
if groupIDFilter != nil && *groupIDFilter > 0 {
|
|
filtered := make([]Account, 0, len(accounts))
|
|
for _, acc := range accounts {
|
|
for _, grp := range acc.Groups {
|
|
if grp != nil && grp.ID == *groupIDFilter {
|
|
filtered = append(filtered, acc)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
accounts = filtered
|
|
}
|
|
|
|
now := time.Now()
|
|
collectedAt := now
|
|
|
|
platform := make(map[string]*PlatformAvailability)
|
|
group := make(map[int64]*GroupAvailability)
|
|
account := make(map[int64]*AccountAvailability)
|
|
|
|
for _, acc := range accounts {
|
|
if acc.ID <= 0 {
|
|
continue
|
|
}
|
|
|
|
isTempUnsched := false
|
|
if acc.TempUnschedulableUntil != nil && now.Before(*acc.TempUnschedulableUntil) {
|
|
isTempUnsched = true
|
|
}
|
|
|
|
isRateLimited := acc.RateLimitResetAt != nil && now.Before(*acc.RateLimitResetAt)
|
|
isOverloaded := acc.OverloadUntil != nil && now.Before(*acc.OverloadUntil)
|
|
hasError := acc.Status == StatusError
|
|
|
|
// Normalize exclusive status flags so the UI doesn't show conflicting badges.
|
|
if hasError {
|
|
isRateLimited = false
|
|
isOverloaded = false
|
|
}
|
|
|
|
isAvailable := acc.Status == StatusActive && acc.Schedulable && !isRateLimited && !isOverloaded && !isTempUnsched
|
|
|
|
if acc.Platform != "" {
|
|
if _, ok := platform[acc.Platform]; !ok {
|
|
platform[acc.Platform] = &PlatformAvailability{
|
|
Platform: acc.Platform,
|
|
}
|
|
}
|
|
p := platform[acc.Platform]
|
|
p.TotalAccounts++
|
|
if isAvailable {
|
|
p.AvailableCount++
|
|
}
|
|
if isRateLimited {
|
|
p.RateLimitCount++
|
|
}
|
|
if hasError {
|
|
p.ErrorCount++
|
|
}
|
|
}
|
|
|
|
for _, grp := range acc.Groups {
|
|
if grp == nil || grp.ID <= 0 {
|
|
continue
|
|
}
|
|
if _, ok := group[grp.ID]; !ok {
|
|
group[grp.ID] = &GroupAvailability{
|
|
GroupID: grp.ID,
|
|
GroupName: grp.Name,
|
|
Platform: grp.Platform,
|
|
}
|
|
}
|
|
g := group[grp.ID]
|
|
g.TotalAccounts++
|
|
if isAvailable {
|
|
g.AvailableCount++
|
|
}
|
|
if isRateLimited {
|
|
g.RateLimitCount++
|
|
}
|
|
if hasError {
|
|
g.ErrorCount++
|
|
}
|
|
}
|
|
|
|
displayGroupID := int64(0)
|
|
displayGroupName := ""
|
|
if len(acc.Groups) > 0 && acc.Groups[0] != nil {
|
|
displayGroupID = acc.Groups[0].ID
|
|
displayGroupName = acc.Groups[0].Name
|
|
}
|
|
|
|
item := &AccountAvailability{
|
|
AccountID: acc.ID,
|
|
AccountName: acc.Name,
|
|
Platform: acc.Platform,
|
|
GroupID: displayGroupID,
|
|
GroupName: displayGroupName,
|
|
Status: acc.Status,
|
|
|
|
IsAvailable: isAvailable,
|
|
IsRateLimited: isRateLimited,
|
|
IsOverloaded: isOverloaded,
|
|
HasError: hasError,
|
|
|
|
ErrorMessage: acc.ErrorMessage,
|
|
}
|
|
|
|
if isRateLimited && acc.RateLimitResetAt != nil {
|
|
item.RateLimitResetAt = acc.RateLimitResetAt
|
|
remainingSec := int64(time.Until(*acc.RateLimitResetAt).Seconds())
|
|
if remainingSec > 0 {
|
|
item.RateLimitRemainingSec = &remainingSec
|
|
}
|
|
}
|
|
if isOverloaded && acc.OverloadUntil != nil {
|
|
item.OverloadUntil = acc.OverloadUntil
|
|
remainingSec := int64(time.Until(*acc.OverloadUntil).Seconds())
|
|
if remainingSec > 0 {
|
|
item.OverloadRemainingSec = &remainingSec
|
|
}
|
|
}
|
|
if isTempUnsched && acc.TempUnschedulableUntil != nil {
|
|
item.TempUnschedulableUntil = acc.TempUnschedulableUntil
|
|
}
|
|
|
|
account[acc.ID] = item
|
|
}
|
|
|
|
return platform, group, account, &collectedAt, nil
|
|
}
|