ut_report.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/apm/model/ut"
  6. "go-common/library/log"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/jinzhu/gorm"
  11. )
  12. // GitReport post simple report using SAGA account
  13. func (s *Service) GitReport(c context.Context, projID int, mrID int, commitID string) (err error) {
  14. var (
  15. pkgs = make([]*ut.PkgAnls, 0)
  16. row = `<tr><td colspan="2"><a href="http://sven.bilibili.co/#/ut/detail?commit_id=%s&pkg=%s">%s</a></td><td>%.2f</td><td>%.2f</td><td>%.2f</td><td style="text-align: center">%s</td></tr>`
  17. msg = fmt.Sprintf(`<pre><h4>单元测试速报</h4>(Commit:<a href="http://sven.bilibili.co/#/ut?merge_id=%d&pn=1&ps=20">%s</a>)</pre>`, mrID, commitID) + `<table><thead><tr><th colspan="2">包名</th><th>覆盖率(%%)</th><th>通过率(%%)</th><th>覆盖率较历史最高(%%)</th><th>是否合格</th></tr></thead><tbody>%s</tbody>%s</table>`
  18. rows string
  19. root = ""
  20. file = &ut.File{}
  21. )
  22. if err = s.DB.Where("commit_id=? AND (pkg!=substring_index(pkg, '/', 5) OR pkg like 'go-common/library/%')", commitID).Find(&pkgs).Error; err != nil {
  23. log.Error("apmSvc.GitReport query error(%v)", err)
  24. return
  25. }
  26. for _, pkg := range pkgs {
  27. t, _ := s.tyrant(pkg)
  28. app := pkg.PKG
  29. if !strings.Contains(pkg.PKG, "/library") && len(pkg.PKG) >= 5 {
  30. app = strings.Join(strings.Split(pkg.PKG, "/")[:5], "/")
  31. }
  32. rows += fmt.Sprintf(row, pkg.CommitID, app, pkg.PKG, pkg.Coverage/100, t.PassRate, t.Increase, "%s")
  33. if t.Tyrant {
  34. rows = fmt.Sprintf(rows, "❌")
  35. } else {
  36. rows = fmt.Sprintf(rows, "✔️")
  37. }
  38. }
  39. if err = s.DB.Select(`count(id) as id, commit_id, sum(statements) as statements, sum(covered_statements) as covered_statements`).
  40. Where(`pkg!=substring_index(pkg, "/", 5) OR ut_pkganls.pkg like 'go-common/library/%'`).Group(`commit_id`).Having("commit_id=?", commitID).First(file).Error; err != nil {
  41. if err != gorm.ErrRecordNotFound {
  42. log.Error("apmSvc.GitReport query error(%v)", err)
  43. return
  44. }
  45. err = nil
  46. } else {
  47. root = fmt.Sprintf(`<tfoot><tr><td><b>总覆盖率: </b>%.2f%%<br><b>总检测文件数: </b>%d</br></td><td><b>Tracked语句数: </b>%d<br><b>覆盖的语句数: </b>%d</br></td><td colspan="4" align="center">总覆盖率 = 覆盖语句数 / Tracked语句数</td></tr></tfoot>`,
  48. float64(file.CoveredStatements)/float64(file.Statements)*100, file.ID, file.Statements, file.CoveredStatements)
  49. }
  50. if err = s.CommentOnMR(c, projID, mrID, fmt.Sprintf(msg, rows, root)); err != nil {
  51. log.Error("apmSvc.GitReport call CommentOnMR error(%v)", err)
  52. return
  53. }
  54. return
  55. }
  56. // WechatReport send wechat msg to a group when mr is merged
  57. func (s *Service) WechatReport(c context.Context, mrid int64, cid, src, des string) (err error) {
  58. var (
  59. pkgs []*ut.PkgAnls
  60. mr = &ut.Merge{}
  61. foot = fmt.Sprintf("\n*覆盖率增长:该包本次合并最后一次commit与过往已合并记录中最大覆盖率进行比较\nMR:http://git.bilibili.co/platform/go-common/merge_requests/%d\n单测报告:http://sven.bilibili.co/#/ut?merge_id=%d&pn=1&ps=20\n", mrid, mrid)
  62. )
  63. if err = s.dao.DB.Where("merge_id=? AND is_merged=?", mrid, 1).First(mr).Error; err != nil {
  64. log.Error("apmSvc.WechatReport Error(%v)", err)
  65. return
  66. }
  67. msg := "【测试姬】新鲜的单测速报出炉啦ᕕ( ᐛ )ᕗ\n\n" + fmt.Sprintf("由 %s 发起的 MR (%s->%s) 合并成功!\n\n", mr.UserName, src, des)
  68. if err = s.dao.DB.Where("commit_id=? AND (pkg!=substring_index(pkg, '/', 5) OR ut_pkganls.pkg like 'go-common/library/%')", cid).Find(&pkgs).Error; err != nil || len(pkgs) == 0 {
  69. log.Error("apmSvc.WechatReport Error(%v)", err)
  70. return
  71. }
  72. for _, pkg := range pkgs {
  73. var (
  74. arrow = ""
  75. maxPkgs = make([]ut.PkgAnls, 0)
  76. increase = pkg.Coverage / 100
  77. file = &ut.File{}
  78. lastFile = &ut.File{}
  79. )
  80. if err = s.DB.Table("ut_pkganls").Joins("left join ut_merge ON ut_merge.merge_id=ut_pkganls.merge_id").Select("ut_pkganls.commit_id, ut_pkganls.coverage").Where("ut_pkganls.pkg=? AND ut_pkganls.merge_id!=? AND ut_merge.is_merged=1", pkg.PKG, mrid).Order("coverage desc").Find(&maxPkgs).Error; err != nil {
  81. log.Error("apmSvc.WechatReport error(%v)", err)
  82. return
  83. }
  84. if len(maxPkgs) != 0 {
  85. increase = pkg.Coverage/100 - maxPkgs[0].Coverage/100
  86. }
  87. if increase < float64(0) {
  88. arrow = "⬇️"
  89. if !strings.Contains(foot, "本次合并后有包覆盖率下降了喔~还需要再加油鸭💪~") {
  90. foot += "\n本次合并后有包覆盖率下降了喔~还需要再加油鸭💪~"
  91. }
  92. } else if increase > float64(0) {
  93. arrow = "⬆️"
  94. }
  95. msg += fmt.Sprintf("*%s\n\t覆盖率:%.2f%%\t覆盖率增长:%.2f%%\t%s\n", pkg.PKG, pkg.Coverage/100, increase, arrow)
  96. if err = s.DB.Select("count(id) as id,commit_id,pkg,sum(statements) as statements,sum(covered_statements) as covered_statements").Group("commit_id,pkg").Having("commit_id=? AND pkg=?", cid, pkg.PKG).First(file).Error; err != nil {
  97. if err == gorm.ErrRecordNotFound {
  98. err = nil
  99. continue
  100. }
  101. log.Error("apmSvc.WechatReport error(%v)", err)
  102. return
  103. }
  104. msg += fmt.Sprintf("\tTracked语句数:%d\t覆盖语句数:%d\n", file.Statements, file.CoveredStatements)
  105. if err = s.DB.Table("ut_file").Joins("left join ut_commit on ut_commit.commit_id = ut_file.commit_id left join ut_merge on ut_merge.merge_id =ut_commit.merge_id").Select("ut_file.commit_id,ut_file.pkg,count(ut_file.id) as id,sum(statements) as statements, sum(covered_statements) as covered_statements,ut_file.mtime").Where("is_merged=1 and ut_merge.merge_id!=?", mrid).Group("commit_id,pkg").Having("pkg=?", pkg.PKG).Order("mtime desc").First(lastFile).Error; err != nil {
  106. if err == gorm.ErrRecordNotFound {
  107. err = nil
  108. continue
  109. }
  110. log.Error("apmSvc.WechatReport error(%v)", err)
  111. return
  112. }
  113. msg += fmt.Sprintf("\tTracked语句增长:%d\t覆盖语句增长:%d\n", file.Statements-lastFile.Statements, file.CoveredStatements-lastFile.CoveredStatements)
  114. }
  115. if err = s.dao.SendWechatToGroup(c, s.c.WeChat.ChatID, msg+foot); err != nil {
  116. log.Error("apmSvc.WechatReport Error(%v)", err)
  117. return
  118. }
  119. return
  120. }
  121. // RankWechatReport send rank to wechat group 19:00 everyday
  122. func (s *Service) RankWechatReport(c context.Context) (err error) {
  123. var (
  124. topRanks []*ut.RankResp
  125. msg = "【测试姬】今日份的单测榜单🎉\n\n"
  126. root = "更多详情请戳:http://sven.bilibili.co/#/ut/leaderboard\n感谢辛勤工作的一天的你☕️一起快乐下班吧☕️\n"
  127. )
  128. if topRanks, err = s.RankTen(c, "desc"); err != nil {
  129. log.Error("apmSvc.RankWechatReport Error(%v)", err)
  130. return
  131. }
  132. msg += "🔘TOP 10\n"
  133. for i, r := range topRanks {
  134. msg += fmt.Sprintf("%d: %s\t分数: %.2f\n", i+1, r.UserName, r.Score)
  135. }
  136. msg += "恭喜以上各位~还请继续保持哟(๑•̀ㅂ•́)و✧\n\n"
  137. if err = s.dao.SendWechatToGroup(c, s.c.WeChat.ChatID, msg+root); err != nil {
  138. log.Error("apmSvc.RankWechatReport Error(%v)", err)
  139. return
  140. }
  141. return
  142. }
  143. // SummaryWechatReport send depts' summary to ut wechat group every Friday(19:00)
  144. func (s *Service) SummaryWechatReport(c context.Context) (err error) {
  145. var (
  146. depts = `"main","ep","openplatform","live","video","bbq"`
  147. msg = fmt.Sprintf("【测试姬】Kratos大仓库单元测试周刊 ( %s )\n\n>> 接入情况: \n", time.Now().Format("2006-01-02"))
  148. covMsg = ">> 覆盖情况: \n"
  149. rankMsg = ">> 本周Top 3应用:\n"
  150. root = "\n\n更多汇总信息请姥爷们查看: http://sven.bilibili.co/#/ut/dashboard?tab=project\n更多文档信息: http://info.bilibili.co/pages/viewpage.action?pageId=6947230\n吐槽建议: http://sven.bilibili.co/#/suggestion/list\n"
  151. tpAcc = "\t* %s\t接入数: %d\t总数: %d\t接入率: %.2f%%\n"
  152. tpCov = "\t* %s\t应用平均覆盖率: %.2f%%\t增长率:%.2f%%\n"
  153. tpProjRank = "\t%d. %s \t包平均覆盖率: %.2f%%\n"
  154. sumTotal, sumAccess int64
  155. sumCov float64
  156. )
  157. s.appsCache.Lock()
  158. defer s.appsCache.Unlock()
  159. for _, dept := range s.appsCache.Dept {
  160. if !strings.Contains(depts, "\""+dept.Name+"\"") {
  161. continue
  162. }
  163. sumTotal += dept.Total
  164. sumAccess += dept.Access
  165. msg += fmt.Sprintf(tpAcc, dept.Name, dept.Access, dept.Total, float64(dept.Access)/float64(dept.Total)*100)
  166. if dept.Coverage == float64(0) {
  167. continue
  168. }
  169. var preCoverage float64
  170. if preCoverage, err = s.dao.GetAppCovCache(c, dept.Name); err != nil {
  171. log.Error("service.SummaryWechatReport GetAppCov Error(%v)", err)
  172. return
  173. }
  174. covMsg += fmt.Sprintf(tpCov, dept.Name, dept.Coverage/100, (dept.Coverage-preCoverage)/100)
  175. }
  176. sort.Slice(s.appsCache.Slice, func(i, j int) bool { return s.appsCache.Slice[i].Coverage > s.appsCache.Slice[j].Coverage })
  177. for k, slice := range s.appsCache.Slice {
  178. if k <= 2 {
  179. rankMsg += fmt.Sprintf(tpProjRank, k+1, slice.Path, slice.Coverage/100)
  180. }
  181. if slice.HasUt == 1 {
  182. sumCov += slice.Coverage
  183. }
  184. }
  185. msg += fmt.Sprintf("\t总接入情况 - 接入应用数: %d,应用总数: %d\n\n", sumAccess, sumTotal) + covMsg + fmt.Sprintf("\t总覆盖情况 - 应用平均覆盖率: %.2f%%\n\n", sumCov/float64(sumAccess)/100) + rankMsg
  186. if err = s.dao.SendWechatToGroup(c, s.c.WeChat.ChatID, msg+root); err != nil {
  187. log.Error("apmSvc.WechatReport Error(%v)", err)
  188. return
  189. }
  190. return
  191. }