edb_monitor_message.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package edbmonitor
  2. import (
  3. "errors"
  4. edbmonitor "eta/eta_api/models/edb_monitor"
  5. "eta/eta_api/models/edb_monitor/response"
  6. "eta/eta_api/utils"
  7. "strconv"
  8. "time"
  9. "github.com/gorilla/websocket"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. )
  12. var (
  13. EDB_MONITOR_MESSAGE_CONNECT_CACHE = "edb_monitor_message_cache:"
  14. )
  15. var MonitorMessageConn = make(map[int]*websocket.Conn)
  16. func ReadEdbMonitorMessage(messageId, adminId int) (msg string, err error) {
  17. message, err := edbmonitor.GetEdbMonitorMessageById(messageId)
  18. if err != nil {
  19. if err.Error() == utils.ErrNoRow() {
  20. msg = "消息不存在"
  21. return
  22. }
  23. msg = "获取消息失败"
  24. return
  25. }
  26. if message.AdminId != adminId {
  27. msg = "您没有权限查看该消息"
  28. err = errors.New("no permission")
  29. return
  30. }
  31. message.IsRead = 1
  32. err = message.Update([]string{"IsRead"})
  33. if err != nil {
  34. msg = "已读失败"
  35. return
  36. }
  37. return
  38. }
  39. func ReadEdbMonitorMessageList(messageId []int, adminId int) (msg string, err error) {
  40. err = edbmonitor.BatchModifyEdbMonitorMessageIsRead(messageId, adminId)
  41. if err != nil {
  42. msg = "已读失败"
  43. return
  44. }
  45. return
  46. }
  47. func EdbMonitorMessageHealth(adminId int) (isClose bool, err error) {
  48. conn := MonitorMessageConn[adminId]
  49. if conn == nil {
  50. err = errors.New("no connection")
  51. isClose = true
  52. return
  53. }
  54. _, msg, err := conn.ReadMessage()
  55. if err != nil {
  56. isClose = true
  57. return
  58. }
  59. if string(msg) == "ping" {
  60. healthKey := EDB_MONITOR_MESSAGE_CONNECT_CACHE + strconv.Itoa(adminId)
  61. err = utils.Rc.Put(healthKey, "1", time.Minute*1)
  62. if err != nil {
  63. return
  64. }
  65. }
  66. return
  67. }
  68. func LogMessage(content, uniqueCode string, triggerTime time.Time, edbInfoId, edbInfoType, adminId, isRead, classifyId int) (err error) {
  69. message := &edbmonitor.EdbMonitorMessage{
  70. EdbInfoId: edbInfoId,
  71. EdbInfoType: edbInfoType,
  72. EdbUniqueCode: uniqueCode,
  73. EdbClassifyId: classifyId,
  74. AdminId: adminId,
  75. IsRead: isRead,
  76. Message: content + "触发预警",
  77. MonitorTriggerTime: triggerTime,
  78. CreateTime: time.Now(),
  79. }
  80. _, err = message.Insert()
  81. return err
  82. }
  83. func SendMessages(adminId, edbInfoId, edbInfoType int, classifyId int, edbUniqueCode, message string, triggerTime string) (err error) {
  84. conn := MonitorMessageConn[adminId]
  85. if conn == nil {
  86. return errors.New("no connection")
  87. }
  88. msg := response.EdbMonitorMessageResp{
  89. EdbInfoId: edbInfoId,
  90. EdbInfoType: edbInfoType,
  91. EdbUniqueCode: edbUniqueCode,
  92. EdbClassifyId: classifyId,
  93. Message: message,
  94. TriggerTime: triggerTime,
  95. }
  96. return conn.WriteJSON(msg)
  97. }
  98. func GetHistoryMessages(adminId int) (items []*response.EdbMonitorMessageResp, err error) {
  99. messageList, err := edbmonitor.GetEdbMonitorMessageByAdminId(adminId)
  100. if err != nil {
  101. return
  102. }
  103. items = toEdbMonitorMessageResp(messageList)
  104. return
  105. }
  106. func GetMessageList(adminid int, currentIndex, pageSize int) (resp response.EdbMonitorMessageListResp, err error) {
  107. startSize := utils.StartIndex(currentIndex, pageSize)
  108. total, err := edbmonitor.GetEdbMonitorMessageCountByAdminId(adminid)
  109. if err != nil {
  110. return
  111. }
  112. if total == 0 {
  113. resp.List = make([]*response.EdbMonitorMessageResp, 0)
  114. resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
  115. return
  116. }
  117. messageList, err := edbmonitor.GetEdbMonitorMessagePageByAdminId(adminid, startSize, pageSize)
  118. if err != nil {
  119. return
  120. }
  121. resp.List = toEdbMonitorMessageResp(messageList)
  122. resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
  123. return
  124. }
  125. func toEdbMonitorMessageResp(items []*edbmonitor.EdbMonitorMessage) (list []*response.EdbMonitorMessageResp) {
  126. list = make([]*response.EdbMonitorMessageResp, 0)
  127. for _, message := range items {
  128. item := response.EdbMonitorMessageResp{
  129. EdbMonitorMessageId: message.EdbMonitorMessageId,
  130. EdbInfoId: message.EdbInfoId,
  131. EdbInfoType: message.EdbInfoType,
  132. EdbUniqueCode: message.EdbUniqueCode,
  133. EdbClassifyId: message.EdbClassifyId,
  134. IsRead: message.IsRead,
  135. Message: message.Message,
  136. TriggerTime: utils.TimeTransferString(utils.FormatDateTime, message.MonitorTriggerTime),
  137. }
  138. list = append(list, &item)
  139. }
  140. return
  141. }