Skip to content

Commit

Permalink
migrated dish_name_tag_average
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 15, 2023
1 parent 9fe80ea commit f888dd7
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 71 deletions.
2 changes: 1 addition & 1 deletion server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func queryTags(cafeteriaID int32, dishID int32, ratingType ModelType, tx *gorm.D
err = tx.Table("dish_to_dish_name_tag mapping").
Where("mapping.dishID = ?", dishID).
Select("mapping.nameTagID as tag").
Joins("JOIN dish_name_tag_average results ON mapping.nameTagID = results.tagID").
Joins("JOIN dish_name_tag_statistic results ON mapping.nameTagID = results.tagID").
Joins("JOIN dish_name_tag_option options ON mapping.nameTagID = options.dishNameTagOption").
Select("mapping.nameTagID as tagId, results.average as avg, " +
"results.min as min, results.max as max, results.std as std").
Expand Down
36 changes: 0 additions & 36 deletions server/backend/cron/average_rating_computation.go

This file was deleted.

15 changes: 2 additions & 13 deletions server/backend/cron/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,16 @@ func New(db *gorm.DB) *CronService {

func (c *CronService) Run() error {
log.WithField("MensaCronActive", env.IsMensaCronActive()).Debug("running cron service")
g := new(errgroup.Group)

if env.IsMensaCronActive() {
g.Go(func() error { return c.dishNameDownloadCron() })
g.Go(func() error { return c.averageRatingComputation() })
}

for {
g := new(errgroup.Group)
log.Trace("Cron: checking for pending")
var res []model.Crontab

c.db.Model(&model.Crontab{}).
Where("`interval` > 0 AND (lastRun+`interval`) < ? AND type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
Where("`interval` > 0 AND (lastRun+`interval`) < ? AND type IN (?, ?, ?, ?, ?, ?, ?, ?, ?)",
time.Now().Unix(),
NewsType,
FileDownloadType,
AverageRatingComputation,
DishNameDownload,
CanteenHeadcount,
IOSNotifications,
Expand Down Expand Up @@ -106,10 +99,6 @@ func (c *CronService) Run() error {
if env.IsMensaCronActive() {
g.Go(c.dishNameDownloadCron)
}
case AverageRatingComputation: //call every five minutes between 11AM and 4 PM on weekdays
if env.IsMensaCronActive() {
g.Go(c.averageRatingComputation)
}
case NewExamResultsHook:
g.Go(func() error { return c.newExamResultsHookCron() })
case MovieType:
Expand Down
18 changes: 17 additions & 1 deletion server/backend/migration/20220713000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ func (n *InitialCafeteriaRatingTagsAverage) TableName() string {
return "cafeteria_rating_tag_average"
}

// InitialDishNameTagAverage stores all precomputed values for the DishName ratings
type InitialDishNameTagAverage struct {
DishNameTagAverage int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishNameTagAverage;type:int;not null;" json:"dishNameTagAverage" `
CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"`
TagID int64 `gorm:"column:tagID;foreignKey:DishNameTagOption;type:int;not null;" json:"tagID"`
Average float32 `gorm:"column:average;type:float;not null;" json:"average" `
Min int8 `gorm:"column:min;type:int;not null;" json:"min"`
Max int8 `gorm:"column:max;type:int;not null;" json:"max"`
Std float32 `gorm:"column:std;type:float;not null;" json:"std"`
}

// TableName sets the insert table name for this struct type
func (n *InitialDishNameTagAverage) TableName() string {
return "dish_name_tag_average"
}

// migrate20220713000000
func (m TumDBMigrator) migrate20220713000000() *gormigrate.Migration {
return &gormigrate.Migration{
Expand All @@ -86,11 +102,11 @@ func (m TumDBMigrator) migrate20220713000000() *gormigrate.Migration {
&model.DishNameTagOptionIncluded{},
&model.DishNameTagOptionExcluded{},
&model.DishNameTag{},
&model.DishNameTagAverage{},
&model.DishRating{},
&model.DishRatingTag{},
&model.DishRatingTagOption{},
&model.DishToDishNameTag{},
&InitialDishNameTagAverage{},
&InitialCafeteriaRatingAverage{},
&InitialCafeteriaRatingTagsAverage{},
&InitialDishRatingAverage{},
Expand Down
29 changes: 26 additions & 3 deletions server/backend/migration/20231015000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,29 @@ func (n *CafeteriaRatingTagsAverage) TableName() string {
return "cafeteria_rating_tag_average"
}

// DishNameTagAverage stores all precomputed values for the DishName ratings
type DishNameTagAverage struct {
DishNameTagAverage int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishNameTagAverage;type:int;not null;" json:"dishNameTagAverage" `
CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"`
TagID int64 `gorm:"column:tagID;foreignKey:DishNameTagOption;type:int;not null;" json:"tagID"`
Average float32 `gorm:"column:average;type:float;not null;" json:"average" `
Min int8 `gorm:"column:min;type:int;not null;" json:"min"`
Max int8 `gorm:"column:max;type:int;not null;" json:"max"`
Std float32 `gorm:"column:std;type:float;not null;" json:"std"`
}

// TableName sets the insert table name for this struct type
func (n *DishNameTagAverage) TableName() string {
return "dish_name_tag_average"
}

// migrate20231015000000
// migrates the static data for the canteen rating system and adds the necessary cronjob entries
func (m TumDBMigrator) migrate20231015000000() *gormigrate.Migration {

Check failure on line 90 in server/backend/migration/20231015000000.go

View workflow job for this annotation

GitHub Actions / lint

func `TumDBMigrator.migrate20231015000000` is unused (unused)
return &gormigrate.Migration{
ID: "20231015000000",
Migrate: func(tx *gorm.DB) error {
tables := []string{"cafeteria_rating_average", "dish_rating_average", "dish_rating_tag_average", "cafeteria_rating_tag_average"}
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
Expand Down Expand Up @@ -109,16 +125,23 @@ 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
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
},
Rollback: func(tx *gorm.DB) error {
createdViews := []string{"cafeteria_rating_statistics", "dish_rating_statistics", "dish_rating_tag_statistics", "cafeteria_rating_tag_average"}
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{})
return tx.AutoMigrate(&CafeteriaRatingAverage{}, &DishRatingAverage{}, &DishRatingTagAverage{}, &CafeteriaRatingTagsAverage{}, &DishNameTagAverage{})
},
}
}
17 changes: 0 additions & 17 deletions server/model/dish_name_tag_average.go

This file was deleted.

11 changes: 11 additions & 0 deletions server/model/dish_name_tag_statistic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

// DishNameTagStatistic is a view for statistics of DishName ratings
type DishNameTagStatistic struct {
CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"`
TagID int64 `gorm:"column:tagID;foreignKey:DishNameTagOption;type:int;not null;" json:"tagID"`
Average float32 `gorm:"column:average;type:float;not null;" json:"average" `
Min int8 `gorm:"column:min;type:int;not null;" json:"min"`
Max int8 `gorm:"column:max;type:int;not null;" json:"max"`
Std float32 `gorm:"column:std;type:float;not null;" json:"std"`
}

0 comments on commit f888dd7

Please sign in to comment.