apply.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package service
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/ep/melloi/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. // QueryApply query apply by apply object
  14. func (s *Service) QueryApply(qar *model.QueryApplyRequest) (*model.QueryApplyResponse, error) {
  15. return s.dao.QueryApply(&qar.Apply, qar.PageNum, qar.PageSize)
  16. }
  17. // QueryUserApplyList query user apply list
  18. func (s *Service) QueryUserApplyList(userName string) ([]*model.Apply, error) {
  19. return s.dao.QueryUserApplyList(userName)
  20. }
  21. // CheckRunPermission check user run permission
  22. func (s *Service) CheckRunPermission(userName string) (ret bool) {
  23. var (
  24. startTime int64
  25. err error
  26. endTime int64
  27. applyList []*model.Apply
  28. )
  29. currentTime := time.Now().Unix()
  30. //白名单的人,不需要做check,直接返回true
  31. if ExistsInSlice(userName, s.c.Melloi.Executor) {
  32. ret = true
  33. return
  34. }
  35. if applyList, err = s.dao.QueryUserApplyList(userName); err != nil {
  36. log.Error("s.dao.QueryUserApplyList err :(%v)", err)
  37. return
  38. }
  39. for _, apply := range applyList {
  40. if startTime, err = strconv.ParseInt(apply.StartTime, 10, 64); err != nil {
  41. return
  42. }
  43. if endTime, err = strconv.ParseInt(apply.EndTime, 10, 64); err != nil {
  44. return
  45. }
  46. if currentTime >= startTime && currentTime <= endTime {
  47. ret = true
  48. break
  49. }
  50. }
  51. return ret
  52. }
  53. // CheckRunTime check perf time
  54. func (s *Service) CheckRunTime() (ret bool) {
  55. currentTime := time.Now()
  56. // 1:30 ~ 12:00
  57. if currentTime.Hour() >= 1 && currentTime.Hour() < 12 {
  58. ret = true
  59. }
  60. // 14:00 ~ 17:00
  61. if currentTime.Hour() >= 14 && currentTime.Hour() < 17 {
  62. ret = true
  63. }
  64. return
  65. }
  66. // UpdateApply update apply info
  67. func (s *Service) UpdateApply(cookie string, apply *model.Apply) (err error) {
  68. var user *model.User
  69. if apply.ID == 0 {
  70. return ecode.MelloiApplyRequestErr
  71. }
  72. if user, err = s.QueryUser(apply.From); err != nil {
  73. log.Error("update apply query user error:(%v)", err)
  74. return
  75. }
  76. if user.ID != 0 {
  77. user.Accept = apply.Status
  78. if err = s.dao.UpdateUser(user); err != nil {
  79. return err
  80. }
  81. // 判断是审批操作,发送微信通知
  82. if apply.Status == 1 {
  83. applyMsg, _ := s.dao.QueryApplyByID(apply.ID)
  84. startTime, _ := strconv.Atoi(applyMsg.StartTime)
  85. st := time.Unix(int64(startTime), 0)
  86. endTime, _ := strconv.Atoi(applyMsg.EndTime)
  87. et := time.Unix(int64(endTime), 0)
  88. content := "[MELLOI]压测申请处理完成 通知 \n 压测服务: " + applyMsg.Path + "\n" + "压测时间:" + st.Format("2006-01-02 15:04:05") + "\n" + "压测结束时间:" + et.Format("2006-01-02 15:04:05") + "\n" + "申请人:" +
  89. applyMsg.From + "\n" + "审批人:" + applyMsg.To + "\n" + "审批时间:" + time.Now().Format("2006-01-02 15:04:05")
  90. log.Info("content:(%s)", content)
  91. // 发送申请通过到群
  92. go s.AddWechatSend(context.TODO(), cookie, content)
  93. // 给申请人发送邮件
  94. go s.SendMail(apply.From+"@bilibili.com", "[MELLOI]压测申请通知", content)
  95. }
  96. return s.dao.UpdateApply(apply)
  97. }
  98. return ecode.MelloiUpdateUserErr
  99. }
  100. // AddApply add new apply
  101. func (s *Service) AddApply(c context.Context, cookie string, apply *model.Apply) (err error) {
  102. apply.Status = -1
  103. apply.Active = 1
  104. // 添加apply到db
  105. if err = s.dao.AddApply(apply); err != nil {
  106. return ecode.MelloiApplyRequestErr
  107. }
  108. // 发送微信消息 & 发送邮件通知
  109. //加密 apply.id|apply.from|apply.to
  110. applyID := strconv.FormatInt(apply.ID, 10)
  111. beStr := applyID + "|" + apply.From + "|" + apply.To
  112. base64Str := base64.StdEncoding.EncodeToString([]byte(beStr))
  113. // 将时间戳转成日期
  114. startTime, _ := strconv.Atoi(apply.StartTime)
  115. st := time.Unix(int64(startTime), 0)
  116. endTime, _ := strconv.Atoi(apply.EndTime)
  117. et := time.Unix(int64(endTime), 0)
  118. // 增加依赖服务列表
  119. var (
  120. userService map[string][]string
  121. serviceList = make(map[string][]string)
  122. serviceDep string
  123. serviceName string
  124. )
  125. serviceName = strings.Replace(apply.Path, "bilibili.", "", 1)
  126. if userService, err = s.QueryDependServiceAdmins(c, serviceName, s.getSessionInCookie(cookie)); err != nil {
  127. log.Error("query depend service admin error(%v)", err)
  128. return
  129. }
  130. for _, v := range userService {
  131. for _, service := range v {
  132. serviceList[service] = nil
  133. }
  134. }
  135. for k := range serviceList {
  136. serviceDep += "\n" + k
  137. }
  138. // 拼接消息体,amd=base64Str
  139. content := fmt.Sprintf("[MELLOI]压测申请处理 通知 \n 压测服务:%s\n压测开始时间段:%s\n压测结束时间段:%s\n申请人:%s\n申请时间:%s\n依赖服务:%s\n审批地址:http://melloi.bilibili.co#/apply-m?platform=mb&amd=%s",
  140. apply.Path, st.Format("2006-01-02 15:04:05"), et.Format("2006-01-02 15:04:05"),
  141. apply.From, time.Now().Format("2006-01-02 15:04:05"), serviceDep, base64Str)
  142. // 消息接收人
  143. var touser []string
  144. touser = append(touser, apply.To)
  145. // 发送微信
  146. go s.dao.PushWechatMsgToPerson(context.TODO(), cookie, touser, content)
  147. // 发送邮件
  148. subject := "Melloi压测申请"
  149. go s.SendMail(apply.To+"@bilibili.com", subject, content)
  150. return
  151. }
  152. // DeleteApply delete apply
  153. func (s *Service) DeleteApply(id int64) error {
  154. return s.dao.DeleteApply(id)
  155. }