ot_app_score.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package apis
  2. import (
  3. "fmt"
  4. "go-admin/app/observe/models"
  5. "go-admin/app/observe/service"
  6. "go-admin/utils"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/gin-gonic/gin/binding"
  10. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  11. )
  12. type AppScore struct {
  13. utils.OtApi
  14. }
  15. func (e AppScore) CalAppsScore(c *gin.Context) {
  16. s := new(service.AppScore)
  17. params := new(models.ScoreParams)
  18. err := e.MakeContext(c).
  19. MakeDB().
  20. Bind(params, binding.JSON, nil).
  21. MakeService(&s.OtService).Errors
  22. if err != nil {
  23. e.Logger.Error(err)
  24. e.Error(500, err, err.Error())
  25. return
  26. }
  27. //使用默认apdex规则
  28. asList := new(models.AppScoreList)
  29. if params.Interval > 60 {
  30. params.Interval = 60 // 固定30分钟, 为了防止线上量太大导致超时
  31. }
  32. if err := s.GetApdexPolicy(params).GetAllAppsScore(asList, params); err != nil {
  33. e.Logger.Error(err)
  34. e.Error(500, err, err.Error())
  35. return
  36. }
  37. e.OK(asList, "查询成功")
  38. }
  39. func (e AppScore) CalAppLatency(c *gin.Context) {
  40. s := new(service.AppAnalyst)
  41. params := new(models.AppInterval)
  42. err := e.MakeContext(c).
  43. MakeDB().
  44. Bind(params, binding.Form, nil).
  45. MakeService(&s.OtService).Errors
  46. if err != nil {
  47. e.Logger.Error(err)
  48. e.Error(500, err, err.Error())
  49. return
  50. }
  51. /*
  52. {
  53. "success":[["数字",1.2],[1.2,1.2]],
  54. "failed":[[1.2,1.2],[1.2,1.2]]
  55. }
  56. */
  57. // e.Logger.Info("params: ", params)
  58. if params.StartTime == 0 || params.EndTime == 0 {
  59. params.EndTime = time.Now().Unix()
  60. params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds())
  61. } else {
  62. params.StartTime = checkSecond(params.StartTime)
  63. params.EndTime = checkSecond(params.EndTime)
  64. }
  65. if params.Percentile == 0 {
  66. params.Percentile = 0.99
  67. }
  68. e.Logger.Debugf("st: %d, et: %d", params.StartTime, params.EndTime)
  69. l := new(models.AppLatencyByInterval)
  70. if err := s.GetAppLatencyListByInterval(c, params, l); err != nil {
  71. e.Logger.Error(err)
  72. e.Error(500, err, err.Error())
  73. return
  74. }
  75. // e.Logger.Debug("latency list: ", l)
  76. e.OK(l, "查询成功")
  77. }
  78. func checkSecond(t int64) int64 {
  79. if t > 1e12 {
  80. return t / 1000
  81. }
  82. return t
  83. }
  84. func (e AppScore) CalAppsScoreByService(c *gin.Context) {
  85. s := new(service.AppScore)
  86. params := new(models.ServiceScoreParams)
  87. err := e.MakeContext(c).
  88. MakeDB().
  89. Bind(params, binding.Form, nil).
  90. MakeService(&s.OtService).Errors
  91. if err != nil {
  92. e.Logger.Error(err)
  93. e.Error(500, err, err.Error())
  94. return
  95. }
  96. if params.StartTime == 0 || params.EndTime == 0 {
  97. params.EndTime = time.Now().Unix()
  98. params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds())
  99. } else {
  100. params.StartTime = checkSecond(params.StartTime)
  101. params.EndTime = checkSecond(params.EndTime)
  102. }
  103. //使用默认apdex规则
  104. gs := new(models.GraphNodeScope)
  105. if err := s.GetAppDetails(c, gs, params); err != nil {
  106. e.Logger.Error(err)
  107. e.Error(500, err, err.Error())
  108. return
  109. }
  110. e.OK(gs, "查询成功")
  111. }
  112. func (e AppScore) CalServicesScoreByApp(c *gin.Context) {
  113. s := new(service.AppScore)
  114. params := new(models.ServiceScoreParams)
  115. err := e.MakeContext(c).
  116. MakeDB().
  117. Bind(params, binding.Form, nil).
  118. MakeService(&s.OtService).Errors
  119. if err != nil {
  120. e.Logger.Error(err)
  121. e.Error(500, err, err.Error())
  122. return
  123. }
  124. if params.StartTime == 0 || params.EndTime == 0 {
  125. params.EndTime = time.Now().Unix()
  126. params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds())
  127. } else {
  128. params.StartTime = checkSecond(params.StartTime)
  129. params.EndTime = checkSecond(params.EndTime)
  130. }
  131. //使用默认apdex规则
  132. gs := make([]models.GraphNodeScope, 0)
  133. if err := s.GetServicesDetails(c, &gs, params); err != nil {
  134. e.Logger.Error(err)
  135. e.Error(500, err, err.Error())
  136. return
  137. }
  138. result := make(map[string][]any)
  139. for _, g := range gs {
  140. result["serivce_data"] = append(result["serivce_data"], g.Title)
  141. result["apdex_data"] = append(result["apdex_data"], g.Apdex)
  142. result["faild_data"] = append(result["faild_data"], fmt.Sprintf("%.2f", g.ArcFaild*100))
  143. result["liveness_data"] = append(result["liveness_data"], g.SpanNum)
  144. }
  145. e.OK(result, "查询成功")
  146. }
  147. func (e AppScore) CalAppsScoreByEdge(c *gin.Context) {
  148. s := new(service.AppScore)
  149. params := new(models.ServiceScoreParams)
  150. err := e.MakeContext(c).
  151. MakeDB().
  152. Bind(params, binding.Form, nil).
  153. MakeService(&s.OtService).Errors
  154. if err != nil {
  155. e.Logger.Error(err)
  156. e.Error(500, err, err.Error())
  157. return
  158. }
  159. if params.StartTime == 0 || params.EndTime == 0 {
  160. params.EndTime = time.Now().Unix()
  161. params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds())
  162. } else {
  163. params.StartTime = checkSecond(params.StartTime)
  164. params.EndTime = checkSecond(params.EndTime)
  165. }
  166. //使用默认apdex规则
  167. gs := make([]models.GraphServiceEdge, 0)
  168. if err := s.GetEdgeDetails(c, &gs, params); err != nil {
  169. e.Logger.Error(err)
  170. e.Error(500, err, err.Error())
  171. return
  172. }
  173. e.OK(gs, "查询成功")
  174. }
  175. func (e AppScore) CalServiceLatency(c *gin.Context) {
  176. s := new(service.AppAnalyst)
  177. params := new(models.AppServiceInterval)
  178. err := e.MakeContext(c).
  179. MakeDB().
  180. Bind(params, binding.Form, nil).
  181. MakeService(&s.OtService).Errors
  182. if err != nil {
  183. e.Logger.Error(err)
  184. e.Error(500, err, err.Error())
  185. return
  186. }
  187. if params.StartTime == 0 || params.EndTime == 0 {
  188. params.EndTime = time.Now().Unix()
  189. params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds())
  190. } else {
  191. params.StartTime = checkSecond(params.StartTime)
  192. params.EndTime = checkSecond(params.EndTime)
  193. }
  194. if params.Percentile == 0 {
  195. params.Percentile = 0.99
  196. }
  197. e.Logger.Debugf("st: %d, et: %d", params.StartTime, params.EndTime)
  198. l := new(models.AppLatencyByInterval)
  199. if err := s.GetServiceLatencyListByInterval(c, params, l); err != nil {
  200. e.Logger.Error(err)
  201. e.Error(500, err, err.Error())
  202. return
  203. }
  204. // e.Logger.Debug("latency list: ", l)
  205. e.OK(l, "查询成功")
  206. }
  207. func (e AppScore) CalEdgeLatency(c *gin.Context) {
  208. s := new(service.AppAnalyst)
  209. params := new(models.AppServiceInterval)
  210. err := e.MakeContext(c).
  211. MakeDB().
  212. Bind(params, binding.Form, nil).
  213. MakeService(&s.OtService).Errors
  214. if err != nil {
  215. e.Logger.Error(err)
  216. e.Error(500, err, err.Error())
  217. return
  218. }
  219. if params.StartTime == 0 || params.EndTime == 0 {
  220. params.EndTime = time.Now().Unix()
  221. params.StartTime = params.EndTime - int64((5 * time.Minute).Seconds())
  222. } else {
  223. params.StartTime = checkSecond(params.StartTime)
  224. params.EndTime = checkSecond(params.EndTime)
  225. }
  226. if params.Percentile == 0 {
  227. params.Percentile = 0.99
  228. }
  229. e.Logger.Debugf("st: %d, et: %d", params.StartTime, params.EndTime)
  230. l := new(models.AppLatencyByInterval)
  231. if err := s.GetEdgeLatencyListByInterval(c, params, l); err != nil {
  232. e.Logger.Error(err)
  233. e.Error(500, err, err.Error())
  234. return
  235. }
  236. // e.Logger.Debug("latency list: ", l)
  237. e.OK(l, "查询成功")
  238. }