mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
refactor: make intervals be string lists of aliases
This commit is contained in:
parent
9ff35b85d0
commit
8fed606e9b
12
models/interval.go
Normal file
12
models/interval.go
Normal file
@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
type IntervalKey []string
|
||||
|
||||
func (k *IntervalKey) HasAlias(s string) bool {
|
||||
for _, e := range *k {
|
||||
if e == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@ -14,34 +14,38 @@ const (
|
||||
SummaryMachine uint8 = 4
|
||||
)
|
||||
|
||||
const (
|
||||
IntervalToday string = "today"
|
||||
IntervalYesterday string = "yesterday"
|
||||
IntervalThisWeek string = "week"
|
||||
IntervalThisMonth string = "month"
|
||||
IntervalThisYear string = "year"
|
||||
IntervalPast7Days string = "last_7_days"
|
||||
IntervalPast30Days string = "last_30_days"
|
||||
IntervalPast12Months string = "last_12_months"
|
||||
IntervalAny string = "any"
|
||||
|
||||
// https://wakatime.com/developers/#summaries
|
||||
IntervalWakatimeToday string = "Today"
|
||||
IntervalWakatimeYesterday string = "Yesterday"
|
||||
IntervalWakatimeLast7Days string = "Last 7 Days"
|
||||
IntervalWakatimeLast7DaysYesterday string = "Last 7 Days from Yesterday"
|
||||
IntervalWakatimeLast14Days string = "Last 14 Days"
|
||||
IntervalWakatimeLast30Days string = "Last 30 Days"
|
||||
IntervalWakatimeThisWeek string = "This Week"
|
||||
IntervalWakatimeLastWeek string = "Last Week"
|
||||
IntervalWakatimeThisMonth string = "This Month"
|
||||
IntervalWakatimeLastMonth string = "Last Month"
|
||||
// Support Wakapi and WakaTime range / interval identifiers
|
||||
// See https://wakatime.com/developers/#summaries
|
||||
var (
|
||||
IntervalToday = &IntervalKey{"today", "Today"}
|
||||
IntervalYesterday = &IntervalKey{"day", "yesterday", "Yesterday"}
|
||||
IntervalThisWeek = &IntervalKey{"week", "This Week"}
|
||||
IntervalLastWeek = &IntervalKey{"Last Week"}
|
||||
IntervalThisMonth = &IntervalKey{"month", "This Month"}
|
||||
IntervalLastMonth = &IntervalKey{"Last Month"}
|
||||
IntervalThisYear = &IntervalKey{"year"}
|
||||
IntervalPast7Days = &IntervalKey{"7_days", "last_7_days", "Last 7 Days"}
|
||||
IntervalPast7DaysYesterday = &IntervalKey{"Last 7 Days from Yesterday"}
|
||||
IntervalPast14Days = &IntervalKey{"Last 14 Days"}
|
||||
IntervalPast30Days = &IntervalKey{"30_days", "last_30_days", "Last 30 Days"}
|
||||
IntervalPast12Months = &IntervalKey{"12_months", "last_12_months"}
|
||||
IntervalAny = &IntervalKey{"any"}
|
||||
)
|
||||
|
||||
func Intervals() []string {
|
||||
return []string{
|
||||
IntervalToday, IntervalYesterday, IntervalThisWeek, IntervalThisMonth, IntervalThisYear, IntervalPast7Days, IntervalPast30Days, IntervalPast12Months, IntervalAny,
|
||||
}
|
||||
var AllIntervals = []*IntervalKey{
|
||||
IntervalToday,
|
||||
IntervalYesterday,
|
||||
IntervalThisWeek,
|
||||
IntervalLastWeek,
|
||||
IntervalThisMonth,
|
||||
IntervalLastMonth,
|
||||
IntervalThisYear,
|
||||
IntervalPast7Days,
|
||||
IntervalPast7DaysYesterday,
|
||||
IntervalPast14Days,
|
||||
IntervalPast30Days,
|
||||
IntervalPast12Months,
|
||||
IntervalAny,
|
||||
}
|
||||
|
||||
const UnknownSummaryKey = "unknown"
|
||||
|
@ -59,7 +59,9 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var interval = models.IntervalPast30Days
|
||||
if groups := intervalReg.FindStringSubmatch(r.URL.Path); len(groups) > 1 {
|
||||
interval = groups[1]
|
||||
if i, err := utils.ParseInterval(groups[1]); err == nil {
|
||||
interval = i
|
||||
}
|
||||
}
|
||||
|
||||
var filters *models.Filters
|
||||
@ -89,7 +91,7 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
utils.RespondJSON(w, http.StatusOK, vm)
|
||||
}
|
||||
|
||||
func (h *BadgeHandler) loadUserSummary(user *models.User, interval string) (*models.Summary, error, int) {
|
||||
func (h *BadgeHandler) loadUserSummary(user *models.User, interval *models.IntervalKey) (*models.Summary, error, int) {
|
||||
err, from, to := utils.ResolveInterval(interval)
|
||||
if err != nil {
|
||||
return nil, err, http.StatusBadRequest
|
||||
|
@ -61,7 +61,7 @@ func (h *StatsHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *StatsHandler) loadUserSummary(user *models.User, rangeKey string) (*models.Summary, error, int) {
|
||||
var start, end time.Time
|
||||
|
||||
if err, parsedFrom, parsedTo := utils.ResolveInterval(rangeKey); err == nil {
|
||||
if err, parsedFrom, parsedTo := utils.ResolveIntervalRaw(rangeKey); err == nil {
|
||||
start, end = parsedFrom, parsedTo
|
||||
} else {
|
||||
return nil, errors.New("invalid 'range' parameter"), http.StatusBadRequest
|
||||
|
@ -68,12 +68,12 @@ func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary
|
||||
var start, end time.Time
|
||||
if rangeParam != "" {
|
||||
// range param takes precedence
|
||||
if err, parsedFrom, parsedTo := utils.ResolveInterval(rangeParam); err == nil {
|
||||
if err, parsedFrom, parsedTo := utils.ResolveIntervalRaw(rangeParam); err == nil {
|
||||
start, end = parsedFrom, parsedTo
|
||||
} else {
|
||||
return nil, errors.New("invalid 'range' parameter"), http.StatusBadRequest
|
||||
}
|
||||
} else if err, parsedFrom, parsedTo := utils.ResolveInterval(startParam); err == nil && startParam == endParam {
|
||||
} else if err, parsedFrom, parsedTo := utils.ResolveIntervalRaw(startParam); err == nil && startParam == endParam {
|
||||
// also accept start param to be a range param
|
||||
start, end = parsedFrom, parsedTo
|
||||
} else {
|
||||
|
@ -7,35 +7,52 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func ResolveInterval(interval string) (err error, from, to time.Time) {
|
||||
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")
|
||||
}
|
||||
|
||||
func ResolveIntervalRaw(interval string) (err error, from, to time.Time) {
|
||||
parsed, err := ParseInterval(interval)
|
||||
if err != nil {
|
||||
return err, time.Time{}, time.Time{}
|
||||
}
|
||||
return ResolveInterval(parsed)
|
||||
}
|
||||
|
||||
func ResolveInterval(interval *models.IntervalKey) (err error, from, to time.Time) {
|
||||
to = time.Now()
|
||||
|
||||
switch interval {
|
||||
case models.IntervalToday, models.IntervalWakatimeToday:
|
||||
case models.IntervalToday:
|
||||
from = StartOfToday()
|
||||
case models.IntervalYesterday, models.IntervalWakatimeYesterday:
|
||||
case models.IntervalYesterday:
|
||||
from = StartOfToday().Add(-24 * time.Hour)
|
||||
to = StartOfToday()
|
||||
case models.IntervalThisWeek, models.IntervalWakatimeThisWeek:
|
||||
case models.IntervalThisWeek:
|
||||
from = StartOfWeek()
|
||||
case models.IntervalWakatimeLastWeek:
|
||||
case models.IntervalLastWeek:
|
||||
from = StartOfWeek().AddDate(0, 0, -7)
|
||||
to = StartOfWeek()
|
||||
case models.IntervalThisMonth, models.IntervalWakatimeThisMonth:
|
||||
case models.IntervalThisMonth:
|
||||
from = StartOfMonth()
|
||||
case models.IntervalWakatimeLastMonth:
|
||||
case models.IntervalLastMonth:
|
||||
from = StartOfMonth().AddDate(0, -1, 0)
|
||||
to = StartOfMonth()
|
||||
case models.IntervalThisYear:
|
||||
from = StartOfYear()
|
||||
case models.IntervalPast7Days, models.IntervalWakatimeLast7Days:
|
||||
case models.IntervalPast7Days:
|
||||
from = StartOfToday().AddDate(0, 0, -7)
|
||||
case models.IntervalWakatimeLast7DaysYesterday:
|
||||
case models.IntervalPast7DaysYesterday:
|
||||
from = StartOfToday().AddDate(0, 0, -1).AddDate(0, 0, -7)
|
||||
to = StartOfToday().AddDate(0, 0, -1)
|
||||
case models.IntervalWakatimeLast14Days:
|
||||
case models.IntervalPast14Days:
|
||||
from = StartOfToday().AddDate(0, 0, -14)
|
||||
case models.IntervalPast30Days, models.IntervalWakatimeLast30Days:
|
||||
case models.IntervalPast30Days:
|
||||
from = StartOfToday().AddDate(0, 0, -30)
|
||||
case models.IntervalPast12Months:
|
||||
from = StartOfToday().AddDate(0, -12, 0)
|
||||
@ -56,7 +73,7 @@ func ParseSummaryParams(r *http.Request) (*models.SummaryParams, error) {
|
||||
var from, to time.Time
|
||||
|
||||
if interval := params.Get("interval"); interval != "" {
|
||||
err, from, to = ResolveInterval(interval)
|
||||
err, from, to = ResolveIntervalRaw(interval)
|
||||
} else {
|
||||
from, err = ParseDate(params.Get("from"))
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user