edb_monitor.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. package edbmonitor
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models/data_manage"
  5. edbmonitor "eta/eta_api/models/edb_monitor"
  6. "eta/eta_api/models/edb_monitor/request"
  7. "eta/eta_api/models/edb_monitor/response"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. )
  17. type SafeEdbMonitorSet struct {
  18. m map[int]struct{}
  19. mu sync.Mutex
  20. }
  21. func NewSafeEdbMonitorSet() *SafeEdbMonitorSet {
  22. return &SafeEdbMonitorSet{
  23. m: make(map[int]struct{}),
  24. }
  25. }
  26. func (e *SafeEdbMonitorSet) Add(id int) {
  27. e.mu.Lock()
  28. defer e.mu.Unlock()
  29. e.m[id] = struct{}{}
  30. }
  31. func (e *SafeEdbMonitorSet) IsExist(id int) (ok bool) {
  32. e.mu.Lock()
  33. defer e.mu.Unlock()
  34. _, ok = e.m[id]
  35. return
  36. }
  37. func (e *SafeEdbMonitorSet) Remove(id int) {
  38. e.mu.Lock()
  39. defer e.mu.Unlock()
  40. delete(e.m, id)
  41. }
  42. var EdbLocalSet = NewSafeEdbMonitorSet()
  43. func GetMonitorList(classifyId, level, state, userId string, pageSize, currentIndex int) (resp response.EdbMonitorInfoListResp, msg string, err error) {
  44. if pageSize <= 0 {
  45. pageSize = utils.PageSize20
  46. }
  47. if currentIndex <= 0 {
  48. currentIndex = 1
  49. }
  50. startSize := utils.StartIndex(currentIndex, pageSize)
  51. var classifyIdList []int
  52. if classifyId != "" {
  53. tmp := strings.Split(classifyId, ",")
  54. for _, v := range tmp {
  55. cv, _ := strconv.Atoi(v)
  56. classifyIdList = append(classifyIdList, cv)
  57. }
  58. }
  59. var levelList []string
  60. if level != "" {
  61. levelList = strings.Split(level, ",")
  62. }
  63. var stateList []int
  64. if state != "" {
  65. tmp := strings.Split(state, ",")
  66. for _, v := range tmp {
  67. st, _ := strconv.Atoi(v)
  68. stateList = append(stateList, st)
  69. }
  70. }
  71. var userIdList []int
  72. if userId != "" {
  73. tmp := strings.Split(userId, ",")
  74. for _, v := range tmp {
  75. ui, _ := strconv.Atoi(v)
  76. userIdList = append(userIdList, ui)
  77. }
  78. }
  79. var conditon string
  80. var pars []interface{}
  81. if len(classifyIdList) != 0 {
  82. conditon += ` AND edb_monitor_classify_id IN (` + utils.GetOrmInReplace(len(classifyIdList)) + ` )`
  83. pars = append(pars, classifyIdList)
  84. }
  85. if len(levelList) != 0 {
  86. conditon += ` AND monitor_level IN (` + utils.GetOrmInReplace(len(levelList)) + ` )`
  87. pars = append(pars, levelList)
  88. }
  89. if len(stateList) != 0 {
  90. conditon += ` AND state IN (` + utils.GetOrmInReplace(len(stateList)) + ` )`
  91. pars = append(pars, stateList)
  92. }
  93. if len(userIdList) != 0 {
  94. conditon += ` AND create_user_id IN (` + utils.GetOrmInReplace(len(userIdList)) + ` )`
  95. pars = append(pars, userIdList)
  96. }
  97. total, err := edbmonitor.GetEdbMonitorInfoCountByCondition(conditon, pars)
  98. if err != nil {
  99. msg = "获取监控列表失败"
  100. err = fmt.Errorf("GetEdbMonitorInfoCountByCondition err:%w", err)
  101. return
  102. }
  103. edbMonitorList, err := edbmonitor.GetEdbMonitorInfoPageByCondition(conditon, pars, startSize, pageSize)
  104. if err != nil {
  105. msg = "获取监控列表失败"
  106. err = fmt.Errorf("GetEdbMonitorInfoPageByCondition err:%w", err)
  107. return
  108. }
  109. var edbInfoId, edbUserId []int
  110. for _, v := range edbMonitorList {
  111. edbUserId = append(edbUserId, v.CreateUserId)
  112. edbInfoId = append(edbInfoId, v.EdbInfoId)
  113. }
  114. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoId)
  115. if err != nil {
  116. msg = "获取监控列表失败"
  117. err = fmt.Errorf("GetEdbInfoByIdList err:%w", err)
  118. return
  119. }
  120. infoMap := make(map[int]string)
  121. for _, v := range edbInfoList {
  122. infoMap[v.EdbInfoId] = v.EdbName
  123. }
  124. userInfoList, err := system.GetSysAdminByIdList(edbUserId)
  125. if err != nil {
  126. msg = "获取监控列表失败"
  127. err = fmt.Errorf("GetSysAdminByIdList err:%w", err)
  128. return
  129. }
  130. userMap := make(map[int]string)
  131. for _, v := range userInfoList {
  132. userMap[v.AdminId] = v.RealName
  133. }
  134. classifyList, err := edbmonitor.GetEdbMonitorClassifyList()
  135. if err != nil {
  136. msg = "获取监控列表失败"
  137. err = fmt.Errorf("GetEdbMonitorClassifyList err:%w", err)
  138. return
  139. }
  140. classifyMap := make(map[int]string)
  141. for _, v := range classifyList {
  142. classifyMap[v.ClassifyId] = v.ClassifyName
  143. }
  144. classifyPathMap := make(map[int]string)
  145. for _, v := range classifyList {
  146. var path []string
  147. if v.ParentId != 0 {
  148. parentName := classifyMap[v.ParentId]
  149. if v.RootId != 0 && v.RootId != v.ParentId {
  150. rootName := classifyMap[v.RootId]
  151. path = append(path, rootName)
  152. }
  153. curName := v.ClassifyName
  154. path = append(path, parentName, curName)
  155. } else {
  156. path = append(path, v.ClassifyName)
  157. }
  158. classifyPathMap[v.ClassifyId] = strings.Join(path, "/")
  159. }
  160. edbMonitorItems := toEdbMonitorInfoItems(edbMonitorList, userMap, classifyPathMap, infoMap)
  161. page := paging.GetPaging(currentIndex, pageSize, total)
  162. resp.List = edbMonitorItems
  163. resp.Paging = page
  164. return
  165. }
  166. func SaveEdbMonitorInfo(req request.EdbMonitorInfoSaveReq, adminId int) (msg string, err error) {
  167. edb, er := data_manage.GetEdbInfoById(req.EdbInfoId)
  168. if er != nil {
  169. if er.Error() == utils.ErrNoRow() {
  170. msg = "指标不存在,请刷新重试"
  171. err = er
  172. return
  173. }
  174. msg = "添加指标预警失败"
  175. err = er
  176. return
  177. }
  178. var edbMonitorInfo *edbmonitor.EdbMonitorInfo
  179. if req.EdbMonitorId > 0 {
  180. ok, msg1, er := OperatePermissionCheck(req.EdbMonitorId, adminId)
  181. if !ok {
  182. if msg1 == "" {
  183. msg1 = "编辑失败"
  184. }
  185. msg = msg1
  186. if er == nil {
  187. er = fmt.Errorf("权限校验失败")
  188. }
  189. err = er
  190. return
  191. }
  192. edbMonitorInfo, er = edbmonitor.GetEdbMonitorInfoById(req.EdbMonitorId)
  193. if er != nil {
  194. if er.Error() == utils.ErrNoRow() {
  195. msg = "指标预警不存在,请刷新重试"
  196. err = er
  197. return
  198. }
  199. msg = "编辑指标预警失败"
  200. err = fmt.Errorf("GetEdbMonitorInfoById err:%w", er)
  201. return
  202. }
  203. var updateCols []string
  204. if edbMonitorInfo.EdbInfoId != req.EdbInfoId {
  205. updateCols = append(updateCols, []string{"edb_info_id", "edb_info_type", "edb_code", "edb_classify_id", "edb_unique_code", "source", "sub_source", "edb_latest_date", "edb_latest_value"}...)
  206. edbMonitorInfo.EdbInfoId = req.EdbInfoId
  207. edbMonitorInfo.EdbInfoType = edb.EdbInfoType
  208. edbMonitorInfo.EdbCode = edb.EdbCode
  209. edbMonitorInfo.EdbClassifyId = edb.ClassifyId
  210. edbMonitorInfo.EdbUniqueCode = edb.UniqueCode
  211. edbMonitorInfo.Source = edb.Source
  212. edbMonitorInfo.SubSource = edb.SubSource
  213. edbMonitorInfo.EdbLatestDate = edb.LatestDate
  214. edbMonitorInfo.EdbLatestValue = edb.LatestValue
  215. }
  216. if edbMonitorInfo.EdbMonitorName != req.EdbMonitorName {
  217. updateCols = append(updateCols, "edb_monitor_name")
  218. edbMonitorInfo.EdbMonitorName = req.EdbMonitorName
  219. }
  220. if edbMonitorInfo.EdbMonitorClassifyId != req.ClassifyId {
  221. updateCols = append(updateCols, "edb_monitor_classify_id")
  222. edbMonitorInfo.EdbMonitorClassifyId = req.ClassifyId
  223. }
  224. if edbMonitorInfo.MonitorType != req.MonitorType {
  225. updateCols = append(updateCols, "monitor_type")
  226. edbMonitorInfo.MonitorType = req.MonitorType
  227. }
  228. monitorData, _ := strconv.ParseFloat(req.MonitorData, 64)
  229. if edbMonitorInfo.MonitorData != monitorData {
  230. updateCols = append(updateCols, "monitor_data")
  231. edbMonitorInfo.MonitorData = monitorData
  232. }
  233. if edbMonitorInfo.MonitorLevel != req.MonitorLevel {
  234. updateCols = append(updateCols, "monitor_level")
  235. edbMonitorInfo.MonitorLevel = req.MonitorLevel
  236. }
  237. if len(updateCols) > 0 {
  238. updateCols = append(updateCols, "modify_time")
  239. edbMonitorInfo.ModifyTime = time.Now()
  240. er := edbMonitorInfo.Update(updateCols)
  241. if er != nil {
  242. msg = "编辑指标预警失败"
  243. err = fmt.Errorf("UpdateEdbMonitorInfoById err:%w", er)
  244. return
  245. }
  246. }
  247. } else {
  248. edbMonitorInfo = new(edbmonitor.EdbMonitorInfo)
  249. edbMonitorInfo.EdbMonitorClassifyId = req.ClassifyId
  250. edbMonitorInfo.EdbMonitorName = req.EdbMonitorName
  251. edbMonitorInfo.EdbInfoId = req.EdbInfoId
  252. edbMonitorInfo.EdbInfoType = edb.EdbInfoType
  253. edbMonitorInfo.EdbUniqueCode = edb.UniqueCode
  254. edbMonitorInfo.EdbClassifyId = edb.ClassifyId
  255. edbMonitorInfo.EdbCode = edb.EdbCode
  256. edbMonitorInfo.Source = edb.Source
  257. edbMonitorInfo.SubSource = edb.SubSource
  258. edbMonitorInfo.EdbLatestDate = edb.LatestDate
  259. edbMonitorInfo.EdbLatestValue = edb.LatestValue
  260. edbMonitorInfo.MonitorType = req.MonitorType
  261. monitorData, _ := strconv.ParseFloat(req.MonitorData, 64)
  262. edbMonitorInfo.MonitorData = monitorData
  263. edbMonitorInfo.MonitorLevel = req.MonitorLevel
  264. edbMonitorInfo.CreateUserId = adminId
  265. edbMonitorInfo.CreateTime = time.Now()
  266. edbMonitorInfo.ModifyTime = time.Now()
  267. insertId, er := edbMonitorInfo.Insert()
  268. if er != nil {
  269. msg = "添加指标预警失败"
  270. err = fmt.Errorf("insert err:%w", er)
  271. return
  272. }
  273. edbMonitorInfo.EdbMonitorId = int(insertId)
  274. }
  275. err = utils.Rc.SAdd(EDB_MONITOR_ID_SET_CACHE, edbMonitorInfo.EdbInfoId)
  276. if err != nil {
  277. utils.FileLog.Info("监控指标添加缓存失败", err)
  278. }
  279. EdbLocalSet.Add(edbMonitorInfo.EdbInfoId)
  280. err = ModifyEdbMonitorStateAndSendMsg(edbMonitorInfo, edb.EdbCode, edb.Source, edb.SubSource)
  281. if err != nil {
  282. msg = "更新指标预警失败"
  283. err = fmt.Errorf("ModifyEdbMonitorState err:%w", err)
  284. return
  285. }
  286. return
  287. }
  288. func ModifyEdbMonitorStateAndSendMsg(edbMonitorInfo *edbmonitor.EdbMonitorInfo, edbCode string, source, subSource int) (err error) {
  289. cond := ` AND edb_code = ? `
  290. pars := []interface{}{edbCode}
  291. latestTwoData, er := data_manage.GetEdbDataListByCondition(cond, pars, source, subSource, 2, 0)
  292. if er != nil {
  293. err = fmt.Errorf("GetEdbDataListByCondition err:%w", er)
  294. return
  295. }
  296. state, err := ModifyEdbMonitorState(edbMonitorInfo, latestTwoData)
  297. if err != nil {
  298. return
  299. }
  300. if state == EDB_MONITOR_STATE_TRIGGER_SUCCESS {
  301. SendAndLogMessage(edbMonitorInfo)
  302. }
  303. return
  304. }
  305. func GetTwoEdbDataByDataTime(edbCode, dataTime string, source, subSource int) (twoData []*data_manage.EdbData, err error) {
  306. cond := ` AND edb_code = ? AND data_time <= ? `
  307. pars := []interface{}{edbCode, dataTime}
  308. twoData, er := data_manage.GetEdbDataListByCondition(cond, pars, source, subSource, 2, 0)
  309. if er != nil {
  310. err = fmt.Errorf("GetEdbDataListByCondition err:%w", er)
  311. return
  312. }
  313. return
  314. }
  315. func ModifyEdbMonitorState(edbMonitorInfo *edbmonitor.EdbMonitorInfo, latestTwoData []*data_manage.EdbData) (state int, err error) {
  316. var updateCols []string
  317. edbMonitorInfo.EdbLatestDate = latestTwoData[0].DataTime
  318. edbMonitorInfo.EdbLatestValue = latestTwoData[0].Value
  319. updateCols = append(updateCols, []string{"edb_latest_date", "edb_latest_value"}...)
  320. state = cmpEdbMonitorState(latestTwoData, edbMonitorInfo.MonitorType, edbMonitorInfo.MonitorData)
  321. if state == EDB_MONITOR_STATE_TRIGGER_SUCCESS {
  322. tmpTime, _ := time.Parse(utils.FormatDate, latestTwoData[0].DataTime)
  323. edbMonitorInfo.EdbTriggerDate = tmpTime
  324. edbMonitorInfo.MonitorTriggerTime = time.Now()
  325. updateCols = append(updateCols, []string{"edb_trigger_date", "monitor_trigger_time"}...)
  326. }
  327. if edbMonitorInfo.State == EDB_MONITOR_STATE_NO_TRIGGER || edbMonitorInfo.State == EDB_MONITOR_STATE_CLOSE {
  328. edbMonitorInfo.State = state
  329. updateCols = append(updateCols, "state")
  330. }
  331. err = edbMonitorInfo.Update(updateCols)
  332. if err != nil {
  333. return
  334. }
  335. return
  336. }
  337. func SendAndLogMessage(edbMonitorInfo *edbmonitor.EdbMonitorInfo) (err error) {
  338. triggerTime := edbMonitorInfo.MonitorTriggerTime.Format(utils.FormatDateTime)
  339. err = SendMessages(edbMonitorInfo.CreateUserId, edbMonitorInfo.EdbInfoId, edbMonitorInfo.EdbInfoType, edbMonitorInfo.EdbClassifyId, edbMonitorInfo.EdbUniqueCode, edbMonitorInfo.EdbMonitorName, triggerTime)
  340. isRead := 1
  341. if err != nil {
  342. err = nil
  343. isRead = 0
  344. }
  345. err = LogMessage(edbMonitorInfo.EdbMonitorName, edbMonitorInfo.EdbUniqueCode, edbMonitorInfo.MonitorTriggerTime, edbMonitorInfo.EdbInfoId, edbMonitorInfo.EdbInfoType, edbMonitorInfo.CreateUserId, isRead, edbMonitorInfo.EdbClassifyId)
  346. return
  347. }
  348. func cmpEdbMonitorState(twoData []*data_manage.EdbData, monitorType int, monitorData float64) int {
  349. if len(twoData) < 2 {
  350. return EDB_MONITOR_STATE_NO_TRIGGER
  351. }
  352. switch monitorType {
  353. case EDB_MONITOR_TYPE_UP:
  354. if twoData[0].Value >= monitorData && twoData[1].Value < monitorData {
  355. return EDB_MONITOR_STATE_TRIGGER_SUCCESS
  356. } else {
  357. return EDB_MONITOR_STATE_NO_TRIGGER
  358. }
  359. case EDB_MONITOR_TYPE_DOWN:
  360. if twoData[0].Value <= monitorData && twoData[1].Value > monitorData {
  361. return EDB_MONITOR_STATE_TRIGGER_SUCCESS
  362. } else {
  363. return EDB_MONITOR_STATE_NO_TRIGGER
  364. }
  365. default:
  366. return EDB_MONITOR_STATE_NO_TRIGGER
  367. }
  368. }
  369. func OperatePermissionCheck(edbMonitorId int, adminId int) (ok bool, msg string, err error) {
  370. monitorInfo, err := edbmonitor.GetEdbMonitorInfoById(edbMonitorId)
  371. if err != nil {
  372. if err.Error() == utils.ErrNoRow() {
  373. msg = "指标预警不存在,请刷新重试"
  374. return
  375. }
  376. msg = "删除失败"
  377. err = fmt.Errorf("获取预警详情失败, err:%w", err)
  378. return
  379. }
  380. if monitorInfo.CreateUserId != adminId {
  381. userInfo, er := system.GetSysAdminById(adminId)
  382. if er != nil {
  383. msg = "删除失败"
  384. err = fmt.Errorf("查询用户信息失败 err:%w", er)
  385. return
  386. }
  387. if userInfo.Role != utils.ROLE_TYPE_CODE_ADMIN {
  388. msg = "无权限操作"
  389. err = fmt.Errorf("无权限操作")
  390. return
  391. }
  392. }
  393. ok = true
  394. return
  395. }
  396. func DeleteEdbMonitorInfo(req request.EdbMonitorInfoDeleteReq, adminId int) (msg string, err error) {
  397. ok, msg, err := OperatePermissionCheck(req.EdbMonitorId, adminId)
  398. if !ok {
  399. if msg == "" {
  400. msg = "删除失败"
  401. }
  402. if err == nil {
  403. err = fmt.Errorf("权限校验失败")
  404. }
  405. return
  406. }
  407. err = edbmonitor.DeleteEdbMonitorInfoById(req.EdbMonitorId)
  408. if err != nil {
  409. msg = "删除失败"
  410. err = fmt.Errorf("删除预警失败 err:%w", err)
  411. }
  412. return
  413. }
  414. func GetEdbMonitorLevelList() (list []string, msg string, err error) {
  415. list, err = edbmonitor.GetEdbMonitorLevelList()
  416. if err != nil {
  417. msg = "获取等级等级列表失败"
  418. err = fmt.Errorf("GetEdbMonitorLevelList err:%w", err)
  419. return
  420. }
  421. return
  422. }
  423. func CloseEdbMonitorInfo(req request.EdbMonitorInfoCloseReq, adminId int) (msg string, err error) {
  424. edbMonitor, er := edbmonitor.GetEdbMonitorInfoById(req.EdbMonitorId)
  425. if er != nil {
  426. if er.Error() == utils.ErrNoRow() {
  427. msg = "指标预警不存在,请刷新重试"
  428. err = er
  429. }
  430. msg = "关闭失败"
  431. err = fmt.Errorf("获取预警详情失败, err:%w", er)
  432. return
  433. }
  434. ok, msg, err := OperatePermissionCheck(req.EdbMonitorId, adminId)
  435. if !ok {
  436. if msg == "" {
  437. msg = "删除失败"
  438. }
  439. if err == nil {
  440. err = fmt.Errorf("权限校验失败")
  441. }
  442. return
  443. }
  444. if edbMonitor.State == EDB_MONITOR_STATE_CLOSE {
  445. msg = "预警已关闭"
  446. err = fmt.Errorf("预警已关闭")
  447. return
  448. }
  449. edbMonitor.State = EDB_MONITOR_STATE_CLOSE
  450. edbMonitor.ModifyTime = time.Now()
  451. er = edbMonitor.Update([]string{"state", "modify_time"})
  452. if er != nil {
  453. msg = "关闭失败"
  454. err = fmt.Errorf("修改预警状态失败, err:%w", er)
  455. return
  456. }
  457. return
  458. }
  459. func RestartEdbMonitorInfo(req request.EdbMonitorInfoRestartReq, adminId int) (msg string, err error) {
  460. edbMonitor, er := edbmonitor.GetEdbMonitorInfoById(req.EdbMonitorId)
  461. if er != nil {
  462. if er.Error() == utils.ErrNoRow() {
  463. msg = "指标预警不存在,请刷新重试"
  464. err = er
  465. }
  466. msg = "重启失败"
  467. err = fmt.Errorf("获取预警详情失败, err:%w", er)
  468. return
  469. }
  470. ok, msg, err := OperatePermissionCheck(req.EdbMonitorId, adminId)
  471. if !ok {
  472. if msg == "" {
  473. msg = "删除失败"
  474. }
  475. if err == nil {
  476. err = fmt.Errorf("权限校验失败")
  477. }
  478. return
  479. }
  480. if edbMonitor.State != EDB_MONITOR_STATE_CLOSE {
  481. msg = "预警未关闭,无需重启"
  482. err = fmt.Errorf("预警未关闭,无需重启")
  483. return
  484. }
  485. edbMonitor.EdbTriggerDate = time.Time{}
  486. edbMonitor.MonitorTriggerTime = time.Time{}
  487. edbMonitor.ModifyTime = time.Now()
  488. err = ModifyEdbMonitorStateAndSendMsg(edbMonitor, edbMonitor.EdbCode, edbMonitor.Source, edbMonitor.SubSource)
  489. if err != nil {
  490. msg = "重启失败"
  491. err = fmt.Errorf("修改预警状态失败, err:%w", err)
  492. return
  493. }
  494. return
  495. }
  496. func GetEdbMonitorInfoUserList() (resp response.EdbMonitorInfoCreateUserResp, msg string, err error) {
  497. userIdList, err := edbmonitor.GetEdbMonitorCreateUserId()
  498. if err != nil {
  499. msg = "获取用户列表失败"
  500. return
  501. }
  502. adminList, err := system.GetAdminItemByIdList(userIdList)
  503. if err != nil {
  504. msg = "获取用户列表失败"
  505. return
  506. }
  507. createUserItem := make([]*response.EdbMonitorCreateUserItem, 0)
  508. for _, v := range adminList {
  509. tmp := new(response.EdbMonitorCreateUserItem)
  510. tmp.AdminId = v.AdminId
  511. tmp.RealName = v.RealName
  512. createUserItem = append(createUserItem, tmp)
  513. }
  514. resp.List = createUserItem
  515. return
  516. }
  517. func InitEdbMonitorInfo() {
  518. edbMonitorList, err := edbmonitor.GetEdbMonitorEdbInfoList()
  519. if err != nil {
  520. utils.FileLog.Error("获取预警列表失败, err:%w", err)
  521. }
  522. edbInfoIdList := make([]interface{}, 0)
  523. for _, v := range edbMonitorList {
  524. if v.EdbInfoId != 0 {
  525. EdbLocalSet.Add(v.EdbInfoId)
  526. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  527. }
  528. }
  529. utils.Rc.SAdd(EDB_MONITOR_ID_SET_CACHE, edbInfoIdList...)
  530. }
  531. func HandleEdbMonitorEdbInfo() {
  532. InitEdbMonitorInfo()
  533. for {
  534. utils.Rc.Brpop(EDB_MONITOR_HANDLE_LIST_CACHE, func(b []byte) {
  535. edbInfo := new(EdbInfoBingLog)
  536. if err := json.Unmarshal(b, &edbInfo); err != nil {
  537. return
  538. }
  539. edbMonitorList, err := edbmonitor.GetEdbMonitorInfoByEdbInfoId(edbInfo.EdbInfoId)
  540. if err != nil {
  541. utils.FileLog.Error("获取预警列表失败, err:%w, edbInfoId:%d", err, edbInfo.EdbInfoId)
  542. return
  543. }
  544. if len(edbMonitorList) == 0 {
  545. utils.Rc.SRem(EDB_MONITOR_ID_SET_CACHE, edbInfo.EdbInfoId)
  546. EdbLocalSet.Remove(edbInfo.EdbInfoId)
  547. return
  548. }
  549. for _, v := range edbMonitorList {
  550. twoData, err := GetTwoEdbDataByDataTime(v.EdbCode, edbInfo.EndDate, v.Source, v.SubSource)
  551. if err != nil {
  552. utils.FileLog.Error("获取数据失败, err:%w, edbInfoId:%d", err, edbInfo.EdbInfoId)
  553. continue
  554. }
  555. state, err := ModifyEdbMonitorState(v, twoData)
  556. if err != nil {
  557. utils.FileLog.Error("更新预警状态失败, err:%w, edbMonitorId:%d", err, v.EdbMonitorId)
  558. continue
  559. }
  560. if state == EDB_MONITOR_STATE_TRIGGER_SUCCESS {
  561. err = SendAndLogMessage(v)
  562. if err != nil {
  563. utils.FileLog.Error("发送预警消息失败, err:%w, edbMonitorId:%d", err, v.EdbMonitorId)
  564. continue
  565. }
  566. }
  567. }
  568. })
  569. }
  570. }
  571. func toEdbMonitorInfoItems(edbmonitor []*edbmonitor.EdbMonitorInfo, userMap, classifyPathMap, infoMap map[int]string) []*response.EdbMonitorInfoItem {
  572. res := make([]*response.EdbMonitorInfoItem, 0, len(edbmonitor))
  573. for _, v := range edbmonitor {
  574. tmp := new(response.EdbMonitorInfoItem)
  575. tmp.EdbMonitorId = v.EdbMonitorId
  576. tmp.EdbMonitorName = v.EdbMonitorName
  577. tmp.EdbMonitorClassifyId = v.EdbMonitorClassifyId
  578. tmp.EdbMonitorClassifyName = classifyPathMap[v.EdbMonitorClassifyId]
  579. tmp.EdbInfoId = v.EdbInfoId
  580. tmp.EdbInfoName = infoMap[v.EdbInfoId]
  581. tmp.EdbUniqueCode = v.EdbUniqueCode
  582. tmp.EdbClassifyId = v.EdbClassifyId
  583. tmp.EdbInfoType = v.EdbInfoType
  584. tmp.EdbCode = v.EdbCode
  585. tmp.Source = v.Source
  586. tmp.SubSource = v.SubSource
  587. tmp.EdbLatestDate = v.EdbLatestDate
  588. tmp.EdbLatestValue = v.EdbLatestValue
  589. tmp.MonitorType = v.MonitorType
  590. tmp.MonitorData = v.MonitorData
  591. tmp.MonitorLevel = v.MonitorLevel
  592. tmp.State = v.State
  593. tmp.EdbTriggerDate = utils.TimeTransferString(utils.FormatDate, v.EdbTriggerDate)
  594. tmp.MonitorTriggerTime = utils.TimeTransferString(utils.FormatDateTime, v.MonitorTriggerTime)
  595. tmp.CreateUserId = v.CreateUserId
  596. tmp.CreateUserName = userMap[v.CreateUserId]
  597. tmp.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  598. tmp.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)
  599. res = append(res, tmp)
  600. }
  601. return res
  602. }