monitor.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package mysql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/admin/main/aegis/model/monitor"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _moniBizRulesSql = "SELECT id,type,bid,name,state,stime,etime,rule,uid,ctime,mtime FROM monitor_rule WHERE bid = ?"
  11. _moniRuleSql = "SELECT id,type,bid,name,state,stime,etime,rule,uid,ctime,mtime FROM monitor_rule WHERE id = ?"
  12. )
  13. // MoniBizRules 获取监控业务的所有配置
  14. func (d *Dao) MoniBizRules(c context.Context, bid int64) (rules []*monitor.Rule, err error) {
  15. var (
  16. rows *xsql.Rows
  17. )
  18. if rows, err = d.db.Query(c, _moniBizRulesSql, bid); err != nil {
  19. log.Error("db.Query() error(%v)", err)
  20. return
  21. }
  22. defer rows.Close()
  23. for rows.Next() {
  24. rule := &monitor.Rule{}
  25. var confStr string
  26. if err = rows.Scan(&rule.ID, &rule.Type, &rule.BID, &rule.Name, &rule.State, &rule.STime, &rule.ETime, &confStr, &rule.UID, &rule.CTime, &rule.MTime); err != nil {
  27. log.Error("rows.Scan error(%v)", err)
  28. return
  29. }
  30. conf := &monitor.RuleConf{}
  31. if err = json.Unmarshal([]byte(confStr), conf); err != nil {
  32. log.Error("json.Unmarshal(%s) error(%v)", confStr, err)
  33. return
  34. }
  35. rule.RuleConf = conf
  36. rules = append(rules, rule)
  37. }
  38. return
  39. }
  40. // MoniRule 根据id获取监控规则
  41. func (d *Dao) MoniRule(c context.Context, rid int64) (rule *monitor.Rule, err error) {
  42. rule = &monitor.Rule{}
  43. var confStr string
  44. row := d.db.QueryRow(c, _moniRuleSql, rid)
  45. if err = row.Scan(&rule.ID, &rule.Type, &rule.BID, &rule.Name, &rule.State, &rule.STime, &rule.ETime, &confStr, &rule.UID, &rule.CTime, &rule.MTime); err != nil {
  46. rule = nil
  47. log.Error("row.Scan error(%v)", err)
  48. return
  49. }
  50. conf := &monitor.RuleConf{}
  51. if err = json.Unmarshal([]byte(confStr), conf); err != nil {
  52. log.Error("json.Unmarshal(%s) error(%v)", confStr, err)
  53. return
  54. }
  55. rule.RuleConf = conf
  56. return
  57. }