notification.go 859 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package models
  2. import (
  3. "time"
  4. "github.com/porter-dev/porter/api/types"
  5. "gorm.io/gorm"
  6. )
  7. type NotificationConfig struct {
  8. gorm.Model
  9. Enabled bool // if notifications are enabled at all
  10. Success bool
  11. Failure bool
  12. LastNotifiedTime time.Time
  13. NotifLimit string
  14. }
  15. func (conf *NotificationConfig) ToNotificationConfigType() *types.NotificationConfig {
  16. return &types.NotificationConfig{
  17. Enabled: conf.Enabled,
  18. Success: conf.Success,
  19. Failure: conf.Failure,
  20. NotifLimit: conf.NotifLimit,
  21. }
  22. }
  23. func (conf *NotificationConfig) ShouldNotify() bool {
  24. // check the last notified time against the notification limit
  25. return conf.LastNotifiedTime.Before(notifLimitToTime(conf.NotifLimit))
  26. }
  27. func notifLimitToTime(notifTime string) time.Time {
  28. // TODO: compute a time that's not just 5 min
  29. return time.Now().Add(-10 * time.Minute)
  30. }