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

feat: add wakatime-compatible alltime endpoint

This commit is contained in:
Ferdinand Mütsch
2020-09-06 12:15:46 +02:00
parent 97cb29ee4d
commit 587ac6a330
9 changed files with 216 additions and 64 deletions

View File

@@ -1,6 +1,9 @@
package utils
import "time"
import (
"fmt"
"time"
)
func StartOfDay() time.Time {
ref := time.Now()
@@ -23,6 +26,14 @@ func StartOfYear() time.Time {
return time.Date(ref.Year(), time.January, 1, 0, 0, 0, 0, ref.Location())
}
func FmtWakatimeDuration(d time.Duration) string {
d = d.Round(time.Minute)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
return fmt.Sprintf("%d hrs %d mins", h, m)
}
// https://stackoverflow.com/a/18632496
func firstDayOfISOWeek(year int, week int, timezone *time.Location) time.Time {
date := time.Date(year, 0, 0, 0, 0, 0, 0, timezone)

52
utils/summary.go Normal file
View File

@@ -0,0 +1,52 @@
package utils
import (
"errors"
"github.com/muety/wakapi/models"
"net/http"
"time"
)
func ParseSummaryParams(r *http.Request) (*models.SummaryParams, error) {
user := r.Context().Value(models.UserKey).(*models.User)
params := r.URL.Query()
interval := params.Get("interval")
from, err := ParseDate(params.Get("from"))
if err != nil {
switch interval {
case models.IntervalToday:
from = StartOfDay()
case models.IntervalLastDay:
from = StartOfDay().Add(-24 * time.Hour)
case models.IntervalLastWeek:
from = StartOfWeek()
case models.IntervalLastMonth:
from = StartOfMonth()
case models.IntervalLastYear:
from = StartOfYear()
case models.IntervalAny:
from = time.Time{}
default:
return nil, errors.New("missing 'from' parameter")
}
}
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == models.IntervalToday
recompute := params.Get("recompute") != "" && params.Get("recompute") != "false"
to := StartOfDay()
if live {
to = time.Now()
}
return &models.SummaryParams{
From: from,
To: to,
User: user,
Recompute: recompute,
}, nil
}