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 2, 2024
1 parent 2c0ffd9 commit a3c96e3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/stats/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions internal/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package stats

import (
"context"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a3c96e3

Please sign in to comment.