package apis import ( "fmt" "go-admin/app/observe/models" "go-admin/app/observe/service" "go-admin/utils" "time" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response" ) type AppScore struct { utils.OtApi } func (e AppScore) CalAppsScore(c *gin.Context) { s := new(service.AppScore) params := new(models.ScoreParams) err := e.MakeContext(c). MakeDB(). Bind(params, binding.JSON, nil). MakeService(&s.OtService).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //使用默认apdex规则 asList := new(models.AppScoreList) if params.Interval > 60 { params.Interval = 60 // 固定30分钟, 为了防止线上量太大导致超时 } if err := s.GetApdexPolicy(params).GetAllAppsScore(asList, params); err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } e.OK(asList, "查询成功") } func (e AppScore) CalAppLatency(c *gin.Context) { s := new(service.AppAnalyst) params := new(models.AppInterval) err := e.MakeContext(c). MakeDB(). Bind(params, binding.Form, nil). MakeService(&s.OtService).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } /* { "success":[["数字",1.2],[1.2,1.2]], "failed":[[1.2,1.2],[1.2,1.2]] } */ // e.Logger.Info("params: ", params) if params.StartTime == 0 || params.EndTime == 0 { params.EndTime = time.Now().Unix() params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds()) } else { params.StartTime = checkSecond(params.StartTime) params.EndTime = checkSecond(params.EndTime) } if params.Percentile == 0 { params.Percentile = 0.99 } e.Logger.Debugf("st: %d, et: %d", params.StartTime, params.EndTime) l := new(models.AppLatencyByInterval) if err := s.GetAppLatencyListByInterval(c, params, l); err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // e.Logger.Debug("latency list: ", l) e.OK(l, "查询成功") } func checkSecond(t int64) int64 { if t > 1e12 { return t / 1000 } return t } func (e AppScore) CalAppsScoreByService(c *gin.Context) { s := new(service.AppScore) params := new(models.ServiceScoreParams) err := e.MakeContext(c). MakeDB(). Bind(params, binding.Form, nil). MakeService(&s.OtService).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } if params.StartTime == 0 || params.EndTime == 0 { params.EndTime = time.Now().Unix() params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds()) } else { params.StartTime = checkSecond(params.StartTime) params.EndTime = checkSecond(params.EndTime) } //使用默认apdex规则 gs := new(models.GraphNodeScope) if err := s.GetAppDetails(c, gs, params); err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } e.OK(gs, "查询成功") } func (e AppScore) CalServicesScoreByApp(c *gin.Context) { s := new(service.AppScore) params := new(models.ServiceScoreParams) err := e.MakeContext(c). MakeDB(). Bind(params, binding.Form, nil). MakeService(&s.OtService).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } if params.StartTime == 0 || params.EndTime == 0 { params.EndTime = time.Now().Unix() params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds()) } else { params.StartTime = checkSecond(params.StartTime) params.EndTime = checkSecond(params.EndTime) } //使用默认apdex规则 gs := make([]models.GraphNodeScope, 0) if err := s.GetServicesDetails(c, &gs, params); err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } result := make(map[string][]any) for _, g := range gs { result["serivce_data"] = append(result["serivce_data"], g.Title) result["apdex_data"] = append(result["apdex_data"], g.Apdex) result["faild_data"] = append(result["faild_data"], fmt.Sprintf("%.2f", g.ArcFaild*100)) result["liveness_data"] = append(result["liveness_data"], g.SpanNum) } e.OK(result, "查询成功") } func (e AppScore) CalAppsScoreByEdge(c *gin.Context) { s := new(service.AppScore) params := new(models.ServiceScoreParams) err := e.MakeContext(c). MakeDB(). Bind(params, binding.Form, nil). MakeService(&s.OtService).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } if params.StartTime == 0 || params.EndTime == 0 { params.EndTime = time.Now().Unix() params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds()) } else { params.StartTime = checkSecond(params.StartTime) params.EndTime = checkSecond(params.EndTime) } //使用默认apdex规则 gs := make([]models.GraphServiceEdge, 0) if err := s.GetEdgeDetails(c, &gs, params); err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } e.OK(gs, "查询成功") } func (e AppScore) CalServiceLatency(c *gin.Context) { s := new(service.AppAnalyst) params := new(models.AppServiceInterval) err := e.MakeContext(c). MakeDB(). Bind(params, binding.Form, nil). MakeService(&s.OtService).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } if params.StartTime == 0 || params.EndTime == 0 { params.EndTime = time.Now().Unix() params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds()) } else { params.StartTime = checkSecond(params.StartTime) params.EndTime = checkSecond(params.EndTime) } if params.Percentile == 0 { params.Percentile = 0.99 } e.Logger.Debugf("st: %d, et: %d", params.StartTime, params.EndTime) l := new(models.AppLatencyByInterval) if err := s.GetServiceLatencyListByInterval(c, params, l); err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // e.Logger.Debug("latency list: ", l) e.OK(l, "查询成功") } func (e AppScore) CalEdgeLatency(c *gin.Context) { s := new(service.AppAnalyst) params := new(models.AppServiceInterval) err := e.MakeContext(c). MakeDB(). Bind(params, binding.Form, nil). MakeService(&s.OtService).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } if params.StartTime == 0 || params.EndTime == 0 { params.EndTime = time.Now().Unix() params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds()) } else { params.StartTime = checkSecond(params.StartTime) params.EndTime = checkSecond(params.EndTime) } if params.Percentile == 0 { params.Percentile = 0.99 } e.Logger.Debugf("st: %d, et: %d", params.StartTime, params.EndTime) l := new(models.AppLatencyByInterval) if err := s.GetEdgeLatencyListByInterval(c, params, l); err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // e.Logger.Debug("latency list: ", l) e.OK(l, "查询成功") }