mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Introduce flag to recompute summaries from raw events instead of using aggregations.
This commit is contained in:
parent
f3fe8124c8
commit
9e726028c3
7
main.go
7
main.go
@ -170,12 +170,7 @@ func migrateLanguages(db *gorm.DB, cfg *models.Config) {
|
|||||||
func addDefaultUser(db *gorm.DB, cfg *models.Config) {
|
func addDefaultUser(db *gorm.DB, cfg *models.Config) {
|
||||||
pw := md5.Sum([]byte(models.DefaultPassword))
|
pw := md5.Sum([]byte(models.DefaultPassword))
|
||||||
pwString := hex.EncodeToString(pw[:])
|
pwString := hex.EncodeToString(pw[:])
|
||||||
var err error
|
apiKey := uuid.NewV4().String()
|
||||||
apiKey := uuid.Must(uuid.NewV4(), err).String()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Unable to create api key.")
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
u := &models.User{ID: models.DefaultUser, Password: pwString, ApiKey: apiKey}
|
u := &models.User{ID: models.DefaultUser, Password: pwString, ApiKey: apiKey}
|
||||||
result := db.FirstOrCreate(u, &models.User{ID: u.ID})
|
result := db.FirstOrCreate(u, &models.User{ID: u.ID})
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/n1try/wakapi/services"
|
"github.com/n1try/wakapi/services"
|
||||||
"github.com/n1try/wakapi/utils"
|
"github.com/n1try/wakapi/utils"
|
||||||
cache "github.com/patrickmn/go-cache"
|
cache "github.com/patrickmn/go-cache"
|
||||||
|
uuid "github.com/satori/go.uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -67,21 +68,27 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == IntervalToday
|
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == IntervalToday
|
||||||
|
recompute := (params.Get("recompute") != "" && params.Get("recompute") != "false")
|
||||||
to := utils.StartOfDay()
|
to := utils.StartOfDay()
|
||||||
if live {
|
if live {
|
||||||
to = time.Now()
|
to = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
var summary *models.Summary
|
var summary *models.Summary
|
||||||
cacheKey := getHash([]time.Time{from, to}, user)
|
var cacheKey string
|
||||||
|
if !recompute {
|
||||||
|
cacheKey = getHash([]time.Time{from, to}, user)
|
||||||
|
} else {
|
||||||
|
cacheKey = uuid.NewV4().String()
|
||||||
|
}
|
||||||
if cachedSummary, ok := h.Cache.Get(cacheKey); !ok {
|
if cachedSummary, ok := h.Cache.Get(cacheKey); !ok {
|
||||||
// Cache Miss
|
// Cache Miss
|
||||||
summary, err = h.SummarySrvc.Construct(from, to, user) // 'to' is always constant
|
summary, err = h.SummarySrvc.Construct(from, to, user, recompute) // 'to' is always constant
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !live {
|
if !live && !recompute {
|
||||||
h.Cache.Set(cacheKey, summary, cache.DefaultExpiration)
|
h.Cache.Set(cacheKey, summary, cache.DefaultExpiration)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -53,7 +53,7 @@ func (srv *AggregationService) Schedule() {
|
|||||||
|
|
||||||
func (srv *AggregationService) summaryWorker(jobs <-chan *AggregationJob, summaries chan<- *models.Summary) {
|
func (srv *AggregationService) summaryWorker(jobs <-chan *AggregationJob, summaries chan<- *models.Summary) {
|
||||||
for job := range jobs {
|
for job := range jobs {
|
||||||
if summary, err := srv.SummaryService.Construct(job.From, job.To, &models.User{ID: job.UserID}); err != nil {
|
if summary, err := srv.SummaryService.Construct(job.From, job.To, &models.User{ID: job.UserID}, true); err != nil {
|
||||||
log.Printf("Failed to generate summary (%v, %v, %s) – %v.\n", job.From, job.To, job.UserID, err)
|
log.Printf("Failed to generate summary (%v, %v, %s) – %v.\n", job.From, job.To, job.UserID, err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Successfully generated summary (%v, %v, %s).\n", job.From, job.To, job.UserID)
|
log.Printf("Successfully generated summary (%v, %v, %s).\n", job.From, job.To, job.UserID)
|
||||||
|
@ -22,10 +22,16 @@ type Interval struct {
|
|||||||
End time.Time
|
End time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *SummaryService) Construct(from, to time.Time, user *models.User) (*models.Summary, error) {
|
func (srv *SummaryService) Construct(from, to time.Time, user *models.User, recompute bool) (*models.Summary, error) {
|
||||||
existingSummaries, err := srv.GetByUserWithin(user, from, to)
|
var existingSummaries []*models.Summary
|
||||||
if err != nil {
|
if recompute {
|
||||||
return nil, err
|
existingSummaries = make([]*models.Summary, 0)
|
||||||
|
} else {
|
||||||
|
summaries, err := srv.GetByUserWithin(user, from, to)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
existingSummaries = summaries
|
||||||
}
|
}
|
||||||
|
|
||||||
missingIntervals := getMissingIntervals(from, to, existingSummaries)
|
missingIntervals := getMissingIntervals(from, to, existingSummaries)
|
||||||
|
Loading…
Reference in New Issue
Block a user