2019-09-25 13:36:09 +01:00
|
|
|
// HTTP request handlers for accessing statistics data and configuration settings
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-10-07 10:03:56 +01:00
|
|
|
"time"
|
2019-09-25 13:36:09 +01:00
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-12-16 17:54:59 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2023-03-23 10:46:57 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2019-09-25 13:36:09 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-03-23 10:46:57 +00:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2019-09-25 13:36:09 +01:00
|
|
|
)
|
|
|
|
|
2021-06-17 17:44:46 +01: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
|
|
|
|
|
2023-08-09 12:33:52 +01:00
|
|
|
// topAddrsFloat is like [topAddrs] but the value is float64 number.
|
|
|
|
type topAddrsFloat = map[string]float64
|
|
|
|
|
2022-08-17 12:09:13 +01:00
|
|
|
// StatsResp is a response to the GET /control/stats.
|
|
|
|
type StatsResp struct {
|
2021-01-21 16:55:41 +00:00
|
|
|
TimeUnits string `json:"time_units"`
|
|
|
|
|
2021-06-17 17:44:46 +01:00
|
|
|
TopQueried []topAddrs `json:"top_queried_domains"`
|
|
|
|
TopClients []topAddrs `json:"top_clients"`
|
|
|
|
TopBlocked []topAddrs `json:"top_blocked_domains"`
|
2021-01-21 16:55:41 +00:00
|
|
|
|
2023-08-09 12:33:52 +01:00
|
|
|
TopUpstreamsResponses []topAddrs `json:"top_upstreams_responses"`
|
|
|
|
TopUpstreamsAvgTime []topAddrsFloat `json:"top_upstreams_avg_time"`
|
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
DNSQueries []uint64 `json:"dns_queries"`
|
|
|
|
|
|
|
|
BlockedFiltering []uint64 `json:"blocked_filtering"`
|
|
|
|
ReplacedSafebrowsing []uint64 `json:"replaced_safebrowsing"`
|
|
|
|
ReplacedParental []uint64 `json:"replaced_parental"`
|
2022-08-17 12:09:13 +01:00
|
|
|
|
|
|
|
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-01-21 16:55:41 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
// handleStats is the handler for the GET /control/stats HTTP API.
|
2022-08-04 17:05:28 +01:00
|
|
|
func (s *StatsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 12:09:13 +01:00
|
|
|
start := time.Now()
|
2023-04-05 13:50:14 +01:00
|
|
|
|
|
|
|
var (
|
2023-08-23 15:09:42 +01:00
|
|
|
resp *StatsResp
|
2023-04-05 13:50:14 +01:00
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
func() {
|
|
|
|
s.confMu.RLock()
|
|
|
|
defer s.confMu.RUnlock()
|
|
|
|
|
|
|
|
resp, ok = s.getData(uint32(s.limit.Hours()))
|
|
|
|
}()
|
|
|
|
|
2022-08-04 17:05:28 +01:00
|
|
|
log.Debug("stats: prepared data in %v", time.Since(start))
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// Don't bring the message to the lower case since it's a part of UI
|
|
|
|
// text for the moment.
|
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "Couldn't get statistics data")
|
|
|
|
|
|
|
|
return
|
2019-09-25 13:36:09 +01:00
|
|
|
}
|
|
|
|
|
2023-08-29 18:03:40 +01:00
|
|
|
aghhttp.WriteJSONResponseOK(w, r, resp)
|
2019-09-25 13:36:09 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 12:09:13 +01:00
|
|
|
// configResp is the response to the GET /control/stats_info.
|
|
|
|
type configResp struct {
|
2019-09-25 13:36:09 +01:00
|
|
|
IntervalDays uint32 `json:"interval"`
|
|
|
|
}
|
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
// getConfigResp is the response to the GET /control/stats_info.
|
|
|
|
type getConfigResp struct {
|
|
|
|
// Ignored is the list of host names, which should not be counted.
|
|
|
|
Ignored []string `json:"ignored"`
|
|
|
|
|
|
|
|
// Interval is the statistics rotation interval in milliseconds.
|
|
|
|
Interval float64 `json:"interval"`
|
|
|
|
|
|
|
|
// Enabled shows if statistics are enabled. It is an aghalg.NullBool to be
|
|
|
|
// able to tell when it's set without using pointers.
|
|
|
|
Enabled aghalg.NullBool `json:"enabled"`
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
// handleStatsInfo is the handler for the GET /control/stats_info HTTP API.
|
2023-03-23 10:46:57 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Remove it when migration to the new API is over.
|
2022-08-04 17:05:28 +01:00
|
|
|
func (s *StatsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
2023-04-05 13:50:14 +01:00
|
|
|
var (
|
|
|
|
enabled bool
|
|
|
|
limit time.Duration
|
|
|
|
)
|
|
|
|
func() {
|
|
|
|
s.confMu.RLock()
|
|
|
|
defer s.confMu.RUnlock()
|
|
|
|
|
|
|
|
enabled, limit = s.enabled, s.limit
|
|
|
|
}()
|
|
|
|
|
|
|
|
days := uint32(limit / timeutil.Day)
|
2023-03-23 10:46:57 +00:00
|
|
|
ok := checkInterval(days)
|
2023-04-05 13:50:14 +01:00
|
|
|
if !ok || (enabled && days == 0) {
|
2023-03-23 10:46:57 +00:00
|
|
|
// NOTE: If interval is custom we set it to 90 days for compatibility
|
|
|
|
// with old API.
|
|
|
|
days = 90
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := configResp{IntervalDays: days}
|
2023-04-05 13:50:14 +01:00
|
|
|
if !enabled {
|
2023-02-13 15:15:33 +00:00
|
|
|
resp.IntervalDays = 0
|
|
|
|
}
|
2023-04-05 13:50:14 +01:00
|
|
|
|
2023-08-29 18:03:40 +01:00
|
|
|
aghhttp.WriteJSONResponseOK(w, r, resp)
|
2019-09-25 13:36:09 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
// handleGetStatsConfig is the handler for the GET /control/stats/config HTTP
|
|
|
|
// API.
|
2023-03-23 10:46:57 +00:00
|
|
|
func (s *StatsCtx) handleGetStatsConfig(w http.ResponseWriter, r *http.Request) {
|
2023-04-05 13:50:14 +01:00
|
|
|
var resp *getConfigResp
|
|
|
|
func() {
|
|
|
|
s.confMu.RLock()
|
|
|
|
defer s.confMu.RUnlock()
|
2023-03-23 10:46:57 +00:00
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
resp = &getConfigResp{
|
|
|
|
Ignored: s.ignored.Values(),
|
|
|
|
Interval: float64(s.limit.Milliseconds()),
|
|
|
|
Enabled: aghalg.BoolToNullBool(s.enabled),
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-29 18:03:40 +01:00
|
|
|
aghhttp.WriteJSONResponseOK(w, r, resp)
|
2023-03-23 10:46:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
// handleStatsConfig is the handler for the POST /control/stats_config HTTP API.
|
2023-03-23 10:46:57 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Remove it when migration to the new API is over.
|
2022-08-04 17:05:28 +01:00
|
|
|
func (s *StatsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 12:09:13 +01:00
|
|
|
reqData := configResp{}
|
2019-09-25 13:36:09 +01:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
|
2019-09-25 13:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkInterval(reqData.IntervalDays) {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
|
2019-09-25 13:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
limit := time.Duration(reqData.IntervalDays) * timeutil.Day
|
|
|
|
|
2023-03-23 10:46:57 +00:00
|
|
|
defer s.configModified()
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
s.confMu.Lock()
|
|
|
|
defer s.confMu.Unlock()
|
2023-03-23 10:46:57 +00:00
|
|
|
|
|
|
|
s.setLimit(limit)
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
// handlePutStatsConfig is the handler for the PUT /control/stats/config/update
|
|
|
|
// HTTP API.
|
2023-03-23 10:46:57 +00:00
|
|
|
func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqData := getConfigResp{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-05 13:13:35 +01:00
|
|
|
engine, err := aghnet.NewIgnoreEngine(reqData.Ignored)
|
2023-03-23 10:46:57 +00:00
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "ignored: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ivl := time.Duration(reqData.Interval) * time.Millisecond
|
|
|
|
err = validateIvl(ivl)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "unsupported interval: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqData.Enabled == aghalg.NBNull {
|
|
|
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "enabled is null")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer s.configModified()
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
s.confMu.Lock()
|
|
|
|
defer s.confMu.Unlock()
|
2023-03-23 10:46:57 +00:00
|
|
|
|
2023-09-05 13:13:35 +01:00
|
|
|
s.ignored = engine
|
2023-03-23 10:46:57 +00:00
|
|
|
s.limit = ivl
|
|
|
|
s.enabled = reqData.Enabled == aghalg.NBTrue
|
2019-09-25 13:36:09 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:50:14 +01:00
|
|
|
// handleStatsReset is the handler for the POST /control/stats_reset HTTP API.
|
2022-08-04 17:05:28 +01:00
|
|
|
func (s *StatsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 12:09:13 +01:00
|
|
|
err := s.clear()
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "stats: %s", err)
|
|
|
|
}
|
2019-09-25 13:36:09 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 12:09:13 +01:00
|
|
|
// initWeb registers the handlers for web endpoints of statistics module.
|
2022-08-04 17:05:28 +01:00
|
|
|
func (s *StatsCtx) initWeb() {
|
|
|
|
if s.httpRegister == nil {
|
2019-09-25 13:36:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-04 17:05:28 +01:00
|
|
|
s.httpRegister(http.MethodGet, "/control/stats", s.handleStats)
|
|
|
|
s.httpRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
|
2023-03-23 10:46:57 +00:00
|
|
|
s.httpRegister(http.MethodGet, "/control/stats/config", s.handleGetStatsConfig)
|
|
|
|
s.httpRegister(http.MethodPut, "/control/stats/config/update", s.handlePutStatsConfig)
|
2023-04-05 13:50:14 +01:00
|
|
|
|
|
|
|
// Deprecated handlers.
|
|
|
|
s.httpRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
|
|
|
|
s.httpRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
|
2019-09-25 13:36:09 +01:00
|
|
|
}
|