1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: add migration for heartbeats count

This commit is contained in:
Ferdinand Mütsch 2021-12-22 15:44:37 +01:00
parent 92f6d44606
commit ec236909c9
4 changed files with 53 additions and 3 deletions

View File

@ -15,6 +15,8 @@ func init() {
return nil return nil
} }
logbuch.Info("this may take a while!")
if cfg.Db.IsMySQL() { if cfg.Db.IsMySQL() {
tx := db.Begin() tx := db.Begin()
if err := tx.Exec("ALTER TABLE heartbeats MODIFY COLUMN id BIGINT UNSIGNED AUTO_INCREMENT").Error; err != nil { if err := tx.Exec("ALTER TABLE heartbeats MODIFY COLUMN id BIGINT UNSIGNED AUTO_INCREMENT").Error; err != nil {

View File

@ -0,0 +1,48 @@
package migrations
import (
"database/sql"
"github.com/emvi/logbuch"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"gorm.io/gorm"
)
func init() {
const name = "20212212-total_summary_heartbeats"
f := migrationFunc{
name: name,
f: func(db *gorm.DB, cfg *config.Config) error {
if hasRun(name, db) {
return nil
}
logbuch.Info("this may take a while!")
// this turns out to actually be way faster than using joins and instead has the benefit of being cross-dialect compatible
var summaries []*models.Summary
if err := db.Model(&models.Summary{}).
Select("id, from_time, to_time, user_id").
Scan(&summaries).Error; err != nil {
return err
}
tx := db.Begin()
for _, s := range summaries {
query := "UPDATE summaries SET num_heartbeats = (SELECT count(id) AS num_heartbeats FROM heartbeats WHERE user_id = @user AND time BETWEEN @from AND @to) WHERE id = @id"
tx.Exec(query, sql.Named("from", s.FromTime), sql.Named("to", s.ToTime), sql.Named("id", s.ID), sql.Named("user", s.UserID))
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
logbuch.Error("failed to retroactively determine total summary heartbeats")
return err
}
setHasRun(name, db)
return nil
},
}
registerPostMigration(f)
}

View File

@ -31,7 +31,7 @@ type Summary struct {
OperatingSystems SummaryItems `json:"operating_systems" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` OperatingSystems SummaryItems `json:"operating_systems" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Machines SummaryItems `json:"machines" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` Machines SummaryItems `json:"machines" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Labels SummaryItems `json:"labels" gorm:"-"` // labels are not persisted, but calculated at runtime, i.e. when summary is retrieved Labels SummaryItems `json:"labels" gorm:"-"` // labels are not persisted, but calculated at runtime, i.e. when summary is retrieved
NumHeartbeats int `json:"-"` NumHeartbeats int `json:"-" gorm:"default:0"`
} }
type SummaryItems []*SummaryItem type SummaryItems []*SummaryItem

View File

@ -81,9 +81,9 @@
<div class="flex space-x-2 justify-end"> <div class="flex space-x-2 justify-end">
<a href="login"> <a href="login">
<button type="button" class="btn-primary">Log in</button> <button type="button" class="btn-default">Log in</button>
</a> </a>
<button type="submit" class="btn-default"> <button type="submit" class="btn-primary">
Create Account Create Account
</button> </button>
</div> </div>