2019-09-25 05:36:09 -07:00
|
|
|
// HTTP request handlers for accessing statistics data and configuration settings
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-10-07 02:03:56 -07:00
|
|
|
"time"
|
2019-09-25 05:36:09 -07:00
|
|
|
|
2021-12-16 10:54:59 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2019-09-25 05:36:09 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2021-06-17 09:44:46 -07:00
|
|
|
// topAddrs is an alias for the types of the TopFoo fields of statsResponse.
|
|
|
|
// The key is either a client's address or a requested address.
|
|
|
|
type topAddrs = map[string]uint64
|
|
|
|
|
2021-01-21 09:55:41 -07:00
|
|
|
// statsResponse is a response for getting statistics.
|
|
|
|
type statsResponse struct {
|
|
|
|
TimeUnits string `json:"time_units"`
|
|
|
|
|
|
|
|
NumDNSQueries uint64 `json:"num_dns_queries"`
|
|
|
|
NumBlockedFiltering uint64 `json:"num_blocked_filtering"`
|
|
|
|
NumReplacedSafebrowsing uint64 `json:"num_replaced_safebrowsing"`
|
|
|
|
NumReplacedSafesearch uint64 `json:"num_replaced_safesearch"`
|
|
|
|
NumReplacedParental uint64 `json:"num_replaced_parental"`
|
|
|
|
|
|
|
|
AvgProcessingTime float64 `json:"avg_processing_time"`
|
|
|
|
|
2021-06-17 09:44:46 -07:00
|
|
|
TopQueried []topAddrs `json:"top_queried_domains"`
|
|
|
|
TopClients []topAddrs `json:"top_clients"`
|
|
|
|
TopBlocked []topAddrs `json:"top_blocked_domains"`
|
2021-01-21 09:55:41 -07:00
|
|
|
|
|
|
|
DNSQueries []uint64 `json:"dns_queries"`
|
|
|
|
|
|
|
|
BlockedFiltering []uint64 `json:"blocked_filtering"`
|
|
|
|
ReplacedSafebrowsing []uint64 `json:"replaced_safebrowsing"`
|
|
|
|
ReplacedParental []uint64 `json:"replaced_parental"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleStats is a handler for getting statistics.
|
2019-09-25 05:36:09 -07:00
|
|
|
func (s *statsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
|
2019-10-07 02:03:56 -07:00
|
|
|
start := time.Now()
|
2019-09-25 05:36:09 -07:00
|
|
|
|
2021-06-17 09:44:46 -07:00
|
|
|
var resp statsResponse
|
|
|
|
if s.conf.limit == 0 {
|
|
|
|
resp = statsResponse{
|
|
|
|
TimeUnits: "days",
|
2019-09-25 05:36:09 -07:00
|
|
|
|
2021-06-17 09:44:46 -07:00
|
|
|
TopBlocked: []topAddrs{},
|
|
|
|
TopClients: []topAddrs{},
|
|
|
|
TopQueried: []topAddrs{},
|
|
|
|
|
|
|
|
BlockedFiltering: []uint64{},
|
|
|
|
DNSQueries: []uint64{},
|
|
|
|
ReplacedParental: []uint64{},
|
|
|
|
ReplacedSafebrowsing: []uint64{},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var ok bool
|
|
|
|
resp, ok = s.getData()
|
|
|
|
|
|
|
|
log.Debug("stats: prepared data in %v", time.Since(start))
|
|
|
|
|
|
|
|
if !ok {
|
2021-12-16 10:54:59 -07:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "Couldn't get statistics data")
|
2021-06-17 09:44:46 -07:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-09-25 05:36:09 -07:00
|
|
|
}
|
|
|
|
|
2019-10-21 07:50:44 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-06-17 09:44:46 -07:00
|
|
|
|
|
|
|
err := json.NewEncoder(w).Encode(resp)
|
2020-12-07 04:32:06 -07:00
|
|
|
if err != nil {
|
2021-12-16 10:54:59 -07:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
2020-12-07 04:32:06 -07:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-09-25 05:36:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
IntervalDays uint32 `json:"interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get configuration
|
|
|
|
func (s *statsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp := config{}
|
2019-12-11 02:38:58 -07:00
|
|
|
resp.IntervalDays = s.conf.limit / 24
|
2019-09-25 05:36:09 -07:00
|
|
|
|
|
|
|
data, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
2021-12-16 10:54:59 -07:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
|
|
|
|
2019-09-25 05:36:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err = w.Write(data)
|
|
|
|
if err != nil {
|
2021-12-16 10:54:59 -07:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "http write: %s", err)
|
2019-09-25 05:36:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set configuration
|
|
|
|
func (s *statsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqData := config{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
2021-12-16 10:54:59 -07:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
|
2019-09-25 05:36:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkInterval(reqData.IntervalDays) {
|
2021-12-16 10:54:59 -07:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
|
2019-09-25 05:36:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.setLimit(int(reqData.IntervalDays))
|
|
|
|
s.conf.ConfigModified()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset data
|
|
|
|
func (s *statsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
|
|
|
s.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register web handlers
|
|
|
|
func (s *statsCtx) initWeb() {
|
|
|
|
if s.conf.HTTPRegister == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-04 04:15:34 -07:00
|
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/stats", s.handleStats)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
|
|
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
|
2019-09-25 05:36:09 -07:00
|
|
|
}
|