Skip to content

Commit

Permalink
stats: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Sep 4, 2024
1 parent bb3c0c8 commit 022c904
Showing 1 changed file with 7 additions and 50 deletions.
57 changes: 7 additions & 50 deletions internal/stats/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,8 @@ func (s *StatsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
if !ok {
// Don't bring the message to the lower case since it's a part of UI
// text for the moment.
aghhttp.ErrorAndLog(
ctx,
s.logger,
r,
w,
http.StatusInternalServerError, "Couldn't get statistics data",
)
const msg = "Couldn't get statistics data"
aghhttp.ErrorAndLog(ctx, s.logger, r, w, http.StatusInternalServerError, msg)

return
}
Expand Down Expand Up @@ -162,28 +157,13 @@ func (s *StatsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
reqData := configResp{}
err := json.NewDecoder(r.Body).Decode(&reqData)
if err != nil {
aghhttp.ErrorAndLog(
ctx,
s.logger,
r,
w,
http.StatusBadRequest,
"json decode: %s",
err,
)
aghhttp.ErrorAndLog(ctx, s.logger, r, w, http.StatusBadRequest, "json decode: %s", err)

return
}

if !checkInterval(reqData.IntervalDays) {
aghhttp.ErrorAndLog(
ctx,
s.logger,
r,
w,
http.StatusBadRequest,
"Unsupported interval",
)
aghhttp.ErrorAndLog(ctx, s.logger, r, w, http.StatusBadRequest, "Unsupported interval")

return
}
Expand All @@ -206,30 +186,14 @@ func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request)
reqData := getConfigResp{}
err := json.NewDecoder(r.Body).Decode(&reqData)
if err != nil {
aghhttp.ErrorAndLog(
ctx,
s.logger,
r,
w,
http.StatusBadRequest,
"json decode: %s",
err,
)
aghhttp.ErrorAndLog(ctx, s.logger, r, w, http.StatusBadRequest, "json decode: %s", err)

return
}

engine, err := aghnet.NewIgnoreEngine(reqData.Ignored)
if err != nil {
aghhttp.ErrorAndLog(
ctx,
s.logger,
r,
w,
http.StatusUnprocessableEntity,
"ignored: %s",
err,
)
aghhttp.ErrorAndLog(ctx, s.logger, r, w, http.StatusUnprocessableEntity, "ignored: %s", err)

return
}
Expand All @@ -251,14 +215,7 @@ func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request)
}

if reqData.Enabled == aghalg.NBNull {
aghhttp.ErrorAndLog(
ctx,
s.logger,
r,
w,
http.StatusUnprocessableEntity,
"enabled is null",
)
aghhttp.ErrorAndLog(ctx, s.logger, r, w, http.StatusUnprocessableEntity, "enabled is null")

return
}
Expand Down

0 comments on commit 022c904

Please sign in to comment.