notification.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package gorm
  2. import (
  3. "github.com/porter-dev/porter/internal/models"
  4. "github.com/porter-dev/porter/internal/repository"
  5. "gorm.io/gorm"
  6. )
  7. type NotificationConfigRepository struct {
  8. db *gorm.DB
  9. }
  10. // NewNotificationConfigRepository creates a new NotificationConfigRepository
  11. func NewNotificationConfigRepository(db *gorm.DB) repository.NotificationConfigRepository {
  12. return NotificationConfigRepository{db: db}
  13. }
  14. // CreateNotificationConfig creates a new NotificationConfig
  15. func (repo NotificationConfigRepository) CreateNotificationConfig(am *models.NotificationConfig) (*models.NotificationConfig, error) {
  16. if err := repo.db.Create(am).Error; err != nil {
  17. return nil, err
  18. }
  19. return am, nil
  20. }
  21. // ReadNotificationConfig reads a NotificationConfig by ID
  22. func (repo NotificationConfigRepository) ReadNotificationConfig(id uint) (*models.NotificationConfig, error) {
  23. ret := &models.NotificationConfig{}
  24. if err := repo.db.Where("id = ?", id).First(&ret).Error; err != nil {
  25. return nil, err
  26. }
  27. return ret, nil
  28. }
  29. // UpdateNotificationConfig updates a given NotificationConfig
  30. func (repo NotificationConfigRepository) UpdateNotificationConfig(am *models.NotificationConfig) (*models.NotificationConfig, error) {
  31. if err := repo.db.Save(am).Error; err != nil {
  32. return nil, err
  33. }
  34. return am, nil
  35. }
  36. type JobNotificationConfigRepository struct {
  37. db *gorm.DB
  38. }
  39. // NewJobNotificationConfigRepository creates a new JobNotificationConfigRepository
  40. func NewJobNotificationConfigRepository(db *gorm.DB) repository.JobNotificationConfigRepository {
  41. return JobNotificationConfigRepository{db: db}
  42. }
  43. // CreateNotificationConfig creates a new JobNotificationConfig
  44. func (repo JobNotificationConfigRepository) CreateNotificationConfig(am *models.JobNotificationConfig) (*models.JobNotificationConfig, error) {
  45. var count int64
  46. query := repo.db.Where("project_id = ? AND cluster_id = ?", am.ProjectID, am.ClusterID)
  47. if err := query.Model([]*models.JobNotificationConfig{}).Count(&count).Error; err != nil {
  48. return nil, err
  49. }
  50. // if the count is greater than 1000, remove the lowest-order events to implement a
  51. // basic fixed-length buffer
  52. if count >= 1000 {
  53. err := repo.db.Exec(`
  54. DELETE FROM job_notification_configs
  55. WHERE project_id = ? AND cluster_id = ? AND
  56. id NOT IN (
  57. SELECT id FROM job_notification_configs j2 WHERE j2.project_id = ? AND j2.cluster_id = ? ORDER BY j2.updated_at desc, j2.id desc LIMIT 999
  58. )
  59. `, am.ProjectID, am.ClusterID, am.ProjectID, am.ClusterID).Error
  60. if err != nil {
  61. return nil, err
  62. }
  63. }
  64. if err := repo.db.Create(am).Error; err != nil {
  65. return nil, err
  66. }
  67. return am, nil
  68. }
  69. // ReadNotificationConfig reads a JobNotificationConfig by ID
  70. func (repo JobNotificationConfigRepository) ReadNotificationConfig(projID, clusterID uint, name, namespace string) (*models.JobNotificationConfig, error) {
  71. ret := &models.JobNotificationConfig{}
  72. if err := repo.db.Where("project_id = ? AND cluster_id = ? AND name = ? AND namespace = ?", projID, clusterID, name, namespace).First(&ret).Error; err != nil {
  73. return nil, err
  74. }
  75. return ret, nil
  76. }
  77. // UpdateNotificationConfig updates a given JobNotificationConfig
  78. func (repo JobNotificationConfigRepository) UpdateNotificationConfig(am *models.JobNotificationConfig) (*models.JobNotificationConfig, error) {
  79. if err := repo.db.Save(am).Error; err != nil {
  80. return nil, err
  81. }
  82. return am, nil
  83. }