wakapi/routes/summary.go

112 lines
2.6 KiB
Go
Raw Normal View History

package routes
import (
2019-05-19 21:34:26 +03:00
"crypto/md5"
"net/http"
2019-05-19 21:34:26 +03:00
"strconv"
"time"
"github.com/n1try/wakapi/models"
"github.com/n1try/wakapi/services"
"github.com/n1try/wakapi/utils"
2019-05-21 22:46:23 +03:00
cache "github.com/patrickmn/go-cache"
uuid "github.com/satori/go.uuid"
)
2019-05-19 21:06:07 +03:00
const (
IntervalToday string = "today"
2019-05-19 21:06:07 +03:00
IntervalLastDay string = "day"
IntervalLastWeek string = "week"
IntervalLastMonth string = "month"
IntervalLastYear string = "year"
IntervalAny string = "any"
2019-05-19 21:06:07 +03:00
)
type SummaryHandler struct {
SummarySrvc *services.SummaryService
2019-05-21 22:46:23 +03:00
Cache *cache.Cache
Initialized bool
}
func (m *SummaryHandler) Init() {
if m.Cache == nil {
m.Cache = cache.New(24*time.Hour, 24*time.Hour)
}
m.Initialized = true
}
func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
2019-05-21 22:46:23 +03:00
if !h.Initialized {
h.Init()
}
2019-05-19 21:14:57 +03:00
user := r.Context().Value(models.UserKey).(*models.User)
params := r.URL.Query()
interval := params.Get("interval")
from, err := utils.ParseDate(params.Get("from"))
if err != nil {
2019-05-19 21:06:07 +03:00
switch interval {
case IntervalToday:
from = utils.StartOfDay()
2019-05-19 21:06:07 +03:00
case IntervalLastDay:
from = utils.StartOfDay().Add(-24 * time.Hour)
case IntervalLastWeek:
from = utils.StartOfWeek()
case IntervalLastMonth:
from = utils.StartOfMonth()
case IntervalLastYear:
from = utils.StartOfYear()
case IntervalAny:
from = time.Time{}
2019-05-19 21:06:07 +03:00
default:
w.WriteHeader(http.StatusBadRequest)
2019-11-08 01:11:19 +03:00
w.Write([]byte("missing 'from' parameter"))
2019-05-19 21:06:07 +03:00
return
}
}
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == IntervalToday
2019-11-08 01:11:19 +03:00
recompute := params.Get("recompute") != "" && params.Get("recompute") != "false"
2019-05-19 21:34:26 +03:00
to := utils.StartOfDay()
if live {
to = time.Now()
}
var summary *models.Summary
var cacheKey string
if !recompute {
cacheKey = getHash([]time.Time{from, to}, user)
} else {
cacheKey = uuid.NewV4().String()
}
2019-10-09 22:57:43 +03:00
if cachedSummary, ok := h.Cache.Get(cacheKey); !ok {
2019-05-19 21:14:57 +03:00
// Cache Miss
summary, err = h.SummarySrvc.Construct(from, to, user, recompute) // 'to' is always constant
2019-05-19 21:14:57 +03:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !live && !recompute {
2019-05-21 22:46:23 +03:00
h.Cache.Set(cacheKey, summary, cache.DefaultExpiration)
2019-05-19 21:34:26 +03:00
}
} else {
2019-05-21 22:46:23 +03:00
summary = cachedSummary.(*models.Summary)
}
utils.RespondJSON(w, http.StatusOK, summary)
}
2019-05-19 21:14:57 +03:00
2019-10-09 22:57:43 +03:00
func getHash(times []time.Time, user *models.User) string {
2019-05-19 21:34:26 +03:00
digest := md5.New()
for _, t := range times {
digest.Write([]byte(strconv.Itoa(int(t.Unix()))))
2019-05-19 21:14:57 +03:00
}
2019-10-09 22:57:43 +03:00
digest.Write([]byte(user.ID))
2019-05-19 21:34:26 +03:00
return string(digest.Sum(nil))
2019-05-19 21:14:57 +03:00
}