2020-09-06 13:15:46 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-02-06 21:52:50 +03:00
|
|
|
func ParseInterval(interval string) (*models.IntervalKey, error) {
|
|
|
|
for _, i := range models.AllIntervals {
|
|
|
|
if i.HasAlias(interval) {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("not a valid interval")
|
|
|
|
}
|
|
|
|
|
2021-04-25 15:15:18 +03:00
|
|
|
func MustResolveIntervalRawTZ(interval string, tz *time.Location) (from, to time.Time) {
|
|
|
|
_, from, to = ResolveIntervalRawTZ(interval, tz)
|
2021-02-12 20:37:30 +03:00
|
|
|
return from, to
|
|
|
|
}
|
|
|
|
|
2021-04-25 15:15:18 +03:00
|
|
|
func ResolveIntervalRawTZ(interval string, tz *time.Location) (err error, from, to time.Time) {
|
2021-02-06 21:52:50 +03:00
|
|
|
parsed, err := ParseInterval(interval)
|
|
|
|
if err != nil {
|
|
|
|
return err, time.Time{}, time.Time{}
|
|
|
|
}
|
2021-04-25 15:15:18 +03:00
|
|
|
return ResolveIntervalTZ(parsed, tz)
|
2021-02-06 21:52:50 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 15:15:18 +03:00
|
|
|
func ResolveIntervalTZ(interval *models.IntervalKey, tz *time.Location) (err error, from, to time.Time) {
|
2021-06-11 17:02:28 +03:00
|
|
|
now := time.Now().In(tz)
|
|
|
|
to = now
|
2020-09-12 17:09:23 +03:00
|
|
|
|
|
|
|
switch interval {
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalToday:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfToday(tz)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalYesterday:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfToday(tz).Add(-24 * time.Hour)
|
|
|
|
to = BeginOfToday(tz)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalThisWeek:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfThisWeek(tz)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalLastWeek:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfThisWeek(tz).AddDate(0, 0, -7)
|
|
|
|
to = BeginOfThisWeek(tz)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalThisMonth:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfThisMonth(tz)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalLastMonth:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfThisMonth(tz).AddDate(0, -1, 0)
|
|
|
|
to = BeginOfThisMonth(tz)
|
2020-09-12 17:09:23 +03:00
|
|
|
case models.IntervalThisYear:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfThisYear(tz)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalPast7Days:
|
2021-06-11 17:02:28 +03:00
|
|
|
from = now.AddDate(0, 0, -7)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalPast7DaysYesterday:
|
2022-03-25 14:48:56 +03:00
|
|
|
from = BeginOfToday(tz).AddDate(0, 0, -1).AddDate(0, 0, -7)
|
|
|
|
to = BeginOfToday(tz).AddDate(0, 0, -1)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalPast14Days:
|
2021-06-11 17:02:28 +03:00
|
|
|
from = now.AddDate(0, 0, -14)
|
2021-02-06 21:52:50 +03:00
|
|
|
case models.IntervalPast30Days:
|
2021-06-11 17:02:28 +03:00
|
|
|
from = now.AddDate(0, 0, -30)
|
2020-09-12 17:09:23 +03:00
|
|
|
case models.IntervalPast12Months:
|
2021-06-11 17:02:28 +03:00
|
|
|
from = now.AddDate(0, -12, 0)
|
2020-09-12 17:09:23 +03:00
|
|
|
case models.IntervalAny:
|
|
|
|
from = time.Time{}
|
|
|
|
default:
|
|
|
|
err = errors.New("invalid interval")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err, from, to
|
|
|
|
}
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
func ParseSummaryParams(r *http.Request) (*models.SummaryParams, error) {
|
2021-03-26 15:10:10 +03:00
|
|
|
user := extractUser(r)
|
2020-09-06 13:15:46 +03:00
|
|
|
params := r.URL.Query()
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
var err error
|
2020-09-12 13:40:38 +03:00
|
|
|
var from, to time.Time
|
|
|
|
|
|
|
|
if interval := params.Get("interval"); interval != "" {
|
2021-04-25 15:15:18 +03:00
|
|
|
err, from, to = ResolveIntervalRawTZ(interval, user.TZ())
|
2021-02-12 20:37:30 +03:00
|
|
|
} else if start := params.Get("start"); start != "" {
|
2021-04-25 15:15:18 +03:00
|
|
|
err, from, to = ResolveIntervalRawTZ(start, user.TZ())
|
2020-09-12 13:40:38 +03:00
|
|
|
} else {
|
2021-04-25 15:15:18 +03:00
|
|
|
from, err = ParseDateTimeTZ(params.Get("from"), user.TZ())
|
2020-09-12 13:40:38 +03:00
|
|
|
if err != nil {
|
2021-04-25 15:15:18 +03:00
|
|
|
return nil, errors.New("missing or invalid 'from' parameter")
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 15:15:18 +03:00
|
|
|
to, err = ParseDateTimeTZ(params.Get("to"), user.TZ())
|
2020-09-12 13:40:38 +03:00
|
|
|
if err != nil {
|
2021-04-25 15:15:18 +03:00
|
|
|
return nil, errors.New("missing or invalid 'to' parameter")
|
2020-09-12 13:40:38 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 13:15:46 +03:00
|
|
|
|
|
|
|
recompute := params.Get("recompute") != "" && params.Get("recompute") != "false"
|
|
|
|
|
2022-01-02 22:04:29 +03:00
|
|
|
filters := ParseSummaryFilters(r)
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
return &models.SummaryParams{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
User: user,
|
|
|
|
Recompute: recompute,
|
2022-01-02 22:04:29 +03:00
|
|
|
Filters: filters,
|
2020-09-06 13:15:46 +03:00
|
|
|
}, nil
|
|
|
|
}
|
2021-03-26 15:10:10 +03:00
|
|
|
|
2022-01-02 22:04:29 +03:00
|
|
|
func ParseSummaryFilters(r *http.Request) *models.Filters {
|
|
|
|
filters := &models.Filters{}
|
|
|
|
if q := r.URL.Query().Get("project"); q != "" {
|
|
|
|
filters.With(models.SummaryProject, q)
|
|
|
|
}
|
|
|
|
if q := r.URL.Query().Get("language"); q != "" {
|
|
|
|
filters.With(models.SummaryLanguage, q)
|
|
|
|
}
|
|
|
|
if q := r.URL.Query().Get("editor"); q != "" {
|
|
|
|
filters.With(models.SummaryEditor, q)
|
|
|
|
}
|
|
|
|
if q := r.URL.Query().Get("machine"); q != "" {
|
|
|
|
filters.With(models.SummaryMachine, q)
|
|
|
|
}
|
|
|
|
if q := r.URL.Query().Get("operating_system"); q != "" {
|
|
|
|
filters.With(models.SummaryOS, q)
|
|
|
|
}
|
|
|
|
if q := r.URL.Query().Get("label"); q != "" {
|
|
|
|
filters.With(models.SummaryLabel, q)
|
|
|
|
}
|
|
|
|
if q := r.URL.Query().Get("branch"); q != "" {
|
|
|
|
filters.With(models.SummaryBranch, q)
|
|
|
|
}
|
|
|
|
return filters
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:10:10 +03:00
|
|
|
func extractUser(r *http.Request) *models.User {
|
|
|
|
type principalGetter interface {
|
|
|
|
GetPrincipal() *models.User
|
|
|
|
}
|
|
|
|
if p := r.Context().Value("principal"); p != nil {
|
|
|
|
return p.(principalGetter).GetPrincipal()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|