Pull request 1963: AG-24051-stats-collector
Updates #6108. Squashed commit of the following: commit ca584c8dbbece70b90f6298a0a18a933a698fcf6 Merge: b6e13623228cfde921Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 23 17:00:52 2023 +0300 Merge branch 'master' into AG-24051-stats-collector commit b6e136232dd619ce09150b608ae5017676031e25 Merge: bbd4780b03722c2846Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 23 16:25:45 2023 +0300 Merge branch 'master' into AG-24051-stats-collector commit bbd4780b03a1c954fe2b349d27f1ab3bf7739518 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 22 17:47:51 2023 +0300 stats: imp test commit cfe3b9bdf5fd75bff98f985884b3bff8a88ae8ee Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 22 16:57:31 2023 +0300 stats: add test commit cb579a157056f79c1c3d08479a718698a74e0bb9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Aug 21 15:24:40 2023 +0300 stats: imp docs commit 3c6ab3affb9ac402db7e3cc3d9696154770e1037 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 17 14:41:35 2023 +0300 stats: imp code commit 125a31b73bb31f7f4886daad9ce7e3bbc97b38c9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Aug 16 12:29:10 2023 +0300 stats: imp test commit 1ba1eb3b7bd540621bf17ca50d4c2ba4bc55a9f8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 15 19:57:34 2023 +0300 stats: add test commit 46622f4fdf2775ddaba626b9786af183680e8889 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 15 15:47:06 2023 +0300 stats: rm stats collector
This commit is contained in:
@@ -14,24 +14,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TODO(e.burkov): Use more realistic data.
|
||||
func TestStatsCollector(t *testing.T) {
|
||||
ng := func(_ *unitDB) uint64 { return 0 }
|
||||
units := make([]*unitDB, 720)
|
||||
|
||||
t.Run("hours", func(t *testing.T) {
|
||||
statsData := statsCollector(units, 0, Hours, ng)
|
||||
assert.Len(t, statsData, 720)
|
||||
})
|
||||
|
||||
t.Run("days", func(t *testing.T) {
|
||||
for i := 0; i != 25; i++ {
|
||||
statsData := statsCollector(units, uint32(i), Days, ng)
|
||||
require.Lenf(t, statsData, 30, "i=%d", i)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestStats_races(t *testing.T) {
|
||||
var r uint32
|
||||
idGen := func() (id uint32) { return atomic.LoadUint32(&r) }
|
||||
@@ -103,3 +85,86 @@ func TestStats_races(t *testing.T) {
|
||||
finWG.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsCtx_FillCollectedStats_daily(t *testing.T) {
|
||||
const (
|
||||
daysCount = 10
|
||||
|
||||
timeUnits = "days"
|
||||
)
|
||||
|
||||
s, err := New(Config{
|
||||
ShouldCountClient: func([]string) bool { return true },
|
||||
Filename: filepath.Join(t.TempDir(), "./stats.db"),
|
||||
Limit: time.Hour,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
testutil.CleanupAndRequireSuccess(t, s.Close)
|
||||
|
||||
sum := make([][]uint64, resultLast)
|
||||
sum[RFiltered] = make([]uint64, daysCount)
|
||||
sum[RSafeBrowsing] = make([]uint64, daysCount)
|
||||
sum[RParental] = make([]uint64, daysCount)
|
||||
|
||||
total := make([]uint64, daysCount)
|
||||
|
||||
dailyData := []*unitDB{}
|
||||
|
||||
for i := 0; i < daysCount*24; i++ {
|
||||
n := uint64(i)
|
||||
nResult := make([]uint64, resultLast)
|
||||
nResult[RFiltered] = n
|
||||
nResult[RSafeBrowsing] = n
|
||||
nResult[RParental] = n
|
||||
|
||||
day := i / 24
|
||||
sum[RFiltered][day] += n
|
||||
sum[RSafeBrowsing][day] += n
|
||||
sum[RParental][day] += n
|
||||
|
||||
t := n * 3
|
||||
|
||||
total[day] += t
|
||||
|
||||
dailyData = append(dailyData, &unitDB{
|
||||
NTotal: t,
|
||||
NResult: nResult,
|
||||
})
|
||||
}
|
||||
|
||||
data := &StatsResp{}
|
||||
|
||||
// In this way we will not skip first hours.
|
||||
curID := uint32(daysCount * 24)
|
||||
|
||||
s.fillCollectedStats(data, dailyData, curID)
|
||||
|
||||
assert.Equal(t, timeUnits, data.TimeUnits)
|
||||
assert.Equal(t, sum[RFiltered], data.BlockedFiltering)
|
||||
assert.Equal(t, sum[RSafeBrowsing], data.ReplacedSafebrowsing)
|
||||
assert.Equal(t, sum[RParental], data.ReplacedParental)
|
||||
assert.Equal(t, total, data.DNSQueries)
|
||||
}
|
||||
|
||||
func TestStatsCtx_DataFromUnits_month(t *testing.T) {
|
||||
const hoursInMonth = 720
|
||||
|
||||
s, err := New(Config{
|
||||
ShouldCountClient: func([]string) bool { return true },
|
||||
Filename: filepath.Join(t.TempDir(), "./stats.db"),
|
||||
Limit: time.Hour,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
testutil.CleanupAndRequireSuccess(t, s.Close)
|
||||
|
||||
units, curID := s.loadUnits(hoursInMonth)
|
||||
require.Len(t, units, hoursInMonth)
|
||||
|
||||
var h uint32
|
||||
for h = 1; h <= hoursInMonth; h++ {
|
||||
data := s.dataFromUnits(units[:h], curID)
|
||||
require.NotNil(t, data)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user