stats.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package service
  2. import (
  3. sysModels "go-admin/app/admin/models"
  4. "go-admin/app/observe/models"
  5. "go-admin/app/observe/service/dto"
  6. "go-admin/utils"
  7. "time"
  8. "github.com/go-admin-team/go-admin-core/sdk/config"
  9. )
  10. const TOTALSTATSKEY = "ob_total_stats"
  11. type Stats struct {
  12. utils.OtService
  13. }
  14. func (s *Stats) SetTotalStats(result *map[string]interface{}) error {
  15. //TODO: 分别设定为统计接口,使用定时调度控制统计频率
  16. var err error
  17. if err = s.GetStatsInOrm(result); err != nil {
  18. return err
  19. }
  20. if err = s.SetCache(TOTALSTATSKEY, result); err != nil {
  21. return err
  22. }
  23. return nil
  24. }
  25. func (s *Stats) GetTotalStats(result *map[string]interface{}) error {
  26. if err := s.GetCache(TOTALSTATSKEY, result); err != nil {
  27. return s.SetTotalStats(result)
  28. }
  29. return nil
  30. }
  31. func (s *Stats) GetStatsInOrm(result *map[string]interface{}) error {
  32. var totalTraces int64
  33. var totalLogs int64
  34. var totalUser int64
  35. var totalService int64
  36. var err error
  37. if err = s.ChOrm.Table(models.TableNameTrace).Count(&totalTraces).Error; err != nil {
  38. s.Log.Error("Statistic total traces error: ", err.Error)
  39. return err
  40. }
  41. if err = s.ChOrm.Table(models.TableNameTraceLog).Count(&totalLogs).Error; err != nil {
  42. s.Log.Error("Statistic total trace_logs error: ", err.Error)
  43. return err
  44. }
  45. var _ss sysModels.SysUser
  46. if err = s.Orm.Table(_ss.TableName()).Count(&totalUser).Error; err != nil {
  47. s.Log.Error("Statistic total users error: ", err.Error)
  48. return err
  49. }
  50. if err = s.Orm.Table(models.TableNameServiceNode).Count(&totalService).Error; err != nil {
  51. s.Log.Error("Statistic total services error: ", err.Error)
  52. return err
  53. }
  54. (*result)["traces"] = totalTraces
  55. (*result)["logs"] = totalLogs
  56. (*result)["users"] = totalUser
  57. (*result)["services"] = totalService
  58. (*result)["uptime"] = time.Now().Unix()
  59. return nil
  60. }
  61. func (s *Stats) SetCache(k string, result *map[string]interface{}) error {
  62. if err := config.GetRedisClient().HMSet(k, *result).Err(); err != nil {
  63. s.Log.Errorf("hset %s err: %v", k, err)
  64. return err
  65. }
  66. return nil
  67. }
  68. func (s *Stats) GetCache(k string, result *map[string]interface{}) error {
  69. r, err := config.GetRedisClient().HGetAll(k).Result()
  70. if err != nil {
  71. s.Log.Errorf("hget %s err: %v", k, err)
  72. return err
  73. }
  74. for ks, v := range r {
  75. (*result)[ks] = v
  76. }
  77. return nil
  78. }
  79. func (s *Stats) GetApdexModelByAppAlias(c *dto.ApdexHandlerReq, result *map[string]interface{}) error {
  80. var err error
  81. defaultR := map[string]string{
  82. "csat_a": "1", //满意样本,csat通常在问卷调查中表达满意度
  83. "csat_b": "2.5", //容忍样本
  84. "csat_c": "5", //无法容忍
  85. // "equation": "Apdex = (1 x 【小于1秒的请求数】 + 0.5 x 【大于1秒小于2.5秒的请求数】) / 【总请求数】"
  86. }
  87. rkey := "apdex_model_" + c.Appalias
  88. exist, err := config.GetRedisClient().Exists(rkey).Result()
  89. if err != nil {
  90. return err
  91. }
  92. if exist == 1 {
  93. r, err := config.GetRedisClient().HGetAll(rkey).Result()
  94. if err != nil {
  95. s.Log.Errorf("hgetall %s err: %v", rkey, err)
  96. return err
  97. }
  98. for ks, v := range r {
  99. (*result)[ks] = v
  100. }
  101. } else {
  102. for ks, v := range defaultR {
  103. (*result)[ks] = v
  104. }
  105. }
  106. return nil
  107. }
  108. func (s *Stats) SetApdexModelByAppAlias(c *dto.ApdexUpdateReq, result *map[string]interface{}) error {
  109. rkey := "apdex_model_" + c.Appalias
  110. if err := config.GetRedisClient().HSet(
  111. rkey,
  112. "csat_a", c.CsatA,
  113. "csat_b", c.CsatB,
  114. "csat_c", c.CsatC).Err(); err != nil {
  115. s.Log.Errorf("hset %s err: %v", rkey, err)
  116. return err
  117. }
  118. return nil
  119. }