base.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 := model.Get("uploadImage").MustBool(false)
  16. return NotifierBase{
  17. Id: id,
  18. Name: name,
  19. IsDeault: isDefault,
  20. Type: notifierType,
  21. UploadImage: uploadImage,
  22. }
  23. }
  24. func defaultShouldNotify(context *alerting.EvalContext) bool {
  25. // Only notify on state change.
  26. if context.PrevAlertState == context.Rule.State {
  27. return false
  28. }
  29. // Do not notify when we become OK for the first time.
  30. if (context.PrevAlertState == m.AlertStatePending) && (context.Rule.State == m.AlertStateOK) {
  31. return false
  32. }
  33. return true
  34. }
  35. func (n *NotifierBase) ShouldNotify(context *alerting.EvalContext) bool {
  36. return defaultShouldNotify(context)
  37. }
  38. func (n *NotifierBase) GetType() string {
  39. return n.Type
  40. }
  41. func (n *NotifierBase) NeedsImage() bool {
  42. return n.UploadImage
  43. }
  44. func (n *NotifierBase) GetNotifierId() int64 {
  45. return n.Id
  46. }
  47. func (n *NotifierBase) GetIsDefault() bool {
  48. return n.IsDeault
  49. }