edb_monitor_message.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package edb_monitor
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/edb_monitor/request"
  7. edbmonitor "eta/eta_api/services/edb_monitor"
  8. "eta/eta_api/utils"
  9. "net/http"
  10. "strconv"
  11. "time"
  12. "github.com/gorilla/websocket"
  13. )
  14. type EdbMonitorMessageController struct {
  15. controllers.BaseAuthController
  16. }
  17. var upgrader = websocket.Upgrader{
  18. ReadBufferSize: 1024,
  19. WriteBufferSize: 1024,
  20. CheckOrigin: func(r *http.Request) bool {
  21. return true
  22. },
  23. }
  24. // GetMonitorLevel
  25. // @Title 预警管理消息
  26. // @Description 预警管理消息
  27. // @Param request body request.EdbMonitorSaveRequest true "每页数据条数"
  28. // @Success 200 {object} models.EnglishReportEmailPageListResp
  29. // @router /message/connect [get]
  30. func (m *EdbMonitorMessageController) Connect() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. m.Data["json"] = br
  34. m.ServeJSON()
  35. }()
  36. sysUser := m.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. var conn *websocket.Conn
  44. connKey := edbmonitor.EDB_MONITOR_MESSAGE_CONNECT_CACHE + strconv.Itoa(sysUser.AdminId)
  45. ok := utils.Rc.IsExist(connKey)
  46. if !ok {
  47. conn = edbmonitor.MonitorMessageConn[sysUser.AdminId]
  48. if conn != nil {
  49. conn.Close()
  50. }
  51. }
  52. err := utils.Rc.Put(connKey, "1", time.Minute*1)
  53. if err != nil {
  54. br.Msg = "系统错误"
  55. br.ErrMsg = "连接失败,err:" + err.Error()
  56. return
  57. }
  58. conn, err = upgrader.Upgrade(m.Ctx.ResponseWriter, m.Ctx.Request, nil)
  59. if err != nil {
  60. br.Msg = "连接失败"
  61. br.ErrMsg = "连接失败,err:" + err.Error()
  62. return
  63. }
  64. defer conn.Close()
  65. edbmonitor.MonitorMessageConn[sysUser.AdminId] = conn
  66. conn.SetCloseHandler(func(code int, text string) error {
  67. delete(edbmonitor.MonitorMessageConn, sysUser.AdminId)
  68. utils.Rc.Delete(connKey)
  69. return nil
  70. })
  71. go func() {
  72. // 心跳检测
  73. for {
  74. isClose, err := edbmonitor.EdbMonitorMessageHealth(sysUser.AdminId)
  75. if err != nil {
  76. utils.FileLog.Error("指标预警信息健康检查失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
  77. return
  78. }
  79. if isClose {
  80. return
  81. }
  82. }
  83. }()
  84. messageList, err := edbmonitor.GetHistoryMessages(sysUser.AdminId)
  85. if err != nil {
  86. utils.FileLog.Error("获取指标预警信息历史失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
  87. }
  88. success := make(chan int, 10)
  89. go func() {
  90. defer close(success)
  91. for i, msg := range messageList {
  92. if i == 0 {
  93. // 多条消息仅发送最新一条
  94. err = edbmonitor.SendMessages(sysUser.AdminId, msg.EdbInfoId, msg.EdbInfoType, msg.EdbClassifyId, msg.EdbUniqueCode, msg.Message, msg.TriggerTime)
  95. if err != nil {
  96. utils.FileLog.Error("指标预警信息发送失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
  97. } else {
  98. success <- msg.EdbMonitorMessageId
  99. }
  100. } else {
  101. success <- msg.EdbMonitorMessageId
  102. }
  103. }
  104. }()
  105. go func() {
  106. readList := make([]int, 0)
  107. for {
  108. msgId, ok := <-success
  109. if !ok {
  110. break
  111. }
  112. readList = append(readList, msgId)
  113. }
  114. _, err = edbmonitor.ReadEdbMonitorMessageList(readList, sysUser.AdminId)
  115. if err != nil {
  116. utils.FileLog.Error("指标预警信息已读失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
  117. }
  118. }()
  119. for {
  120. ok = utils.Rc.IsExist(connKey)
  121. if !ok {
  122. br.Msg = "连接已断开"
  123. br.Ret = 200
  124. br.Success = true
  125. return
  126. }
  127. time.Sleep(10 * time.Second)
  128. }
  129. }
  130. // Close
  131. // @Title 预警管理消息
  132. // @Description 预警管理消息
  133. // @Param request body request.EdbMonitorSaveRequest true "每页数据条数"
  134. // @Success 200 {object} models.EnglishReportEmailPageListResp
  135. // @router /message/close [post]
  136. func (m *EdbMonitorMessageController) Close() {
  137. br := new(models.BaseResponse).Init()
  138. defer func() {
  139. m.Data["json"] = br
  140. m.ServeJSON()
  141. }()
  142. sysUser := m.SysUser
  143. if sysUser == nil {
  144. br.Msg = "请登录"
  145. br.ErrMsg = "请登录,SysUser Is Empty"
  146. br.Ret = 408
  147. return
  148. }
  149. conn := edbmonitor.MonitorMessageConn[sysUser.AdminId]
  150. if conn != nil {
  151. conn.Close()
  152. }
  153. br.Msg = "关闭成功"
  154. br.Ret = 200
  155. br.Success = true
  156. }
  157. // List
  158. // @Title 预警管理消息列表
  159. // @Description 预警管理消息列表
  160. // @Param PageSize query int true "每页数据条数"
  161. // @Param CurrentIndex query int true "当前页页码,从1开始"
  162. // @Success 200 {object} response.EdbMonitorClassifyTree
  163. // @router /message/list [get]
  164. func (c *EdbMonitorMessageController) List() {
  165. br := new(models.BaseResponse).Init()
  166. defer func() {
  167. c.Data["json"] = br
  168. c.ServeJSON()
  169. }()
  170. sysUser := c.SysUser
  171. if sysUser == nil {
  172. br.Msg = "请登录"
  173. br.ErrMsg = "请登录,SysUser Is Empty"
  174. br.Ret = 408
  175. return
  176. }
  177. pageSize, _ := c.GetInt("PageSize")
  178. currentIndex, _ := c.GetInt("CurrentIndex")
  179. resp, err := edbmonitor.GetMessageList(sysUser.AdminId, currentIndex, pageSize)
  180. if err != nil {
  181. br.Msg = "获取失败"
  182. br.ErrMsg = err.Error()
  183. return
  184. }
  185. br.Data = resp
  186. br.Msg = "获取成功"
  187. br.Ret = 200
  188. br.Success = true
  189. }
  190. // Read
  191. // @Title 预警管理消息已读
  192. // @Description 预警管理消息已读
  193. // @Param request body request.EdbMonitorSaveRequest true "每页数据条数"
  194. // @Success 200 {object} models.EnglishReportEmailPageListResp
  195. // @router /message/read [post]
  196. func (m *EdbMonitorMessageController) Read() {
  197. br := new(models.BaseResponse).Init()
  198. defer func() {
  199. m.Data["json"] = br
  200. m.ServeJSON()
  201. }()
  202. sysUser := m.SysUser
  203. if sysUser == nil {
  204. br.Msg = "请登录"
  205. br.ErrMsg = "请登录,SysUser Is Empty"
  206. br.Ret = 408
  207. return
  208. }
  209. var req request.EdbMonitorMessageReadReq
  210. if err := json.Unmarshal(m.Ctx.Input.RequestBody, &req); err != nil {
  211. br.Msg = "参数错误"
  212. br.ErrMsg = "参数错误,err:" + err.Error()
  213. return
  214. }
  215. if req.EdbMonitorMessageId <= 0 {
  216. br.Msg = "参数错误"
  217. return
  218. }
  219. msg, err := edbmonitor.ReadEdbMonitorMessage(req.EdbMonitorMessageId, sysUser.AdminId)
  220. if err != nil {
  221. if msg == "" {
  222. msg = "系统错误"
  223. }
  224. br.Msg = msg
  225. br.ErrMsg = "读取消息失败,err:" + err.Error()
  226. return
  227. }
  228. br.Msg = "已读成功"
  229. br.Ret = 200
  230. br.Success = true
  231. }