* stats: refactor: move HTTP handlers to stats/
DNS module passes additional parameters to Stats module. This allows Stats to handle HTTP requests by itself - completely removing all stats-related code from outside.
This commit is contained in:
@@ -269,9 +269,6 @@ func parseConfig() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !checkStatsInterval(config.DNS.StatsInterval) {
|
||||
config.DNS.StatsInterval = 1
|
||||
}
|
||||
if !checkFiltersUpdateIntervalHours(config.DNS.FiltersUpdateIntervalHours) {
|
||||
config.DNS.FiltersUpdateIntervalHours = 24
|
||||
}
|
||||
@@ -356,6 +353,12 @@ func (c *configuration) write() error {
|
||||
config.Users = config.auth.GetUsers()
|
||||
}
|
||||
|
||||
if config.stats != nil {
|
||||
sdc := stats.DiskConfig{}
|
||||
config.stats.WriteDiskConfig(&sdc)
|
||||
config.DNS.StatsInterval = sdc.Interval
|
||||
}
|
||||
|
||||
configFile := config.getConfigFilename()
|
||||
log.Debug("Writing YAML file: %s", configFile)
|
||||
yamlText, err := yaml.Marshal(&config)
|
||||
|
||||
@@ -569,14 +569,11 @@ func registerControlHandlers() {
|
||||
registerRewritesHandlers()
|
||||
RegisterBlockedServicesHandlers()
|
||||
RegisterQueryLogHandlers()
|
||||
RegisterStatsHandlers()
|
||||
RegisterAuthHandlers()
|
||||
|
||||
http.HandleFunc("/dns-query", postInstall(handleDOH))
|
||||
}
|
||||
|
||||
type httpHandlerType func(http.ResponseWriter, *http.Request)
|
||||
|
||||
func httpRegister(method string, url string, handler httpHandlerType) {
|
||||
func httpRegister(method string, url string, handler func(http.ResponseWriter, *http.Request)) {
|
||||
http.Handle(url, postInstallHandler(optionalAuthHandler(gziphandler.GzipHandler(ensureHandler(method, handler)))))
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
package home
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/stats"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
)
|
||||
|
||||
type statsConfig struct {
|
||||
Interval uint32 `json:"interval"`
|
||||
}
|
||||
|
||||
// Get stats configuration
|
||||
func handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
||||
resp := statsConfig{}
|
||||
resp.Interval = config.DNS.StatsInterval
|
||||
|
||||
jsonVal, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "json encode: %s", err)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(jsonVal)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "http write: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Set stats configuration
|
||||
func handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
||||
reqData := statsConfig{}
|
||||
err := json.NewDecoder(r.Body).Decode(&reqData)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusBadRequest, "json decode: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !checkStatsInterval(reqData.Interval) {
|
||||
httpError(w, http.StatusBadRequest, "Unsupported interval")
|
||||
return
|
||||
}
|
||||
|
||||
config.DNS.StatsInterval = reqData.Interval
|
||||
_ = config.write()
|
||||
|
||||
config.stats.Configure(int(config.DNS.StatsInterval))
|
||||
|
||||
returnOK(w)
|
||||
}
|
||||
|
||||
// handleStats returns aggregated stats data
|
||||
func handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
units := stats.Hours
|
||||
if config.DNS.StatsInterval > 7 {
|
||||
units = stats.Days
|
||||
}
|
||||
counter := log.StartTimer()
|
||||
d := config.stats.GetData(units)
|
||||
counter.LogElapsed("Stats: prepared data")
|
||||
|
||||
if d == nil {
|
||||
httpError(w, http.StatusInternalServerError, "Couldn't get statistics data")
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(d)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "json encode: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
// handleStatsReset resets the stats
|
||||
func handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
||||
config.stats.Clear()
|
||||
returnOK(w)
|
||||
}
|
||||
|
||||
// RegisterStatsHandlers - register handlers
|
||||
func RegisterStatsHandlers() {
|
||||
httpRegister(http.MethodGet, "/control/stats", handleStats)
|
||||
httpRegister(http.MethodPost, "/control/stats_reset", handleStatsReset)
|
||||
httpRegister(http.MethodPost, "/control/stats_config", handleStatsConfig)
|
||||
httpRegister(http.MethodGet, "/control/stats_info", handleStatsInfo)
|
||||
}
|
||||
|
||||
func checkStatsInterval(i uint32) bool {
|
||||
return i == 1 || i == 7 || i == 30 || i == 90
|
||||
}
|
||||
11
home/dns.go
11
home/dns.go
@@ -21,6 +21,11 @@ type dnsContext struct {
|
||||
whois *Whois
|
||||
}
|
||||
|
||||
// Called by other modules when configuration is changed
|
||||
func onConfigModified() {
|
||||
_ = config.write()
|
||||
}
|
||||
|
||||
// initDNSServer creates an instance of the dnsforward.Server
|
||||
// Please note that we must do it even if we don't start it
|
||||
// so that we had access to the query log and the stats
|
||||
@@ -31,8 +36,10 @@ func initDNSServer(baseDir string) {
|
||||
}
|
||||
|
||||
statsConf := stats.Config{
|
||||
Filename: filepath.Join(baseDir, "stats.db"),
|
||||
LimitDays: config.DNS.StatsInterval,
|
||||
Filename: filepath.Join(baseDir, "stats.db"),
|
||||
LimitDays: config.DNS.StatsInterval,
|
||||
ConfigModified: onConfigModified,
|
||||
HTTPRegister: httpRegister,
|
||||
}
|
||||
config.stats, err = stats.New(statsConf)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user