From 406049d86c9e11e2fd0d5ebba20b4e6eef418d90 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Tue, 2 Jan 2024 21:52:53 +0100 Subject: [PATCH] removed the scheduling configuration from the db and moved it to commented code --- server/backend/cron/ios_notifications.go | 3 +- .../scheduling/repository.go | 23 ------- .../ios_notifications/scheduling/service.go | 54 ++++----------- server/backend/migration/20240101000000.go | 63 +++++++++++++++++ server/backend/migration/migration.go | 3 +- .../scheduling/service.go | 2 - server/model/ios_scheduling_priority.go | 67 ------------------- 7 files changed, 80 insertions(+), 135 deletions(-) delete mode 100644 server/backend/ios_notifications/scheduling/repository.go create mode 100644 server/backend/migration/20240101000000.go delete mode 100644 server/model/ios_scheduling_priority.go diff --git a/server/backend/cron/ios_notifications.go b/server/backend/cron/ios_notifications.go index 99c51161..fa39a0e4 100644 --- a/server/backend/cron/ios_notifications.go +++ b/server/backend/cron/ios_notifications.go @@ -14,11 +14,10 @@ func (c *CronService) iosNotificationsCron() error { return nil } - repo := scheduling.NewRepository(c.db) devicesRepo := device.NewRepository(c.db) schedulerRepo := scheduled_update_log.NewRepository(c.db) - service := scheduling.NewService(repo, devicesRepo, schedulerRepo, c.APNs) + service := scheduling.NewService(devicesRepo, schedulerRepo, c.APNs) return service.HandleScheduledCron() } diff --git a/server/backend/ios_notifications/scheduling/repository.go b/server/backend/ios_notifications/scheduling/repository.go deleted file mode 100644 index fb78f528..00000000 --- a/server/backend/ios_notifications/scheduling/repository.go +++ /dev/null @@ -1,23 +0,0 @@ -package scheduling - -import ( - "github.com/TUM-Dev/Campus-Backend/server/model" - "gorm.io/gorm" -) - -type Repository struct { - DB *gorm.DB -} - -func (repository *Repository) FindSchedulingPriorities() ([]model.IOSSchedulingPriority, error) { - var priorities []model.IOSSchedulingPriority - err := repository.DB.Find(&priorities).Error - - return priorities, err -} - -func NewRepository(db *gorm.DB) *Repository { - return &Repository{ - DB: db, - } -} diff --git a/server/backend/ios_notifications/scheduling/service.go b/server/backend/ios_notifications/scheduling/service.go index bec9256b..e3c40cb2 100644 --- a/server/backend/ios_notifications/scheduling/service.go +++ b/server/backend/ios_notifications/scheduling/service.go @@ -4,6 +4,7 @@ package scheduling import ( "sync" + "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" @@ -21,21 +22,12 @@ var devicesToUpdate = promauto.NewGauge(prometheus.GaugeOpts{ }) type Service struct { - Repository *Repository DevicesRepository *device.Repository SchedulerLogRepository *scheduled_update_log.Repository - Priority *model.IOSSchedulingPriority APNs *apns.Service } func (service *Service) HandleScheduledCron() error { - priorities, err := service.Repository.FindSchedulingPriorities() - if err != nil { - return err - } - - currentPriority := findIOSSchedulingPriorityForNow(priorities) - devices, err := service.DevicesRepository.GetDevicesThatShouldUpdateGrades() if err != nil { log.WithError(err).Error("can't get devices") @@ -49,7 +41,8 @@ func (service *Service) HandleScheduledCron() error { return nil } - devices = service.selectDevicesToUpdate(devices, currentPriority.Priority) + priority := findSchedulingPriority() + devices = service.selectDevicesToUpdate(devices, priority) log.Infof("Updating %d devices", len(devices)) @@ -122,50 +115,31 @@ func (service *Service) selectDevicesToUpdate(devices []model.IOSDeviceLastUpdat return devices[:maxDevicesToCheck] } +func findSchedulingPriority() int { + now := time.Now() -func findIOSSchedulingPriorityForNow(priorities []model.IOSSchedulingPriority) *model.IOSSchedulingPriority { - var prioritiesThatAreInRange []model.IOSSchedulingPriority - - for _, priority := range priorities { - if priority.IsCurrentlyInRange() { - prioritiesThatAreInRange = append(prioritiesThatAreInRange, priority) - } + isNight := 1 <= now.Hour() && now.Hour() <= 6 + if isNight { + return 1 } - if len(prioritiesThatAreInRange) == 0 { - return model.DefaultIOSSchedulingPriority() + isDuringSummerSemester := 32 <= now.YearDay() && now.YearDay() <= 106 + isDuringWinterSemester := 152 <= now.YearDay() && now.YearDay() <= 288 + if isDuringWinterSemester || isDuringSummerSemester { + return 10 } - return mergeIOSSchedulingPriorities(prioritiesThatAreInRange) -} - -func mergeIOSSchedulingPriorities(priorities []model.IOSSchedulingPriority) *model.IOSSchedulingPriority { - mergedPriority := model.DefaultIOSSchedulingPriority() - prioritiesSum := 0 - - for _, priority := range priorities { - if priority.IsMorePreciseThan(mergedPriority) { - mergedPriority = &priority - } - - prioritiesSum += priority.Priority - } - - mergedPriority.Priority = prioritiesSum / len(priorities) - - return mergedPriority + return 5 } -func NewService(repository *Repository, +func NewService( devicesRepository *device.Repository, schedulerRepository *scheduled_update_log.Repository, apnsService *apns.Service, ) *Service { return &Service{ - Repository: repository, DevicesRepository: devicesRepository, SchedulerLogRepository: schedulerRepository, - Priority: model.DefaultIOSSchedulingPriority(), APNs: apnsService, } } diff --git a/server/backend/migration/20240101000000.go b/server/backend/migration/20240101000000.go new file mode 100644 index 00000000..03bc744a --- /dev/null +++ b/server/backend/migration/20240101000000.go @@ -0,0 +1,63 @@ +package migration + +import ( + "github.com/go-gormigrate/gormigrate/v2" + "gorm.io/gorm" +) + +// IOSSchedulingPriority stores some default priorities for the scheduling of grade updates. +type IOSSchedulingPriority struct { + ID int64 `gorm:"primary_key;auto_increment;not_null" json:"id"` + FromDay int `gorm:"not null" json:"from_day"` + ToDay int `gorm:"not null" json:"to_day"` + FromHour int `gorm:"not null" json:"from_hour"` + ToHour int `gorm:"not null" json:"to_hour"` + Priority int `gorm:"not null" json:"priority"` +} + +// migrate20240101000000 +// inlined the scheduling priorities to not be configured in the DB +func migrate20240101000000() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "20240101000000", + Migrate: func(tx *gorm.DB) error { + return tx.Exec("DROP table ios_scheduling_priorities").Error + }, + Rollback: func(tx *gorm.DB) error { + if err := tx.Migrator().AutoMigrate(&IOSSchedulingPriority{}); err != nil { + return err + } + if err := tx.Create(&IOSSchedulingPriority{ + ID: 1, + FromDay: 152, + ToDay: 288, + FromHour: 0, + ToHour: 23, + Priority: 10, + }).Error; err != nil { + return err + } + if err := tx.Create(&IOSSchedulingPriority{ + ID: 2, + FromDay: 32, + ToDay: 106, + FromHour: 0, + ToHour: 23, + Priority: 10, + }).Error; err != nil { + return err + } + if err := tx.Create(&IOSSchedulingPriority{ + ID: 3, + FromDay: 1, + ToDay: 365, + FromHour: 1, + ToHour: 6, + Priority: 5, + }).Error; err != nil { + return err + } + return nil + }, + } +} diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index c870ca87..dc1e68f2 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -44,7 +44,6 @@ func autoMigrate(db *gorm.DB) error { //&model.IOSGrade{}, -- not a gorm model //&model.IOSRemoteNotification...{}, -- wtf??? &model.IOSScheduledUpdateLog{}, - &model.IOSSchedulingPriority{}, &model.Kino{}, &model.NewExamResultsSubscriber{}, &model.News{}, @@ -80,7 +79,9 @@ func manualMigrate(db *gorm.DB) error { migrate20230826000000(), migrate20231003000000(), migrate20231023000000(), + migrate20240101000000(), migrate20240102000000(), + migrate20240103000000(), } return gormigrate.New(db, gormigrateOptions, migrations).Migrate() } diff --git a/server/backend/new_exam_results_hook/scheduling/service.go b/server/backend/new_exam_results_hook/scheduling/service.go index 917f3dad..4720ee71 100644 --- a/server/backend/new_exam_results_hook/scheduling/service.go +++ b/server/backend/new_exam_results_hook/scheduling/service.go @@ -12,7 +12,6 @@ import ( type Service struct { Repository *Repository DevicesRepository *device.Repository - Priority *model.IOSSchedulingPriority APNs *apns.Service } @@ -87,7 +86,6 @@ func NewService(repository *Repository, return &Service{ Repository: repository, DevicesRepository: devicesRepository, - Priority: model.DefaultIOSSchedulingPriority(), APNs: apnsService, } } diff --git a/server/model/ios_scheduling_priority.go b/server/model/ios_scheduling_priority.go deleted file mode 100644 index 39bd9ab5..00000000 --- a/server/model/ios_scheduling_priority.go +++ /dev/null @@ -1,67 +0,0 @@ -package model - -import ( - "fmt" - "time" -) - -// IOSSchedulingPriority stores some default priorities for the scheduling of -// grade updates. -type IOSSchedulingPriority struct { - ID int64 `gorm:"primary_key;auto_increment;not_null" json:"id"` - FromDay int `gorm:"not null" json:"from_day"` - ToDay int `gorm:"not null" json:"to_day"` - FromHour int `gorm:"not null" json:"from_hour"` - ToHour int `gorm:"not null" json:"to_hour"` - Priority int `gorm:"not null" json:"priority"` -} - -// IsCurrentlyInRange returns true if the current time is in the range of the -// scheduling priority. -func (p *IOSSchedulingPriority) IsCurrentlyInRange() bool { - now := time.Now() - yearDay := now.YearDay() - - if p.FromDay <= yearDay && p.ToDay >= yearDay { - hour := now.Hour() - - if p.FromHour <= hour && p.ToHour >= hour { - return true - } - } - - return false -} - -// IsMorePreciseThan compares two Priorities and returns true if the current -// priority is more precise than the other one. -// -// Example: -// A priority with FromDay=1, ToDay=365, FromHour=6, ToHour=8 is more precise -// than a priority with FromDay=1, ToDay=365, FromHour=0, ToHour=23. -// In case the current hour is between 6 and 8. -func (p *IOSSchedulingPriority) IsMorePreciseThan(other *IOSSchedulingPriority) bool { - if p.FromDay == other.FromDay && p.ToDay == other.ToDay { - if p.FromHour == other.FromHour && p.ToHour == other.ToHour { - return p.Priority > other.Priority - } - - return p.FromHour > other.FromHour && p.ToHour < other.ToHour - } - - return p.FromDay > other.FromDay && p.ToDay < other.ToDay -} - -func (p *IOSSchedulingPriority) String() string { - return fmt.Sprintf("Day: %d-%d, Hour: %d-%d, Priority: %d", p.FromDay, p.ToDay, p.FromHour, p.ToHour, p.Priority) -} - -func DefaultIOSSchedulingPriority() *IOSSchedulingPriority { - return &IOSSchedulingPriority{ - FromDay: 1, - ToDay: 365, - FromHour: 0, - ToHour: 23, - Priority: 5, - } -}