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

fix: speed up settings page (resolve #226)

This commit is contained in:
Ferdinand Mütsch 2021-08-06 16:36:01 +02:00
parent 0af5fab75f
commit 533b5d62fc
5 changed files with 495 additions and 482 deletions

File diff suppressed because it is too large Load Diff

11
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"embed" "embed"
"github.com/muety/wakapi/models"
"io/fs" "io/fs"
"log" "log"
"net" "net"
@ -237,6 +238,16 @@ func main() {
middlewares.NewFileTypeFilterMiddleware([]string{".go"})(fileServer), middlewares.NewFileTypeFilterMiddleware([]string{".go"})(fileServer),
) )
// Miscellaneous
// Pre-warm projects cache
allUsers, err := userService.GetAll()
if err == nil {
logbuch.Info("pre-warming user project cache")
for _, u := range allUsers {
go heartbeatService.GetEntitySetByUser(models.SummaryProject, u)
}
}
// Listen HTTP // Listen HTTP
listen(router) listen(router)
} }

View File

@ -147,7 +147,7 @@ func (h *SettingsHandler) dispatchAction(action string) action {
case "toggle_wakatime": case "toggle_wakatime":
return h.actionSetWakatimeApiKey return h.actionSetWakatimeApiKey
case "import_wakatime": case "import_wakatime":
return h.actionImportWaktime return h.actionImportWakatime
case "regenerate_summaries": case "regenerate_summaries":
return h.actionRegenerateSummaries return h.actionRegenerateSummaries
case "delete_account": case "delete_account":
@ -447,7 +447,7 @@ func (h *SettingsHandler) actionSetWakatimeApiKey(w http.ResponseWriter, r *http
return http.StatusOK, "Wakatime API Key updated successfully", "" return http.StatusOK, "Wakatime API Key updated successfully", ""
} }
func (h *SettingsHandler) actionImportWaktime(w http.ResponseWriter, r *http.Request) (int, string, string) { func (h *SettingsHandler) actionImportWakatime(w http.ResponseWriter, r *http.Request) (int, string, string) {
if h.config.IsDev() { if h.config.IsDev() {
loadTemplates() loadTemplates()
} }

View File

@ -16,6 +16,7 @@ import (
type HeartbeatService struct { type HeartbeatService struct {
config *config.Config config *config.Config
cache *cache.Cache cache *cache.Cache
cache2 *cache.Cache
eventBus *hub.Hub eventBus *hub.Hub
repository repositories.IHeartbeatRepository repository repositories.IHeartbeatRepository
languageMappingSrvc ILanguageMappingService languageMappingSrvc ILanguageMappingService
@ -25,6 +26,7 @@ func NewHeartbeatService(heartbeatRepo repositories.IHeartbeatRepository, langua
srv := &HeartbeatService{ srv := &HeartbeatService{
config: config.Get(), config: config.Get(),
cache: cache.New(24*time.Hour, 24*time.Hour), cache: cache.New(24*time.Hour, 24*time.Hour),
cache2: cache.New(cache.NoExpiration, cache.NoExpiration),
eventBus: config.EventBus(), eventBus: config.EventBus(),
repository: heartbeatRepo, repository: heartbeatRepo,
languageMappingSrvc: languageMappingService, languageMappingSrvc: languageMappingService,
@ -145,7 +147,7 @@ func (srv *HeartbeatService) GetFirstByUsers() ([]*models.TimeByUser, error) {
func (srv *HeartbeatService) GetEntitySetByUser(entityType uint8, user *models.User) ([]string, error) { func (srv *HeartbeatService) GetEntitySetByUser(entityType uint8, user *models.User) ([]string, error) {
cacheKey := srv.getEntityUserCacheKey(entityType, user) cacheKey := srv.getEntityUserCacheKey(entityType, user)
if results, found := srv.cache.Get(cacheKey); found { if results, found := srv.cache2.Get(cacheKey); found {
return utils.SetToStrings(results.(map[string]bool)), nil return utils.SetToStrings(results.(map[string]bool)), nil
} }
@ -161,7 +163,7 @@ func (srv *HeartbeatService) GetEntitySetByUser(entityType uint8, user *models.U
} }
} }
srv.cache.Set(cacheKey, utils.StringsToSet(filtered), cache.DefaultExpiration) srv.cache2.Set(cacheKey, utils.StringsToSet(filtered), cache.DefaultExpiration)
return filtered, nil return filtered, nil
} }
@ -188,11 +190,11 @@ func (srv *HeartbeatService) getEntityUserCacheKey(entityType uint8, user *model
func (srv *HeartbeatService) updateEntityUserCache(entityType uint8, entityKey string, user *models.User) { func (srv *HeartbeatService) updateEntityUserCache(entityType uint8, entityKey string, user *models.User) {
cacheKey := srv.getEntityUserCacheKey(entityType, user) cacheKey := srv.getEntityUserCacheKey(entityType, user)
if entities, found := srv.cache.Get(cacheKey); found { if entities, found := srv.cache2.Get(cacheKey); found {
if _, ok := entities.(map[string]bool)[entityKey]; !ok { if _, ok := entities.(map[string]bool)[entityKey]; !ok {
// new project / language / ..., which is not yet present in cache, arrived as part of a heartbeats // new project / language / ..., which is not yet present in cache, arrived as part of a heartbeats
// -> invalidate cache // -> invalidate cache
srv.cache.Delete(cacheKey) srv.cache2.Delete(cacheKey)
} }
} }
} }

View File

@ -1 +1 @@
1.29.2 1.29.3