61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/company/ai-ops/internal/domain/model"
|
|
"github.com/company/ai-ops/internal/domain/repository"
|
|
)
|
|
|
|
// MetricService 是指标业务逻辑层
|
|
type MetricService struct {
|
|
metricRepo repository.MetricRepository
|
|
alertRepo repository.AlertRepository
|
|
}
|
|
|
|
func NewMetricService(mr repository.MetricRepository, ar repository.AlertRepository) *MetricService {
|
|
return &MetricService{metricRepo: mr, alertRepo: ar}
|
|
}
|
|
|
|
// GetRealtimeMetrics 获取首页实时指标
|
|
func (s *MetricService) GetRealtimeMetrics(ctx context.Context) (*model.RealtimeMetrics, error) {
|
|
return s.metricRepo.GetRealtime(ctx)
|
|
}
|
|
|
|
// GetSupplierCount 获取活跃供应商数量
|
|
func (s *MetricService) GetSupplierCount(ctx context.Context) (*model.SupplierCount, error) {
|
|
// 从指标库查询供应商健康状态
|
|
points, err := s.metricRepo.Query(ctx, model.MetricQueryRequest{
|
|
Name: "supplier_health",
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query supplier health: %w", err)
|
|
}
|
|
|
|
var healthy, unhealthy int
|
|
for _, p := range points {
|
|
if p.Value > 0.5 {
|
|
healthy++
|
|
} else {
|
|
unhealthy++
|
|
}
|
|
}
|
|
|
|
return &model.SupplierCount{
|
|
Total: healthy + unhealthy,
|
|
Healthy: healthy,
|
|
Unhealthy: unhealthy,
|
|
}, nil
|
|
}
|
|
|
|
// GetOpenAlertCount 获取未关闭告警数量
|
|
func (s *MetricService) GetOpenAlertCount(ctx context.Context) (*model.AlertCount, error) {
|
|
return s.alertRepo.GetOpenCount(ctx)
|
|
}
|
|
|
|
// QueryMetrics 指标下钻查询
|
|
func (s *MetricService) QueryMetrics(ctx context.Context, req model.MetricQueryRequest) ([]model.MetricPoint, error) {
|
|
return s.metricRepo.Query(ctx, req)
|
|
}
|