123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 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, "查询成功")
- }
|