notification.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }
  31. type JobNotificationConfig struct {
  32. gorm.Model
  33. Name string
  34. Namespace string
  35. ProjectID uint
  36. ClusterID uint
  37. LastNotifiedTime time.Time
  38. }
  39. func (conf *JobNotificationConfig) ShouldNotify() bool {
  40. // check the last notified time against the notification limit
  41. return conf.LastNotifiedTime.Before(time.Now().Add(-24 * time.Hour))
  42. }