Skip to content

Commit

Permalink
Fix & refactor adguard stats
Browse files Browse the repository at this point in the history
  • Loading branch information
svilenmarkov committed Sep 10, 2024
1 parent adef350 commit db1ed9e
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions internal/feed/adguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ func FetchAdguardStats(instanceURL, username, password string) (*DNSStats, error
return nil, err
}

var topBlockedDomainsCount = min(len(responseJson.TopBlockedDomains), 5)

stats := &DNSStats{
TotalQueries: responseJson.TotalQueries,
BlockedQueries: responseJson.BlockedQueries,
ResponseTime: int(responseJson.ResponseTime * 1000),
TotalQueries: responseJson.TotalQueries,
BlockedQueries: responseJson.BlockedQueries,
ResponseTime: int(responseJson.ResponseTime * 1000),
TopBlockedDomains: make([]DNSStatsBlockedDomain, 0, topBlockedDomainsCount),
}

if stats.TotalQueries <= 0 {
Expand All @@ -43,8 +46,6 @@ func FetchAdguardStats(instanceURL, username, password string) (*DNSStats, error

stats.BlockedPercent = int(float64(responseJson.BlockedQueries) / float64(responseJson.TotalQueries) * 100)

var topBlockedDomainsCount = min(len(responseJson.TopBlockedDomains), 5)

for i := 0; i < topBlockedDomainsCount; i++ {
domain := responseJson.TopBlockedDomains[i]
var firstDomain string
Expand All @@ -59,39 +60,59 @@ func FetchAdguardStats(instanceURL, username, password string) (*DNSStats, error
}

stats.TopBlockedDomains = append(stats.TopBlockedDomains, DNSStatsBlockedDomain{
Domain: firstDomain,
PercentBlocked: int(float64(domain[firstDomain]) / float64(responseJson.BlockedQueries) * 100),
Domain: firstDomain,
})

if stats.BlockedQueries > 0 {
stats.TopBlockedDomains[i].PercentBlocked = int(float64(domain[firstDomain]) / float64(responseJson.BlockedQueries) * 100)
}
}

// Adguard _should_ return data for the last 24 hours in a 1 hour interval
if len(responseJson.QueriesSeries) != 24 || len(responseJson.BlockedSeries) != 24 {
return stats, nil
queriesSeries := responseJson.QueriesSeries
blockedSeries := responseJson.BlockedSeries

const bars = 8
const hoursSpan = 24
const hoursPerBar int = hoursSpan / bars

if len(queriesSeries) > hoursSpan {
queriesSeries = queriesSeries[len(queriesSeries)-hoursSpan:]
} else if len(queriesSeries) < hoursSpan {
queriesSeries = append(make([]int, hoursSpan-len(queriesSeries)), queriesSeries...)
}

if len(blockedSeries) > hoursSpan {
blockedSeries = blockedSeries[len(blockedSeries)-hoursSpan:]
} else if len(blockedSeries) < hoursSpan {
blockedSeries = append(make([]int, hoursSpan-len(blockedSeries)), blockedSeries...)
}

maxQueriesInSeries := 0

for i := 0; i < 8; i++ {
for i := 0; i < bars; i++ {
queries := 0
blocked := 0

for j := 0; j < 3; j++ {
queries += responseJson.QueriesSeries[i*3+j]
blocked += responseJson.BlockedSeries[i*3+j]
for j := 0; j < hoursPerBar; j++ {
queries += queriesSeries[i*hoursPerBar+j]
blocked += blockedSeries[i*hoursPerBar+j]
}

stats.Series[i] = DNSStatsSeries{
Queries: queries,
Blocked: blocked,
PercentBlocked: int(float64(blocked) / float64(queries) * 100),
Queries: queries,
Blocked: blocked,
}

if queries > 0 {
stats.Series[i].PercentBlocked = int(float64(blocked) / float64(queries) * 100)
}

if queries > maxQueriesInSeries {
maxQueriesInSeries = queries
}
}

for i := 0; i < 8; i++ {
for i := 0; i < bars; i++ {
stats.Series[i].PercentTotal = int(float64(stats.Series[i].Queries) / float64(maxQueriesInSeries) * 100)
}

Expand Down

0 comments on commit db1ed9e

Please sign in to comment.