Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Jul 27, 2023
1 parent 2a48001 commit 8a3d6b1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ See also the [v0.107.35 GitHub milestone][ms-v0.107.35].
NOTE: Add new changes BELOW THIS COMMENT.
-->

### Added

- Two new metrics showing total number of responses from each upstream DNS
server and their average processing time in the Web UI ([#1453]).

### Fixed

- `bufio.Scanner: token too long` and other errors when trying to add
Expand All @@ -34,6 +39,7 @@ NOTE: Add new changes BELOW THIS COMMENT.
- Default exposure of the non-standard ports 784 and 8853 for DNS-over-QUIC in
the `Dockerfile`.

[#1453]: https://github.com/AdguardTeam/AdGuardHome/issues/1453
[#6003]: https://github.com/AdguardTeam/AdGuardHome/issues/6003

<!--
Expand Down
27 changes: 9 additions & 18 deletions internal/stats/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type unit struct {
// upstreamsResponses stores the number of responses from each upstream.
upstreamsResponses map[string]uint64

// upstreamsTimeSum stores the sum of processing time in milliseconds of
// upstreamsTimeSum stores the sum of processing time in microseconds of
// responses from each upstream.
upstreamsTimeSum map[string]uint64

Expand Down Expand Up @@ -154,14 +154,14 @@ type unitDB struct {
// UpstreamsResponses is the number of responses from each upstream.
UpstreamsResponses []countPair

// UpstreamsTimeSum is the sum of processing time in milliseconds of
// UpstreamsTimeSum is the sum of processing time in microseconds of
// responses from each upstream.
UpstreamsTimeSum []countPair

// NTotal is the total number of requests.
NTotal uint64

// TimeAvg is the average of processing times in milliseconds of all the
// TimeAvg is the average of processing times in microseconds of all the
// requests in the unit.
TimeAvg uint32
}
Expand Down Expand Up @@ -302,13 +302,13 @@ func (u *unit) add(e *Entry) {
}

u.clients[e.Client]++
// NOTE: Microseconds for compatibility reasons.
u.timeSum += uint64(e.Time.Microseconds())
t := uint64(e.Time.Microseconds())
u.timeSum += t
u.nTotal++

if e.Upstream != "" {
u.upstreamsResponses[e.Upstream]++
u.upstreamsTimeSum[e.Upstream] += uint64(e.Time.Milliseconds())
u.upstreamsTimeSum[e.Upstream] += t
}
}

Expand Down Expand Up @@ -538,24 +538,15 @@ func topUpstreamsPairs(units []*unitDB) (topUpstreamsResponses, topUpstreamsAvgT
total := upstreamsTimeSum[u]

if total != 0 {
upstreamsAvgTime[u] = total / n
// Calculate average and convert from microseconds to milliseconds.
upstreamsAvgTime[u] = total / n / uint64(time.Microsecond)
}
}

upstreamsPairs := convertMapToSlice(upstreamsResponses, maxUpstreams)
topUpstreamsResponses = convertTopSlice(upstreamsPairs)

// Prepare the list of upstream average processing times by reusing the
// list of upstream responses.
for i, pair := range upstreamsPairs {
upstreamsPairs[i].Count = upstreamsAvgTime[pair.Name]
}

// Sort in ascending order.
slices.SortFunc(upstreamsPairs, func(a, b countPair) (sortsBefore bool) {
return a.Count < b.Count
})

upstreamsPairs = convertMapToSlice(upstreamsAvgTime, maxUpstreams)
topUpstreamsAvgTime = convertTopSlice(upstreamsPairs)

return topUpstreamsResponses, topUpstreamsAvgTime
Expand Down
18 changes: 9 additions & 9 deletions internal/stats/unit_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestUnit_Deserialize(t *testing.T) {
}
}

func Test_TopUpstreamsPairs(t *testing.T) {
func TestTopUpstreamsPairs(t *testing.T) {
testCases := []struct {
db *unitDB
name string
Expand Down Expand Up @@ -129,7 +129,7 @@ func Test_TopUpstreamsPairs(t *testing.T) {
"1.2.3.4": 2,
}},
wantAvgTime: []topAddrs{{
"1.2.3.4": 123456,
"1.2.3.4": 123,
}},
}, {
name: "sorted",
Expand All @@ -147,10 +147,10 @@ func Test_TopUpstreamsPairs(t *testing.T) {
{"1.1.1.1", 2},
},
UpstreamsTimeSum: []countPair{
{"3.3.3.3", 800},
{"2.2.2.2", 40},
{"4.4.4.4", 16000},
{"1.1.1.1", 2},
{"3.3.3.3", 800_000},
{"2.2.2.2", 40_000},
{"4.4.4.4", 16_000_000},
{"1.1.1.1", 2_000},
},
},
wantResponses: []topAddrs{
Expand All @@ -160,10 +160,10 @@ func Test_TopUpstreamsPairs(t *testing.T) {
{"1.1.1.1": 2},
},
wantAvgTime: []topAddrs{
{"1.1.1.1": 1},
{"2.2.2.2": 10},
{"3.3.3.3": 100},
{"4.4.4.4": 1000},
{"3.3.3.3": 100},
{"2.2.2.2": 10},
{"1.1.1.1": 1},
},
}}

Expand Down
11 changes: 5 additions & 6 deletions openapi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

## v0.107.36: API changes

### The new fields `"top_upstreams_responses"` and `"top_upstreams_avg_time"` in `Stats`
### The new fields `"top_upstreams_responses"` and `"top_upstreams_avg_time"` in `Stats` object

* The new optional field `"top_upstreams_responses"` in `GET /control/stats`
method shows the total number of responses from each upstream.
* The new field `"top_upstreams_responses"` in `GET /control/stats` method
shows the total number of responses from each upstream.

* The new optional field `"top_upstrems_avg_time"` in `GET /control/stats`
method shows the average processing time in milliseconds of requests from
each upstream in ascending order.
* The new field `"top_upstrems_avg_time"` in `GET /control/stats` method shows
the average processing time in milliseconds of requests from each upstream.

## v0.107.30: API changes

Expand Down
4 changes: 2 additions & 2 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@
'avg_processing_time':
'type': 'number'
'format': 'float'
'description': 'Average time in milliseconds on processing a DNS'
'description': 'Average time in seconds on processing a DNS request'
'example': 0.34
'top_queried_domains':
'type': 'array'
Expand All @@ -1751,7 +1751,7 @@
'top_upstreams_avg_time':
'type': 'array'
'description': 'Average processing time in milliseconds of requests
from each upstream in ascending order.'
from each upstream.'
'items':
'$ref': '#/components/schemas/TopArrayEntry'
'maxItems': 100
Expand Down

0 comments on commit 8a3d6b1

Please sign in to comment.