2019-09-25 13:36:09 +01:00
|
|
|
// HTTP request handlers for accessing statistics data and configuration settings
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-10-07 10:03:56 +01:00
|
|
|
"time"
|
2019-09-25 13:36:09 +01:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func httpError(r *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) {
|
|
|
|
text := fmt.Sprintf(format, args...)
|
|
|
|
|
|
|
|
log.Info("Stats: %s %s: %s", r.Method, r.URL, text)
|
|
|
|
|
|
|
|
http.Error(w, text, code)
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:55:41 +00: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"`
|
|
|
|
|
|
|
|
TopQueried []map[string]uint64 `json:"top_queried_domains"`
|
|
|
|
TopClients []map[string]uint64 `json:"top_clients"`
|
|
|
|
TopBlocked []map[string]uint64 `json:"top_blocked_domains"`
|
|
|
|
|
|
|
|
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 13:36:09 +01:00
|
|
|
func (s *statsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
|
2019-10-07 10:03:56 +01:00
|
|
|
start := time.Now()
|
2021-01-21 16:55:41 +00:00
|
|
|
response, ok := s.getData()
|
2019-10-07 10:03:56 +01:00
|
|
|
log.Debug("Stats: prepared data in %v", time.Since(start))
|
2019-09-25 13:36:09 +01:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
if !ok {
|
2019-09-25 13:36:09 +01:00
|
|
|
httpError(r, w, http.StatusInternalServerError, "Couldn't get statistics data")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:50:44 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-01-21 16:55:41 +00:00
|
|
|
err := json.NewEncoder(w).Encode(response)
|
2020-12-07 11:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-09-25 13:36:09 +01: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 09:38:58 +00:00
|
|
|
resp.IntervalDays = s.conf.limit / 24
|
2019-09-25 13:36:09 +01:00
|
|
|
|
|
|
|
data, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err = w.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "http write: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set configuration
|
|
|
|
func (s *statsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqData := config{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkInterval(reqData.IntervalDays) {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
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 11:15:34 +00: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 13:36:09 +01:00
|
|
|
}
|