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
|
SummaryMachine uint8 = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// Support Wakapi and WakaTime range / interval identifiers
|
||||||
IntervalToday string = "today"
|
// See https://wakatime.com/developers/#summaries
|
||||||
IntervalYesterday string = "yesterday"
|
var (
|
||||||
IntervalThisWeek string = "week"
|
IntervalToday = &IntervalKey{"today", "Today"}
|
||||||
IntervalThisMonth string = "month"
|
IntervalYesterday = &IntervalKey{"day", "yesterday", "Yesterday"}
|
||||||
IntervalThisYear string = "year"
|
IntervalThisWeek = &IntervalKey{"week", "This Week"}
|
||||||
IntervalPast7Days string = "last_7_days"
|
IntervalLastWeek = &IntervalKey{"Last Week"}
|
||||||
IntervalPast30Days string = "last_30_days"
|
IntervalThisMonth = &IntervalKey{"month", "This Month"}
|
||||||
IntervalPast12Months string = "last_12_months"
|
IntervalLastMonth = &IntervalKey{"Last Month"}
|
||||||
IntervalAny string = "any"
|
IntervalThisYear = &IntervalKey{"year"}
|
||||||
|
IntervalPast7Days = &IntervalKey{"7_days", "last_7_days", "Last 7 Days"}
|
||||||
// https://wakatime.com/developers/#summaries
|
IntervalPast7DaysYesterday = &IntervalKey{"Last 7 Days from Yesterday"}
|
||||||
IntervalWakatimeToday string = "Today"
|
IntervalPast14Days = &IntervalKey{"Last 14 Days"}
|
||||||
IntervalWakatimeYesterday string = "Yesterday"
|
IntervalPast30Days = &IntervalKey{"30_days", "last_30_days", "Last 30 Days"}
|
||||||
IntervalWakatimeLast7Days string = "Last 7 Days"
|
IntervalPast12Months = &IntervalKey{"12_months", "last_12_months"}
|
||||||
IntervalWakatimeLast7DaysYesterday string = "Last 7 Days from Yesterday"
|
IntervalAny = &IntervalKey{"any"}
|
||||||
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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Intervals() []string {
|
var AllIntervals = []*IntervalKey{
|
||||||
return []string{
|
IntervalToday,
|
||||||
IntervalToday, IntervalYesterday, IntervalThisWeek, IntervalThisMonth, IntervalThisYear, IntervalPast7Days, IntervalPast30Days, IntervalPast12Months, IntervalAny,
|
IntervalYesterday,
|
||||||
}
|
IntervalThisWeek,
|
||||||
|
IntervalLastWeek,
|
||||||
|
IntervalThisMonth,
|
||||||
|
IntervalLastMonth,
|
||||||
|
IntervalThisYear,
|
||||||
|
IntervalPast7Days,
|
||||||
|
IntervalPast7DaysYesterday,
|
||||||
|
IntervalPast14Days,
|
||||||
|
IntervalPast30Days,
|
||||||
|
IntervalPast12Months,
|
||||||
|
IntervalAny,
|
||||||
}
|
}
|
||||||
|
|
||||||
const UnknownSummaryKey = "unknown"
|
const UnknownSummaryKey = "unknown"
|
||||||
|
@ -59,7 +59,9 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var interval = models.IntervalPast30Days
|
var interval = models.IntervalPast30Days
|
||||||
if groups := intervalReg.FindStringSubmatch(r.URL.Path); len(groups) > 1 {
|
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
|
var filters *models.Filters
|
||||||
@ -89,7 +91,7 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
utils.RespondJSON(w, http.StatusOK, vm)
|
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)
|
err, from, to := utils.ResolveInterval(interval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err, http.StatusBadRequest
|
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) {
|
func (h *StatsHandler) loadUserSummary(user *models.User, rangeKey string) (*models.Summary, error, int) {
|
||||||
var start, end time.Time
|
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
|
start, end = parsedFrom, parsedTo
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("invalid 'range' parameter"), http.StatusBadRequest
|
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
|
var start, end time.Time
|
||||||
if rangeParam != "" {
|
if rangeParam != "" {
|
||||||
// range param takes precedence
|
// 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
|
start, end = parsedFrom, parsedTo
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("invalid 'range' parameter"), http.StatusBadRequest
|
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
|
// also accept start param to be a range param
|
||||||
start, end = parsedFrom, parsedTo
|
start, end = parsedFrom, parsedTo
|
||||||
} else {
|
} else {
|
||||||
|
@ -7,35 +7,52 @@ import (
|
|||||||
"time"
|
"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()
|
to = time.Now()
|
||||||
|
|
||||||
switch interval {
|
switch interval {
|
||||||
case models.IntervalToday, models.IntervalWakatimeToday:
|
case models.IntervalToday:
|
||||||
from = StartOfToday()
|
from = StartOfToday()
|
||||||
case models.IntervalYesterday, models.IntervalWakatimeYesterday:
|
case models.IntervalYesterday:
|
||||||
from = StartOfToday().Add(-24 * time.Hour)
|
from = StartOfToday().Add(-24 * time.Hour)
|
||||||
to = StartOfToday()
|
to = StartOfToday()
|
||||||
case models.IntervalThisWeek, models.IntervalWakatimeThisWeek:
|
case models.IntervalThisWeek:
|
||||||
from = StartOfWeek()
|
from = StartOfWeek()
|
||||||
case models.IntervalWakatimeLastWeek:
|
case models.IntervalLastWeek:
|
||||||
from = StartOfWeek().AddDate(0, 0, -7)
|
from = StartOfWeek().AddDate(0, 0, -7)
|
||||||
to = StartOfWeek()
|
to = StartOfWeek()
|
||||||
case models.IntervalThisMonth, models.IntervalWakatimeThisMonth:
|
case models.IntervalThisMonth:
|
||||||
from = StartOfMonth()
|
from = StartOfMonth()
|
||||||
case models.IntervalWakatimeLastMonth:
|
case models.IntervalLastMonth:
|
||||||
from = StartOfMonth().AddDate(0, -1, 0)
|
from = StartOfMonth().AddDate(0, -1, 0)
|
||||||
to = StartOfMonth()
|
to = StartOfMonth()
|
||||||
case models.IntervalThisYear:
|
case models.IntervalThisYear:
|
||||||
from = StartOfYear()
|
from = StartOfYear()
|
||||||
case models.IntervalPast7Days, models.IntervalWakatimeLast7Days:
|
case models.IntervalPast7Days:
|
||||||
from = StartOfToday().AddDate(0, 0, -7)
|
from = StartOfToday().AddDate(0, 0, -7)
|
||||||
case models.IntervalWakatimeLast7DaysYesterday:
|
case models.IntervalPast7DaysYesterday:
|
||||||
from = StartOfToday().AddDate(0, 0, -1).AddDate(0, 0, -7)
|
from = StartOfToday().AddDate(0, 0, -1).AddDate(0, 0, -7)
|
||||||
to = StartOfToday().AddDate(0, 0, -1)
|
to = StartOfToday().AddDate(0, 0, -1)
|
||||||
case models.IntervalWakatimeLast14Days:
|
case models.IntervalPast14Days:
|
||||||
from = StartOfToday().AddDate(0, 0, -14)
|
from = StartOfToday().AddDate(0, 0, -14)
|
||||||
case models.IntervalPast30Days, models.IntervalWakatimeLast30Days:
|
case models.IntervalPast30Days:
|
||||||
from = StartOfToday().AddDate(0, 0, -30)
|
from = StartOfToday().AddDate(0, 0, -30)
|
||||||
case models.IntervalPast12Months:
|
case models.IntervalPast12Months:
|
||||||
from = StartOfToday().AddDate(0, -12, 0)
|
from = StartOfToday().AddDate(0, -12, 0)
|
||||||
@ -56,7 +73,7 @@ func ParseSummaryParams(r *http.Request) (*models.SummaryParams, error) {
|
|||||||
var from, to time.Time
|
var from, to time.Time
|
||||||
|
|
||||||
if interval := params.Get("interval"); interval != "" {
|
if interval := params.Get("interval"); interval != "" {
|
||||||
err, from, to = ResolveInterval(interval)
|
err, from, to = ResolveIntervalRaw(interval)
|
||||||
} else {
|
} else {
|
||||||
from, err = ParseDate(params.Get("from"))
|
from, err = ParseDate(params.Get("from"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user