2020-09-06 13:15:46 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseSummaryParams(r *http.Request) (*models.SummaryParams, error) {
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
params := r.URL.Query()
|
|
|
|
interval := params.Get("interval")
|
|
|
|
|
|
|
|
from, err := ParseDate(params.Get("from"))
|
|
|
|
if err != nil {
|
|
|
|
switch interval {
|
|
|
|
case models.IntervalToday:
|
2020-09-12 00:24:51 +03:00
|
|
|
from = StartOfToday()
|
2020-09-06 13:15:46 +03:00
|
|
|
case models.IntervalLastDay:
|
2020-09-12 00:24:51 +03:00
|
|
|
from = StartOfToday().Add(-24 * time.Hour)
|
2020-09-06 13:15:46 +03:00
|
|
|
case models.IntervalLastWeek:
|
|
|
|
from = StartOfWeek()
|
|
|
|
case models.IntervalLastMonth:
|
|
|
|
from = StartOfMonth()
|
|
|
|
case models.IntervalLastYear:
|
|
|
|
from = StartOfYear()
|
|
|
|
case models.IntervalAny:
|
|
|
|
from = time.Time{}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("missing 'from' parameter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == models.IntervalToday
|
|
|
|
|
|
|
|
recompute := params.Get("recompute") != "" && params.Get("recompute") != "false"
|
|
|
|
|
2020-09-12 00:24:51 +03:00
|
|
|
to := StartOfToday()
|
2020-09-06 13:15:46 +03:00
|
|
|
if live {
|
|
|
|
to = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &models.SummaryParams{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
User: user,
|
|
|
|
Recompute: recompute,
|
|
|
|
}, nil
|
|
|
|
}
|