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
}
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{

17
main.go
View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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

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) {
result := r.db.Model(&models.User{}).Updates(user)
result := r.db.Model(user).Updates(user)
if err := result.Error; err != nil {
return nil, err
}

View File

@ -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 {