2020-09-06 13:15:46 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
func ResolveInterval(interval string) (err error, from, to time.Time) {
|
|
|
|
to = time.Now()
|
|
|
|
|
|
|
|
switch interval {
|
2021-01-31 18:23:47 +03:00
|
|
|
case models.IntervalToday, models.IntervalWakatimeToday:
|
2020-09-12 17:09:23 +03:00
|
|
|
from = StartOfToday()
|
2021-01-31 18:23:47 +03:00
|
|
|
case models.IntervalYesterday, models.IntervalWakatimeYesterday:
|
2020-09-12 17:09:23 +03:00
|
|
|
from = StartOfToday().Add(-24 * time.Hour)
|
|
|
|
to = StartOfToday()
|
2021-01-31 18:23:47 +03:00
|
|
|
case models.IntervalThisWeek, models.IntervalWakatimeThisWeek:
|
2020-09-12 17:09:23 +03:00
|
|
|
from = StartOfWeek()
|
2021-01-31 18:23:47 +03:00
|
|
|
case models.IntervalWakatimeLastWeek:
|
|
|
|
from = StartOfWeek().AddDate(0, 0, -7)
|
|
|
|
to = StartOfWeek()
|
|
|
|
case models.IntervalThisMonth, models.IntervalWakatimeThisMonth:
|
2020-09-12 17:09:23 +03:00
|
|
|
from = StartOfMonth()
|
2021-01-31 18:23:47 +03:00
|
|
|
case models.IntervalWakatimeLastMonth:
|
|
|
|
from = StartOfMonth().AddDate(0, -1, 0)
|
|
|
|
to = StartOfMonth()
|
2020-09-12 17:09:23 +03:00
|
|
|
case models.IntervalThisYear:
|
|
|
|
from = StartOfYear()
|
2021-01-31 18:23:47 +03:00
|
|
|
case models.IntervalPast7Days, models.IntervalWakatimeLast7Days:
|
2020-09-12 17:09:23 +03:00
|
|
|
from = StartOfToday().AddDate(0, 0, -7)
|
2021-01-31 18:23:47 +03:00
|
|
|
case models.IntervalWakatimeLast7DaysYesterday:
|
|
|
|
from = StartOfToday().AddDate(0, 0, -1).AddDate(0, 0, -7)
|
|
|
|
to = StartOfToday().AddDate(0, 0, -1)
|
|
|
|
case models.IntervalWakatimeLast14Days:
|
|
|
|
from = StartOfToday().AddDate(0, 0, -14)
|
|
|
|
case models.IntervalPast30Days, models.IntervalWakatimeLast30Days:
|
2020-09-12 17:09:23 +03:00
|
|
|
from = StartOfToday().AddDate(0, 0, -30)
|
|
|
|
case models.IntervalPast12Months:
|
|
|
|
from = StartOfToday().AddDate(0, -12, 0)
|
|
|
|
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) {
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
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 != "" {
|
2020-09-12 17:09:23 +03:00
|
|
|
err, from, to = ResolveInterval(interval)
|
2020-09-12 13:40:38 +03:00
|
|
|
} else {
|
|
|
|
from, err = ParseDate(params.Get("from"))
|
|
|
|
if err != nil {
|
2020-09-06 13:15:46 +03:00
|
|
|
return nil, errors.New("missing 'from' parameter")
|
|
|
|
}
|
|
|
|
|
2020-09-12 13:40:38 +03:00
|
|
|
to, err = ParseDate(params.Get("to"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("missing 'to' parameter")
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 13:15:46 +03:00
|
|
|
|
|
|
|
recompute := params.Get("recompute") != "" && params.Get("recompute") != "false"
|
|
|
|
|
|
|
|
return &models.SummaryParams{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
User: user,
|
|
|
|
Recompute: recompute,
|
|
|
|
}, nil
|
|
|
|
}
|