2019-08-22 16:34:58 +03:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2024-02-08 20:39:18 +03:00
|
|
|
"slices"
|
2019-08-22 16:34:58 +03:00
|
|
|
"time"
|
|
|
|
|
2023-09-05 15:13:35 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-09-09 13:31:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2022-08-04 19:05:28 +03:00
|
|
|
"go.etcd.io/bbolt"
|
2023-08-09 14:33:52 +03:00
|
|
|
"golang.org/x/exp/maps"
|
2019-08-22 16:34:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-08-17 14:09:13 +03:00
|
|
|
// maxDomains is the max number of top domains to return.
|
|
|
|
maxDomains = 100
|
2023-08-09 14:33:52 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// maxClients is the max number of top clients to return.
|
|
|
|
maxClients = 100
|
2023-08-09 14:33:52 +03:00
|
|
|
|
|
|
|
// maxUpstreams is the max number of top upstreams to return.
|
|
|
|
maxUpstreams = 100
|
2019-08-22 16:34:58 +03:00
|
|
|
)
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// UnitIDGenFunc is the signature of a function that generates a unique ID for
|
|
|
|
// the statistics unit.
|
|
|
|
type UnitIDGenFunc func() (id uint32)
|
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
// Supported values of [StatsResp.TimeUnits].
|
2022-08-17 14:09:13 +03:00
|
|
|
const (
|
2023-08-23 17:09:42 +03:00
|
|
|
timeUnitsHours = "hours"
|
|
|
|
timeUnitsDays = "days"
|
2022-08-17 14:09:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Result is the resulting code of processing the DNS request.
|
|
|
|
type Result int
|
|
|
|
|
|
|
|
// Supported Result values.
|
2022-08-04 19:05:28 +03:00
|
|
|
//
|
2022-08-17 14:09:13 +03:00
|
|
|
// TODO(e.burkov): Think about better naming.
|
|
|
|
const (
|
|
|
|
RNotFiltered Result = iota + 1
|
|
|
|
RFiltered
|
|
|
|
RSafeBrowsing
|
|
|
|
RSafeSearch
|
|
|
|
RParental
|
|
|
|
|
|
|
|
resultLast = RParental + 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Entry is a statistics data entry.
|
|
|
|
type Entry struct {
|
|
|
|
// Clients is the client's primary ID.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Make this a {net.IP, string} enum?
|
|
|
|
Client string
|
|
|
|
|
|
|
|
// Domain is the domain name requested.
|
|
|
|
Domain string
|
|
|
|
|
2023-08-09 14:33:52 +03:00
|
|
|
// Upstream is the upstream DNS server.
|
|
|
|
Upstream string
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// Result is the result of processing the request.
|
|
|
|
Result Result
|
|
|
|
|
2023-11-09 16:14:05 +03:00
|
|
|
// ProcessingTime is the duration of the request processing from the start
|
|
|
|
// of the request including timeouts.
|
|
|
|
ProcessingTime time.Duration
|
|
|
|
|
|
|
|
// UpstreamTime is the duration of the successful request to the upstream.
|
|
|
|
UpstreamTime time.Duration
|
2023-08-09 14:33:52 +03:00
|
|
|
}
|
|
|
|
|
2023-09-07 20:37:24 +03:00
|
|
|
// validate returns an error if entry is not valid.
|
2023-08-09 14:33:52 +03:00
|
|
|
func (e *Entry) validate() (err error) {
|
|
|
|
switch {
|
|
|
|
case e.Result == 0:
|
|
|
|
return errors.Error("result code is not set")
|
|
|
|
case e.Result >= resultLast:
|
|
|
|
return fmt.Errorf("unknown result code %d", e.Result)
|
|
|
|
case e.Domain == "":
|
|
|
|
return errors.Error("domain is empty")
|
|
|
|
case e.Client == "":
|
|
|
|
return errors.Error("client is empty")
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// unit collects the statistics data for a specific period of time.
|
2019-08-22 16:34:58 +03:00
|
|
|
type unit struct {
|
2023-04-05 15:50:14 +03:00
|
|
|
// domains stores the number of requests for each domain.
|
|
|
|
domains map[string]uint64
|
|
|
|
|
|
|
|
// blockedDomains stores the number of requests for each domain that has
|
|
|
|
// been blocked.
|
|
|
|
blockedDomains map[string]uint64
|
|
|
|
|
|
|
|
// clients stores the number of requests from each client.
|
|
|
|
clients map[string]uint64
|
|
|
|
|
2023-08-09 14:33:52 +03:00
|
|
|
// upstreamsResponses stores the number of responses from each upstream.
|
|
|
|
upstreamsResponses map[string]uint64
|
|
|
|
|
2023-11-09 16:14:05 +03:00
|
|
|
// upstreamsTimeSum stores the sum of durations of successful queries in
|
|
|
|
// microseconds to each upstream.
|
2023-08-09 14:33:52 +03:00
|
|
|
upstreamsTimeSum map[string]uint64
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// nResult stores the number of requests grouped by it's result.
|
|
|
|
nResult []uint64
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// id is the unique unit's identifier. It's set to an absolute hour number
|
|
|
|
// since the beginning of UNIX time by the default ID generating function.
|
2022-08-17 14:09:13 +03:00
|
|
|
//
|
|
|
|
// Must not be rewritten after creating to be accessed concurrently without
|
|
|
|
// using mu.
|
2022-08-04 19:05:28 +03:00
|
|
|
id uint32
|
|
|
|
|
|
|
|
// nTotal stores the total number of requests.
|
|
|
|
nTotal uint64
|
2023-04-05 15:50:14 +03:00
|
|
|
|
2023-08-09 14:33:52 +03:00
|
|
|
// timeSum stores the sum of processing time in microseconds of each request
|
2022-08-04 19:05:28 +03:00
|
|
|
// written by the unit.
|
|
|
|
timeSum uint64
|
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// newUnit allocates the new *unit.
|
|
|
|
func newUnit(id uint32) (u *unit) {
|
|
|
|
return &unit{
|
2023-08-09 14:33:52 +03:00
|
|
|
domains: map[string]uint64{},
|
|
|
|
blockedDomains: map[string]uint64{},
|
|
|
|
clients: map[string]uint64{},
|
|
|
|
upstreamsResponses: map[string]uint64{},
|
|
|
|
upstreamsTimeSum: map[string]uint64{},
|
|
|
|
nResult: make([]uint64, resultLast),
|
|
|
|
id: id,
|
2022-08-17 14:09:13 +03:00
|
|
|
}
|
2022-08-04 19:05:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// countPair is a single name-number pair for deserializing statistics data into
|
|
|
|
// the database.
|
2019-08-22 16:34:58 +03:00
|
|
|
type countPair struct {
|
|
|
|
Name string
|
2019-09-10 17:59:10 +03:00
|
|
|
Count uint64
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// unitDB is the structure for serializing statistics data into the database.
|
2023-04-05 15:50:14 +03:00
|
|
|
//
|
|
|
|
// NOTE: Do not change the names or types of fields, as this structure is used
|
|
|
|
// for GOB encoding.
|
2019-08-22 16:34:58 +03:00
|
|
|
type unitDB struct {
|
2022-08-04 19:05:28 +03:00
|
|
|
// NResult is the number of requests by the result's kind.
|
2019-09-10 17:59:10 +03:00
|
|
|
NResult []uint64
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Domains is the number of requests for each domain name.
|
|
|
|
Domains []countPair
|
2023-04-05 15:50:14 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// BlockedDomains is the number of requests blocked for each domain name.
|
2019-08-22 16:34:58 +03:00
|
|
|
BlockedDomains []countPair
|
2023-04-05 15:50:14 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Clients is the number of requests from each client.
|
|
|
|
Clients []countPair
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2023-08-09 14:33:52 +03:00
|
|
|
// UpstreamsResponses is the number of responses from each upstream.
|
|
|
|
UpstreamsResponses []countPair
|
|
|
|
|
|
|
|
// UpstreamsTimeSum is the sum of processing time in microseconds of
|
|
|
|
// responses from each upstream.
|
|
|
|
UpstreamsTimeSum []countPair
|
|
|
|
|
2023-04-05 15:50:14 +03:00
|
|
|
// NTotal is the total number of requests.
|
|
|
|
NTotal uint64
|
|
|
|
|
2023-08-09 14:33:52 +03:00
|
|
|
// TimeAvg is the average of processing times in microseconds of all the
|
2022-08-04 19:05:28 +03:00
|
|
|
// requests in the unit.
|
|
|
|
TimeAvg uint32
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// newUnitID is the default UnitIDGenFunc that generates the unique id hourly.
|
|
|
|
func newUnitID() (id uint32) {
|
|
|
|
const secsInHour = int64(time.Hour / time.Second)
|
|
|
|
|
|
|
|
return uint32(time.Now().Unix() / secsInHour)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
func finishTxn(tx *bbolt.Tx, commit bool) (err error) {
|
|
|
|
if commit {
|
|
|
|
err = errors.Annotate(tx.Commit(), "committing: %w")
|
|
|
|
} else {
|
|
|
|
err = errors.Annotate(tx.Rollback(), "rolling back: %w")
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2022-08-04 19:05:28 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
return err
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:50:37 +03:00
|
|
|
// bucketNameLen is the length of a bucket, a 64-bit unsigned integer.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Find out why a 64-bit integer is used when IDs seem to
|
|
|
|
// always be 32 bits.
|
|
|
|
const bucketNameLen = 8
|
|
|
|
|
|
|
|
// idToUnitName converts a numerical ID into a database unit name.
|
|
|
|
func idToUnitName(id uint32) (name []byte) {
|
2022-08-04 19:05:28 +03:00
|
|
|
n := [bucketNameLen]byte{}
|
|
|
|
binary.BigEndian.PutUint64(n[:], uint64(id))
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
return n[:]
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:50:37 +03:00
|
|
|
// unitNameToID converts a database unit name into a numerical ID. ok is false
|
|
|
|
// if name is not a valid database unit name.
|
|
|
|
func unitNameToID(name []byte) (id uint32, ok bool) {
|
|
|
|
if len(name) < bucketNameLen {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint32(binary.BigEndian.Uint64(name)), true
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:00:17 +03:00
|
|
|
// compareCount used to sort countPair by Count in descending order.
|
|
|
|
func (a countPair) compareCount(b countPair) (res int) {
|
|
|
|
switch x, y := a.Count, b.Count; {
|
|
|
|
case x > y:
|
|
|
|
return -1
|
|
|
|
case x < y:
|
|
|
|
return +1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
func convertMapToSlice(m map[string]uint64, max int) (s []countPair) {
|
|
|
|
s = make([]countPair, 0, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
s = append(s, countPair{Name: k, Count: v})
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2023-08-10 20:00:17 +03:00
|
|
|
slices.SortFunc(s, countPair.compareCount)
|
2022-08-17 14:09:13 +03:00
|
|
|
if max > len(s) {
|
|
|
|
max = len(s)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2021-02-11 17:55:37 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
return s[:max]
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
func convertSliceToMap(a []countPair) (m map[string]uint64) {
|
|
|
|
m = map[string]uint64{}
|
2019-08-22 16:34:58 +03:00
|
|
|
for _, it := range a {
|
2019-09-10 17:59:10 +03:00
|
|
|
m[it.Name] = it.Count
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2022-08-17 14:09:13 +03:00
|
|
|
|
2019-08-22 16:34:58 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// serialize converts u to the *unitDB. It's safe for concurrent use. u must
|
|
|
|
// not be nil.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (u *unit) serialize() (udb *unitDB) {
|
|
|
|
var timeAvg uint32 = 0
|
2019-08-22 16:34:58 +03:00
|
|
|
if u.nTotal != 0 {
|
2022-08-04 19:05:28 +03:00
|
|
|
timeAvg = uint32(u.timeSum / u.nTotal)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2020-11-06 12:15:08 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
return &unitDB{
|
2023-08-09 14:33:52 +03:00
|
|
|
NTotal: u.nTotal,
|
|
|
|
NResult: append([]uint64{}, u.nResult...),
|
|
|
|
Domains: convertMapToSlice(u.domains, maxDomains),
|
|
|
|
BlockedDomains: convertMapToSlice(u.blockedDomains, maxDomains),
|
|
|
|
Clients: convertMapToSlice(u.clients, maxClients),
|
|
|
|
UpstreamsResponses: convertMapToSlice(u.upstreamsResponses, maxUpstreams),
|
|
|
|
UpstreamsTimeSum: convertMapToSlice(u.upstreamsTimeSum, maxUpstreams),
|
|
|
|
TimeAvg: timeAvg,
|
2022-08-04 19:05:28 +03:00
|
|
|
}
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2024-09-09 13:31:54 +03:00
|
|
|
// loadUnitFromDB loads unit by id from the database.
|
|
|
|
func (s *StatsCtx) loadUnitFromDB(tx *bbolt.Tx, id uint32) (udb *unitDB) {
|
2022-08-17 14:09:13 +03:00
|
|
|
bkt := tx.Bucket(idToUnitName(id))
|
|
|
|
if bkt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 13:31:54 +03:00
|
|
|
s.logger.Debug("loading unit", "id", id)
|
2022-08-17 14:09:13 +03:00
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.Write(bkt.Get([]byte{0}))
|
|
|
|
udb = &unitDB{}
|
|
|
|
|
|
|
|
err := gob.NewDecoder(&buf).Decode(udb)
|
|
|
|
if err != nil {
|
2024-09-09 13:31:54 +03:00
|
|
|
s.logger.Error("gob decode", slogutil.KeyError, err)
|
2022-08-17 14:09:13 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return udb
|
|
|
|
}
|
|
|
|
|
2023-09-07 20:37:24 +03:00
|
|
|
// deserialize assigns the appropriate values from udb to u. u must not be nil.
|
2022-08-04 19:05:28 +03:00
|
|
|
// It's safe for concurrent use.
|
|
|
|
func (u *unit) deserialize(udb *unitDB) {
|
|
|
|
if udb == nil {
|
|
|
|
return
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2019-09-10 18:04:43 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
u.nTotal = udb.NTotal
|
|
|
|
u.nResult = make([]uint64, resultLast)
|
|
|
|
copy(u.nResult, udb.NResult)
|
2021-01-27 18:32:13 +03:00
|
|
|
u.domains = convertSliceToMap(udb.Domains)
|
|
|
|
u.blockedDomains = convertSliceToMap(udb.BlockedDomains)
|
|
|
|
u.clients = convertSliceToMap(udb.Clients)
|
2023-08-09 14:33:52 +03:00
|
|
|
u.upstreamsResponses = convertSliceToMap(udb.UpstreamsResponses)
|
|
|
|
u.upstreamsTimeSum = convertSliceToMap(udb.UpstreamsTimeSum)
|
2022-08-04 19:05:28 +03:00
|
|
|
u.timeSum = uint64(udb.TimeAvg) * udb.NTotal
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// add adds new data to u. It's safe for concurrent use.
|
2023-08-09 14:33:52 +03:00
|
|
|
func (u *unit) add(e *Entry) {
|
|
|
|
u.nResult[e.Result]++
|
|
|
|
if e.Result == RNotFiltered {
|
|
|
|
u.domains[e.Domain]++
|
2022-08-17 14:09:13 +03:00
|
|
|
} else {
|
2023-08-09 14:33:52 +03:00
|
|
|
u.blockedDomains[e.Domain]++
|
2022-08-17 14:09:13 +03:00
|
|
|
}
|
|
|
|
|
2023-08-09 14:33:52 +03:00
|
|
|
u.clients[e.Client]++
|
2023-11-09 16:14:05 +03:00
|
|
|
pt := uint64(e.ProcessingTime.Microseconds())
|
|
|
|
u.timeSum += pt
|
2022-08-17 14:09:13 +03:00
|
|
|
u.nTotal++
|
2023-08-09 14:33:52 +03:00
|
|
|
|
|
|
|
if e.Upstream != "" {
|
|
|
|
u.upstreamsResponses[e.Upstream]++
|
2023-11-09 16:14:05 +03:00
|
|
|
ut := uint64(e.UpstreamTime.Microseconds())
|
|
|
|
u.upstreamsTimeSum[e.Upstream] += ut
|
2023-08-09 14:33:52 +03:00
|
|
|
}
|
2022-08-17 14:09:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// flushUnitToDB puts udb to the database at id.
|
2024-09-09 13:31:54 +03:00
|
|
|
func (s *StatsCtx) flushUnitToDB(udb *unitDB, tx *bbolt.Tx, id uint32) (err error) {
|
|
|
|
s.logger.Debug("flushing unit", "id", id, "req_num", udb.NTotal)
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2021-08-27 14:50:37 +03:00
|
|
|
bkt, err := tx.CreateBucketIfNotExists(idToUnitName(id))
|
2019-08-22 16:34:58 +03:00
|
|
|
if err != nil {
|
2022-08-17 14:09:13 +03:00
|
|
|
return fmt.Errorf("creating bucket: %w", err)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
err = gob.NewEncoder(buf).Encode(udb)
|
2019-08-22 16:34:58 +03:00
|
|
|
if err != nil {
|
2022-08-17 14:09:13 +03:00
|
|
|
return fmt.Errorf("encoding unit: %w", err)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = bkt.Put([]byte{0}, buf.Bytes())
|
|
|
|
if err != nil {
|
2022-08-17 14:09:13 +03:00
|
|
|
return fmt.Errorf("putting unit to database: %w", err)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
return nil
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
func convertTopSlice(a []countPair) (m []map[string]uint64) {
|
|
|
|
m = make([]map[string]uint64, 0, len(a))
|
2019-08-22 16:34:58 +03:00
|
|
|
for _, it := range a {
|
2022-08-04 19:05:28 +03:00
|
|
|
m = append(m, map[string]uint64{it.Name: it.Count})
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2022-08-04 19:05:28 +03:00
|
|
|
|
2019-08-22 16:34:58 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
// pairsGetter is a signature for topsCollector argument.
|
|
|
|
type pairsGetter func(u *unitDB) (pairs []countPair)
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// topsCollector collects statistics about highest values from the given *unitDB
|
2021-02-09 19:38:31 +03:00
|
|
|
// slice using pg to retrieve data.
|
2023-09-05 15:13:35 +03:00
|
|
|
func topsCollector(units []*unitDB, max int, ignored *aghnet.IgnoreEngine, pg pairsGetter) []map[string]uint64 {
|
2021-02-09 19:38:31 +03:00
|
|
|
m := map[string]uint64{}
|
|
|
|
for _, u := range units {
|
2022-08-04 19:05:28 +03:00
|
|
|
for _, cp := range pg(u) {
|
2023-02-13 18:15:33 +03:00
|
|
|
if !ignored.Has(cp.Name) {
|
|
|
|
m[cp.Name] += cp.Count
|
|
|
|
}
|
2021-02-09 19:38:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
a2 := convertMapToSlice(m, max)
|
2022-08-17 14:09:13 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
return convertTopSlice(a2)
|
|
|
|
}
|
|
|
|
|
2022-08-31 18:57:02 +03:00
|
|
|
// getData returns the statistics data using the following algorithm:
|
|
|
|
//
|
|
|
|
// 1. Prepare a slice of N units, where N is the value of "limit" configuration
|
|
|
|
// setting. Load data for the most recent units from the file. If a unit
|
|
|
|
// with required ID doesn't exist, just add an empty unit. Get data for the
|
|
|
|
// current unit.
|
|
|
|
//
|
|
|
|
// 2. Process data from the units and prepare an output map object, including
|
|
|
|
// per time unit counters (DNS queries per time-unit, blocked queries per
|
|
|
|
// time unit, etc.). If the time unit is hour, just add values from each
|
|
|
|
// unit to the slice; otherwise, the time unit is day, so aggregate per-hour
|
|
|
|
// data into days.
|
|
|
|
//
|
|
|
|
// To get the top counters (queries per domain, queries per blocked domain,
|
|
|
|
// etc.), first sum up data for all units into a single map. Then, get the
|
|
|
|
// pairs with the highest numbers.
|
|
|
|
//
|
|
|
|
// The total counters (DNS queries, blocked, etc.) are just the sum of data
|
|
|
|
// for all units.
|
2023-08-23 17:09:42 +03:00
|
|
|
func (s *StatsCtx) getData(limit uint32) (resp *StatsResp, ok bool) {
|
2022-08-04 19:05:28 +03:00
|
|
|
if limit == 0 {
|
2023-08-23 17:09:42 +03:00
|
|
|
return &StatsResp{
|
2022-08-04 19:05:28 +03:00
|
|
|
TimeUnits: "days",
|
|
|
|
|
2023-08-09 14:33:52 +03:00
|
|
|
TopBlocked: []topAddrs{},
|
|
|
|
TopClients: []topAddrs{},
|
|
|
|
TopQueried: []topAddrs{},
|
|
|
|
TopUpstreamsResponses: []topAddrs{},
|
|
|
|
TopUpstreamsAvgTime: []topAddrsFloat{},
|
2022-08-04 19:05:28 +03:00
|
|
|
|
|
|
|
BlockedFiltering: []uint64{},
|
|
|
|
DNSQueries: []uint64{},
|
|
|
|
ReplacedParental: []uint64{},
|
|
|
|
ReplacedSafebrowsing: []uint64{},
|
|
|
|
}, true
|
|
|
|
}
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
units, curID := s.loadUnits(limit)
|
2019-10-07 15:55:09 +03:00
|
|
|
if units == nil {
|
2023-08-23 17:09:42 +03:00
|
|
|
return &StatsResp{}, false
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
return s.dataFromUnits(units, curID), true
|
2023-08-09 14:33:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// dataFromUnits collects and returns the statistics data.
|
2023-08-23 17:09:42 +03:00
|
|
|
func (s *StatsCtx) dataFromUnits(units []*unitDB, curID uint32) (resp *StatsResp) {
|
2023-08-09 14:33:52 +03:00
|
|
|
topUpstreamsResponses, topUpstreamsAvgTime := topUpstreamsPairs(units)
|
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
resp = &StatsResp{
|
2023-08-09 14:33:52 +03:00
|
|
|
TopQueried: topsCollector(units, maxDomains, s.ignored, func(u *unitDB) (pairs []countPair) { return u.Domains }),
|
|
|
|
TopBlocked: topsCollector(units, maxDomains, s.ignored, func(u *unitDB) (pairs []countPair) { return u.BlockedDomains }),
|
|
|
|
TopUpstreamsResponses: topUpstreamsResponses,
|
|
|
|
TopUpstreamsAvgTime: topUpstreamsAvgTime,
|
|
|
|
TopClients: topsCollector(units, maxClients, nil, topClientPairs(s)),
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
s.fillCollectedStats(resp, units, curID)
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
// Total counters:
|
|
|
|
sum := unitDB{
|
2022-08-04 19:05:28 +03:00
|
|
|
NResult: make([]uint64, resultLast),
|
2021-02-09 19:38:31 +03:00
|
|
|
}
|
2023-08-09 14:33:52 +03:00
|
|
|
var timeN uint32
|
2019-08-22 16:34:58 +03:00
|
|
|
for _, u := range units {
|
|
|
|
sum.NTotal += u.NTotal
|
|
|
|
sum.TimeAvg += u.TimeAvg
|
|
|
|
if u.TimeAvg != 0 {
|
|
|
|
timeN++
|
|
|
|
}
|
|
|
|
sum.NResult[RFiltered] += u.NResult[RFiltered]
|
|
|
|
sum.NResult[RSafeBrowsing] += u.NResult[RSafeBrowsing]
|
|
|
|
sum.NResult[RSafeSearch] += u.NResult[RSafeSearch]
|
|
|
|
sum.NResult[RParental] += u.NResult[RParental]
|
|
|
|
}
|
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
resp.NumDNSQueries = sum.NTotal
|
|
|
|
resp.NumBlockedFiltering = sum.NResult[RFiltered]
|
|
|
|
resp.NumReplacedSafebrowsing = sum.NResult[RSafeBrowsing]
|
|
|
|
resp.NumReplacedSafesearch = sum.NResult[RSafeSearch]
|
|
|
|
resp.NumReplacedParental = sum.NResult[RParental]
|
2019-08-22 16:34:58 +03:00
|
|
|
|
|
|
|
if timeN != 0 {
|
2023-08-23 17:09:42 +03:00
|
|
|
resp.AvgProcessingTime = microsecondsToSeconds(float64(sum.TimeAvg / timeN))
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// fillCollectedStats fills data with collected statistics.
|
|
|
|
func (s *StatsCtx) fillCollectedStats(data *StatsResp, units []*unitDB, curID uint32) {
|
|
|
|
size := len(units)
|
|
|
|
data.TimeUnits = timeUnitsHours
|
|
|
|
|
|
|
|
daysCount := size / 24
|
2024-03-12 13:13:59 +03:00
|
|
|
if daysCount > 7 {
|
2023-08-23 17:09:42 +03:00
|
|
|
size = daysCount
|
|
|
|
data.TimeUnits = timeUnitsDays
|
|
|
|
}
|
|
|
|
|
|
|
|
data.DNSQueries = make([]uint64, size)
|
|
|
|
data.BlockedFiltering = make([]uint64, size)
|
|
|
|
data.ReplacedSafebrowsing = make([]uint64, size)
|
|
|
|
data.ReplacedParental = make([]uint64, size)
|
|
|
|
|
|
|
|
if data.TimeUnits == timeUnitsDays {
|
|
|
|
s.fillCollectedStatsDaily(data, units, curID, size)
|
|
|
|
|
|
|
|
return
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
for i, u := range units {
|
|
|
|
data.DNSQueries[i] += u.NTotal
|
|
|
|
data.BlockedFiltering[i] += u.NResult[RFiltered]
|
|
|
|
data.ReplacedSafebrowsing[i] += u.NResult[RSafeBrowsing]
|
|
|
|
data.ReplacedParental[i] += u.NResult[RParental]
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2023-08-23 17:09:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fillCollectedStatsDaily fills data with collected daily statistics. units
|
|
|
|
// must contain data for the count of days.
|
2023-12-20 19:16:22 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Improve collection of statistics for frontend. Dashboard
|
|
|
|
// cards should contain statistics for the whole interval without rounding to
|
|
|
|
// days.
|
2023-08-23 17:09:42 +03:00
|
|
|
func (s *StatsCtx) fillCollectedStatsDaily(
|
|
|
|
data *StatsResp,
|
|
|
|
units []*unitDB,
|
|
|
|
curHour uint32,
|
|
|
|
days int,
|
|
|
|
) {
|
|
|
|
// Per time unit counters: 720 hours may span 31 days, so we skip data for
|
|
|
|
// the first hours in this case. align_ceil(24)
|
|
|
|
hours := countHours(curHour, days)
|
|
|
|
units = units[len(units)-hours:]
|
|
|
|
|
2024-04-04 15:52:39 +03:00
|
|
|
for i, u := range units {
|
2023-08-23 17:09:42 +03:00
|
|
|
day := i / 24
|
|
|
|
|
|
|
|
data.DNSQueries[day] += u.NTotal
|
|
|
|
data.BlockedFiltering[day] += u.NResult[RFiltered]
|
|
|
|
data.ReplacedSafebrowsing[day] += u.NResult[RSafeBrowsing]
|
|
|
|
data.ReplacedParental[day] += u.NResult[RParental]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// countHours returns the number of hours in the last days.
|
|
|
|
func countHours(curHour uint32, days int) (n int) {
|
|
|
|
hoursInCurDay := int(curHour % 24)
|
|
|
|
if hoursInCurDay == 0 {
|
|
|
|
hoursInCurDay = 24
|
|
|
|
}
|
|
|
|
|
|
|
|
hoursInRestDays := (days - 1) * 24
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2023-08-23 17:09:42 +03:00
|
|
|
return hoursInRestDays + hoursInCurDay
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2023-04-14 15:25:04 +03:00
|
|
|
|
|
|
|
func topClientPairs(s *StatsCtx) (pg pairsGetter) {
|
|
|
|
return func(u *unitDB) (clients []countPair) {
|
|
|
|
for _, c := range u.Clients {
|
|
|
|
if c.Name != "" && !s.shouldCountClient([]string{c.Name}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
clients = append(clients, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return clients
|
|
|
|
}
|
|
|
|
}
|
2023-08-09 14:33:52 +03:00
|
|
|
|
|
|
|
// topUpstreamsPairs returns sorted lists of number of total responses and the
|
|
|
|
// average of processing time for each upstream.
|
|
|
|
func topUpstreamsPairs(
|
|
|
|
units []*unitDB,
|
|
|
|
) (topUpstreamsResponses []topAddrs, topUpstreamsAvgTime []topAddrsFloat) {
|
|
|
|
upstreamsResponses := topAddrs{}
|
|
|
|
upstreamsTimeSum := topAddrsFloat{}
|
|
|
|
|
|
|
|
for _, u := range units {
|
|
|
|
for _, cp := range u.UpstreamsResponses {
|
|
|
|
upstreamsResponses[cp.Name] += cp.Count
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cp := range u.UpstreamsTimeSum {
|
|
|
|
upstreamsTimeSum[cp.Name] += float64(cp.Count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreamsAvgTime := topAddrsFloat{}
|
|
|
|
|
|
|
|
for u, n := range upstreamsResponses {
|
|
|
|
total := upstreamsTimeSum[u]
|
|
|
|
|
|
|
|
if total != 0 {
|
|
|
|
upstreamsAvgTime[u] = microsecondsToSeconds(total / float64(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreamsPairs := convertMapToSlice(upstreamsResponses, maxUpstreams)
|
|
|
|
topUpstreamsResponses = convertTopSlice(upstreamsPairs)
|
|
|
|
|
|
|
|
return topUpstreamsResponses, prepareTopUpstreamsAvgTime(upstreamsAvgTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// microsecondsToSeconds converts microseconds to seconds.
|
|
|
|
//
|
|
|
|
// NOTE: Frontend expects time duration in seconds as floating-point number
|
|
|
|
// with double precision.
|
|
|
|
func microsecondsToSeconds(n float64) (r float64) {
|
|
|
|
const micro = 1e-6
|
|
|
|
|
|
|
|
return n * micro
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareTopUpstreamsAvgTime returns sorted list of average processing times
|
|
|
|
// of the DNS requests from each upstream.
|
|
|
|
func prepareTopUpstreamsAvgTime(
|
|
|
|
upstreamsAvgTime topAddrsFloat,
|
|
|
|
) (topUpstreamsAvgTime []topAddrsFloat) {
|
|
|
|
keys := maps.Keys(upstreamsAvgTime)
|
|
|
|
|
2023-08-10 20:00:17 +03:00
|
|
|
slices.SortFunc(keys, func(a, b string) (res int) {
|
|
|
|
switch x, y := upstreamsAvgTime[a], upstreamsAvgTime[b]; {
|
|
|
|
case x > y:
|
|
|
|
return -1
|
|
|
|
case x < y:
|
|
|
|
return +1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
2023-08-09 14:33:52 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
topUpstreamsAvgTime = make([]topAddrsFloat, 0, len(upstreamsAvgTime))
|
|
|
|
for _, k := range keys {
|
|
|
|
topUpstreamsAvgTime = append(topUpstreamsAvgTime, topAddrsFloat{k: upstreamsAvgTime[k]})
|
|
|
|
}
|
|
|
|
|
|
|
|
return topUpstreamsAvgTime
|
|
|
|
}
|