From e6a04cc76d7aedc6a69d3d2b50cb456df132bb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Fri, 6 Nov 2020 14:07:07 +0100 Subject: [PATCH] chore: remove cleanup functionality chore: minor code changes --- config/legacy.go | 3 --- main.go | 17 ----------------- models/language_mapping.go | 16 ++++++++++------ repositories/language_mapping.go | 4 ++++ repositories/user.go | 2 +- services/heartbeat.go | 20 -------------------- 6 files changed, 15 insertions(+), 47 deletions(-) diff --git a/config/legacy.go b/config/legacy.go index 80ad686..ff3e7f2 100644 --- a/config/legacy.go +++ b/config/legacy.go @@ -76,8 +76,6 @@ func migrateLegacyConfig() error { basePath = basePathEnv } - cleanUp := cfg.Section("app").Key("cleanup").MustBool(false) - // Read custom languages customLangs := make(map[string]string) languageKeys := cfg.Section("languages").Keys() @@ -89,7 +87,6 @@ func migrateLegacyConfig() error { config := &Config{ Env: env, App: appConfig{ - CleanUp: cleanUp, CustomLanguages: customLangs, }, Security: securityConfig{ diff --git a/main.go b/main.go index 99c15ca..0538b49 100644 --- a/main.go +++ b/main.go @@ -56,11 +56,6 @@ func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) } - // Show data loss warning - if config.App.CleanUp { - promptAbort("`CLEANUP` is set to `true`, which may cause data loss. Are you sure to continue?", 5) - } - // Connect to database var err error db, err = gorm.Open(config.Db.GetDialector(), &gorm.Config{}) @@ -102,10 +97,6 @@ func main() { // Aggregate heartbeats to summaries and persist them go aggregationService.Schedule() - if config.App.CleanUp { - go heartbeatService.ScheduleCleanUp() - } - // TODO: move endpoint registration to the respective routes files // Handlers @@ -194,11 +185,3 @@ func runDatabaseMigrations() { log.Fatal(err) } } - -func promptAbort(message string, timeoutSec int) { - log.Printf("[WARNING] %s.\nTo abort server startup, press Ctrl+C.\n", message) - for i := timeoutSec; i > 0; i-- { - log.Printf("Starting in %d seconds ...\n", i) - time.Sleep(1 * time.Second) - } -} diff --git a/models/language_mapping.go b/models/language_mapping.go index 13b05b8..e0aee9d 100644 --- a/models/language_mapping.go +++ b/models/language_mapping.go @@ -4,14 +4,18 @@ type LanguageMapping struct { ID uint `json:"id" gorm:"primary_key"` User *User `json:"-" gorm:"not null"` UserID string `json:"-" gorm:"not null; index:idx_language_mapping_user; uniqueIndex:idx_language_mapping_composite"` - Extension string `json:"extension" gorm:"uniqueIndex:idx_language_mapping_composite"` - Language string `json:"language"` + Extension string `json:"extension" gorm:"uniqueIndex:idx_language_mapping_composite; type:varchar(16)"` + Language string `json:"language" gorm:"type:varchar(64)"` } -func validateLanguage(language string) bool { - return len(language) >= 1 +func (m *LanguageMapping) IsValid() bool { + return m.validateLanguage() && m.validateExtension() } -func validateExtension(extension string) bool { - return len(extension) >= 1 +func (m *LanguageMapping) validateLanguage() bool { + return len(m.Language) >= 1 +} + +func (m *LanguageMapping) validateExtension() bool { + return len(m.Extension) >= 1 } diff --git a/repositories/language_mapping.go b/repositories/language_mapping.go index 27270fe..9a18fb3 100644 --- a/repositories/language_mapping.go +++ b/repositories/language_mapping.go @@ -1,6 +1,7 @@ package repositories import ( + "errors" "github.com/muety/wakapi/config" "github.com/muety/wakapi/models" "gorm.io/gorm" @@ -34,6 +35,9 @@ func (r *LanguageMappingRepository) GetByUser(userId string) ([]*models.Language } func (r *LanguageMappingRepository) Insert(mapping *models.LanguageMapping) (*models.LanguageMapping, error) { + if !mapping.IsValid() { + return nil, errors.New("invalid mapping") + } result := r.db.Create(mapping) if err := result.Error; err != nil { return nil, err diff --git a/repositories/user.go b/repositories/user.go index 71e3423..e3964e5 100644 --- a/repositories/user.go +++ b/repositories/user.go @@ -54,7 +54,7 @@ func (r *UserRepository) InsertOrGet(user *models.User) (*models.User, bool, err } func (r *UserRepository) Update(user *models.User) (*models.User, error) { - result := r.db.Model(&models.User{}).Updates(user) + result := r.db.Model(user).Updates(user) if err := result.Error; err != nil { return nil, err } diff --git a/services/heartbeat.go b/services/heartbeat.go index 48a57dd..0123a73 100644 --- a/services/heartbeat.go +++ b/services/heartbeat.go @@ -1,11 +1,8 @@ package services import ( - "github.com/jasonlvhit/gocron" "github.com/muety/wakapi/config" "github.com/muety/wakapi/repositories" - "github.com/muety/wakapi/utils" - "log" "time" "github.com/muety/wakapi/models" @@ -49,23 +46,6 @@ func (srv *HeartbeatService) DeleteBefore(t time.Time) error { return srv.repository.DeleteBefore(t) } -func (srv *HeartbeatService) CleanUp() error { - refTime := utils.StartOfToday().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() -} - func (srv *HeartbeatService) augmented(heartbeats []*models.Heartbeat, userId string) ([]*models.Heartbeat, error) { languageMapping, err := srv.languageMappingSrvc.ResolveByUser(userId) if err != nil {