2021-02-03 23:28:02 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-01-02 03:22:58 +03:00
|
|
|
"net/http"
|
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
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
|
|
|
su "github.com/muety/wakapi/routes/utils"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SummaryApiHandler) RegisterRoutes(router *mux.Router) {
|
2021-02-03 23:40:01 +03:00
|
|
|
r := router.PathPrefix("/summary").Subrouter()
|
2021-02-06 22:09:08 +03:00
|
|
|
r.Use(
|
|
|
|
middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler,
|
|
|
|
)
|
2021-04-30 19:08:53 +03:00
|
|
|
r.Path("").Methods(http.MethodGet).HandlerFunc(h.Get)
|
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
|
|
|
|
// @Param interval query string false "Interval identifier" Enums(today, yesterday, week, month, year, 7_days, last_7_days, 30_days, last_30_days, 12_months, last_12_months, any)
|
|
|
|
// @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"
|
|
|
|
// @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) {
|
|
|
|
summary, err, status := su.LoadUserSummary(h.summarySrvc, r)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:26:47 +03:00
|
|
|
utils.RespondJSON(w, r, http.StatusOK, summary)
|
2021-02-03 23:28:02 +03:00
|
|
|
}
|