stats: imp code

This commit is contained in:
Stanislav Chzhen 2024-09-02 20:44:11 +03:00
parent 2c0ffd91fd
commit a3c96e3d21
3 changed files with 9 additions and 4 deletions

View file

@ -61,7 +61,7 @@ func (s *StatsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
resp, ok = s.getData(uint32(s.limit.Hours())) 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 { if !ok {
// Don't bring the message to the lower case since it's a part of UI // Don't bring the message to the lower case since it's a part of UI

View file

@ -3,6 +3,7 @@
package stats package stats
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
@ -387,7 +388,11 @@ func (s *StatsCtx) openDB() (err error) {
db, err = bbolt.Open(s.filename, 0o644, nil) db, err = bbolt.Open(s.filename, 0o644, nil)
if err != nil { if err != nil {
if err.Error() == "invalid argument" { 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 return err
@ -597,8 +602,7 @@ func (s *StatsCtx) loadUnits(limit uint32) (units []*unitDB, curID uint32) {
if unitsLen := len(units); unitsLen != int(limit) { if unitsLen := len(units); unitsLen != int(limit) {
// Should not happen. // Should not happen.
s.logger.Error("number of loaded units not equal to limit", "loaded", unitsLen, "limit", limit) panic(fmt.Errorf("loaded %d units when the desired number is %d", unitsLen, limit))
units = units[:limit]
} }
return units, curID return units, curID

View file

@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// TODO(s.chzhen): Remove once [aghhttp.Error] starts using slog.
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m) testutil.DiscardLogOutput(m)
} }