ut_app.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/apm/model/ut"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. "time"
  8. )
  9. // SetAppCovCache set apps and depts coverage into redis.
  10. func (d *Dao) SetAppCovCache(c context.Context) (err error) {
  11. var (
  12. apps []*ut.App
  13. deptApps []*ut.App
  14. conn = d.Redis.Get(c)
  15. expireTime = int64(time.Duration(d.c.Redis.ExpireTime) / time.Second)
  16. )
  17. defer conn.Close()
  18. if err = d.DB.Find(&apps).Error; err != nil {
  19. log.Error("d.AddAppCovCache DB.Find Error(%v)", err)
  20. return
  21. }
  22. if err = d.DB.Raw(`select substring_index(substring_index(path,"/",4),"/",-1) as path,avg(coverage) as coverage,max(mtime) as mtime from ut_app where has_ut=1 group by substring_index(substring_index(path,"/",4),"/",-1)`).
  23. Find(&deptApps).Error; err != nil {
  24. log.Error("d.AddAppCovCache DB.Find Error(%v)", err)
  25. return
  26. }
  27. apps = append(apps, deptApps...)
  28. for _, app := range apps {
  29. if err = conn.Send("SETEX", app.Path, expireTime, app.Coverage); err != nil {
  30. log.Error("d.AddAppCovCache Redis.SETEX Error(%v)", err)
  31. return
  32. }
  33. }
  34. return
  35. }
  36. // GetAppCovCache get apps and depts coverage from redis.
  37. func (d *Dao) GetAppCovCache(c context.Context, path string) (coverage float64, err error) {
  38. var (
  39. conn = d.Redis.Get(c)
  40. exist bool
  41. key = path
  42. )
  43. defer conn.Close()
  44. if exist, err = redis.Bool(conn.Do("EXISTS", key)); err != nil {
  45. log.Error("d.GetAppCov Error(%v)", err)
  46. return
  47. }
  48. if exist {
  49. if coverage, err = redis.Float64(conn.Do("GET", key)); err != nil {
  50. log.Error("d.GetAppCov Error(%v)", err)
  51. return
  52. }
  53. }
  54. return
  55. }