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

chore: remove cleanup functionality

chore: minor code changes
This commit is contained in:
Ferdinand Mütsch 2020-11-06 14:07:07 +01:00
parent cb8f68df82
commit e6a04cc76d
6 changed files with 15 additions and 47 deletions

View File

@ -76,8 +76,6 @@ func migrateLegacyConfig() error {
basePath = basePathEnv basePath = basePathEnv
} }
cleanUp := cfg.Section("app").Key("cleanup").MustBool(false)
// Read custom languages // Read custom languages
customLangs := make(map[string]string) customLangs := make(map[string]string)
languageKeys := cfg.Section("languages").Keys() languageKeys := cfg.Section("languages").Keys()
@ -89,7 +87,6 @@ func migrateLegacyConfig() error {
config := &Config{ config := &Config{
Env: env, Env: env,
App: appConfig{ App: appConfig{
CleanUp: cleanUp,
CustomLanguages: customLangs, CustomLanguages: customLangs,
}, },
Security: securityConfig{ Security: securityConfig{

17
main.go
View File

@ -56,11 +56,6 @@ func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile) 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 // Connect to database
var err error var err error
db, err = gorm.Open(config.Db.GetDialector(), &gorm.Config{}) db, err = gorm.Open(config.Db.GetDialector(), &gorm.Config{})
@ -102,10 +97,6 @@ func main() {
// Aggregate heartbeats to summaries and persist them // Aggregate heartbeats to summaries and persist them
go aggregationService.Schedule() go aggregationService.Schedule()
if config.App.CleanUp {
go heartbeatService.ScheduleCleanUp()
}
// TODO: move endpoint registration to the respective routes files // TODO: move endpoint registration to the respective routes files
// Handlers // Handlers
@ -194,11 +185,3 @@ func runDatabaseMigrations() {
log.Fatal(err) 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)
}
}

View File

@ -4,14 +4,18 @@ type LanguageMapping struct {
ID uint `json:"id" gorm:"primary_key"` ID uint `json:"id" gorm:"primary_key"`
User *User `json:"-" gorm:"not null"` User *User `json:"-" gorm:"not null"`
UserID string `json:"-" gorm:"not null; index:idx_language_mapping_user; uniqueIndex:idx_language_mapping_composite"` 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"` Extension string `json:"extension" gorm:"uniqueIndex:idx_language_mapping_composite; type:varchar(16)"`
Language string `json:"language"` Language string `json:"language" gorm:"type:varchar(64)"`
} }
func validateLanguage(language string) bool { func (m *LanguageMapping) IsValid() bool {
return len(language) >= 1 return m.validateLanguage() && m.validateExtension()
} }
func validateExtension(extension string) bool { func (m *LanguageMapping) validateLanguage() bool {
return len(extension) >= 1 return len(m.Language) >= 1
}
func (m *LanguageMapping) validateExtension() bool {
return len(m.Extension) >= 1
} }

View File

@ -1,6 +1,7 @@
package repositories package repositories
import ( import (
"errors"
"github.com/muety/wakapi/config" "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models" "github.com/muety/wakapi/models"
"gorm.io/gorm" "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) { func (r *LanguageMappingRepository) Insert(mapping *models.LanguageMapping) (*models.LanguageMapping, error) {
if !mapping.IsValid() {
return nil, errors.New("invalid mapping")
}
result := r.db.Create(mapping) result := r.db.Create(mapping)
if err := result.Error; err != nil { if err := result.Error; err != nil {
return nil, err return nil, err

View File

@ -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) { 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 { if err := result.Error; err != nil {
return nil, err return nil, err
} }

View File

@ -1,11 +1,8 @@
package services package services
import ( import (
"github.com/jasonlvhit/gocron"
"github.com/muety/wakapi/config" "github.com/muety/wakapi/config"
"github.com/muety/wakapi/repositories" "github.com/muety/wakapi/repositories"
"github.com/muety/wakapi/utils"
"log"
"time" "time"
"github.com/muety/wakapi/models" "github.com/muety/wakapi/models"
@ -49,23 +46,6 @@ func (srv *HeartbeatService) DeleteBefore(t time.Time) error {
return srv.repository.DeleteBefore(t) 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) { func (srv *HeartbeatService) augmented(heartbeats []*models.Heartbeat, userId string) ([]*models.Heartbeat, error) {
languageMapping, err := srv.languageMappingSrvc.ResolveByUser(userId) languageMapping, err := srv.languageMappingSrvc.ResolveByUser(userId)
if err != nil { if err != nil {