ut_dash.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/main/apm/model/ut"
  9. "go-common/library/log"
  10. )
  11. // DashCurveGraph is a curve graph for leader to show the team members' code coverage
  12. func (s *Service) DashCurveGraph(c context.Context, username string, req *ut.PCurveReq) (res []*ut.PCurveResp, err error) {
  13. s.appsCache.Lock()
  14. paths := s.appsCache.PathsByOwner(username)
  15. s.appsCache.Unlock()
  16. if err = s.DB.Table("ut_pkganls").Select("SUBSTRING_INDEX(pkg, '/', 5) AS pkg, ROUND(AVG(coverage/100),2) as coverage, SUM(assertions) as assertions, SUM(panics) as panics, SUM(passed) as passed, ROUND(SUM(passed)/SUM(assertions)*100,2) as pass_rate, SUM(failures) as failures, mtime").
  17. Where("mtime BETWEEN ? AND ? AND SUBSTRING_INDEX(pkg, '/', 5) IN (?) AND (pkg!=SUBSTRING_INDEX(pkg, '/', 5) OR p.pkg like 'go-common/library/%')", time.Unix(req.StartTime, 0).Format("2006-01-02"), time.Unix(req.EndTime, 0).Format("2006-01-02"), paths).
  18. Group("date(mtime),SUBSTRING_INDEX(pkg, '/', 5)").Order("mtime DESC").Find(&res).Error; err != nil {
  19. log.Error("s.ProjectCurveGraph execute sql error(%v)", err)
  20. return
  21. }
  22. return
  23. }
  24. // DashGraphDetail project graph detail for members.
  25. func (s *Service) DashGraphDetail(c context.Context, username string, req *ut.PCurveReq) (res []*ut.PCurveDetailResp, err error) {
  26. var (
  27. paths []string
  28. )
  29. if req.Path == "" || req.Path == "All" {
  30. s.appsCache.Lock()
  31. paths = s.appsCache.PathsByOwner(username)
  32. s.appsCache.Unlock()
  33. } else {
  34. paths = append(paths, req.Path)
  35. }
  36. if err = s.DB.Table("ut_pkganls").Select("ROUND(AVG(coverage/100),2) as coverage, SUM(assertions) as assertions, SUM(panics) as panics, SUM(passed) as passed, ROUND(SUM(passed)/SUM(assertions)*100,2) as pass_rate, SUM(failures) as failures, ut_commit.mtime, ut_commit.username").
  37. Joins("INNER JOIN ut_commit ON ut_commit.commit_id=ut_pkganls.commit_id").
  38. Where("ut_commit.mtime BETWEEN ? AND ? AND SUBSTRING_INDEX(pkg, '/', 5) IN (?) AND pkg!=SUBSTRING_INDEX(pkg, '/', 5)", time.Unix(req.StartTime, 0).Format("2006-01-02"), time.Unix(req.EndTime, 0).Format("2006-01-02"), paths).
  39. Group("ut_commit.username").Find(&res).Error; err != nil {
  40. log.Error("s.ProjectGraphDetail execute sql error(%v)", err)
  41. return
  42. }
  43. return
  44. }
  45. // DashGraphDetailSingle project graph detail for Single member.
  46. func (s *Service) DashGraphDetailSingle(c context.Context, username string, req *ut.PCurveReq) (res []*ut.PCurveDetailResp, err error) {
  47. s.appsCache.Lock()
  48. paths := s.appsCache.PathsByOwner(username)
  49. s.appsCache.Unlock()
  50. if err = s.DB.Table("ut_pkganls").Select("SUBSTRING_INDEX(pkg, '/', 5) AS pkg, ROUND(AVG(coverage/100),2) as coverage, SUM(assertions) as assertions, SUM(panics) as panics, SUM(passed) as passed, ROUND(SUM(passed)/SUM(assertions)*100,2) as pass_rate, SUM(failures) as failures, ut_commit.mtime, ut_commit.username").
  51. Joins("INNER JOIN ut_commit ON ut_commit.commit_id=ut_pkganls.commit_id").
  52. Where("ut_commit.mtime BETWEEN ? AND ? AND SUBSTRING_INDEX(pkg, '/', 5) IN (?) AND ut_commit.username=? AND (pkg!=SUBSTRING_INDEX(pkg, '/', 5) OR p.pkg like 'go-common/library/%')", time.Unix(req.StartTime, 0).Format("2006-01-02"), time.Unix(req.EndTime, 0).Format("2006-01-02"), paths, req.User).
  53. Group("SUBSTRING_INDEX(pkg,'/',5)").Find(&res).Error; err != nil {
  54. log.Error("s.ProjectGraphDetailSingle execute sql error(%v)", err)
  55. return
  56. }
  57. return
  58. }
  59. // DashPkgsTree get all the lastest committed pkgs by username
  60. func (s *Service) DashPkgsTree(c context.Context, path string, username string) (pkgs []*ut.PkgAnls, err error) {
  61. var (
  62. mtime = time.Now().Add(-time.Hour * 24 * 30) //two week
  63. count = strconv.Itoa(strings.Count(path, "/") + 1)
  64. hql = fmt.Sprintf("select id,commit_id,merge_id,concat(substring_index(pkg,'/',%s),'/') as pkg,ROUND(AVG(coverage/100),2) as coverage,ROUND(SUM(passed)/SUM(assertions)*100,2) as pass_rate,sum(panics) as panics,sum(failures) as failures,sum(skipped) as skipped,sum(passed) as passed,sum(assertions) as assertions,html_url,report_url,ctime,mtime from `ut_pkganls` as tmp where tmp.mtime>'%s' and tmp.mtime = (select max(`ut_pkganls`.mtime) from `ut_pkganls` inner join `ut_commit` on ut_commit.`commit_id`=ut_pkganls.`commit_id` where pkg = tmp.pkg and username='%s') group by substring_index(pkg,'/',%s) having pkg like '%s%%'",
  65. count, mtime, username, count, path)
  66. )
  67. if err = s.DB.Raw(hql).Find(&pkgs).Error; err != nil {
  68. log.Error("apm.Svc DashboardPkgs error(%v)", err)
  69. return
  70. }
  71. for _, pkg := range pkgs {
  72. if strings.HasSuffix(pkg.PKG, ".go") {
  73. continue
  74. }
  75. if path == pkg.PKG {
  76. if strings.HasPrefix(path, "go-common/app") && strings.Count(path, "/") < 6 {
  77. continue
  78. }
  79. var files []*ut.PkgAnls
  80. if files, err = s.dao.ParseUTFiles(c, pkg.HTMLURL); err != nil {
  81. log.Error("apm.Svc DashboardPkgs error(%v)", err)
  82. return
  83. }
  84. if len(files) == 0 {
  85. return nil, fmt.Errorf("Get .go files error. Please check your hosts")
  86. }
  87. *pkg = *files[0]
  88. pkgs = append(pkgs, files[1:]...)
  89. }
  90. }
  91. return
  92. }