ut_app.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/admin/main/apm/model/ut"
  7. "go-common/library/log"
  8. )
  9. var (
  10. _upsertUtApp = "INSERT INTO ut_app (path,owner) VALUES %s"
  11. )
  12. // AppsCache flush cache for apps.
  13. func (s *Service) AppsCache(c context.Context) (err error) {
  14. var (
  15. appSlice []*ut.App
  16. appMap = make(map[string]*ut.App)
  17. ownerMap = make(map[string][]*ut.App)
  18. deptMap = make(map[string]*ut.Department)
  19. )
  20. if err = s.DB.Table("ut_app").Find(&appSlice).Error; err != nil {
  21. log.Error("s.AppsCache.Find() error(%v)", err)
  22. return
  23. }
  24. for _, app := range appSlice {
  25. appMap[app.Path] = app
  26. owners := strings.Split(app.Owner, ",")
  27. for _, owner := range owners {
  28. ownerMap[owner] = append(ownerMap[owner], app)
  29. }
  30. pathSlice := strings.Split(app.Path, "/")
  31. if len(pathSlice) < 2 {
  32. continue
  33. }
  34. deptName := pathSlice[len(pathSlice)-2]
  35. if deptMap[deptName] == nil {
  36. deptMap[deptName] = &ut.Department{}
  37. }
  38. deptMap[deptName].Name = deptName
  39. deptMap[deptName].Total++
  40. if app.HasUt == 1 {
  41. deptMap[deptName].Access++
  42. deptMap[deptName].Coverage += app.Coverage
  43. }
  44. }
  45. for _, v := range deptMap {
  46. if v.Access > 0 {
  47. v.Coverage = v.Coverage / float64(v.Access)
  48. }
  49. }
  50. s.appsCache.Lock()
  51. s.appsCache.Map = appMap
  52. s.appsCache.Slice = appSlice
  53. s.appsCache.Owner = ownerMap
  54. s.appsCache.Dept = deptMap
  55. s.appsCache.Unlock()
  56. return
  57. }
  58. // AddUTApp add path to ut_app
  59. func (s *Service) AddUTApp(c context.Context, apps []*ut.App) (err error) {
  60. var (
  61. valueStrings []string
  62. valueArgs []interface{}
  63. )
  64. s.appsCache.Lock()
  65. for _, app := range apps {
  66. var cache, ok = s.appsCache.Map[app.Path]
  67. if !ok || cache == nil {
  68. valueStrings = append(valueStrings, "(?,?)")
  69. valueArgs = append(valueArgs, app.Path)
  70. valueArgs = append(valueArgs, app.Owner)
  71. continue
  72. }
  73. if cache.Owner == app.Owner {
  74. continue
  75. }
  76. if err = s.DB.Table("ut_app").Where("ID=?", cache.ID).
  77. Update("owner", app.Owner).Error; err != nil {
  78. log.Error("AddUTApp err (%v)", err)
  79. return
  80. }
  81. cache.Owner = app.Owner
  82. }
  83. s.appsCache.Unlock()
  84. if len(valueStrings) == 0 {
  85. return
  86. }
  87. stmt := fmt.Sprintf(_upsertUtApp, strings.Join(valueStrings, ","))
  88. if err = s.DB.Exec(stmt, valueArgs...).Error; err != nil {
  89. return
  90. }
  91. // update AppsCache
  92. if err = s.AppsCache(c); err != nil {
  93. return
  94. }
  95. return
  96. }
  97. // UpdateUTApp update has_ut=1
  98. func (s *Service) UpdateUTApp(c context.Context, pkg *ut.PkgAnls) (err error) {
  99. s.appsCache.Lock()
  100. defer s.appsCache.Unlock()
  101. path := paserPkg(pkg.PKG)
  102. app, ok := s.appsCache.Map[path]
  103. if !ok || (app.HasUt != 0 && app.Coverage == pkg.Coverage) {
  104. log.Info("s.UpdateUTApp(%s) skiped.", pkg.PKG)
  105. return
  106. }
  107. app.HasUt = 1
  108. app.Coverage = pkg.Coverage
  109. if err = s.DB.Table("ut_app").Where("ID=?", app.ID).Updates(app).Error; err != nil {
  110. log.Error("UpdateUTApp err (%v)", err)
  111. return
  112. }
  113. return
  114. }
  115. func paserPkg(pkg string) (path string) {
  116. temp := strings.Split(pkg, "/")
  117. if len(temp) < int(5) {
  118. path = pkg
  119. return
  120. }
  121. path = strings.Join(temp[0:5], "/")
  122. return
  123. }
  124. // UTApps .
  125. func (s *Service) UTApps(c context.Context, v *ut.AppReq) (result []*ut.App, count int, err error) {
  126. if v.Path != "" {
  127. if err = s.DB.Table("ut_app").Where("path LIKE ?", "%"+v.Path+"%").
  128. Count(&count).Find(&result).Error; err != nil {
  129. log.Error("UtProject err (%v)", err)
  130. return
  131. }
  132. } else {
  133. if err = s.DB.Table("ut_app").Where("has_ut=?", v.HasUt).Count(&count).Offset((v.Pn - 1) * v.Ps).
  134. Limit(v.Ps).Find(&result).Error; err != nil {
  135. log.Error("UtProject err (%v)", err)
  136. return
  137. }
  138. }
  139. for _, v := range result {
  140. v.Link = parsePath(v.Path)
  141. }
  142. return
  143. }
  144. // "go-common/app/service/main/share" to "go-common/tree/master/app/service/main/share"
  145. func parsePath(path string) (link string) {
  146. temp := strings.SplitN(path, "/", 2)
  147. link = fmt.Sprintf("%s%s%s", temp[0], "/tree/master/", temp[1])
  148. return
  149. }