2022-01-28 14:28:47 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-03-25 14:48:56 +03:00
|
|
|
"github.com/duke-git/lancet/v2/datetime"
|
2023-03-03 22:40:50 +03:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2022-01-28 14:28:47 +03:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/middlewares"
|
|
|
|
wakatime "github.com/muety/wakapi/models/compat/wakatime/v1"
|
|
|
|
routeutils "github.com/muety/wakapi/routes/utils"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HeartbeatsResult struct {
|
2022-02-13 12:59:14 +03:00
|
|
|
Data []*wakatime.HeartbeatEntry `json:"data"`
|
|
|
|
End string `json:"end"`
|
|
|
|
Start string `json:"start"`
|
|
|
|
Timezone string `json:"timezone"`
|
2022-01-28 14:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type HeartbeatHandler struct {
|
|
|
|
userSrvc services.IUserService
|
|
|
|
heartbeatSrvc services.IHeartbeatService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHeartbeatHandler(userService services.IUserService, heartbeatService services.IHeartbeatService) *HeartbeatHandler {
|
|
|
|
return &HeartbeatHandler{
|
|
|
|
userSrvc: userService,
|
|
|
|
heartbeatSrvc: heartbeatService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:40:50 +03:00
|
|
|
func (h *HeartbeatHandler) RegisterRoutes(router chi.Router) {
|
|
|
|
router.Group(func(r chi.Router) {
|
|
|
|
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler)
|
|
|
|
r.Get("/compat/wakatime/v1/users/{user}/heartbeats", h.Get)
|
|
|
|
})
|
2022-01-28 14:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Get heartbeats of user for specified date
|
|
|
|
// @ID get-heartbeats
|
|
|
|
// @Tags heartbeat
|
|
|
|
// @Param date query string true "Date"
|
|
|
|
// @Param user path string true "Username (or current)"
|
|
|
|
// @Security ApiKeyAuth
|
2022-02-13 12:59:14 +03:00
|
|
|
// @Success 200 {object} HeartbeatsResult
|
2022-01-28 14:28:47 +03:00
|
|
|
// @Failure 400 {string} string "bad date"
|
|
|
|
// @Router /compat/wakatime/v1/users/{user}/heartbeats [get]
|
|
|
|
func (h *HeartbeatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, err := routeutils.CheckEffectiveUser(w, r, h.userSrvc, "current")
|
|
|
|
if err != nil {
|
|
|
|
return // response was already sent by util function
|
|
|
|
}
|
|
|
|
|
|
|
|
params := r.URL.Query()
|
|
|
|
dateParam := params.Get("date")
|
|
|
|
date, err := time.Parse(conf.SimpleDateFormat, dateParam)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("bad date"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
timezone := user.TZ()
|
2022-03-25 14:48:56 +03:00
|
|
|
rangeFrom, rangeTo := datetime.BeginOfDay(date.In(timezone)), datetime.EndOfDay(date.In(timezone))
|
2022-01-28 14:28:47 +03:00
|
|
|
|
|
|
|
heartbeats, err := h.heartbeatSrvc.GetAllWithin(rangeFrom, rangeTo, user)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(conf.ErrInternalServerError))
|
|
|
|
conf.Log().Request(r).Error("failed to retrieve heartbeats - %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := HeartbeatsResult{
|
2022-02-13 12:59:14 +03:00
|
|
|
Data: wakatime.HeartbeatsToCompat(heartbeats),
|
2022-01-28 14:28:47 +03:00
|
|
|
Start: rangeFrom.UTC().Format(time.RFC3339),
|
|
|
|
End: rangeTo.UTC().Format(time.RFC3339),
|
|
|
|
Timezone: timezone.String(),
|
|
|
|
}
|
2022-12-01 12:57:07 +03:00
|
|
|
helpers.RespondJSON(w, r, http.StatusOK, res)
|
2022-01-28 14:28:47 +03:00
|
|
|
}
|