Pull request: all: make stats disableable, imp code

Updates #2141.

Squashed commit of the following:

commit d8f3bcd9927b00a1d4b8b60b43144bc4b4469311
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jun 17 19:10:53 2021 +0300

    stats: imp docs, names

commit 97eae3c2da5585467ca024bdacdbf922bcc8b444
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jun 17 18:51:49 2021 +0300

    all: make stats disableable, imp code
This commit is contained in:
Ainar Garipov
2021-06-17 19:44:46 +03:00
parent 5104b79cf6
commit dbe8b92dfc
7 changed files with 89 additions and 33 deletions

View File

@@ -147,7 +147,7 @@ func (s *statsCtx) Start() {
}
func checkInterval(days uint32) bool {
return days == 1 || days == 7 || days == 30 || days == 90
return days == 0 || days == 1 || days == 7 || days == 30 || days == 90
}
func (s *statsCtx) dbOpen() bool {
@@ -251,7 +251,7 @@ func (s *statsCtx) periodicFlush() {
}
id := s.conf.UnitID()
if ptr.id == id {
if ptr.id == id || s.conf.limit == 0 {
time.Sleep(time.Second)
continue
@@ -412,9 +412,11 @@ func convertTopSlice(a []countPair) []map[string]uint64 {
}
func (s *statsCtx) setLimit(limitDays int) {
conf := *s.conf
conf.limit = uint32(limitDays) * 24
s.conf = &conf
s.conf.limit = uint32(limitDays) * 24
if limitDays == 0 {
s.clear()
}
log.Debug("stats: set limit: %d", limitDays)
}
@@ -488,6 +490,10 @@ func (s *statsCtx) getClientIP(ip net.IP) (clientIP net.IP) {
}
func (s *statsCtx) Update(e Entry) {
if s.conf.limit == 0 {
return
}
if e.Result == 0 ||
e.Result >= rLast ||
e.Domain == "" ||
@@ -695,6 +701,10 @@ func (s *statsCtx) getData() (statsResponse, bool) {
}
func (s *statsCtx) GetTopClientsIP(maxCount uint) []net.IP {
if s.conf.limit == 0 {
return nil
}
units, _ := s.loadUnits(s.conf.limit)
if units == nil {
return nil