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

141 lines
3.2 KiB
Go
Raw Normal View History

package routes
import (
2020-02-20 16:28:55 +03:00
"errors"
"html/template"
"net/http"
2020-02-20 16:28:55 +03:00
"path"
"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 {
2020-02-20 16:28:55 +03:00
SummarySrvc *services.SummaryService
Initialized bool
indexTemplate *template.Template
2019-05-21 22:46:23 +03:00
}
func (m *SummaryHandler) Init() {
2020-02-20 17:39:56 +03:00
m.loadTemplates()
m.Initialized = true
}
func (m *SummaryHandler) loadTemplates() {
2020-02-20 16:28:55 +03:00
indexTplPath := "views/index.tpl.html"
indexTpl, err := template.New(path.Base(indexTplPath)).Funcs(template.FuncMap{
"json": utils.Json,
2020-02-20 17:39:56 +03:00
"date": utils.FormatDateHuman,
2020-02-20 16:28:55 +03:00
}).ParseFiles(indexTplPath)
if err != nil {
panic(err)
2019-05-21 22:46:23 +03:00
}
2020-02-20 16:28:55 +03:00
m.indexTemplate = indexTpl
}
2020-03-31 12:24:44 +03:00
func (h *SummaryHandler) ApiGet(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
2020-02-20 16:28:55 +03:00
summary, err, status := loadUserSummary(r, h.SummarySrvc)
if err != nil {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
return
}
utils.RespondJSON(w, http.StatusOK, summary)
}
func (h *SummaryHandler) Index(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if !h.Initialized {
h.Init()
}
2020-02-20 17:39:56 +03:00
if h.SummarySrvc.Config.IsDev() {
h.loadTemplates()
}
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-02-20 16:28:55 +03:00
summary, err, status := loadUserSummary(r, h.SummarySrvc)
if err != nil {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
return
}
2020-03-31 12:24:44 +03:00
vm := models.SummaryViewModel{
Summary: summary,
LanguageColors: utils.FilterLanguageColors(h.SummarySrvc.Config.LanguageColors, summary),
}
h.indexTemplate.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
}