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

87 lines
2.6 KiB
Go
Raw Normal View History

package v1
import (
"github.com/duke-git/lancet/v2/datetime"
"github.com/muety/wakapi/helpers"
"net/http"
"time"
"github.com/gorilla/mux"
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"`
}
type HeartbeatHandler struct {
userSrvc services.IUserService
heartbeatSrvc services.IHeartbeatService
}
func NewHeartbeatHandler(userService services.IUserService, heartbeatService services.IHeartbeatService) *HeartbeatHandler {
return &HeartbeatHandler{
userSrvc: userService,
heartbeatSrvc: heartbeatService,
}
}
func (h *HeartbeatHandler) RegisterRoutes(router *mux.Router) {
r := router.PathPrefix("").Subrouter()
r.Use(
middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler,
)
r.Path("/compat/wakatime/v1/users/{user}/heartbeats").Methods(http.MethodGet).HandlerFunc(h.Get)
}
// @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
// @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()
rangeFrom, rangeTo := datetime.BeginOfDay(date.In(timezone)), datetime.EndOfDay(date.In(timezone))
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),
Start: rangeFrom.UTC().Format(time.RFC3339),
End: rangeTo.UTC().Format(time.RFC3339),
Timezone: timezone.String(),
}
helpers.RespondJSON(w, r, http.StatusOK, res)
}