From 19a8c61f77b2d4d6967dce825bac3d6d2af6a477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sat, 12 Sep 2020 12:40:38 +0200 Subject: [PATCH] feat: add more pre-configured intervals (resolve #51) --- README.md | 5 +---- models/summary.go | 15 +++++++++------ utils/summary.go | 42 ++++++++++++++++++++++++++++-------------- version.txt | 2 +- views/summary.tpl.html | 17 ++++++++++------- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 7aa2b9c..aa9cdc2 100644 --- a/README.md +++ b/README.md @@ -80,17 +80,14 @@ INSERT INTO aliases (`type`, `user_id`, `key`, `value`) VALUES (0, 'your_usernam * OS ~ type **3** * Machine ~ type **4** -**NOTE:** In order for the aliases to take effect for non-live statistics, you would either have to wait 24 hours for the cache to be invalidated or restart Wakapi. - ## API Endpoints The following API endpoints are available. A more detailed Swagger documentation is about to come ([#40](https://github.com/muety/wakapi/issues/40)). * `POST /api/heartbeat` * `GET /api/summary` * `string` parameter `interval`: One of `today`, `day`, `week`, `month`, `year`, `any` - * `bool` parameter `live`: Whether to compute the summary to present time * `GET /api/compat/v1/users/current/all_time_since_today` (see [Wakatime API docs](https://wakatime.com/developers#all_time_since_today)) -* `GET /api/compat/v1/users/current/summaries` (see [Wakatime API docs](https://wakatime.com/developers#summaries)) (⏳ [coming soon](https://github.com/muety/wakapi/issues/44)) +* `GET /api/compat/v1/users/current/summaries` (see [Wakatime API docs](https://wakatime.com/developers#summaries)) * `GET /api/health` ## Prometheus Export diff --git a/models/summary.go b/models/summary.go index 2c4d333..46ab044 100644 --- a/models/summary.go +++ b/models/summary.go @@ -14,12 +14,15 @@ const ( ) const ( - IntervalToday string = "today" - IntervalLastDay string = "day" - IntervalLastWeek string = "week" - IntervalLastMonth string = "month" - IntervalLastYear string = "year" - IntervalAny string = "any" + IntervalToday string = "today" + IntervalYesterday string = "day" + IntervalThisWeek string = "week" + IntervalThisMonth string = "month" + IntervalThisYear string = "year" + IntervalPast7Days string = "7_days" + IntervalPast30Days string = "30_days" + IntervalPast12Months string = "12_months" + IntervalAny string = "any" ) const UnknownSummaryKey = "unknown" diff --git a/utils/summary.go b/utils/summary.go index c85ee10..9f45ca1 100644 --- a/utils/summary.go +++ b/utils/summary.go @@ -10,37 +10,51 @@ import ( 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 { + var from, to time.Time + + if interval := params.Get("interval"); interval != "" { + to = time.Now() + switch interval { case models.IntervalToday: from = StartOfToday() - case models.IntervalLastDay: + case models.IntervalYesterday: from = StartOfToday().Add(-24 * time.Hour) - case models.IntervalLastWeek: + to = StartOfToday() + case models.IntervalThisWeek: from = StartOfWeek() - case models.IntervalLastMonth: + case models.IntervalThisMonth: from = StartOfMonth() - case models.IntervalLastYear: + case models.IntervalThisYear: from = StartOfYear() + case models.IntervalPast7Days: + from = StartOfToday().AddDate(0, 0, -7) + case models.IntervalPast30Days: + from = StartOfToday().AddDate(0, 0, -30) + case models.IntervalPast12Months: + from = StartOfToday().AddDate(0, -12, 0) case models.IntervalAny: from = time.Time{} default: + return nil, errors.New("invalid interval") + } + } else { + var err error + + from, err = ParseDate(params.Get("from")) + if err != nil { return nil, errors.New("missing 'from' parameter") } + + to, err = ParseDate(params.Get("to")) + if err != nil { + return nil, errors.New("missing 'to' parameter") + } } - live := (params.Get("live") != "" && params.Get("live") != "false") || interval == models.IntervalToday - recompute := params.Get("recompute") != "" && params.Get("recompute") != "false" - to := StartOfToday() - if live { - to = time.Now() - } - return &models.SummaryParams{ From: from, To: to, diff --git a/version.txt b/version.txt index 70ad429..62321af 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.10.2 \ No newline at end of file +1.10.3 \ No newline at end of file diff --git a/views/summary.tpl.html b/views/summary.tpl.html index de2782d..24ba2a5 100644 --- a/views/summary.tpl.html +++ b/views/summary.tpl.html @@ -36,13 +36,16 @@

Your Coding Statistics 🤓

-
- Today (live) - Yesterday - This Week - This Month - This Year - All Time + {{ template "alerts.tpl.html" . }}