2019-05-06 01:40:41 +03:00
|
|
|
package routes
|
2019-05-05 23:36:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-11-01 22:14:10 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2019-05-05 23:36:49 +03:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
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
|
|
|
)
|
|
|
|
|
2019-05-06 01:40:41 +03:00
|
|
|
type HeartbeatHandler struct {
|
2020-11-01 22:14:10 +03:00
|
|
|
config *conf.Config
|
|
|
|
heartbeatSrvc *services.HeartbeatService
|
|
|
|
languageMappingSrvc *services.LanguageMappingService
|
2019-05-06 01:40:41 +03:00
|
|
|
}
|
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
func NewHeartbeatHandler(heartbeatService *services.HeartbeatService, languageMappingService *services.LanguageMappingService) *HeartbeatHandler {
|
2020-05-24 14:41:19 +03:00
|
|
|
return &HeartbeatHandler{
|
2020-11-01 22:14:10 +03:00
|
|
|
config: conf.Get(),
|
|
|
|
heartbeatSrvc: heartbeatService,
|
|
|
|
languageMappingSrvc: languageMappingService,
|
2019-05-05 23:36:49 +03:00
|
|
|
}
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
2019-05-09 01:07:38 +03:00
|
|
|
|
2020-08-29 22:13:56 +03:00
|
|
|
type heartbeatResponseVm struct {
|
|
|
|
Responses [][]interface{} `json:"responses"`
|
|
|
|
}
|
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
func (h *HeartbeatHandler) ApiPost(w http.ResponseWriter, r *http.Request) {
|
2019-05-17 09:40:03 +03:00
|
|
|
var heartbeats []*models.Heartbeat
|
2019-05-11 18:49:56 +03:00
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
2019-05-09 01:07:38 +03:00
|
|
|
opSys, editor, _ := utils.ParseUserAgent(r.Header.Get("User-Agent"))
|
2020-08-29 22:20:23 +03:00
|
|
|
machineName := r.Header.Get("X-Machine-Name")
|
2019-05-09 01:07:38 +03:00
|
|
|
|
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 {
|
2019-05-19 20:49:27 +03:00
|
|
|
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
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
/*languageMappings, err := h.languageMappingSrvc.ResolveByUser(user.ID)
|
2020-10-25 09:22:10 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
2020-11-01 22:14:10 +03:00
|
|
|
}*/
|
2020-10-25 09:22:10 +03:00
|
|
|
|
2019-05-21 18:16:46 +03:00
|
|
|
for _, hb := range heartbeats {
|
|
|
|
hb.OperatingSystem = opSys
|
|
|
|
hb.Editor = editor
|
2020-08-29 22:20:23 +03:00
|
|
|
hb.Machine = machineName
|
2019-05-21 18:16:46 +03:00
|
|
|
hb.User = user
|
|
|
|
hb.UserID = user.ID
|
|
|
|
|
|
|
|
if !hb.Valid() {
|
2019-05-19 20:49:27 +03:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2019-05-11 18:49:56 +03:00
|
|
|
w.Write([]byte("Invalid heartbeat object."))
|
|
|
|
return
|
|
|
|
}
|
2019-05-09 01:07:38 +03:00
|
|
|
}
|
2019-05-05 23:36:49 +03:00
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
if err := h.heartbeatSrvc.InsertBatch(heartbeats); err != nil {
|
2019-05-19 20:49:27 +03:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-05-05 23:36:49 +03:00
|
|
|
os.Stderr.WriteString(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-29 22:13:56 +03:00
|
|
|
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
|
|
|
}
|