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

116 lines
2.9 KiB
Go
Raw Normal View History

package routes
import (
2020-02-20 16:28:55 +03:00
"errors"
"net/http"
"time"
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils"
)
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
config *models.Config
2019-05-21 22:46:23 +03:00
}
func NewSummaryHandler(summaryService *services.SummaryService) *SummaryHandler {
return &SummaryHandler{
summarySrvc: summaryService,
config: models.GetConfig(),
}
}
2020-03-31 12:24:44 +03:00
func (h *SummaryHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
summary, err, status := loadUserSummary(r, h.summarySrvc)
2020-02-20 16:28:55 +03:00
if err != nil {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
return
}
utils.RespondJSON(w, http.StatusOK, summary)
}
func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()
2020-02-20 17:39:56 +03:00
}
2020-02-20 17:54:20 +03:00
q := r.URL.Query()
if q.Get("interval") == "" && q.Get("from") == "" {
q.Set("interval", "today")
r.URL.RawQuery = q.Encode()
}
summary, err, status := loadUserSummary(r, h.summarySrvc)
2020-02-20 16:28:55 +03:00
if err != nil {
respondAlert(w, err.Error(), "", "summary.tpl.html", status)
return
}
user := r.Context().Value(models.UserKey).(*models.User)
if user == nil {
respondAlert(w, "unauthorized", "", "summary.tpl.html", http.StatusUnauthorized)
2020-02-20 16:28:55 +03:00
return
}
2020-03-31 12:24:44 +03:00
vm := models.SummaryViewModel{
Summary: summary,
LanguageColors: utils.FilterLanguageColors(h.config.LanguageColors, summary),
ApiKey: user.ApiKey,
2020-03-31 12:24:44 +03:00
}
templates["summary.tpl.html"].Execute(w, vm)
2020-02-20 16:28:55 +03:00
}
func loadUserSummary(r *http.Request, summaryService *services.SummaryService) (*models.Summary, error, int) {
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:
2020-02-20 16:28:55 +03:00
return nil, errors.New("missing 'from' parameter"), http.StatusBadRequest
2019-05-19 21:06:07 +03:00
}
}
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
2020-02-20 16:28:55 +03:00
summary, err = summaryService.Construct(from, to, user, recompute) // 'to' is always constant
if err != nil {
return nil, err, http.StatusInternalServerError
}
2020-02-20 16:28:55 +03:00
return summary, nil, http.StatusOK
2019-05-19 21:14:57 +03:00
}