Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no ios sheduling priorities #303

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions server/backend/cron/ios_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
23 changes: 0 additions & 23 deletions server/backend/ios_notifications/scheduling/repository.go

This file was deleted.

54 changes: 14 additions & 40 deletions server/backend/ios_notifications/scheduling/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package scheduling

import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -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")
Expand All @@ -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))

Expand Down Expand Up @@ -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,
}
}
63 changes: 63 additions & 0 deletions server/backend/migration/20240101000000.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
3 changes: 2 additions & 1 deletion server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down Expand Up @@ -80,7 +79,9 @@ func manualMigrate(db *gorm.DB) error {
migrate20230826000000(),
migrate20231003000000(),
migrate20231023000000(),
migrate20240101000000(),
migrate20240102000000(),
migrate20240103000000(),
}
return gormigrate.New(db, gormigrateOptions, migrations).Migrate()
}
Expand Down
2 changes: 0 additions & 2 deletions server/backend/new_exam_results_hook/scheduling/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
type Service struct {
Repository *Repository
DevicesRepository *device.Repository
Priority *model.IOSSchedulingPriority
APNs *apns.Service
}

Expand Down Expand Up @@ -87,7 +86,6 @@ func NewService(repository *Repository,
return &Service{
Repository: repository,
DevicesRepository: devicesRepository,
Priority: model.DefaultIOSSchedulingPriority(),
APNs: apnsService,
}
}
67 changes: 0 additions & 67 deletions server/model/ios_scheduling_priority.go

This file was deleted.

Loading