2020-09-06 13:15:46 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-03-03 22:40:50 +03:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-11-08 12:12:49 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2021-02-06 22:09:08 +03:00
|
|
|
"github.com/muety/wakapi/middlewares"
|
2020-09-06 13:15:46 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-09-12 17:09:23 +03:00
|
|
|
v1 "github.com/muety/wakapi/models/compat/wakatime/v1"
|
2023-03-15 23:47:12 +03:00
|
|
|
"github.com/muety/wakapi/models/types"
|
2021-05-19 11:18:18 +03:00
|
|
|
routeutils "github.com/muety/wakapi/routes/utils"
|
2020-09-06 13:15:46 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
2021-12-26 19:02:14 +03:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2020-09-06 13:15:46 +03:00
|
|
|
)
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
type AllTimeHandler struct {
|
2020-11-08 12:12:49 +03:00
|
|
|
config *conf.Config
|
2021-02-06 22:09:08 +03:00
|
|
|
userSrvc services.IUserService
|
2020-11-08 12:12:49 +03:00
|
|
|
summarySrvc services.ISummaryService
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
|
2021-02-06 22:09:08 +03:00
|
|
|
func NewAllTimeHandler(userService services.IUserService, summaryService services.ISummaryService) *AllTimeHandler {
|
2020-09-12 17:09:23 +03:00
|
|
|
return &AllTimeHandler{
|
2021-02-06 22:09:08 +03:00
|
|
|
userSrvc: userService,
|
2020-09-06 13:15:46 +03:00
|
|
|
summarySrvc: summaryService,
|
2020-11-08 12:12:49 +03:00
|
|
|
config: conf.Get(),
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:40:50 +03:00
|
|
|
func (h *AllTimeHandler) RegisterRoutes(router chi.Router) {
|
|
|
|
router.Group(func(r chi.Router) {
|
|
|
|
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler)
|
|
|
|
r.Get("/compat/wakatime/v1/users/{user}/all_time_since_today", h.Get)
|
|
|
|
})
|
2021-01-30 12:34:52 +03:00
|
|
|
}
|
|
|
|
|
2021-02-07 13:54:07 +03:00
|
|
|
// @Summary Retrieve summary for all time
|
|
|
|
// @Description Mimics https://wakatime.com/developers#all_time_since_today
|
|
|
|
// @ID get-all-time
|
|
|
|
// @Tags wakatime
|
|
|
|
// @Produce json
|
|
|
|
// @Param user path string true "User ID to fetch data for (or 'current')"
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Success 200 {object} v1.AllTimeViewModel
|
2022-01-02 03:22:58 +03:00
|
|
|
// @Router /compat/wakatime/v1/users/{user}/all_time_since_today [get]
|
2021-02-03 23:28:02 +03:00
|
|
|
func (h *AllTimeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
2021-05-19 11:18:18 +03:00
|
|
|
user, err := routeutils.CheckEffectiveUser(w, r, h.userSrvc, "current")
|
|
|
|
if err != nil {
|
|
|
|
return // response was already sent by util function
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
|
2022-12-01 12:57:07 +03:00
|
|
|
summary, err, status := h.loadUserSummary(user, helpers.ParseSummaryFilters(r))
|
2020-09-06 13:15:46 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-26 19:02:14 +03:00
|
|
|
vm := v1.NewAllTimeFrom(summary)
|
2022-12-01 12:57:07 +03:00
|
|
|
helpers.RespondJSON(w, r, http.StatusOK, vm)
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
|
2021-12-26 19:02:14 +03:00
|
|
|
func (h *AllTimeHandler) loadUserSummary(user *models.User, filters *models.Filters) (*models.Summary, error, int) {
|
2020-09-06 13:15:46 +03:00
|
|
|
summaryParams := &models.SummaryParams{
|
|
|
|
From: time.Time{},
|
|
|
|
To: time.Now(),
|
|
|
|
User: user,
|
|
|
|
Recompute: false,
|
|
|
|
}
|
|
|
|
|
2023-03-15 23:47:12 +03:00
|
|
|
var retrieveSummary types.SummaryRetriever = h.summarySrvc.Retrieve
|
2020-11-07 14:01:35 +03:00
|
|
|
if summaryParams.Recompute {
|
|
|
|
retrieveSummary = h.summarySrvc.Summarize
|
|
|
|
}
|
|
|
|
|
2021-12-26 19:02:14 +03:00
|
|
|
summary, err := h.summarySrvc.Aliased(
|
|
|
|
summaryParams.From,
|
|
|
|
summaryParams.To,
|
|
|
|
summaryParams.User,
|
|
|
|
retrieveSummary,
|
|
|
|
filters,
|
|
|
|
summaryParams.Recompute,
|
|
|
|
)
|
2020-09-06 13:15:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil, http.StatusOK
|
|
|
|
}
|