mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
chore: cache active users with hourly precision
This commit is contained in:
parent
4d2a160ccb
commit
5e96e2a601
File diff suppressed because it is too large
Load Diff
@ -39,8 +39,8 @@ func (m *UserServiceMock) GetAllByReports(b bool) ([]*models.User, error) {
|
|||||||
return args.Get(0).([]*models.User), args.Error(1)
|
return args.Get(0).([]*models.User), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *UserServiceMock) GetActive() ([]*models.User, error) {
|
func (m *UserServiceMock) GetActive(b bool) ([]*models.User, error) {
|
||||||
args := m.Called()
|
args := m.Called(b)
|
||||||
return args.Get(0).([]*models.User), args.Error(1)
|
return args.Get(0).([]*models.User), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,7 +228,7 @@ func (h *MetricsHandler) getAdminMetrics(user *models.User) (*mm.Metrics, error)
|
|||||||
totalUsers, _ := h.userSrvc.Count()
|
totalUsers, _ := h.userSrvc.Count()
|
||||||
totalHeartbeats, _ := h.heartbeatSrvc.Count()
|
totalHeartbeats, _ := h.heartbeatSrvc.Count()
|
||||||
|
|
||||||
activeUsers, err := h.userSrvc.GetActive()
|
activeUsers, err := h.userSrvc.GetActive(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logbuch.Error("failed to retrieve active users for metric – %v", err)
|
logbuch.Error("failed to retrieve active users for metric – %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -90,7 +90,7 @@ type IUserService interface {
|
|||||||
GetUserByResetToken(string) (*models.User, error)
|
GetUserByResetToken(string) (*models.User, error)
|
||||||
GetAll() ([]*models.User, error)
|
GetAll() ([]*models.User, error)
|
||||||
GetAllByReports(bool) ([]*models.User, error)
|
GetAllByReports(bool) ([]*models.User, error)
|
||||||
GetActive() ([]*models.User, error)
|
GetActive(bool) ([]*models.User, error)
|
||||||
Count() (int64, error)
|
Count() (int64, error)
|
||||||
CreateOrGet(*models.Signup, bool) (*models.User, bool, error)
|
CreateOrGet(*models.Signup, bool) (*models.User, bool, error)
|
||||||
Update(*models.User) (*models.User, error)
|
Update(*models.User) (*models.User, error)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/leandro-lugaresi/hub"
|
"github.com/leandro-lugaresi/hub"
|
||||||
"github.com/muety/wakapi/config"
|
"github.com/muety/wakapi/config"
|
||||||
"github.com/muety/wakapi/models"
|
"github.com/muety/wakapi/models"
|
||||||
@ -51,7 +52,7 @@ func (srv *UserService) GetUserByKey(key string) (*models.User, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.cache.Set(u.ID, u, cache.DefaultExpiration)
|
srv.cache.SetDefault(u.ID, u)
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,9 +72,24 @@ func (srv *UserService) GetAllByReports(reportsEnabled bool) ([]*models.User, er
|
|||||||
return srv.repository.GetAllByReports(reportsEnabled)
|
return srv.repository.GetAllByReports(reportsEnabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *UserService) GetActive() ([]*models.User, error) {
|
func (srv *UserService) GetActive(exact bool) ([]*models.User, error) {
|
||||||
minDate := time.Now().Add(-24 * time.Hour * time.Duration(srv.config.App.InactiveDays))
|
minDate := time.Now().Add(-24 * time.Hour * time.Duration(srv.config.App.InactiveDays))
|
||||||
return srv.repository.GetByLastActiveAfter(minDate)
|
if !exact {
|
||||||
|
minDate = utils.FloorDateHour(minDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheKey := fmt.Sprintf("%s--active", minDate.String())
|
||||||
|
if u, ok := srv.cache.Get(cacheKey); ok {
|
||||||
|
return u.([]*models.User), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := srv.repository.GetByLastActiveAfter(minDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.cache.SetDefault(cacheKey, results)
|
||||||
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *UserService) Count() (int64, error) {
|
func (srv *UserService) Count() (int64, error) {
|
||||||
|
@ -55,6 +55,11 @@ func FloorDate(date time.Time) time.Time {
|
|||||||
return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
|
return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FloorDateHour rounds date down to the start of the current hour and keeps the time zone
|
||||||
|
func FloorDateHour(date time.Time) time.Time {
|
||||||
|
return time.Date(date.Year(), date.Month(), date.Day(), date.Hour(), 0, 0, 0, date.Location())
|
||||||
|
}
|
||||||
|
|
||||||
// CeilDate rounds date up to the start of next day if date is not already a start (00:00:00)
|
// CeilDate rounds date up to the start of next day if date is not already a start (00:00:00)
|
||||||
func CeilDate(date time.Time) time.Time {
|
func CeilDate(date time.Time) time.Time {
|
||||||
floored := FloorDate(date)
|
floored := FloorDate(date)
|
||||||
|
Loading…
Reference in New Issue
Block a user