ut.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "go-common/app/admin/main/apm/conf"
  13. "go-common/app/admin/main/apm/model/ut"
  14. "go-common/library/log"
  15. "github.com/jinzhu/gorm"
  16. )
  17. const (
  18. _sagaWechatURL = "http://uat-saga-admin.bilibili.co/ep/admin/saga/v2/wechat"
  19. _gitCommitsAPI = "http://git.bilibili.co/api/v4/projects/682/repository/commits"
  20. )
  21. // ParseUTFiles parse html to get specific file
  22. func (d *Dao) ParseUTFiles(c context.Context, url string) (pkgs []*ut.PkgAnls, err error) {
  23. var (
  24. req *http.Request
  25. html []byte
  26. files []string
  27. )
  28. if req, err = http.NewRequest(http.MethodGet, url, nil); err != nil {
  29. log.Error("apmSvc.ParseUTFiless error (%v)", err)
  30. return
  31. }
  32. if html, err = d.client.Raw(c, req); err != nil {
  33. log.Error("apmSvc.ParseUTFiles error (%v)", err)
  34. return
  35. }
  36. reg := regexp.MustCompile(`<option(.*)</option>`)
  37. files = reg.FindAllString(string(html), -1)
  38. for _, file := range files {
  39. cov, _ := strconv.ParseFloat(file[strings.Index(file, "(")+1:strings.Index(file, "%)")], 64)
  40. pkg := &ut.PkgAnls{
  41. PKG: file[strings.Index(file, "go-common") : strings.Index(file, ".go")+3],
  42. Coverage: cov,
  43. HTMLURL: url + "#" + file[strings.Index(file, `="`)+2:strings.Index(file, `">`)],
  44. }
  45. pkgs = append(pkgs, pkg)
  46. }
  47. return
  48. }
  49. // SendWechatToUsers send msg to multiple users by saga-admin api
  50. func (d *Dao) SendWechatToUsers(c context.Context, users []string, msg string) (err error) {
  51. var (
  52. req *http.Request
  53. b = &bytes.Buffer{}
  54. url = _sagaWechatURL + "/message/send"
  55. res struct {
  56. Code int `json:"code"`
  57. Message string `json:"message"`
  58. }
  59. body = &ut.WechatUsersMsg{
  60. ToUser: users,
  61. Content: msg,
  62. }
  63. )
  64. if err = json.NewEncoder(b).Encode(body); err != nil {
  65. log.Error("apmSvc.SendWechatToUsers Error(%v)", err)
  66. return
  67. }
  68. if req, err = http.NewRequest(http.MethodPost, url, b); err != nil {
  69. log.Error("apmSvc.SendWechatToUsers Error(%v)", err)
  70. return
  71. }
  72. if err = d.client.Do(c, req, &res); err != nil {
  73. log.Error("apmSvc.SendWechatToUsers Error(%v)", err)
  74. return
  75. }
  76. if res.Code != 0 {
  77. err = fmt.Errorf("Http response Code(%v)!=0", res.Code)
  78. log.Error("apmSvc.SendWechatToUsers Error(%v)", err)
  79. return
  80. }
  81. return
  82. }
  83. // SendWechatToGroup send msg to a group by saga-admin api
  84. func (d *Dao) SendWechatToGroup(c context.Context, chatid, msg string) (err error) {
  85. var (
  86. num int
  87. req *http.Request
  88. b = &bytes.Buffer{}
  89. url = _sagaWechatURL + "/appchat/send"
  90. body = &ut.WechatGroupMsg{
  91. ChatID: chatid,
  92. MsgType: "text",
  93. Safe: 0,
  94. }
  95. )
  96. msgBlock := strings.Split(msg, "\n")
  97. if len(msgBlock)%40 == 0 {
  98. num = len(msgBlock)/40 - 1
  99. } else {
  100. num = len(msgBlock) / 40
  101. }
  102. for i := 0; i <= num; i++ {
  103. var (
  104. res struct {
  105. Code int `json:"code"`
  106. Message string `json:"message"`
  107. }
  108. )
  109. start, end := 40*i, 40*(i+1)
  110. if end > len(msgBlock) {
  111. end = len(msgBlock)
  112. }
  113. body.Text = &ut.TextContent{
  114. Content: strings.Join(msgBlock[start:end], "\n") + fmt.Sprintf("\n(%d/%d)", i+1, num+1),
  115. }
  116. if err = json.NewEncoder(b).Encode(body); err != nil {
  117. log.Error("apmSvc.SendWechatToGroup Error(%v)", err)
  118. return
  119. }
  120. if req, err = http.NewRequest(http.MethodPost, url, b); err != nil {
  121. log.Error("apmSvc.SendWechatToGroup Error(%v)", err)
  122. return
  123. }
  124. if err = d.client.Do(c, req, &res); err != nil {
  125. log.Error("apmSvc.SendWechatToGroup Error(%v)", err)
  126. return
  127. }
  128. if res.Code != 0 {
  129. err = fmt.Errorf("Http response Code(%v)!=0", res.Code)
  130. log.Error("apmSvc.SendWechatToGroup Error(%v)", err)
  131. return
  132. }
  133. }
  134. return
  135. }
  136. // GitLabCommits transfer gitlab uri,now support get method
  137. func (d *Dao) GitLabCommits(c context.Context, commitID string) (commit *ut.GitlabCommit, err error) {
  138. var req *http.Request
  139. params := url.Values{}
  140. params.Set("private_token", conf.Conf.Gitlab.Token)
  141. if req, err = http.NewRequest(http.MethodGet, _gitCommitsAPI+"/"+commitID+"?"+params.Encode(), nil); err != nil {
  142. log.Error("GitLabCommits http.NewRequest error(%v) params(%s)", err, params.Encode())
  143. return
  144. }
  145. if err = d.client.Do(c, req, &commit); err != nil {
  146. log.Error("GitLabCommits d.client.Do error(%v) params(%s)", err, params.Encode())
  147. return
  148. }
  149. return
  150. }
  151. // GetCoverage get the none-file coverage by commitID and pkg (pkg cannot be fileName)
  152. func (d *Dao) GetCoverage(c context.Context, commitID, pkg string) (cov float64, err error) {
  153. var (
  154. count = strings.Count(pkg, "/")
  155. file = &ut.File{}
  156. )
  157. if len(pkg) == 0 {
  158. err = fmt.Errorf("The pkg should not be empty")
  159. return
  160. }
  161. if pkg[len(pkg)-1] != '/' {
  162. count++
  163. }
  164. err = d.DB.Select(`commit_id,substring_index(name,"/",?) as pkg,round(sum(covered_statements)/sum(statements)*100,2) as coverage`, count).Group(fmt.Sprintf(`commit_id,substring_index(name,"/",%d)`, count)).Having("commit_id=? and pkg=?", commitID, pkg).First(file).Error
  165. if err == gorm.ErrRecordNotFound {
  166. cov, err = 0.00, nil
  167. return
  168. } else if err != nil {
  169. log.Error("dao.GetCoverage commitID(%s) pkg(%s) error(%v)", commitID, pkg, err)
  170. return
  171. }
  172. cov = file.Coverage
  173. return
  174. }