From a3c96e3d211cc5e11ba09e334748f65a44b8960a Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Mon, 2 Sep 2024 20:44:11 +0300 Subject: [PATCH] stats: imp code --- internal/stats/http.go | 2 +- internal/stats/stats.go | 10 +++++++--- internal/stats/stats_test.go | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/stats/http.go b/internal/stats/http.go index d151e74d..4c9320d1 100644 --- a/internal/stats/http.go +++ b/internal/stats/http.go @@ -61,7 +61,7 @@ func (s *StatsCtx) handleStats(w http.ResponseWriter, r *http.Request) { resp, ok = s.getData(uint32(s.limit.Hours())) }() - s.logger.Debug("prepared data", "elapsed", time.Since(start)) + s.logger.Debug("prepared data", "elapsed", timeutil.Duration{Duration: time.Since(start)}) if !ok { // Don't bring the message to the lower case since it's a part of UI diff --git a/internal/stats/stats.go b/internal/stats/stats.go index b3372c7b..feb17f86 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -3,6 +3,7 @@ package stats import ( + "context" "fmt" "io" "log/slog" @@ -387,7 +388,11 @@ func (s *StatsCtx) openDB() (err error) { db, err = bbolt.Open(s.filename, 0o644, nil) if err != nil { if err.Error() == "invalid argument" { - s.logger.Error("AdGuard Home cannot be initialized due to an incompatible file system.\nPlease read the explanation here: https://github.com/AdguardTeam/AdGuardHome/wiki/Getting-Started#limitations") + const lines = `AdGuard Home cannot be initialized due to an incompatible file system. +Please read the explanation here: https://github.com/AdguardTeam/AdGuardHome/wiki/Getting-Started#limitations` + + // TODO(s.chzhen): Use passed context. + slogutil.PrintLines(context.TODO(), s.logger, slog.LevelError, "opening database", lines) } return err @@ -597,8 +602,7 @@ func (s *StatsCtx) loadUnits(limit uint32) (units []*unitDB, curID uint32) { if unitsLen := len(units); unitsLen != int(limit) { // Should not happen. - s.logger.Error("number of loaded units not equal to limit", "loaded", unitsLen, "limit", limit) - units = units[:limit] + panic(fmt.Errorf("loaded %d units when the desired number is %d", unitsLen, limit)) } return units, curID diff --git a/internal/stats/stats_test.go b/internal/stats/stats_test.go index e5af7031..6e7d1710 100644 --- a/internal/stats/stats_test.go +++ b/internal/stats/stats_test.go @@ -22,6 +22,7 @@ import ( "github.com/stretchr/testify/require" ) +// TODO(s.chzhen): Remove once [aghhttp.Error] starts using slog. func TestMain(m *testing.M) { testutil.DiscardLogOutput(m) }