From 2f3e25bee56d2c6ddcf4aa2fc6a1dc51ed9b06e1 Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Wed, 1 Mar 2023 12:25:14 +0300 Subject: [PATCH] all: fix fmt --- internal/querylog/http.go | 5 ++--- internal/stats/http.go | 19 +++++++++---------- internal/stats/stats.go | 11 +++++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/internal/querylog/http.go b/internal/querylog/http.go index e78f54fd..226600f0 100644 --- a/internal/querylog/http.go +++ b/internal/querylog/http.go @@ -42,7 +42,7 @@ type getConfigResp struct { // Ignored is the list of host names, which should not be written to log. Ignored []string `json:"ignored"` - // Interval is the querylog rotation interval in milliseconds + // Interval is the querylog rotation interval in milliseconds. Interval float64 `json:"interval"` // Enabled shows if the querylog is enabled. It is an aghalg.NullBool to @@ -103,8 +103,7 @@ func (l *queryLog) handleQueryLogInfo(w http.ResponseWriter, r *http.Request) { ivl := l.conf.RotationIvl - ok := checkInterval(ivl) - if !ok { + if !checkInterval(ivl) { // NOTE: If interval is custom we set it to 90 days for compatibility // with old API. ivl = timeutil.Day * 90 diff --git a/internal/stats/http.go b/internal/stats/http.go index db4ffa8e..5686193a 100644 --- a/internal/stats/http.go +++ b/internal/stats/http.go @@ -11,6 +11,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/aghhttp" "github.com/AdguardTeam/AdGuardHome/internal/aghnet" "github.com/AdguardTeam/golibs/log" + "github.com/AdguardTeam/golibs/timeutil" "golang.org/x/exp/slices" ) @@ -116,10 +117,7 @@ func (s *StatsCtx) handleGetStatsConfig(w http.ResponseWriter, r *http.Request) Interval: float64(s.limit.Milliseconds()), Ignored: ignored, } - err := aghhttp.WriteJSONResponse(w, r, resp) - if err != nil { - log.Debug("stats: write response: %s", err) - } + _ = aghhttp.WriteJSONResponse(w, r, resp) } // handleStatsConfig handles requests to the POST /control/stats_config @@ -146,11 +144,12 @@ func (s *StatsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) { s.lock.Lock() defer s.lock.Unlock() - s.setLimitLocked(int(reqData.IntervalDays)) + limit := time.Duration(reqData.IntervalDays) * timeutil.Day + s.setLimitLocked(limit) } -// handlePutStatsConfig handles requests to the PUT -// /control/stats/config/update endpoint. +// handlePutStatsConfig handles requests to the PUT /control/stats/config/update +// endpoint. func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request) { reqData := getConfigResp{} err := json.NewDecoder(r.Body).Decode(&reqData) @@ -171,15 +170,15 @@ func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request) defer s.configModified() - s.lock.Lock() - defer s.lock.Unlock() - if reqData.Enabled == aghalg.NBNull { aghhttp.Error(r, w, http.StatusUnprocessableEntity, "enabled is null") return } + s.lock.Lock() + defer s.lock.Unlock() + s.enabled = reqData.Enabled == aghalg.NBTrue s.limit = ivl diff --git a/internal/stats/stats.go b/internal/stats/stats.go index dd192dcb..16226bff 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -453,15 +453,14 @@ func (s *StatsCtx) periodicFlush() { log.Debug("periodic flushing finished") } -// setLimitLocked sets the limit interval without locking. For internal use -// only. +// setLimitLocked sets the limit without locking. For internal use only. // // TODO(s.chzhen): Remove it when migration to the new API is over. -func (s *StatsCtx) setLimitLocked(limitDays int) { - if limitDays != 0 { +func (s *StatsCtx) setLimitLocked(limit time.Duration) { + if limit != 0 { s.enabled = true - s.limit = time.Duration(limitDays) * timeutil.Day - log.Debug("stats: set limit: %d days", limitDays) + s.limit = limit + log.Debug("stats: set limit: %d days", int(limit.Hours()/24)) return }