2019-09-25 15:36:09 +03:00
|
|
|
// HTTP request handlers for accessing statistics data and configuration settings
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-08-17 14:09:13 +03:00
|
|
|
"sync/atomic"
|
2019-10-07 12:03:56 +03:00
|
|
|
"time"
|
2019-09-25 15:36:09 +03:00
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2019-09-25 15:36:09 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2021-06-17 19:44:46 +03: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
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// StatsResp is a response to the GET /control/stats.
|
|
|
|
type StatsResp struct {
|
2021-01-21 19:55:41 +03:00
|
|
|
TimeUnits string `json:"time_units"`
|
|
|
|
|
2021-06-17 19:44:46 +03:00
|
|
|
TopQueried []topAddrs `json:"top_queried_domains"`
|
|
|
|
TopClients []topAddrs `json:"top_clients"`
|
|
|
|
TopBlocked []topAddrs `json:"top_blocked_domains"`
|
2021-01-21 19:55:41 +03:00
|
|
|
|
|
|
|
DNSQueries []uint64 `json:"dns_queries"`
|
|
|
|
|
|
|
|
BlockedFiltering []uint64 `json:"blocked_filtering"`
|
|
|
|
ReplacedSafebrowsing []uint64 `json:"replaced_safebrowsing"`
|
|
|
|
ReplacedParental []uint64 `json:"replaced_parental"`
|
2022-08-17 14:09:13 +03: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 19:55:41 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// handleStats handles requests to the GET /control/stats endpoint.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 14:09:13 +03:00
|
|
|
limit := atomic.LoadUint32(&s.limitHours)
|
2022-08-04 19:05:28 +03:00
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
start := time.Now()
|
|
|
|
resp, ok := s.getData(limit)
|
2022-08-04 19:05:28 +03: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 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2019-10-21 17:50:44 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-06-17 19:44:46 +03:00
|
|
|
|
|
|
|
err := json.NewEncoder(w).Encode(resp)
|
2020-12-07 14:32:06 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
2020-12-07 14:32:06 +03:00
|
|
|
}
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// configResp is the response to the GET /control/stats_info.
|
|
|
|
type configResp struct {
|
2019-09-25 15:36:09 +03:00
|
|
|
IntervalDays uint32 `json:"interval"`
|
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// handleStatsInfo handles requests to the GET /control/stats_info endpoint.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 14:09:13 +03:00
|
|
|
resp := configResp{IntervalDays: atomic.LoadUint32(&s.limitHours) / 24}
|
2021-12-16 20:54:59 +03:00
|
|
|
|
2019-09-25 15:36:09 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-08-17 14:09:13 +03:00
|
|
|
|
|
|
|
err := json.NewEncoder(w).Encode(resp)
|
2019-09-25 15:36:09 +03:00
|
|
|
if err != nil {
|
2022-08-17 14:09:13 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// handleStatsConfig handles requests to the POST /control/stats_config
|
|
|
|
// endpoint.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 14:09:13 +03:00
|
|
|
reqData := configResp{}
|
2019-09-25 15:36:09 +03:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
|
2019-09-25 15:36:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkInterval(reqData.IntervalDays) {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
|
2019-09-25 15:36:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.setLimit(int(reqData.IntervalDays))
|
2022-08-04 19:05:28 +03:00
|
|
|
s.configModified()
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// handleStatsReset handles requests to the POST /control/stats_reset endpoint.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 14:09:13 +03:00
|
|
|
err := s.clear()
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "stats: %s", err)
|
|
|
|
}
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:13 +03:00
|
|
|
// initWeb registers the handlers for web endpoints of statistics module.
|
2022-08-04 19:05:28 +03:00
|
|
|
func (s *StatsCtx) initWeb() {
|
|
|
|
if s.httpRegister == nil {
|
2019-09-25 15:36:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:05:28 +03:00
|
|
|
s.httpRegister(http.MethodGet, "/control/stats", s.handleStats)
|
|
|
|
s.httpRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
|
|
|
|
s.httpRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
|
|
|
|
s.httpRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
|
2019-09-25 15:36:09 +03:00
|
|
|
}
|