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:
parent
92f6d44606
commit
ec236909c9
@ -15,6 +15,8 @@ func init() {
|
||||
return nil
|
||||
}
|
||||
|
||||
logbuch.Info("this may take a while!")
|
||||
|
||||
if cfg.Db.IsMySQL() {
|
||||
tx := db.Begin()
|
||||
if err := tx.Exec("ALTER TABLE heartbeats MODIFY COLUMN id BIGINT UNSIGNED AUTO_INCREMENT").Error; err != nil {
|
||||
|
48
migrations/20212212_total_summary_heartbeats.go
Normal file
48
migrations/20212212_total_summary_heartbeats.go
Normal 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)
|
||||
}
|
@ -31,7 +31,7 @@ type Summary struct {
|
||||
OperatingSystems SummaryItems `json:"operating_systems" 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
|
||||
NumHeartbeats int `json:"-"`
|
||||
NumHeartbeats int `json:"-" gorm:"default:0"`
|
||||
}
|
||||
|
||||
type SummaryItems []*SummaryItem
|
||||
|
@ -81,9 +81,9 @@
|
||||
|
||||
<div class="flex space-x-2 justify-end">
|
||||
<a href="login">
|
||||
<button type="button" class="btn-primary">Log in</button>
|
||||
<button type="button" class="btn-default">Log in</button>
|
||||
</a>
|
||||
<button type="submit" class="btn-default">
|
||||
<button type="submit" class="btn-primary">
|
||||
Create Account
|
||||
</button>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user