1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: add missing query params to wakatime endpoints (resolve #109)

This commit is contained in:
Ferdinand Mütsch 2021-01-31 16:23:47 +01:00
parent 979549448c
commit 3e5a51c272
6 changed files with 57 additions and 47 deletions

View File

@ -22,7 +22,7 @@ type BadgeData struct {
func NewBadgeDataFrom(summary *models.Summary, filters *models.Filters) *BadgeData { func NewBadgeDataFrom(summary *models.Summary, filters *models.Filters) *BadgeData {
var total time.Duration var total time.Duration
if hasFilter, _, _ := filters.First(); hasFilter { if hasFilter, _, _ := filters.One(); hasFilter {
total = summary.TotalTimeByFilters(filters) total = summary.TotalTimeByFilters(filters)
} else { } else {
total = summary.TotalTime() total = summary.TotalTime()

View File

@ -29,7 +29,7 @@ func NewFiltersWith(entity uint8, key string) *Filters {
return &Filters{} return &Filters{}
} }
func (f *Filters) First() (bool, uint8, string) { func (f *Filters) One() (bool, uint8, string) {
if f.Project != "" { if f.Project != "" {
return true, SummaryProject, f.Project return true, SummaryProject, f.Project
} else if f.OS != "" { } else if f.OS != "" {
@ -43,25 +43,3 @@ func (f *Filters) First() (bool, uint8, string) {
} }
return false, 0, "" return false, 0, ""
} }
func (f *Filters) All() []*FilterElement {
all := make([]*FilterElement, 0)
if f.Project != "" {
all = append(all, &FilterElement{Type: SummaryProject, Key: f.Project})
}
if f.Editor != "" {
all = append(all, &FilterElement{Type: SummaryEditor, Key: f.Editor})
}
if f.Language != "" {
all = append(all, &FilterElement{Type: SummaryLanguage, Key: f.Language})
}
if f.Machine != "" {
all = append(all, &FilterElement{Type: SummaryMachine, Key: f.Machine})
}
if f.OS != "" {
all = append(all, &FilterElement{Type: SummaryOS, Key: f.OS})
}
return all
}

View File

@ -24,6 +24,18 @@ const (
IntervalPast30Days string = "30_days" IntervalPast30Days string = "30_days"
IntervalPast12Months string = "12_months" IntervalPast12Months string = "12_months"
IntervalAny string = "any" 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"
) )
func Intervals() []string { func Intervals() []string {
@ -188,11 +200,12 @@ func (s *Summary) TotalTimeByKey(entityType uint8, key string) (timeSum time.Dur
return timeSum return timeSum
} }
func (s *Summary) TotalTimeByFilters(filter *Filters) (timeSum time.Duration) { func (s *Summary) TotalTimeByFilters(filters *Filters) (timeSum time.Duration) {
for _, f := range filter.All() { do, typeId, key := filters.One()
timeSum += s.TotalTimeByKey(f.Type, f.Key) if do {
return s.TotalTimeByKey(typeId, key)
} }
return timeSum return s.TotalTime()
} }
func (s *Summary) WithResolvedAliases(resolve AliasResolver) *Summary { func (s *Summary) WithResolvedAliases(resolve AliasResolver) *Summary {

View File

@ -31,11 +31,10 @@ func (h *SummariesHandler) RegisterAPIRoutes(router *mux.Router) {
router.Path("/summaries").Methods(http.MethodGet).HandlerFunc(h.ApiGet) router.Path("/summaries").Methods(http.MethodGet).HandlerFunc(h.ApiGet)
} }
/* // TODO: Support parameters: project, branches, timeout, writes_only, timezone
TODO: support parameters: project, branches, timeout, writes_only, timezone // See https://wakatime.com/developers#summaries.
https://wakatime.com/developers#summaries // Timezone can be specified via an offset suffix (e.g. +02:00) in date strings.
timezone can be specified via an offset suffix (e.g. +02:00) in date strings // Requires https://github.com/muety/wakapi/issues/108.
*/
func (h *SummariesHandler) ApiGet(w http.ResponseWriter, r *http.Request) { func (h *SummariesHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
@ -54,28 +53,36 @@ func (h *SummariesHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
return return
} }
vm := v1.NewSummariesFrom(summaries, &models.Filters{}) filters := &models.Filters{}
if projectQuery := r.URL.Query().Get("project"); projectQuery != "" {
filters.Project = projectQuery
}
vm := v1.NewSummariesFrom(summaries, filters)
utils.RespondJSON(w, http.StatusOK, vm) utils.RespondJSON(w, http.StatusOK, vm)
} }
func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary, error, int) { func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary, error, int) {
user := r.Context().Value(models.UserKey).(*models.User) user := r.Context().Value(models.UserKey).(*models.User)
params := r.URL.Query() params := r.URL.Query()
rangeParam, startParam, endParam := params.Get("range"), params.Get("start"), params.Get("end")
var start, end time.Time var start, end time.Time
// TODO: find out what other special dates are supported by wakatime (e.g. tomorrow, yesterday, ...?) if rangeParam != "" {
if startKey, endKey := params.Get("start"), params.Get("end"); startKey == "today" && startKey == endKey { if err, parsedFrom, parsedTo := utils.ResolveInterval(rangeParam); err == nil {
start = utils.StartOfToday() start, end = parsedFrom, parsedTo
end = time.Now() } else {
return nil, errors.New("invalid 'range' parameter"), http.StatusBadRequest
}
} else { } else {
var err error var err error
start, err = time.Parse(time.RFC3339, strings.Replace(startKey, " ", "+", 1)) start, err = time.Parse(time.RFC3339, strings.Replace(startParam, " ", "+", 1))
if err != nil { if err != nil {
return nil, errors.New("missing required 'start' parameter"), http.StatusBadRequest return nil, errors.New("missing required 'start' parameter"), http.StatusBadRequest
} }
end, err = time.Parse(time.RFC3339, strings.Replace(endKey, " ", "+", 1)) end, err = time.Parse(time.RFC3339, strings.Replace(endParam, " ", "+", 1))
if err != nil { if err != nil {
return nil, errors.New("missing required 'end' parameter"), http.StatusBadRequest return nil, errors.New("missing required 'end' parameter"), http.StatusBadRequest
} }

View File

@ -11,21 +11,33 @@ func ResolveInterval(interval string) (err error, from, to time.Time) {
to = time.Now() to = time.Now()
switch interval { switch interval {
case models.IntervalToday: case models.IntervalToday, models.IntervalWakatimeToday:
from = StartOfToday() from = StartOfToday()
case models.IntervalYesterday: case models.IntervalYesterday, models.IntervalWakatimeYesterday:
from = StartOfToday().Add(-24 * time.Hour) from = StartOfToday().Add(-24 * time.Hour)
to = StartOfToday() to = StartOfToday()
case models.IntervalThisWeek: case models.IntervalThisWeek, models.IntervalWakatimeThisWeek:
from = StartOfWeek() from = StartOfWeek()
case models.IntervalThisMonth: case models.IntervalWakatimeLastWeek:
from = StartOfWeek().AddDate(0, 0, -7)
to = StartOfWeek()
case models.IntervalThisMonth, models.IntervalWakatimeThisMonth:
from = StartOfMonth() from = StartOfMonth()
case models.IntervalWakatimeLastMonth:
from = StartOfMonth().AddDate(0, -1, 0)
to = StartOfMonth()
case models.IntervalThisYear: case models.IntervalThisYear:
from = StartOfYear() from = StartOfYear()
case models.IntervalPast7Days: case models.IntervalPast7Days, models.IntervalWakatimeLast7Days:
from = StartOfToday().AddDate(0, 0, -7) from = StartOfToday().AddDate(0, 0, -7)
case models.IntervalPast30Days: 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:
from = StartOfToday().AddDate(0, 0, -30) from = StartOfToday().AddDate(0, 0, -30)
from = StartOfToday().AddDate(0, -6, 0)
case models.IntervalPast12Months: case models.IntervalPast12Months:
from = StartOfToday().AddDate(0, -12, 0) from = StartOfToday().AddDate(0, -12, 0)
case models.IntervalAny: case models.IntervalAny:

View File

@ -1 +1 @@
1.20.3 1.21.0