mirror of
https://github.com/glanceapp/glance.git
synced 2024-11-22 00:13:55 +01:00
Cover edge case when Pihole's privacy settings are enabled
This commit is contained in:
parent
eaf08d42dd
commit
28167403a4
@ -18,6 +18,17 @@ type piholeStatsResponse struct {
|
|||||||
DomainsBlocked int `json:"domains_being_blocked"`
|
DomainsBlocked int `json:"domains_being_blocked"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If user has some level of privacy enabled on Pihole, `json:"top_ads"` is an empty array
|
||||||
|
// Use alternate struct without that field to avoid error when unmarshalling
|
||||||
|
type piholeStatsResponsePrivate struct {
|
||||||
|
TotalQueries int `json:"dns_queries_today"`
|
||||||
|
QueriesSeries map[int64]int `json:"domains_over_time"`
|
||||||
|
BlockedQueries int `json:"ads_blocked_today"`
|
||||||
|
BlockedSeries map[int64]int `json:"ads_over_time"`
|
||||||
|
BlockedPercentage float64 `json:"ads_percentage_today"`
|
||||||
|
DomainsBlocked int `json:"domains_being_blocked"`
|
||||||
|
}
|
||||||
|
|
||||||
func FetchPiholeStats(instanceURL, token string) (*DNSStats, error) {
|
func FetchPiholeStats(instanceURL, token string) (*DNSStats, error) {
|
||||||
if token == "" {
|
if token == "" {
|
||||||
return nil, errors.New("missing API token")
|
return nil, errors.New("missing API token")
|
||||||
@ -31,13 +42,27 @@ func FetchPiholeStats(instanceURL, token string) (*DNSStats, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
responseJson, err := decodeJsonFromRequest[piholeStatsResponse](defaultClient, request)
|
responseJson, err := decodeJsonFromRequest[piholeStatsResponse](defaultClient, request)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
// Refer to piholeStatsResponsePrivate above
|
||||||
|
responseJsonPriv, err :=
|
||||||
|
decodeJsonFromRequest[piholeStatsResponsePrivate](defaultClient, request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the results back to responseJson, leaving the TopBlockedDomains field empty
|
||||||
|
responseJson.TotalQueries = responseJsonPriv.TotalQueries
|
||||||
|
responseJson.QueriesSeries = responseJsonPriv.QueriesSeries
|
||||||
|
responseJson.BlockedQueries = responseJsonPriv.BlockedQueries
|
||||||
|
responseJson.BlockedSeries = responseJsonPriv.BlockedSeries
|
||||||
|
responseJson.BlockedPercentage = responseJsonPriv.BlockedPercentage
|
||||||
|
responseJson.TopBlockedDomains = make(map[string]int)
|
||||||
|
responseJson.DomainsBlocked = responseJsonPriv.DomainsBlocked
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := &DNSStats{
|
stats := &DNSStats{
|
||||||
TotalQueries: responseJson.TotalQueries,
|
TotalQueries: responseJson.TotalQueries,
|
||||||
BlockedQueries: responseJson.BlockedQueries,
|
BlockedQueries: responseJson.BlockedQueries,
|
||||||
|
Loading…
Reference in New Issue
Block a user