123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package service
- import (
- sysModels "go-admin/app/admin/models"
- "go-admin/app/observe/models"
- "go-admin/app/observe/service/dto"
- "go-admin/utils"
- "time"
- "github.com/go-admin-team/go-admin-core/sdk/config"
- )
- const TOTALSTATSKEY = "ob_total_stats"
- type Stats struct {
- utils.OtService
- }
- func (s *Stats) SetTotalStats(result *map[string]interface{}) error {
- //TODO: 分别设定为统计接口,使用定时调度控制统计频率
- var err error
- if err = s.GetStatsInOrm(result); err != nil {
- return err
- }
- if err = s.SetCache(TOTALSTATSKEY, result); err != nil {
- return err
- }
- return nil
- }
- func (s *Stats) GetTotalStats(result *map[string]interface{}) error {
- if err := s.GetCache(TOTALSTATSKEY, result); err != nil {
- return s.SetTotalStats(result)
- }
- return nil
- }
- func (s *Stats) GetStatsInOrm(result *map[string]interface{}) error {
- var totalTraces int64
- var totalLogs int64
- var totalUser int64
- var totalService int64
- var err error
- if err = s.ChOrm.Table(models.TableNameTrace).Count(&totalTraces).Error; err != nil {
- s.Log.Error("Statistic total traces error: ", err.Error)
- return err
- }
- if err = s.ChOrm.Table(models.TableNameTraceLog).Count(&totalLogs).Error; err != nil {
- s.Log.Error("Statistic total trace_logs error: ", err.Error)
- return err
- }
- var _ss sysModels.SysUser
- if err = s.Orm.Table(_ss.TableName()).Count(&totalUser).Error; err != nil {
- s.Log.Error("Statistic total users error: ", err.Error)
- return err
- }
- if err = s.Orm.Table(models.TableNameServiceNode).Count(&totalService).Error; err != nil {
- s.Log.Error("Statistic total services error: ", err.Error)
- return err
- }
- (*result)["traces"] = totalTraces
- (*result)["logs"] = totalLogs
- (*result)["users"] = totalUser
- (*result)["services"] = totalService
- (*result)["uptime"] = time.Now().Unix()
- return nil
- }
- func (s *Stats) SetCache(k string, result *map[string]interface{}) error {
- if err := config.GetRedisClient().HMSet(k, *result).Err(); err != nil {
- s.Log.Errorf("hset %s err: %v", k, err)
- return err
- }
- return nil
- }
- func (s *Stats) GetCache(k string, result *map[string]interface{}) error {
- r, err := config.GetRedisClient().HGetAll(k).Result()
- if err != nil {
- s.Log.Errorf("hget %s err: %v", k, err)
- return err
- }
- for ks, v := range r {
- (*result)[ks] = v
- }
- return nil
- }
- func (s *Stats) GetApdexModelByAppAlias(c *dto.ApdexHandlerReq, result *map[string]interface{}) error {
- var err error
- defaultR := map[string]string{
- "csat_a": "1", //满意样本,csat通常在问卷调查中表达满意度
- "csat_b": "2.5", //容忍样本
- "csat_c": "5", //无法容忍
- // "equation": "Apdex = (1 x 【小于1秒的请求数】 + 0.5 x 【大于1秒小于2.5秒的请求数】) / 【总请求数】"
- }
- rkey := "apdex_model_" + c.Appalias
- exist, err := config.GetRedisClient().Exists(rkey).Result()
- if err != nil {
- return err
- }
- if exist == 1 {
- r, err := config.GetRedisClient().HGetAll(rkey).Result()
- if err != nil {
- s.Log.Errorf("hgetall %s err: %v", rkey, err)
- return err
- }
- for ks, v := range r {
- (*result)[ks] = v
- }
- } else {
- for ks, v := range defaultR {
- (*result)[ks] = v
- }
- }
- return nil
- }
- func (s *Stats) SetApdexModelByAppAlias(c *dto.ApdexUpdateReq, result *map[string]interface{}) error {
- rkey := "apdex_model_" + c.Appalias
- if err := config.GetRedisClient().HSet(
- rkey,
- "csat_a", c.CsatA,
- "csat_b", c.CsatB,
- "csat_c", c.CsatC).Err(); err != nil {
- s.Log.Errorf("hset %s err: %v", rkey, err)
- return err
- }
- return nil
- }
|