mirror of
https://github.com/glanceapp/glance.git
synced 2024-11-25 01:44:47 +01:00
Fix & refactor adguard stats
This commit is contained in:
parent
adef35049f
commit
db1ed9e257
@ -31,10 +31,13 @@ func FetchAdguardStats(instanceURL, username, password string) (*DNSStats, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var topBlockedDomainsCount = min(len(responseJson.TopBlockedDomains), 5)
|
||||||
|
|
||||||
stats := &DNSStats{
|
stats := &DNSStats{
|
||||||
TotalQueries: responseJson.TotalQueries,
|
TotalQueries: responseJson.TotalQueries,
|
||||||
BlockedQueries: responseJson.BlockedQueries,
|
BlockedQueries: responseJson.BlockedQueries,
|
||||||
ResponseTime: int(responseJson.ResponseTime * 1000),
|
ResponseTime: int(responseJson.ResponseTime * 1000),
|
||||||
|
TopBlockedDomains: make([]DNSStatsBlockedDomain, 0, topBlockedDomainsCount),
|
||||||
}
|
}
|
||||||
|
|
||||||
if stats.TotalQueries <= 0 {
|
if stats.TotalQueries <= 0 {
|
||||||
@ -43,8 +46,6 @@ func FetchAdguardStats(instanceURL, username, password string) (*DNSStats, error
|
|||||||
|
|
||||||
stats.BlockedPercent = int(float64(responseJson.BlockedQueries) / float64(responseJson.TotalQueries) * 100)
|
stats.BlockedPercent = int(float64(responseJson.BlockedQueries) / float64(responseJson.TotalQueries) * 100)
|
||||||
|
|
||||||
var topBlockedDomainsCount = min(len(responseJson.TopBlockedDomains), 5)
|
|
||||||
|
|
||||||
for i := 0; i < topBlockedDomainsCount; i++ {
|
for i := 0; i < topBlockedDomainsCount; i++ {
|
||||||
domain := responseJson.TopBlockedDomains[i]
|
domain := responseJson.TopBlockedDomains[i]
|
||||||
var firstDomain string
|
var firstDomain string
|
||||||
@ -59,31 +60,51 @@ func FetchAdguardStats(instanceURL, username, password string) (*DNSStats, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
stats.TopBlockedDomains = append(stats.TopBlockedDomains, DNSStatsBlockedDomain{
|
stats.TopBlockedDomains = append(stats.TopBlockedDomains, DNSStatsBlockedDomain{
|
||||||
Domain: firstDomain,
|
Domain: firstDomain,
|
||||||
PercentBlocked: int(float64(domain[firstDomain]) / float64(responseJson.BlockedQueries) * 100),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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
|
queriesSeries := responseJson.QueriesSeries
|
||||||
if len(responseJson.QueriesSeries) != 24 || len(responseJson.BlockedSeries) != 24 {
|
blockedSeries := responseJson.BlockedSeries
|
||||||
return stats, nil
|
|
||||||
|
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
|
maxQueriesInSeries := 0
|
||||||
|
|
||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < bars; i++ {
|
||||||
queries := 0
|
queries := 0
|
||||||
blocked := 0
|
blocked := 0
|
||||||
|
|
||||||
for j := 0; j < 3; j++ {
|
for j := 0; j < hoursPerBar; j++ {
|
||||||
queries += responseJson.QueriesSeries[i*3+j]
|
queries += queriesSeries[i*hoursPerBar+j]
|
||||||
blocked += responseJson.BlockedSeries[i*3+j]
|
blocked += blockedSeries[i*hoursPerBar+j]
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.Series[i] = DNSStatsSeries{
|
stats.Series[i] = DNSStatsSeries{
|
||||||
Queries: queries,
|
Queries: queries,
|
||||||
Blocked: blocked,
|
Blocked: blocked,
|
||||||
PercentBlocked: int(float64(blocked) / float64(queries) * 100),
|
}
|
||||||
|
|
||||||
|
if queries > 0 {
|
||||||
|
stats.Series[i].PercentBlocked = int(float64(blocked) / float64(queries) * 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
if queries > maxQueriesInSeries {
|
if queries > maxQueriesInSeries {
|
||||||
@ -91,7 +112,7 @@ func FetchAdguardStats(instanceURL, username, password string) (*DNSStats, error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
stats.Series[i].PercentTotal = int(float64(stats.Series[i].Queries) / float64(maxQueriesInSeries) * 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user