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

119 lines
3.4 KiB
Go
Raw Normal View History

package api
2019-05-05 23:36:49 +03:00
import (
"encoding/json"
"github.com/gorilla/mux"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/middlewares"
customMiddleware "github.com/muety/wakapi/middlewares/custom"
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils"
"net/http"
2019-05-06 01:40:41 +03:00
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/models"
2019-05-05 23:36:49 +03:00
)
type HeartbeatApiHandler struct {
config *conf.Config
userSrvc services.IUserService
heartbeatSrvc services.IHeartbeatService
languageMappingSrvc services.ILanguageMappingService
2019-05-06 01:40:41 +03:00
}
func NewHeartbeatApiHandler(userService services.IUserService, heartbeatService services.IHeartbeatService, languageMappingService services.ILanguageMappingService) *HeartbeatApiHandler {
return &HeartbeatApiHandler{
config: conf.Get(),
userSrvc: userService,
heartbeatSrvc: heartbeatService,
languageMappingSrvc: languageMappingService,
2019-05-05 23:36:49 +03:00
}
}
type heartbeatResponseVm struct {
Responses [][]interface{} `json:"responses"`
}
func (h *HeartbeatApiHandler) RegisterRoutes(router *mux.Router) {
r := router.PathPrefix("/heartbeat").Subrouter()
r.Use(
middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler,
customMiddleware.NewWakatimeRelayMiddleware().Handler,
)
2021-02-05 16:50:00 +03:00
r.Methods(http.MethodPost).HandlerFunc(h.Post)
}
2021-02-07 13:54:07 +03:00
// @Summary Push a new heartbeat
// @ID post-heartbeat
// @Tags heartbeat
// @Accept json
// @Param heartbeat body models.Heartbeat true "A heartbeat"
// @Security ApiKeyAuth
// @Success 201
// @Router /heartbeat [post]
func (h *HeartbeatApiHandler) Post(w http.ResponseWriter, r *http.Request) {
var heartbeats []*models.Heartbeat
user := middlewares.GetPrincipal(r)
opSys, editor, _ := utils.ParseUserAgent(r.Header.Get("User-Agent"))
machineName := r.Header.Get("X-Machine-Name")
2019-05-05 23:36:49 +03:00
dec := json.NewDecoder(r.Body)
2019-05-11 18:49:56 +03:00
if err := dec.Decode(&heartbeats); err != nil {
w.WriteHeader(http.StatusBadRequest)
2019-05-05 23:36:49 +03:00
w.Write([]byte(err.Error()))
return
}
2019-05-11 18:49:56 +03:00
2019-05-21 18:16:46 +03:00
for _, hb := range heartbeats {
hb.OperatingSystem = opSys
hb.Editor = editor
hb.Machine = machineName
2019-05-21 18:16:46 +03:00
hb.User = user
hb.UserID = user.ID
if !hb.Valid() {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("invalid heartbeat object"))
2019-05-11 18:49:56 +03:00
return
}
2021-01-31 19:46:50 +03:00
hb.Hashed()
}
2019-05-05 23:36:49 +03:00
if err := h.heartbeatSrvc.InsertBatch(heartbeats); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(conf.ErrInternalServerError))
2021-04-16 16:59:39 +03:00
conf.Log().Request(r).Error("failed to batch-insert heartbeats %v", err)
2019-05-05 23:36:49 +03:00
return
}
if !user.HasData {
user.HasData = true
if _, err := h.userSrvc.Update(user); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(conf.ErrInternalServerError))
2021-04-16 16:59:39 +03:00
conf.Log().Request(r).Error("failed to update user %v", err)
return
}
}
utils.RespondJSON(w, http.StatusCreated, constructSuccessResponse(len(heartbeats)))
}
// construct weird response format (see https://github.com/wakatime/wakatime/blob/2e636d389bf5da4e998e05d5285a96ce2c181e3d/wakatime/api.py#L288)
// to make the cli consider all heartbeats to having been successfully saved
// response looks like: { "responses": [ [ { "data": {...} }, 201 ], ... ] }
func constructSuccessResponse(n int) *heartbeatResponseVm {
responses := make([][]interface{}, n)
for i := 0; i < n; i++ {
r := make([]interface{}, 2)
r[0] = nil
r[1] = http.StatusCreated
responses[i] = r
}
return &heartbeatResponseVm{
Responses: responses,
}
2019-05-05 23:36:49 +03:00
}