all: add todo

This commit is contained in:
Stanislav Chzhen 2023-02-16 14:13:40 +03:00
parent 85190f7297
commit 3203300e7a
7 changed files with 31 additions and 29 deletions

View File

@ -486,7 +486,7 @@ func (c *configuration) write() (err error) {
if Context.stats != nil { if Context.stats != nil {
statsConf := stats.Config{} statsConf := stats.Config{}
Context.stats.WriteDiskConfig(&statsConf) Context.stats.WriteDiskConfig(&statsConf)
config.Stats.Interval = timeutil.Duration{Duration: statsConf.LimitDays} config.Stats.Interval = timeutil.Duration{Duration: statsConf.LimitIvl}
config.Stats.Enabled = statsConf.Enabled config.Stats.Enabled = statsConf.Enabled
config.Stats.Ignored = statsConf.Ignored.Values() config.Stats.Ignored = statsConf.Ignored.Values()
sort.Strings(config.Stats.Ignored) sort.Strings(config.Stats.Ignored)

View File

@ -51,7 +51,7 @@ func initDNS() (err error) {
statsConf := stats.Config{ statsConf := stats.Config{
Filename: filepath.Join(baseDir, "stats.db"), Filename: filepath.Join(baseDir, "stats.db"),
LimitDays: config.Stats.Interval.Duration, LimitIvl: config.Stats.Interval.Duration,
ConfigModified: onConfigModified, ConfigModified: onConfigModified,
HTTPRegister: httpRegister, HTTPRegister: httpRegister,
Enabled: config.Stats.Enabled, Enabled: config.Stats.Enabled,

View File

@ -57,6 +57,7 @@ type configJSONv2 struct {
// Register web handlers // Register web handlers
func (l *queryLog) initWeb() { func (l *queryLog) initWeb() {
// TODO(s.chzhen): Remove deprecated API.
l.conf.HTTPRegister(http.MethodGet, "/control/querylog", l.handleQueryLog) l.conf.HTTPRegister(http.MethodGet, "/control/querylog", l.handleQueryLog)
l.conf.HTTPRegister(http.MethodGet, "/control/querylog_info", l.handleQueryLogInfo) l.conf.HTTPRegister(http.MethodGet, "/control/querylog_info", l.handleQueryLogInfo)
l.conf.HTTPRegister(http.MethodPost, "/control/querylog_clear", l.handleQueryLogClear) l.conf.HTTPRegister(http.MethodPost, "/control/querylog_clear", l.handleQueryLogClear)

View File

@ -47,7 +47,7 @@ func (s *StatsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
defer s.lock.Unlock() defer s.lock.Unlock()
start := time.Now() start := time.Now()
resp, ok := s.getData(uint32(s.limitHours.Hours())) resp, ok := s.getData(uint32(s.limitIvl.Hours()))
log.Debug("stats: prepared data in %v", time.Since(start)) log.Debug("stats: prepared data in %v", time.Since(start))
if !ok { if !ok {
@ -84,7 +84,7 @@ func (s *StatsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
days := uint32(s.limitHours.Hours() / 24) days := uint32(s.limitIvl.Hours() / 24)
ok := checkInterval(days) ok := checkInterval(days)
if !ok || (s.enabled && days == 0) { if !ok || (s.enabled && days == 0) {
@ -106,7 +106,7 @@ func (s *StatsCtx) handleStatsInfoV2(w http.ResponseWriter, r *http.Request) {
resp := configRespV2{ resp := configRespV2{
Enabled: aghalg.BoolToNullBool(s.enabled), Enabled: aghalg.BoolToNullBool(s.enabled),
Interval: float64(s.limitHours.Milliseconds()), Interval: float64(s.limitIvl.Milliseconds()),
Ignored: s.ignored.Values(), Ignored: s.ignored.Values(),
} }
err := aghhttp.WriteJSONResponse(w, r, resp) err := aghhttp.WriteJSONResponse(w, r, resp)
@ -166,7 +166,7 @@ func (s *StatsCtx) handleStatsConfigV2(w http.ResponseWriter, r *http.Request) {
s.enabled = reqData.Enabled == aghalg.NBTrue s.enabled = reqData.Enabled == aghalg.NBTrue
s.limitHours = ivl s.limitIvl = ivl
if len(reqData.Ignored) > 0 { if len(reqData.Ignored) > 0 {
set, serr := aghnet.NewDomainNameSet(reqData.Ignored) set, serr := aghnet.NewDomainNameSet(reqData.Ignored)
@ -194,6 +194,7 @@ func (s *StatsCtx) initWeb() {
return return
} }
// TODO(s.chzhen): Remove deprecated API.
s.httpRegister(http.MethodGet, "/control/stats", s.handleStats) s.httpRegister(http.MethodGet, "/control/stats", s.handleStats)
s.httpRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset) s.httpRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
s.httpRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig) s.httpRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)

View File

@ -43,9 +43,9 @@ type Config struct {
// Filename is the name of the database file. // Filename is the name of the database file.
Filename string Filename string
// LimitDays is the maximum number of days to collect statistics into the // LimitIvl is the interval for collecting statistics into the current
// current unit. // unit.
LimitDays time.Duration LimitIvl time.Duration
// Enabled tells if the statistics are enabled. // Enabled tells if the statistics are enabled.
Enabled bool Enabled bool
@ -106,9 +106,9 @@ type StatsCtx struct {
// enabled tells if the statistics are enabled. // enabled tells if the statistics are enabled.
enabled bool enabled bool
// limitHours is the maximum number of hours to collect statistics into the // limitIvl is the interval for collecting statistics into the current
// current unit. // unit.
limitHours time.Duration limitIvl time.Duration
// ignored is the list of host names, which should not be counted. // ignored is the list of host names, which should not be counted.
ignored *stringutil.Set ignored *stringutil.Set
@ -128,13 +128,13 @@ func New(conf Config) (s *StatsCtx, err error) {
ignored: conf.Ignored, ignored: conf.Ignored,
} }
if conf.LimitDays < time.Hour { if conf.LimitIvl < time.Hour {
return nil, errors.Error("interval: less than an hour") return nil, errors.Error("interval: less than an hour")
} }
if conf.LimitDays > timeutil.Day*365 { if conf.LimitIvl > timeutil.Day*365 {
return nil, errors.Error("interval: more than a year") return nil, errors.Error("interval: more than a year")
} }
s.limitHours = conf.LimitDays s.limitIvl = conf.LimitIvl
if s.unitIDGen = newUnitID; conf.UnitID != nil { if s.unitIDGen = newUnitID; conf.UnitID != nil {
s.unitIDGen = conf.UnitID s.unitIDGen = conf.UnitID
@ -155,7 +155,7 @@ func New(conf Config) (s *StatsCtx, err error) {
return nil, fmt.Errorf("stats: opening a transaction: %w", err) return nil, fmt.Errorf("stats: opening a transaction: %w", err)
} }
deleted := deleteOldUnits(tx, id-uint32(s.limitHours.Hours())-1) deleted := deleteOldUnits(tx, id-uint32(s.limitIvl.Hours())-1)
udb = loadUnitFromDB(tx, id) udb = loadUnitFromDB(tx, id)
err = finishTxn(tx, deleted > 0) err = finishTxn(tx, deleted > 0)
@ -236,7 +236,7 @@ func (s *StatsCtx) Update(e Entry) {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
if !s.enabled || s.limitHours == 0 { if !s.enabled || s.limitIvl == 0 {
return return
} }
@ -268,7 +268,7 @@ func (s *StatsCtx) WriteDiskConfig(dc *Config) {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
dc.LimitDays = s.limitHours / 24 dc.LimitIvl = s.limitIvl / 24
dc.Enabled = s.enabled dc.Enabled = s.enabled
dc.Ignored = s.ignored dc.Ignored = s.ignored
} }
@ -278,7 +278,7 @@ func (s *StatsCtx) TopClientsIP(maxCount uint) (ips []netip.Addr) {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
limit := uint32(s.limitHours.Hours()) limit := uint32(s.limitIvl.Hours())
if !s.enabled || limit == 0 { if !s.enabled || limit == 0 {
return nil return nil
} }
@ -382,7 +382,7 @@ func (s *StatsCtx) flush() (cont bool, sleepFor time.Duration) {
return false, 0 return false, 0
} }
limit := uint32(s.limitHours.Hours()) limit := uint32(s.limitIvl.Hours())
if limit == 0 || ptr.id == id { if limit == 0 || ptr.id == id {
return true, time.Second return true, time.Second
} }
@ -445,7 +445,7 @@ func (s *StatsCtx) periodicFlush() {
func (s *StatsCtx) setLimit(limitDays int) { func (s *StatsCtx) setLimit(limitDays int) {
if limitDays != 0 { if limitDays != 0 {
s.enabled = true s.enabled = true
s.limitHours = time.Duration(int(timeutil.Day) * limitDays) s.limitIvl = time.Duration(int(timeutil.Day) * limitDays)
log.Debug("stats: set limit: %d days", limitDays) log.Debug("stats: set limit: %d days", limitDays)
return return

View File

@ -37,7 +37,7 @@ func TestStats_races(t *testing.T) {
conf := Config{ conf := Config{
UnitID: idGen, UnitID: idGen,
Filename: filepath.Join(t.TempDir(), "./stats.db"), Filename: filepath.Join(t.TempDir(), "./stats.db"),
LimitDays: time.Hour * 24, LimitIvl: time.Hour * 24,
} }
s, err := New(conf) s, err := New(conf)

View File

@ -53,7 +53,7 @@ func TestStats(t *testing.T) {
handlers := map[string]http.Handler{} handlers := map[string]http.Handler{}
conf := stats.Config{ conf := stats.Config{
Filename: filepath.Join(t.TempDir(), "stats.db"), Filename: filepath.Join(t.TempDir(), "stats.db"),
LimitDays: time.Hour * 24, LimitIvl: time.Hour * 24,
Enabled: true, Enabled: true,
UnitID: constUnitID, UnitID: constUnitID,
HTTPRegister: func(_, url string, handler http.HandlerFunc) { HTTPRegister: func(_, url string, handler http.HandlerFunc) {
@ -159,7 +159,7 @@ func TestLargeNumbers(t *testing.T) {
conf := stats.Config{ conf := stats.Config{
Filename: filepath.Join(t.TempDir(), "stats.db"), Filename: filepath.Join(t.TempDir(), "stats.db"),
LimitDays: time.Hour * 24, LimitIvl: time.Hour * 24,
Enabled: true, Enabled: true,
UnitID: func() (id uint32) { return atomic.LoadUint32(&curHour) }, UnitID: func() (id uint32) { return atomic.LoadUint32(&curHour) },
HTTPRegister: func(_, url string, handler http.HandlerFunc) { handlers[url] = handler }, HTTPRegister: func(_, url string, handler http.HandlerFunc) { handlers[url] = handler },