+ statistics: store in separate file
+ GET /control/stats handler
This commit is contained in:
committed by
Ildar Kamalov
parent
60eb55bdce
commit
4a58266ba3
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
||||
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
|
||||
"github.com/AdguardTeam/AdGuardHome/dnsforward"
|
||||
"github.com/AdguardTeam/AdGuardHome/stats"
|
||||
"github.com/AdguardTeam/golibs/file"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
@@ -68,6 +69,7 @@ type configuration struct {
|
||||
controlLock sync.Mutex
|
||||
transport *http.Transport
|
||||
client *http.Client
|
||||
stats stats.Stats
|
||||
|
||||
// cached version.json to avoid hammering github.io for each page reload
|
||||
versionCheckJSON []byte
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/stats"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
)
|
||||
|
||||
@@ -11,9 +12,8 @@ type statsConfig struct {
|
||||
Interval uint `json:"interval"`
|
||||
}
|
||||
|
||||
// Get stats configuration
|
||||
func handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
||||
log.Tracef("%s %v", r.Method, r.URL)
|
||||
|
||||
resp := statsConfig{}
|
||||
resp.Interval = config.DNS.StatsInterval
|
||||
|
||||
@@ -29,9 +29,8 @@ func handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set stats configuration
|
||||
func handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
||||
log.Tracef("%s %v", r.Method, r.URL)
|
||||
|
||||
reqData := statsConfig{}
|
||||
err := json.NewDecoder(r.Body).Decode(&reqData)
|
||||
if err != nil {
|
||||
@@ -45,8 +44,47 @@ func handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
config.DNS.StatsInterval = reqData.Interval
|
||||
config.stats.Configure(int(config.DNS.StatsInterval))
|
||||
|
||||
httpUpdateConfigReloadDNSReturnOK(w, r)
|
||||
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 uint) bool {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
|
||||
"github.com/AdguardTeam/AdGuardHome/dnsforward"
|
||||
"github.com/AdguardTeam/AdGuardHome/stats"
|
||||
"github.com/AdguardTeam/dnsproxy/proxy"
|
||||
"github.com/AdguardTeam/dnsproxy/upstream"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
@@ -33,7 +34,11 @@ func initDNSServer(baseDir string) {
|
||||
log.Fatalf("Cannot create DNS data dir at %s: %s", baseDir, err)
|
||||
}
|
||||
|
||||
config.dnsServer = dnsforward.NewServer(baseDir)
|
||||
config.stats = stats.New("./data/stats.db", int(config.DNS.StatsInterval), nil)
|
||||
if config.stats == nil {
|
||||
log.Fatal("config.stats == nil")
|
||||
}
|
||||
config.dnsServer = dnsforward.NewServer(baseDir, config.stats)
|
||||
|
||||
initRDNS()
|
||||
}
|
||||
@@ -178,5 +183,7 @@ func stopDNSServer() error {
|
||||
return errorx.Decorate(err, "Couldn't stop forwarding DNS server")
|
||||
}
|
||||
|
||||
config.stats.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user