2020-12-07 14:32:06 +03:00
|
|
|
// Package stats provides units for managing statistics of the filtering DNS
|
|
|
|
// server.
|
2019-08-22 16:34:58 +03:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2022-08-04 19:05:28 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2019-08-22 16:34:58 +03:00
|
|
|
)
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// UnitIDGenFunc is the signature of a function that generates a unique ID for
|
|
|
|
// the statistics unit.
|
|
|
|
type UnitIDGenFunc func() (id uint32)
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// DiskConfig is the configuration structure that is stored in file.
|
2019-09-25 15:36:09 +03:00
|
|
|
type DiskConfig struct {
|
2022-08-04 19:05:28 +03:00
|
|
|
// Interval is the number of days for which the statistics are collected
|
|
|
|
// before flushing to the database.
|
|
|
|
Interval uint32 `yaml:"statistics_interval"`
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Config is the configuration structure for the statistics collecting.
|
2019-09-16 16:14:52 +03:00
|
|
|
type Config struct {
|
2022-08-04 19:05:28 +03:00
|
|
|
// UnitID is the function to generate the identifier for current unit. If
|
|
|
|
// nil, the default function is used, see newUnitID.
|
|
|
|
UnitID UnitIDGenFunc
|
2019-09-25 15:36:09 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// ConfigModified will be called each time the configuration changed via web
|
|
|
|
// interface.
|
2019-09-25 15:36:09 +03:00
|
|
|
ConfigModified func()
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// HTTPRegister is the function that registers handlers for the stats
|
|
|
|
// endpoints.
|
|
|
|
HTTPRegister aghhttp.RegisterFunc
|
2019-12-11 12:38:58 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Filename is the name of the database file.
|
|
|
|
Filename string
|
2019-09-16 16:14:52 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// LimitDays is the maximum number of days to collect statistics into the
|
|
|
|
// current unit.
|
|
|
|
LimitDays uint32
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Interface is the statistics interface to be used by other packages.
|
|
|
|
type Interface interface {
|
|
|
|
// Start begins the statistics collecting.
|
2020-01-16 14:25:40 +03:00
|
|
|
Start()
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Close stops the statistics collecting.
|
2019-08-22 16:34:58 +03:00
|
|
|
Close()
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Update collects the incoming statistics data.
|
2019-08-22 16:34:58 +03:00
|
|
|
Update(e Entry)
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// GetTopClientIP returns at most limit IP addresses corresponding to the
|
|
|
|
// clients with the most number of requests.
|
2021-01-20 17:27:53 +03:00
|
|
|
GetTopClientsIP(limit uint) []net.IP
|
2019-10-07 15:56:33 +03:00
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// WriteDiskConfig puts the Interface's configuration to the dc.
|
2019-09-25 15:36:09 +03:00
|
|
|
WriteDiskConfig(dc *DiskConfig)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// TimeUnit is the unit of measuring time while aggregating the statistics.
|
2019-08-22 16:34:58 +03:00
|
|
|
type TimeUnit int
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Supported TimeUnit values.
|
2019-08-22 16:34:58 +03:00
|
|
|
const (
|
|
|
|
Hours TimeUnit = iota
|
|
|
|
Days
|
|
|
|
)
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Result is the resulting code of processing the DNS request.
|
2019-08-22 16:34:58 +03:00
|
|
|
type Result int
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Supported Result values.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Think about better naming.
|
2019-08-22 16:34:58 +03:00
|
|
|
const (
|
|
|
|
RNotFiltered Result = iota + 1
|
|
|
|
RFiltered
|
|
|
|
RSafeBrowsing
|
|
|
|
RSafeSearch
|
|
|
|
RParental
|
2022-08-04 19:05:28 +03:00
|
|
|
|
|
|
|
resultLast = RParental + 1
|
2019-08-22 16:34:58 +03:00
|
|
|
)
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
// Entry is a statistics data entry.
|
2019-08-22 16:34:58 +03:00
|
|
|
type Entry struct {
|
2021-01-27 18:32:13 +03:00
|
|
|
// Clients is the client's primary ID.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Make this a {net.IP, string} enum?
|
|
|
|
Client string
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
// Domain is the domain name requested.
|
2019-08-22 16:34:58 +03:00
|
|
|
Domain string
|
2022-08-04 19:05:28 +03:00
|
|
|
|
|
|
|
// Result is the result of processing the request.
|
2019-08-22 16:34:58 +03:00
|
|
|
Result Result
|
2022-08-04 19:05:28 +03:00
|
|
|
|
|
|
|
// Time is the duration of the request processing in milliseconds.
|
|
|
|
Time uint32
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|