2021-02-03 23:28:02 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
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-02 22:04:29 +03:00
|
|
|
routeutils "github.com/muety/wakapi/routes/utils"
|
2022-01-02 03:22:58 +03:00
|
|
|
"net/http"
|
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2021-02-06 22:09:08 +03:00
|
|
|
"github.com/muety/wakapi/middlewares"
|
2021-02-03 23:28:02 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SummaryApiHandler struct {
|
|
|
|
config *conf.Config
|
2021-02-06 22:09:08 +03:00
|
|
|
userSrvc services.IUserService
|
2021-02-03 23:28:02 +03:00
|
|
|
summarySrvc services.ISummaryService
|
|
|
|
}
|
|
|
|
|
2021-02-06 22:09:08 +03:00
|
|
|
func NewSummaryApiHandler(userService services.IUserService, summaryService services.ISummaryService) *SummaryApiHandler {
|
2021-02-03 23:28:02 +03:00
|
|
|
return &SummaryApiHandler{
|
|
|
|
summarySrvc: summaryService,
|
2021-02-06 22:09:08 +03:00
|
|
|
userSrvc: userService,
|
2021-02-03 23:28:02 +03:00
|
|
|
config: conf.Get(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:40:50 +03:00
|
|
|
func (h *SummaryApiHandler) RegisterRoutes(router chi.Router) {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler)
|
|
|
|
r.Get("/", h.Get)
|
|
|
|
|
|
|
|
router.Mount("/summary", r)
|
2021-02-03 23:28:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-07 13:54:07 +03:00
|
|
|
// @Summary Retrieve a summary
|
|
|
|
// @ID get-summary
|
|
|
|
// @Tags summary
|
|
|
|
// @Produce json
|
2022-08-19 18:14:00 +03:00
|
|
|
// @Param interval query string false "Interval identifier" Enums(today, yesterday, week, month, year, 7_days, last_7_days, 30_days, last_30_days, 6_months, last_6_months, 12_months, last_12_months, last_year, any, all_time)
|
2021-02-07 13:54:07 +03:00
|
|
|
// @Param from query string false "Start date (e.g. '2021-02-07')"
|
|
|
|
// @Param to query string false "End date (e.g. '2021-02-08')"
|
|
|
|
// @Param recompute query bool false "Whether to recompute the summary from raw heartbeat or use cache"
|
2021-12-26 21:29:17 +03:00
|
|
|
// @Param project query string false "Project to filter by"
|
|
|
|
// @Param language query string false "Language to filter by"
|
|
|
|
// @Param editor query string false "Editor to filter by"
|
|
|
|
// @Param operating_system query string false "OS to filter by"
|
|
|
|
// @Param machine query string false "Machine to filter by"
|
|
|
|
// @Param label query string false "Project label to filter by"
|
2021-02-07 13:54:07 +03:00
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Success 200 {object} models.Summary
|
2022-01-02 03:22:58 +03:00
|
|
|
// @Router /summary [get]
|
2021-02-03 23:28:02 +03:00
|
|
|
func (h *SummaryApiHandler) Get(w http.ResponseWriter, r *http.Request) {
|
2022-01-02 22:04:29 +03:00
|
|
|
summary, err, status := routeutils.LoadUserSummary(h.summarySrvc, r)
|
2021-02-03 23:28:02 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:57:07 +03:00
|
|
|
helpers.RespondJSON(w, r, http.StatusOK, summary)
|
2021-02-03 23:28:02 +03:00
|
|
|
}
|