2019-08-08 09:40:41 +00:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2019-08-22 13:34:58 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/stats"
|
2019-08-08 09:40:41 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statsConfig struct {
|
|
|
|
Interval uint `json:"interval"`
|
|
|
|
}
|
|
|
|
|
2019-08-22 13:34:58 +00:00
|
|
|
// Get stats configuration
|
2019-08-08 09:40:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 13:34:58 +00:00
|
|
|
// Set stats configuration
|
2019-08-08 09:40:41 +00:00
|
|
|
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
|
2019-09-10 10:52:19 +00:00
|
|
|
_ = config.write()
|
|
|
|
|
2019-08-22 13:34:58 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-08-08 09:40:41 +00:00
|
|
|
|
2019-08-22 13:34:58 +00:00
|
|
|
// 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)
|
2019-08-08 09:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkStatsInterval(i uint) bool {
|
|
|
|
return i == 1 || i == 7 || i == 30 || i == 90
|
|
|
|
}
|