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

feat: add more pre-configured intervals (resolve #51)

This commit is contained in:
Ferdinand Mütsch 2020-09-12 12:40:38 +02:00
parent fde8c35362
commit 19a8c61f77
5 changed files with 49 additions and 32 deletions

View File

@ -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

View File

@ -15,10 +15,13 @@ const (
const (
IntervalToday string = "today"
IntervalLastDay string = "day"
IntervalLastWeek string = "week"
IntervalLastMonth string = "month"
IntervalLastYear string = "year"
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"
)

View File

@ -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")
}
}
live := (params.Get("live") != "" && params.Get("live") != "false") || interval == models.IntervalToday
to, err = ParseDate(params.Get("to"))
if err != nil {
return nil, errors.New("missing 'to' parameter")
}
}
recompute := params.Get("recompute") != "" && params.Get("recompute") != "false"
to := StartOfToday()
if live {
to = time.Now()
}
return &models.SummaryParams{
From: from,
To: to,

View File

@ -1 +1 @@
1.10.2
1.10.3

View File

@ -36,13 +36,16 @@
<h1 class="font-semibold text-2xl text-white m-0 border-b-4 border-green-700">Your Coding Statistics 🤓</h1>
</div>
<div class="text-white text-sm flex items-center justify-center mt-4">
<a href="summary?interval=today" class="m-1 border-b border-green-700">Today (live)</a>
<a href="summary?interval=day" class="m-1 border-b border-green-700">Yesterday</a>
<a href="summary?interval=week" class="m-1 border-b border-green-700">This Week</a>
<a href="summary?interval=month" class="m-1 border-b border-green-700">This Month</a>
<a href="summary?interval=year" class="m-1 border-b border-green-700">This Year</a>
<a href="summary?interval=any" class="m-1 border-b border-green-700">All Time</a>
<div class="text-white text-sm flex items-center justify-center mt-4 self-center max-w-lg flex-wrap">
<a href="summary?interval=today" class="mx-2 my-1 border-b border-green-700">Today</a>
<a href="summary?interval=day" class="mx-2 my-1 border-b border-green-700">Yesterday</a>
<a href="summary?interval=week" class="mx-2 my-1 border-b border-green-700">This Week</a>
<a href="summary?interval=month" class="mx-2 my-1 border-b border-green-700">This Month</a>
<a href="summary?interval=year" class="mx-2 my-1 border-b border-green-700">This Year</a>
<a href="summary?interval=7_days" class="mx-2 my-1 border-b border-green-700">Past 7 Days</a>
<a href="summary?interval=30_days" class="mx-2 my-1 border-b border-green-700">Past 30 Days</a>
<a href="summary?interval=12_months" class="mx-2 my-1 border-b border-green-700">Past 12 Months</a>
<a href="summary?interval=any" class="mx-2 my-1 border-b border-green-700">All Time</a>
</div>
{{ template "alerts.tpl.html" . }}