monitor.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go-common/app/admin/main/reply/model"
  9. accmdl "go-common/app/service/main/account/api"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/queue/databus/report"
  13. )
  14. // MonitorStats return monitor stats.
  15. func (s *Service) MonitorStats(c context.Context, mode, page, pageSize int64, adminIDs, sort, order, startTime, endTime string) (res *model.StatsMonitorResult, err error) {
  16. return s.dao.MonitorStats(c, mode, page, pageSize, adminIDs, sort, order, startTime, endTime)
  17. }
  18. // MonitorSearch return monitor result from search.
  19. func (s *Service) MonitorSearch(c context.Context, sp *model.SearchMonitorParams, page, pageSize int64) (res *model.SearchMonitorResult, err error) {
  20. if res, err = s.dao.SearchMonitor(c, sp, page, pageSize); err != nil {
  21. log.Error("s.dao.SearchMonitor(%v,%d,%d) error(%v)", sp, page, pageSize, err)
  22. }
  23. return
  24. }
  25. // UpMonitorState set monitor state into subject attr.
  26. func (s *Service) UpMonitorState(c context.Context, adminID int64, adName string, oid int64, typ, state int32, remark string) (err error) {
  27. sub, err := s.dao.Subject(c, oid, typ)
  28. if err != nil {
  29. return
  30. }
  31. if sub == nil {
  32. err = ecode.NothingFound
  33. return
  34. }
  35. var logState int32
  36. switch state {
  37. case model.MonitorClose:
  38. if sub.AttrVal(model.SubAttrMonitor) == model.AttrYes {
  39. logState = model.AdminOperSubMonitorClose
  40. } else if sub.AttrVal(model.SubAttrAudit) == model.AttrYes {
  41. logState = model.AdminOperSubAuditClose
  42. } else {
  43. err = ecode.ReplyIllegalSubState
  44. return
  45. }
  46. sub.AttrSet(model.AttrNo, model.SubAttrMonitor)
  47. sub.AttrSet(model.AttrNo, model.SubAttrAudit)
  48. case model.MonitorOpen:
  49. sub.AttrSet(model.AttrYes, model.SubAttrMonitor)
  50. sub.AttrSet(model.AttrNo, model.SubAttrAudit)
  51. logState = model.AdminOperSubMonitorOpen
  52. case model.MonitorAudit:
  53. sub.AttrSet(model.AttrNo, model.SubAttrMonitor)
  54. sub.AttrSet(model.AttrYes, model.SubAttrAudit)
  55. logState = model.AdminOperSubAuditOpen
  56. default:
  57. err = ecode.RequestErr
  58. return
  59. }
  60. // update attr
  61. now := time.Now()
  62. if _, err = s.dao.UpSubjectAttr(c, oid, typ, sub.Attr, now); err != nil {
  63. log.Error("s.dao.UpSubjectAttr(%d,%d,%d,%d) state:%d error(%v)", typ, oid, sub.Attr, state, err)
  64. return
  65. }
  66. if err = s.dao.DelSubjectCache(c, oid, typ); err != nil {
  67. log.Error("MonitorState del subject cache error(%v)", err)
  68. }
  69. // update search index
  70. if err = s.dao.UpSearchMonitor(c, sub, remark); err != nil {
  71. log.Error("s.dao.UpdateMonitor(%v) error(%v)", sub, err)
  72. return
  73. }
  74. s.dao.AddAdminLog(c, []int64{oid}, []int64{0}, adminID, typ, model.AdminIsNew, model.AdminIsNotReport, logState, fmt.Sprintf("修改监控状态为: %d", state), remark, now)
  75. report.Manager(&report.ManagerInfo{
  76. UID: adminID,
  77. Uname: adName,
  78. Business: 41,
  79. Type: int(typ),
  80. Oid: oid,
  81. Ctime: now,
  82. Action: model.ReportActionReplyMonitor,
  83. Content: map[string]interface{}{
  84. "remark": remark,
  85. },
  86. Index: []interface{}{sub.Mid, logState, state},
  87. })
  88. return
  89. }
  90. // MointorLog MointorLog
  91. func (s *Service) MointorLog(c context.Context, sp model.LogSearchParam) (result *model.MonitorLogResult, err error) {
  92. var (
  93. mids []int64
  94. userInfo map[int64]*accmdl.Info
  95. )
  96. adNames := map[int64]string{}
  97. result = &model.MonitorLogResult{
  98. Logs: []*model.MonitorLog{},
  99. }
  100. sp.Action = "monitor"
  101. reportData, err := s.dao.ReportLog(c, sp)
  102. if err != nil {
  103. return
  104. }
  105. result.Page = reportData.Page
  106. result.Sort = reportData.Sort
  107. result.Order = reportData.Order
  108. for i, data := range reportData.Result {
  109. mid := data.Index0
  110. reportData.Result[i].OidStr = strconv.FormatInt(reportData.Result[i].Oid, 10)
  111. logState := data.Index1
  112. state := data.Index2
  113. title, link, _ := s.TitleLink(c, data.Oid, data.Type)
  114. var extra map[string]string
  115. if data.Content != "" {
  116. err = json.Unmarshal([]byte(data.Content), &extra)
  117. if err != nil {
  118. log.Error("MointorLog unmarshal failed!err:=%v", err)
  119. return
  120. }
  121. }
  122. if data.AdminName == "" {
  123. adNames[data.AdminID] = ""
  124. }
  125. result.Logs = append(result.Logs, &model.MonitorLog{
  126. Mid: mid,
  127. AdminID: data.AdminID,
  128. AdminName: data.AdminName,
  129. Oid: data.Oid,
  130. OidStr: data.OidStr,
  131. Type: data.Type,
  132. Remark: extra["remark"],
  133. CTime: data.Ctime,
  134. LogState: logState,
  135. State: state,
  136. Title: title,
  137. RedirectURL: link,
  138. })
  139. mids = append(mids, mid)
  140. }
  141. if len(adNames) > 0 {
  142. s.dao.AdminName(c, adNames)
  143. }
  144. if len(mids) > 0 {
  145. var infosReply *accmdl.InfosReply
  146. infosReply, err = s.accSrv.Infos3(c, &accmdl.MidsReq{Mids: mids})
  147. if err != nil {
  148. log.Error(" s.accSrv.Infos3 (%v) error(%v)", mids, err)
  149. err = nil
  150. return
  151. }
  152. userInfo = infosReply.Infos
  153. }
  154. for _, log := range result.Logs {
  155. if user, ok := userInfo[log.Mid]; ok {
  156. log.UserName = user.GetName()
  157. }
  158. if log.AdminName == "" {
  159. log.AdminName = adNames[log.AdminID]
  160. }
  161. }
  162. return
  163. }