Skip to content

Commit

Permalink
removed the AverageRatingComputation cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 15, 2023
1 parent f888dd7 commit 1527f10
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
31 changes: 11 additions & 20 deletions server/backend/cron/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ var StorageDir = "/Storage/" // target location of files

// names for cron jobs as specified in database
const (
NewsType = "news"
FileDownloadType = "fileDownload"
DishNameDownload = "dishNameDownload"
AverageRatingComputation = "averageRatingComputation"
CanteenHeadcount = "canteenHeadCount"
IOSNotifications = "iosNotifications"
IOSActivityReset = "iosActivityReset"
NewExamResultsHook = "newExamResultsHook"
MovieType = "movie"
FeedbackEmail = "feedbackEmail"
NewsType = "news"
FileDownloadType = "fileDownload"
DishNameDownload = "dishNameDownload"
CanteenHeadcount = "canteenHeadCount"
IOSNotifications = "iosNotifications"
IOSActivityReset = "iosActivityReset"
NewExamResultsHook = "newExamResultsHook"
MovieType = "movie"
FeedbackEmail = "feedbackEmail"

/* MensaType = "mensa"
AlarmType = "alarm" */
Expand Down Expand Up @@ -72,18 +71,10 @@ func (c *CronService) Run() error {

for _, cronjob := range res {
// Persist run to DB right away
var offset int32 = 0
if env.IsMensaCronActive() {
if cronjob.Type.String == AverageRatingComputation {
if time.Now().Hour() == 16 {
offset = 18 * 3600 // fast-forward 18 Hours to the next day + does not need to be computed overnight
}
}
}
cronFields := log.Fields{"Cron (id)": cronjob.Cron, "type": cronjob.Type.String, "offset": offset, "LastRun": cronjob.LastRun, "interval": cronjob.Interval, "id (not real id)": cronjob.ID.Int64}
cronFields := log.Fields{"Cron (id)": cronjob.Cron, "type": cronjob.Type.String, "LastRun": cronjob.LastRun, "interval": cronjob.Interval, "id (not real id)": cronjob.ID.Int64}
log.WithFields(cronFields).Trace("Running cronjob")

cronjob.LastRun = int32(time.Now().Unix()) + offset
cronjob.LastRun = int32(time.Now().Unix())
c.db.Save(&cronjob)

// Run each job in a separate goroutine, so we can parallelize them
Expand Down
32 changes: 26 additions & 6 deletions server/backend/migration/20231015000000.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package migration

import (
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -91,12 +93,21 @@ func (m TumDBMigrator) migrate20231015000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20231015000000",
Migrate: func(tx *gorm.DB) error {
// cronjob
if err := tx.Delete(&model.Crontab{}, "type = 'averageRatingComputation'").Error; err != nil {
return err
}
if err := SafeEnumAdd(tx, &model.Crontab{}, "type", "averageRatingComputation"); err != nil {
return err
}
// tables
tables := []string{"cafeteria_rating_average", "dish_rating_average", "dish_rating_tag_average", "cafeteria_rating_tag_average", "dish_name_tag_average"}
for _, table := range tables {
if err := tx.Migrator().DropTable(table); err != nil {
return err
}
}
// views
if err := tx.Exec(`CREATE VIEW cafeteria_rating_statistics AS
SELECT cafeteriaID, Avg(points) AS average, MIN(points) AS min, Max(points) AS max, STD(points) AS std
FROM cafeteria_rating
Expand Down Expand Up @@ -125,23 +136,32 @@ JOIN cafeteria_rating_tag crt ON cr.cafeteriaRating = crt.correspondingRating
GROUP BY cr.cafeteriaID, crt.tagID`).Error; err != nil {
return err
}
if err := tx.Exec(`CREATE VIEW dish_name_tag_statistics AS
return tx.Exec(`CREATE VIEW dish_name_tag_statistics AS
SELECT mr.cafeteriaID as cafeteriaID, mnt.tagnameID as tagID, AVG(mnt.points) as average, MAX(mnt.points) as max, MIN(mnt.points) as min, STD(mnt.points) as std
FROM dish_rating mr
JOIN dish_name_tag mnt ON mr.dishRating = mnt.correspondingRating
GROUP BY mr.cafeteriaID, mnt.tagnameID`).Error; err != nil {
return err
}
return nil
GROUP BY mr.cafeteriaID, mnt.tagnameID`).Error
},
Rollback: func(tx *gorm.DB) error {
// views
createdViews := []string{"cafeteria_rating_statistics", "dish_rating_statistics", "dish_rating_tag_statistics", "cafeteria_rating_tag_statistics", "dish_name_tag_statistics"}
for _, view := range createdViews {
if err := tx.Exec("DROP VIEW IF EXISTS " + view).Error; err != nil {
return err
}
}
return tx.AutoMigrate(&CafeteriaRatingAverage{}, &DishRatingAverage{}, &DishRatingTagAverage{}, &CafeteriaRatingTagsAverage{}, &DishNameTagAverage{})
// tables
if err := tx.AutoMigrate(&CafeteriaRatingAverage{}, &DishRatingAverage{}, &DishRatingTagAverage{}, &CafeteriaRatingTagsAverage{}, &DishNameTagAverage{}); err != nil {
return err
}
// cronjob
if err := SafeEnumRemove(tx, &model.Crontab{}, "type", "averageRatingComputation"); err != nil {
return err
}
return tx.Create(&model.Crontab{
Interval: 300,
Type: null.StringFrom("averageRatingComputation"),
}).Error
},
}
}

0 comments on commit 1527f10

Please sign in to comment.