Skip to content

Commit

Permalink
fix changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Feb 9, 2023
1 parent 23c3b6d commit a193509
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 24 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ NOTE: Add new changes BELOW THIS COMMENT.

### Added

- The ability to disable statistics by using the new `statistics.enable` field.
- The ability to exclude domain names from the query log or statistics by using
the new `querylog.ignored` or `statistics.ignored` fields ([#1717], [#4299]).

- The ability to disable statistics by using the new `statistics.enable` field.

### Changed

#### Configuration Changes
Expand Down
10 changes: 5 additions & 5 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,11 @@ func (c *configuration) write() (err error) {
}

if Context.stats != nil {
sdc := stats.Config{}
Context.stats.WriteDiskConfig(&sdc)
config.Stats.Interval = atomic.LoadUint32(&sdc.LimitDays)
config.Stats.Enabled = sdc.Enabled
config.Stats.Ignored = sdc.Ignored.Values()
statsConf := stats.Config{}
Context.stats.WriteDiskConfig(&statsConf)
config.Stats.Interval = atomic.LoadUint32(&statsConf.LimitDays)
config.Stats.Enabled = statsConf.Enabled
config.Stats.Ignored = statsConf.Ignored.Values()
sort.Strings(config.Stats.Ignored)
}

Expand Down
46 changes: 32 additions & 14 deletions internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,14 @@ func initDNS() (err error) {
ConfigModified: onConfigModified,
HTTPRegister: httpRegister,
Enabled: config.Stats.Enabled,
Ignored: stringutil.NewSet(),
}
for _, v := range config.Stats.Ignored {
host := strings.ToLower(strings.TrimSuffix(v, "."))
if statsConf.Ignored.Has(host) {
return fmt.Errorf("statistics: duplicate ignored host %s", host)
}

statsConf.Ignored.Add(host)
set, err := nonDupEmptyHostNames(config.Stats.Ignored)
if err != nil {
return fmt.Errorf("statistics: ignored list: %w", err)
}

statsConf.Ignored = set
Context.stats, err = stats.New(statsConf)
if err != nil {
return fmt.Errorf("init stats: %w", err)
Expand All @@ -83,16 +81,14 @@ func initDNS() (err error) {
MemSize: config.QueryLog.MemSize,
Enabled: config.QueryLog.Enabled,
FileEnabled: config.QueryLog.FileEnabled,
Ignored: stringutil.NewSet(),
}
for _, v := range config.QueryLog.Ignored {
host := strings.ToLower(strings.TrimSuffix(v, "."))
if conf.Ignored.Has(host) {
return fmt.Errorf("querylog: duplicate ignored host %s", host)
}

conf.Ignored.Add(host)
set, err = nonDupEmptyHostNames(config.QueryLog.Ignored)
if err != nil {
return fmt.Errorf("querylog: ignored list %w", err)
}

conf.Ignored = set
Context.queryLog = querylog.New(conf)

Context.filters, err = filtering.New(config.DNS.DnsfilterConf, nil)
Expand Down Expand Up @@ -536,3 +532,25 @@ func closeDNSServer() {

log.Debug("all dns modules are closed")
}

// nonDupEmptyHostNames returns nil and error, if list has duplicate or empty
// host name. Otherwise returns a set, which contains lowercase host names
// without dot at the end, and nil error.
func nonDupEmptyHostNames(list []string) (set *stringutil.Set, err error) {
set = stringutil.NewSet()

for _, v := range list {
host := strings.ToLower(strings.TrimSuffix(v, "."))
if host == "" {
return nil, errors.Error("host name is empty")
}

if set.Has(host) {
return nil, fmt.Errorf("duplicate host name %q", host)
}

set.Add(host)
}

return set, nil
}
3 changes: 3 additions & 0 deletions internal/home/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,9 @@ func upgradeSchema15to16(diskConf yobj) (err error) {
k := "statistics_interval"
v, has := dns[k]
if has {
if v == 0 {
stats["enabled"] = false
}
stats["interval"] = v
}
delete(dns, k)
Expand Down
16 changes: 16 additions & 0 deletions internal/home/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,22 @@ func TestUpgradeSchema15to16(t *testing.T) {
},
want: defaultWantObj,
name: "default_values",
}, {
in: yobj{
"dns": map[string]any{
"statistics_interval": 0,
},
},
want: yobj{
"statistics": map[string]any{
"enabled": false,
"interval": 0,
"ignored": []any{},
},
"dns": map[string]any{},
"schema_version": newSchemaVer,
},
name: "stats_disabled",
}}

for _, tc := range testCases {
Expand Down
1 change: 1 addition & 0 deletions internal/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ func (s *StatsCtx) setLimit(limitDays int) {
s.enabled = true
atomic.StoreUint32(&s.limitHours, uint32(24*limitDays))
log.Debug("stats: set limit: %d days", limitDays)

return
}

Expand Down
5 changes: 2 additions & 3 deletions internal/stats/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,9 @@ func topsCollector(units []*unitDB, max int, ignored *stringutil.Set, pg pairsGe
m := map[string]uint64{}
for _, u := range units {
for _, cp := range pg(u) {
if ignored.Has(cp.Name) {
continue
if !ignored.Has(cp.Name) {
m[cp.Name] += cp.Count
}
m[cp.Name] += cp.Count
}
}
a2 := convertMapToSlice(m, max)
Expand Down

0 comments on commit a193509

Please sign in to comment.