alarm_rule.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package utils
  4. import (
  5. "smart-alarm/model"
  6. "github.com/jaryhe/gopkgs/database"
  7. )
  8. /*
  9. var AlarmRuleMap = map[string]model.TAlarmRule{}
  10. var ruleMutex sync.Mutex
  11. // 初始化告警规则
  12. func InitAlarmRule() {
  13. rule := &model.TAlarmRule{}
  14. where := map[string]interface{}{"is_on": true}
  15. ruleList, err := rule.QueryAll(database.DB(), where)
  16. if err == nil {
  17. for k, _ := range ruleList {
  18. AlarmRuleMap[ruleList[k].Sn] = ruleList[k]
  19. }
  20. }
  21. }
  22. // 获取告警规则
  23. func GetAlarmRule(sn string) *model.TAlarmRule {
  24. ruleMutex.Lock()
  25. defer ruleMutex.Unlock()
  26. if value, ok := AlarmRuleMap[sn]; ok {
  27. return &value
  28. } else {
  29. return nil
  30. }
  31. }
  32. // 设置告警规则
  33. func SetAlarmRule(rule *model.TAlarmRule) {
  34. ruleMutex.Lock()
  35. defer ruleMutex.Unlock()
  36. AlarmRuleMap[rule.Sn] = *rule
  37. }
  38. func DeleteAlarmRule(sn string) {
  39. ruleMutex.Lock()
  40. defer ruleMutex.Unlock()
  41. delete(AlarmRuleMap, sn)
  42. }
  43. */
  44. // 获取告警规则
  45. func GetAlarmRule(projectId int64, sn string) *model.TAlarmRule {
  46. // TODO 从redis里面获取
  47. rule := &model.TAlarmRule{}
  48. filter := map[string]interface{}{"project_id": projectId, "sn": sn, "is_on": true}
  49. err := rule.Query(database.DB(), filter)
  50. if err != nil {
  51. return nil
  52. }
  53. return rule
  54. }