2021-02-06 18:05:34 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
conf "github.com/muety/wakapi/config"
|
2021-02-06 22:09:08 +03:00
|
|
|
"github.com/muety/wakapi/middlewares"
|
2021-02-06 18:05:34 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
v1 "github.com/muety/wakapi/models/compat/wakatime/v1"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StatsHandler struct {
|
|
|
|
config *conf.Config
|
2021-02-06 22:09:08 +03:00
|
|
|
userSrvc services.IUserService
|
2021-02-06 18:05:34 +03:00
|
|
|
summarySrvc services.ISummaryService
|
|
|
|
}
|
|
|
|
|
2021-02-06 22:09:08 +03:00
|
|
|
func NewStatsHandler(userService services.IUserService, summaryService services.ISummaryService) *StatsHandler {
|
2021-02-06 18:05:34 +03:00
|
|
|
return &StatsHandler{
|
2021-02-06 22:09:08 +03:00
|
|
|
userSrvc: userService,
|
2021-02-06 18:05:34 +03:00
|
|
|
summarySrvc: summaryService,
|
|
|
|
config: conf.Get(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *StatsHandler) RegisterRoutes(router *mux.Router) {
|
2021-02-07 00:40:54 +03:00
|
|
|
r := router.PathPrefix("").Subrouter()
|
2021-02-06 22:09:08 +03:00
|
|
|
r.Use(
|
2021-02-07 00:32:03 +03:00
|
|
|
middlewares.NewAuthenticateMiddleware(h.userSrvc).WithOptionalFor([]string{"/"}).Handler,
|
2021-02-06 22:09:08 +03:00
|
|
|
)
|
2021-02-07 00:40:54 +03:00
|
|
|
r.Path("/v1/users/{user}/stats/{range}").Methods(http.MethodGet).HandlerFunc(h.Get)
|
|
|
|
r.Path("/compat/wakatime/v1/users/{user}/stats/{range}").Methods(http.MethodGet).HandlerFunc(h.Get)
|
2021-02-12 12:29:01 +03:00
|
|
|
|
|
|
|
// Also works without range, see https://github.com/anuraghazra/github-readme-stats/issues/865#issuecomment-776186592
|
|
|
|
r.Path("/v1/users/{user}/stats").Methods(http.MethodGet).HandlerFunc(h.Get)
|
|
|
|
r.Path("/compat/wakatime/v1/users/{user}/stats").Methods(http.MethodGet).HandlerFunc(h.Get)
|
2021-02-06 18:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support filtering (requires https://github.com/muety/wakapi/issues/108)
|
|
|
|
|
2021-04-30 11:12:28 +03:00
|
|
|
// @Summary Retrieve statistics for a given user
|
|
|
|
// @Description Mimics https://wakatime.com/developers#stats
|
|
|
|
// @ID get-wakatimes-tats
|
|
|
|
// @Tags wakatime
|
|
|
|
// @Produce json
|
|
|
|
// @Param user path string true "User ID to fetch data for (or 'current')"
|
|
|
|
// @Param range query string false "Range interval identifier" Enums(today, yesterday, week, month, year, 7_days, last_7_days, 30_days, last_30_days, 12_months, last_12_months, any)
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Success 200 {object} v1.StatsViewModel
|
|
|
|
// @Router /compat/wakatime/v1/users/{user}/stats/{range} [get]
|
2021-02-06 18:05:34 +03:00
|
|
|
func (h *StatsHandler) Get(w http.ResponseWriter, r *http.Request) {
|
2021-02-07 00:32:03 +03:00
|
|
|
var vars = mux.Vars(r)
|
|
|
|
var authorizedUser, requestedUser *models.User
|
2021-02-06 18:05:34 +03:00
|
|
|
|
2021-03-26 15:10:10 +03:00
|
|
|
authorizedUser = middlewares.GetPrincipal(r)
|
2021-02-07 00:32:03 +03:00
|
|
|
if authorizedUser != nil && vars["user"] == "current" {
|
|
|
|
vars["user"] = authorizedUser.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedUser, err := h.userSrvc.GetUserById(vars["user"])
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte("user not found"))
|
|
|
|
return
|
|
|
|
}
|
2021-02-06 18:05:34 +03:00
|
|
|
|
2021-02-12 12:29:01 +03:00
|
|
|
rangeParam := vars["range"]
|
|
|
|
if rangeParam == "" {
|
|
|
|
rangeParam = (*models.IntervalPast7Days)[0]
|
|
|
|
}
|
|
|
|
|
2021-04-25 15:15:18 +03:00
|
|
|
err, rangeFrom, rangeTo := utils.ResolveIntervalRawTZ(rangeParam, requestedUser.TZ())
|
2021-02-07 00:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("invalid range"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-11 18:41:45 +03:00
|
|
|
minStart := rangeTo.Add(-24 * time.Hour * time.Duration(requestedUser.ShareDataMaxDays))
|
2021-02-07 00:32:03 +03:00
|
|
|
if (authorizedUser == nil || requestedUser.ID != authorizedUser.ID) &&
|
2021-02-07 01:23:26 +03:00
|
|
|
rangeFrom.Before(minStart) && requestedUser.ShareDataMaxDays >= 0 {
|
2021-02-06 18:05:34 +03:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2021-02-07 00:32:03 +03:00
|
|
|
w.Write([]byte("requested time range too broad"))
|
2021-02-06 18:05:34 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-07 00:32:03 +03:00
|
|
|
summary, err, status := h.loadUserSummary(requestedUser, rangeFrom, rangeTo)
|
2021-02-06 18:05:34 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-07 00:32:03 +03:00
|
|
|
stats := v1.NewStatsFrom(summary, &models.Filters{})
|
|
|
|
|
|
|
|
// post filter stats according to user's given sharing permissions
|
|
|
|
if !requestedUser.ShareEditors {
|
|
|
|
stats.Data.Editors = nil
|
|
|
|
}
|
|
|
|
if !requestedUser.ShareLanguages {
|
|
|
|
stats.Data.Languages = nil
|
|
|
|
}
|
|
|
|
if !requestedUser.ShareProjects {
|
|
|
|
stats.Data.Projects = nil
|
|
|
|
}
|
|
|
|
if !requestedUser.ShareOSs {
|
|
|
|
stats.Data.OperatingSystems = nil
|
|
|
|
}
|
|
|
|
if !requestedUser.ShareMachines {
|
|
|
|
stats.Data.Machines = nil
|
2021-02-06 18:05:34 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 22:26:47 +03:00
|
|
|
utils.RespondJSON(w, r, http.StatusOK, stats)
|
2021-02-06 18:05:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-07 00:32:03 +03:00
|
|
|
func (h *StatsHandler) loadUserSummary(user *models.User, start, end time.Time) (*models.Summary, error, int) {
|
2021-02-06 18:05:34 +03:00
|
|
|
overallParams := &models.SummaryParams{
|
|
|
|
From: start,
|
|
|
|
To: end,
|
|
|
|
User: user,
|
|
|
|
Recompute: false,
|
|
|
|
}
|
|
|
|
|
2021-03-25 01:31:04 +03:00
|
|
|
summary, err := h.summarySrvc.Aliased(overallParams.From, overallParams.To, user, h.summarySrvc.Retrieve, false)
|
2021-02-06 18:05:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil, http.StatusOK
|
|
|
|
}
|