diff --git a/config.ini b/config.ini index 28238d5..226d7e6 100644 --- a/config.ini +++ b/config.ini @@ -2,6 +2,9 @@ listen = 127.0.0.1 port = 3000 +[app] +cleanup = true + [database] max_connections = 2 diff --git a/main.go b/main.go index 1f8390c..a374584 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,8 @@ func readConfig() *models.Config { port = cfg.Section("server").Key("port").MustInt() } + cleanUp := cfg.Section("app").Key("cleanup").MustBool(false) + // Read custom languages customLangs := make(map[string]string) languageKeys := cfg.Section("languages").Keys() @@ -73,6 +75,7 @@ func readConfig() *models.Config { DbName: dbName, DbDialect: "mysql", DbMaxConn: dbMaxConn, + CleanUp: cleanUp, CustomLanguages: customLangs, } } @@ -83,7 +86,7 @@ func main() { // Connect to database db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config)) - db.LogMode(false) + db.LogMode(config.IsDev()) db.DB().SetMaxIdleConns(int(config.DbMaxConn)) db.DB().SetMaxOpenConns(int(config.DbMaxConn)) if err != nil { @@ -118,6 +121,10 @@ func main() { // Aggregate heartbeats to summaries and persist them go aggregationSrvc.Schedule() + if config.CleanUp { + go heartbeatSrvc.ScheduleCleanUp() + } + // Handlers heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc} summaryHandler := &routes.SummaryHandler{SummarySrvc: summarySrvc} diff --git a/models/config.go b/models/config.go index dc490ca..d389179 100644 --- a/models/config.go +++ b/models/config.go @@ -11,6 +11,7 @@ type Config struct { DbName string DbDialect string DbMaxConn uint + CleanUp bool CustomLanguages map[string]string } diff --git a/services/aggregation.go b/services/aggregation.go index b8bbe71..67d7f31 100644 --- a/services/aggregation.go +++ b/services/aggregation.go @@ -11,7 +11,7 @@ import ( ) const ( - intervalDays int = 1 // TODO: Make configurable + aggregateIntervalDays int = 1 // TODO: Make configurable ) type AggregationService struct { @@ -126,7 +126,7 @@ func generateUserJobs(userId string, lastAggregation time.Time, jobs chan<- *Agg from = time.Date( lastAggregation.Year(), lastAggregation.Month(), - lastAggregation.Day()+intervalDays, + lastAggregation.Day()+aggregateIntervalDays, 0, 0, 0, 0, lastAggregation.Location(), ) @@ -136,7 +136,7 @@ func generateUserJobs(userId string, lastAggregation time.Time, jobs chan<- *Agg to = time.Date( from.Year(), from.Month(), - from.Day()+intervalDays, + from.Day()+aggregateIntervalDays, 0, 0, 0, 0, from.Location(), ) diff --git a/services/heartbeat.go b/services/heartbeat.go index 4333b97..0999601 100644 --- a/services/heartbeat.go +++ b/services/heartbeat.go @@ -1,6 +1,9 @@ package services import ( + "github.com/jasonlvhit/gocron" + "github.com/n1try/wakapi/utils" + "log" "time" "github.com/jinzhu/gorm" @@ -8,7 +11,10 @@ import ( gormbulk "github.com/t-tiger/gorm-bulk-insert" ) -const TableHeartbeat = "heartbeat" +const ( + TableHeartbeat = "heartbeat" + cleanUpInterval = time.Duration(aggregateIntervalDays) * 2 * 24 * time.Hour +) type HeartbeatService struct { Config *models.Config @@ -55,3 +61,29 @@ func (srv *HeartbeatService) GetFirstUserHeartbeats(userIds []string) ([]*models } return heartbeats, nil } + +func (srv *HeartbeatService) DeleteBefore(t time.Time) error { + if err := srv.Db. + Where("time <= ?", t). + Delete(models.Heartbeat{}).Error; err != nil { + return err + } + return nil +} + +func (srv *HeartbeatService) CleanUp() error { + refTime := utils.StartOfDay().Add(-cleanUpInterval) + if err := srv.DeleteBefore(refTime); err != nil { + log.Printf("Failed to clean up heartbeats older than %v – %v\n", refTime, err) + return err + } + log.Printf("Successfully cleaned up heartbeats older than %v\n", refTime) + return nil +} + +func (srv *HeartbeatService) ScheduleCleanUp() { + srv.CleanUp() + + gocron.Every(1).Day().At("02:30").Do(srv.CleanUp) + <-gocron.Start() +}