alert.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package model
  2. import (
  3. "fmt"
  4. "sparrow/pkg/models"
  5. "github.com/jinzhu/gorm"
  6. )
  7. // Alert model
  8. type Alert struct {
  9. db *gorm.DB
  10. }
  11. // Init init Alert
  12. func (a *Alert) Init(db *gorm.DB) *Alert {
  13. a.db = db
  14. return a
  15. }
  16. // Create create a alert rule
  17. func (a *Alert) Create(alert *models.AlarmRule) error {
  18. err := a.db.Create(alert).Error
  19. if err == nil {
  20. cache := getCache()
  21. key := fmt.Sprintf("Alert:%d", alert.ID)
  22. cache.Set(key, &alert)
  23. }
  24. return err
  25. }
  26. // Delete delete a alert rule
  27. func (a *Alert) Delete(alert *models.AlarmRule) error {
  28. cache := getCache()
  29. key := fmt.Sprintf("Alert:%d", alert.ID)
  30. if _, ok := cache.Get(key); ok {
  31. cache.Delete(key)
  32. }
  33. return a.db.Delete(alert).Error
  34. }
  35. // Update 指定厂商ID
  36. func (a *Alert) Update(vendorid uint, alert *models.AlarmRule) (data models.AlarmRule, err error) {
  37. cache := getCache()
  38. key := fmt.Sprintf("Alert:%d", alert.ID)
  39. if _, ok := cache.Get(key); ok {
  40. cache.Delete(key)
  41. }
  42. err = a.db.Model(&data).Update(alert).Where("vendor_id = ?", vendorid).Error
  43. if err == nil {
  44. cache.Set(key, &data)
  45. }
  46. return
  47. }
  48. // GetRules 获取告警列表
  49. // vendorid:厂商ID
  50. // proid:产品ID,可选
  51. // procalid:协议ID, 可选
  52. // name: 规则名称,可选,模糊搜索
  53. func (a *Alert) GetRules(vendorid uint, pi, ps, proid, protocalid int, name string) (datas []models.AlarmRule, total int, err error) {
  54. tx := a.db.Where("vendor_id = ? and 1=1")
  55. if proid != 0 {
  56. tx = tx.Where("product_id = ?")
  57. }
  58. if protocalid != 0 {
  59. tx = tx.Where("protocal_id = ?", protocalid)
  60. }
  61. if name != "" {
  62. tx = tx.Where("alert_name = ?", name)
  63. }
  64. err = tx.Limit(ps).Offset((pi - 1) * ps).Find(&datas).Error
  65. err = tx.Model(&models.AlarmRule{}).Count(&total).Error
  66. return
  67. }
  68. // GetAlarmRule 获取数据内容
  69. func (a *Alert) GetAlarmRule(vendorid uint, alertid int) (data models.AlarmRule, err error) {
  70. cache := getCache()
  71. key := fmt.Sprintf("Alert:%d", alertid)
  72. if v, ok := cache.Get(key); ok {
  73. _d := v.(*models.AlarmRule)
  74. data = *_d
  75. } else {
  76. err = a.db.Where("vendor_id = ? and id = ?", vendorid, alertid).First(&data).Error
  77. if err == nil {
  78. cache.Set(key, &data)
  79. }
  80. }
  81. return
  82. }
  83. // CheckProtocalRuleCount 返回数据点已经添加的告警数量
  84. func (a *Alert) CheckProtocalRuleCount(vendorid uint, protocalID int) (total int, err error) {
  85. err = a.db.Model(&models.AlarmRule{}).
  86. Where("vendor_id = ? and protocal_id = ?", vendorid, protocalID).
  87. Count(&total).
  88. Error
  89. return
  90. }