notification.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // Base64Config is a base64-encoded column that stores notification config in protobuf format
  15. Base64Config string `json:"base64_config" gorm:"default:''"`
  16. }
  17. func (conf *NotificationConfig) ToNotificationConfigType() *types.NotificationConfig {
  18. return &types.NotificationConfig{
  19. Enabled: conf.Enabled,
  20. Success: conf.Success,
  21. Failure: conf.Failure,
  22. NotifLimit: conf.NotifLimit,
  23. }
  24. }
  25. func (conf *NotificationConfig) ShouldNotify() bool {
  26. // check the last notified time against the notification limit
  27. return conf.LastNotifiedTime.Before(notifLimitToTime(conf.NotifLimit))
  28. }
  29. func notifLimitToTime(notifTime string) time.Time {
  30. // TODO: compute a time that's not just 5 min
  31. return time.Now().Add(-10 * time.Minute)
  32. }
  33. type JobNotificationConfig struct {
  34. gorm.Model
  35. Name string
  36. Namespace string
  37. ProjectID uint
  38. ClusterID uint
  39. LastNotifiedTime time.Time
  40. }
  41. func (conf *JobNotificationConfig) ShouldNotify() bool {
  42. // check the last notified time against the notification limit
  43. return conf.LastNotifiedTime.Before(time.Now().Add(-24 * time.Hour))
  44. }