base.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package notifiers
  2. import (
  3. "github.com/grafana/grafana/pkg/components/simplejson"
  4. m "github.com/grafana/grafana/pkg/models"
  5. "github.com/grafana/grafana/pkg/services/alerting"
  6. )
  7. type NotifierBase struct {
  8. Name string
  9. Type string
  10. Id int64
  11. IsDeault bool
  12. UploadImage bool
  13. }
  14. func NewNotifierBase(id int64, isDefault bool, name, notifierType string, model *simplejson.Json) NotifierBase {
  15. uploadImage := true
  16. value, exist := model.CheckGet("uploadImage")
  17. if exist {
  18. uploadImage = value.MustBool()
  19. }
  20. return NotifierBase{
  21. Id: id,
  22. Name: name,
  23. IsDeault: isDefault,
  24. Type: notifierType,
  25. UploadImage: uploadImage,
  26. }
  27. }
  28. func defaultShouldNotify(context *alerting.EvalContext) bool {
  29. // Only notify on state change.
  30. if context.PrevAlertState == context.Rule.State {
  31. return false
  32. }
  33. // Do not notify when we become OK for the first time.
  34. if (context.PrevAlertState == m.AlertStatePending) && (context.Rule.State == m.AlertStateOK) {
  35. return false
  36. }
  37. return true
  38. }
  39. func (n *NotifierBase) ShouldNotify(context *alerting.EvalContext) bool {
  40. return defaultShouldNotify(context)
  41. }
  42. func (n *NotifierBase) GetType() string {
  43. return n.Type
  44. }
  45. func (n *NotifierBase) NeedsImage() bool {
  46. return n.UploadImage
  47. }
  48. func (n *NotifierBase) GetNotifierId() int64 {
  49. return n.Id
  50. }
  51. func (n *NotifierBase) GetIsDefault() bool {
  52. return n.IsDeault
  53. }