2019-05-19 20:49:27 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2020-02-20 16:28:55 +03:00
|
|
|
"errors"
|
2019-05-19 20:49:27 +03:00
|
|
|
"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 20:49:27 +03:00
|
|
|
)
|
|
|
|
|
2019-05-19 21:06:07 +03:00
|
|
|
const (
|
2019-07-07 11:32:28 +03:00
|
|
|
IntervalToday string = "today"
|
2019-05-19 21:06:07 +03:00
|
|
|
IntervalLastDay string = "day"
|
|
|
|
IntervalLastWeek string = "week"
|
|
|
|
IntervalLastMonth string = "month"
|
|
|
|
IntervalLastYear string = "year"
|
2020-01-09 02:19:24 +03:00
|
|
|
IntervalAny string = "any"
|
2019-05-19 21:06:07 +03:00
|
|
|
)
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
type SummaryHandler struct {
|
2020-05-24 22:19:05 +03:00
|
|
|
summarySrvc *services.SummaryService
|
2020-05-24 14:41:19 +03:00
|
|
|
config *models.Config
|
2019-05-21 22:46:23 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 18:32:26 +03:00
|
|
|
func NewSummaryHandler(summaryService *services.SummaryService) *SummaryHandler {
|
2020-05-24 14:41:19 +03:00
|
|
|
return &SummaryHandler{
|
2020-05-24 22:19:05 +03:00
|
|
|
summarySrvc: summaryService,
|
2020-05-24 18:32:26 +03:00
|
|
|
config: models.GetConfig(),
|
2020-05-24 11:37:31 +03:00
|
|
|
}
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 12:24:44 +03:00
|
|
|
func (h *SummaryHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 22:19:05 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:19:05 +03:00
|
|
|
summary, err, status := loadUserSummary(r, h.summarySrvc)
|
2020-02-20 16:28:55 +03:00
|
|
|
if err != nil {
|
2020-05-24 22:19:05 +03:00
|
|
|
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,
|
2020-05-24 14:41:19 +03:00
|
|
|
LanguageColors: utils.FilterLanguageColors(h.config.LanguageColors, summary),
|
2020-05-24 22:19:05 +03:00
|
|
|
ApiKey: user.ApiKey,
|
2020-03-31 12:24:44 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 14:41:19 +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) {
|
2019-05-19 20:49:27 +03:00
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
params := r.URL.Query()
|
2019-07-07 11:32:28 +03:00
|
|
|
interval := params.Get("interval")
|
2019-05-19 20:49:27 +03:00
|
|
|
from, err := utils.ParseDate(params.Get("from"))
|
|
|
|
if err != nil {
|
2019-05-19 21:06:07 +03:00
|
|
|
switch interval {
|
2019-05-23 09:14:32 +03:00
|
|
|
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()
|
2020-01-09 02:19:24 +03:00
|
|
|
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
|
|
|
}
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:14:32 +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
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
return summary, nil, http.StatusOK
|
2019-05-19 21:14:57 +03:00
|
|
|
}
|