2022-08-17 14:09:13 +03:00
|
|
|
package stats_test
|
2019-08-22 16:34:58 +03:00
|
|
|
|
|
|
|
import (
|
2022-08-17 14:09:13 +03:00
|
|
|
"encoding/json"
|
2019-08-22 16:34:58 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-08-17 14:09:13 +03:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"path/filepath"
|
2019-08-22 16:34:58 +03:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
2023-08-09 14:33:52 +03:00
|
|
|
"time"
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2023-09-05 15:13:35 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2022-08-17 14:09:13 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
2024-09-09 13:31:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2022-11-09 14:37:07 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-10-22 11:58:18 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2023-03-23 13:46:57 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2023-04-07 13:17:40 +03:00
|
|
|
"github.com/miekg/dns"
|
2019-08-22 16:34:58 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 19:38:31 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-22 16:34:58 +03:00
|
|
|
)
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// constUnitID is the UnitIDGenFunc which always return 0.
|
|
|
|
func constUnitID() (id uint32) { return 0 }
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
func assertSuccessAndUnmarshal(t *testing.T, to any, handler http.Handler, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
require.NotNil(t, handler)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
require.Equal(t, http.StatusOK, rw.Code)
|
|
|
|
|
|
|
|
data := rw.Body.Bytes()
|
|
|
|
if to == nil {
|
|
|
|
assert.Empty(t, data)
|
|
|
|
|
|
|
|
return
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
err := json.Unmarshal(data, to)
|
|
|
|
require.NoError(t, err)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStats(t *testing.T) {
|
2022-11-09 14:37:07 +03:00
|
|
|
cliIP := netutil.IPv4Localhost()
|
2022-08-17 14:09:13 +03:00
|
|
|
cliIPStr := cliIP.String()
|
|
|
|
|
|
|
|
handlers := map[string]http.Handler{}
|
|
|
|
conf := stats.Config{
|
2024-09-09 13:31:54 +03:00
|
|
|
Logger: slogutil.NewDiscardLogger(),
|
2023-04-07 13:17:40 +03:00
|
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
|
|
Limit: timeutil.Day,
|
|
|
|
Enabled: true,
|
|
|
|
UnitID: constUnitID,
|
2022-08-17 14:09:13 +03:00
|
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) {
|
|
|
|
handlers[url] = handler
|
|
|
|
},
|
2019-09-16 16:14:52 +03:00
|
|
|
}
|
2021-02-09 19:38:31 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
s, err := stats.New(conf)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
s.Start()
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
|
|
|
|
t.Run("data", func(t *testing.T) {
|
|
|
|
const reqDomain = "domain"
|
2023-08-09 14:33:52 +03:00
|
|
|
const respUpstream = "upstream"
|
|
|
|
|
|
|
|
entries := []*stats.Entry{{
|
2023-11-09 16:14:05 +03:00
|
|
|
Domain: reqDomain,
|
|
|
|
Client: cliIPStr,
|
|
|
|
Result: stats.RFiltered,
|
|
|
|
ProcessingTime: time.Microsecond * 123456,
|
|
|
|
Upstream: respUpstream,
|
|
|
|
UpstreamTime: time.Microsecond * 222222,
|
2022-08-17 14:09:13 +03:00
|
|
|
}, {
|
2023-11-09 16:14:05 +03:00
|
|
|
Domain: reqDomain,
|
|
|
|
Client: cliIPStr,
|
|
|
|
Result: stats.RNotFiltered,
|
|
|
|
ProcessingTime: time.Microsecond * 123456,
|
|
|
|
Upstream: respUpstream,
|
|
|
|
UpstreamTime: time.Microsecond * 222222,
|
2022-08-17 14:09:13 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
wantData := &stats.StatsResp{
|
2023-08-09 14:33:52 +03:00
|
|
|
TimeUnits: "hours",
|
|
|
|
TopQueried: []map[string]uint64{0: {reqDomain: 1}},
|
|
|
|
TopClients: []map[string]uint64{0: {cliIPStr: 2}},
|
|
|
|
TopBlocked: []map[string]uint64{0: {reqDomain: 1}},
|
|
|
|
TopUpstreamsResponses: []map[string]uint64{0: {respUpstream: 2}},
|
2023-11-09 16:14:05 +03:00
|
|
|
TopUpstreamsAvgTime: []map[string]float64{0: {respUpstream: 0.222222}},
|
2022-08-17 14:09:13 +03:00
|
|
|
DNSQueries: []uint64{
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
|
|
|
|
},
|
|
|
|
BlockedFiltering: []uint64{
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
|
|
|
},
|
|
|
|
ReplacedSafebrowsing: []uint64{
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
},
|
|
|
|
ReplacedParental: []uint64{
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
},
|
|
|
|
NumDNSQueries: 2,
|
|
|
|
NumBlockedFiltering: 1,
|
|
|
|
NumReplacedSafebrowsing: 0,
|
|
|
|
NumReplacedSafesearch: 0,
|
|
|
|
NumReplacedParental: 0,
|
|
|
|
AvgProcessingTime: 0.123456,
|
|
|
|
}
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
for _, e := range entries {
|
|
|
|
s.Update(e)
|
|
|
|
}
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
data := &stats.StatsResp{}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
assert.Equal(t, wantData, data)
|
|
|
|
})
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
t.Run("tops", func(t *testing.T) {
|
|
|
|
topClients := s.TopClientsIP(2)
|
|
|
|
require.NotEmpty(t, topClients)
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-11-09 14:37:07 +03:00
|
|
|
assert.Equal(t, cliIP, topClients[0])
|
2022-08-17 14:09:13 +03:00
|
|
|
})
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/control/stats_reset", nil)
|
|
|
|
assertSuccessAndUnmarshal(t, nil, handlers["/control/stats_reset"], req)
|
|
|
|
|
|
|
|
_24zeroes := [24]uint64{}
|
|
|
|
emptyData := &stats.StatsResp{
|
2023-08-09 14:33:52 +03:00
|
|
|
TimeUnits: "hours",
|
|
|
|
TopQueried: []map[string]uint64{},
|
|
|
|
TopClients: []map[string]uint64{},
|
|
|
|
TopBlocked: []map[string]uint64{},
|
|
|
|
TopUpstreamsResponses: []map[string]uint64{},
|
|
|
|
TopUpstreamsAvgTime: []map[string]float64{},
|
|
|
|
DNSQueries: _24zeroes[:],
|
|
|
|
BlockedFiltering: _24zeroes[:],
|
|
|
|
ReplacedSafebrowsing: _24zeroes[:],
|
|
|
|
ReplacedParental: _24zeroes[:],
|
2022-08-17 14:09:13 +03:00
|
|
|
}
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
|
|
data := &stats.StatsResp{}
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
|
|
assert.Equal(t, emptyData, data)
|
|
|
|
})
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
2022-08-17 14:09:13 +03:00
|
|
|
var curHour uint32 = 1
|
|
|
|
handlers := map[string]http.Handler{}
|
|
|
|
|
|
|
|
conf := stats.Config{
|
2024-09-09 13:31:54 +03:00
|
|
|
Logger: slogutil.NewDiscardLogger(),
|
2023-04-07 13:17:40 +03:00
|
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
|
|
Limit: timeutil.Day,
|
|
|
|
Enabled: true,
|
|
|
|
UnitID: func() (id uint32) { return atomic.LoadUint32(&curHour) },
|
|
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) { handlers[url] = handler },
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
s, err := stats.New(conf)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
s.Start()
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
2021-02-04 15:12:34 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
const (
|
|
|
|
hoursNum = 12
|
|
|
|
cliNumPerHour = 1000
|
|
|
|
)
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
2020-11-06 12:15:08 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
for h := 0; h < hoursNum; h++ {
|
|
|
|
atomic.AddUint32(&curHour, 1)
|
2021-02-09 19:38:31 +03:00
|
|
|
|
2024-04-04 15:52:39 +03:00
|
|
|
for i := range cliNumPerHour {
|
2022-08-17 14:09:13 +03:00
|
|
|
ip := net.IP{127, 0, byte((i & 0xff00) >> 8), byte(i & 0xff)}
|
2023-08-09 14:33:52 +03:00
|
|
|
e := &stats.Entry{
|
2023-11-09 16:14:05 +03:00
|
|
|
Domain: fmt.Sprintf("domain%d.hour%d", i, h),
|
|
|
|
Client: ip.String(),
|
|
|
|
Result: stats.RNotFiltered,
|
|
|
|
ProcessingTime: 123456,
|
2022-08-17 14:09:13 +03:00
|
|
|
}
|
|
|
|
s.Update(e)
|
2021-02-09 19:38:31 +03:00
|
|
|
}
|
2022-08-17 14:09:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data := &stats.StatsResp{}
|
|
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
|
|
assert.Equal(t, hoursNum*cliNumPerHour, int(data.NumDNSQueries))
|
2019-10-09 17:17:57 +03:00
|
|
|
}
|
2023-04-07 13:17:40 +03:00
|
|
|
|
|
|
|
func TestShouldCount(t *testing.T) {
|
|
|
|
const (
|
|
|
|
ignored1 = "ignor.ed"
|
|
|
|
ignored2 = "ignored.to"
|
|
|
|
)
|
2023-09-05 15:13:35 +03:00
|
|
|
ignored := []string{ignored1, ignored2}
|
|
|
|
engine, err := aghnet.NewIgnoreEngine(ignored)
|
|
|
|
require.NoError(t, err)
|
2023-04-07 13:17:40 +03:00
|
|
|
|
|
|
|
s, err := stats.New(stats.Config{
|
2024-09-09 13:31:54 +03:00
|
|
|
Logger: slogutil.NewDiscardLogger(),
|
2023-04-07 13:17:40 +03:00
|
|
|
Enabled: true,
|
|
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
|
|
Limit: timeutil.Day,
|
2023-09-05 15:13:35 +03:00
|
|
|
Ignored: engine,
|
2023-04-07 13:17:40 +03:00
|
|
|
ShouldCountClient: func(ids []string) (a bool) {
|
|
|
|
return ids[0] != "no_count"
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s.Start()
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
wantCount assert.BoolAssertionFunc
|
|
|
|
name string
|
|
|
|
host string
|
|
|
|
ids []string
|
|
|
|
}{{
|
|
|
|
name: "count",
|
|
|
|
host: "example.com",
|
|
|
|
ids: []string{"whatever"},
|
|
|
|
wantCount: assert.True,
|
|
|
|
}, {
|
|
|
|
name: "no_count_ignored_1",
|
|
|
|
host: ignored1,
|
|
|
|
ids: []string{"whatever"},
|
|
|
|
wantCount: assert.False,
|
|
|
|
}, {
|
|
|
|
name: "no_count_ignored_2",
|
|
|
|
host: ignored2,
|
|
|
|
ids: []string{"whatever"},
|
|
|
|
wantCount: assert.False,
|
|
|
|
}, {
|
|
|
|
name: "no_count_client_ignore",
|
|
|
|
host: "example.com",
|
|
|
|
ids: []string{"no_count"},
|
|
|
|
wantCount: assert.False,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
res := s.ShouldCount(tc.host, dns.TypeA, dns.ClassINET, tc.ids)
|
|
|
|
|
|
|
|
tc.wantCount(t, res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|