* stats: remove old code
This commit is contained in:
committed by
Ildar Kamalov
parent
fe23b33d7e
commit
60eb55bdce
150
home/control.go
150
home/control.go
@@ -1,12 +1,10 @@
|
||||
package home
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -177,149 +175,6 @@ func handleQueryLog(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func handleStatsTop(w http.ResponseWriter, r *http.Request) {
|
||||
s := config.dnsServer.GetStatsTop()
|
||||
|
||||
// use manual json marshalling because we want maps to be sorted by value
|
||||
statsJSON := bytes.Buffer{}
|
||||
statsJSON.WriteString("{\n")
|
||||
|
||||
gen := func(json *bytes.Buffer, name string, top map[string]int, addComma bool) {
|
||||
json.WriteString(" ")
|
||||
json.WriteString(fmt.Sprintf("%q", name))
|
||||
json.WriteString(": {\n")
|
||||
sorted := sortByValue(top)
|
||||
// no more than 50 entries
|
||||
if len(sorted) > 50 {
|
||||
sorted = sorted[:50]
|
||||
}
|
||||
for i, key := range sorted {
|
||||
json.WriteString(" ")
|
||||
json.WriteString(fmt.Sprintf("%q", key))
|
||||
json.WriteString(": ")
|
||||
json.WriteString(strconv.Itoa(top[key]))
|
||||
if i+1 != len(sorted) {
|
||||
json.WriteByte(',')
|
||||
}
|
||||
json.WriteByte('\n')
|
||||
}
|
||||
json.WriteString(" }")
|
||||
if addComma {
|
||||
json.WriteByte(',')
|
||||
}
|
||||
json.WriteByte('\n')
|
||||
}
|
||||
gen(&statsJSON, "top_queried_domains", s.Domains, true)
|
||||
gen(&statsJSON, "top_blocked_domains", s.Blocked, true)
|
||||
gen(&statsJSON, "top_clients", s.Clients, true)
|
||||
statsJSON.WriteString(" \"stats_period\": \"24 hours\"\n")
|
||||
statsJSON.WriteString("}\n")
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err := w.Write(statsJSON.Bytes())
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// handleStatsReset resets the stats caches
|
||||
func handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
||||
config.dnsServer.PurgeStats()
|
||||
_, err := fmt.Fprintf(w, "OK\n")
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// handleStats returns aggregated stats data for the 24 hours
|
||||
func handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
summed := config.dnsServer.GetAggregatedStats()
|
||||
|
||||
statsJSON, err := json.Marshal(summed)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "Unable to marshal status json: %s", err)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(statsJSON)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "Unable to write response json: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// HandleStatsHistory returns historical stats data for the 24 hours
|
||||
func handleStatsHistory(w http.ResponseWriter, r *http.Request) {
|
||||
// handle time unit and prepare our time window size
|
||||
timeUnitString := r.URL.Query().Get("time_unit")
|
||||
var timeUnit time.Duration
|
||||
switch timeUnitString {
|
||||
case "seconds":
|
||||
timeUnit = time.Second
|
||||
case "minutes":
|
||||
timeUnit = time.Minute
|
||||
case "hours":
|
||||
timeUnit = time.Hour
|
||||
case "days":
|
||||
timeUnit = time.Hour * 24
|
||||
default:
|
||||
http.Error(w, "Must specify valid time_unit parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// parse start and end time
|
||||
startTime, err := time.Parse(time.RFC3339, r.URL.Query().Get("start_time"))
|
||||
if err != nil {
|
||||
httpError(w, http.StatusBadRequest, "Must specify valid start_time parameter: %s", err)
|
||||
return
|
||||
}
|
||||
endTime, err := time.Parse(time.RFC3339, r.URL.Query().Get("end_time"))
|
||||
if err != nil {
|
||||
httpError(w, http.StatusBadRequest, "Must specify valid end_time parameter: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := config.dnsServer.GetStatsHistory(timeUnit, startTime, endTime)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusBadRequest, "Cannot get stats history: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
statsJSON, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "Unable to marshal status json: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(statsJSON)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "Unable to write response json: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// sortByValue is a helper function for querylog API
|
||||
func sortByValue(m map[string]int) []string {
|
||||
type kv struct {
|
||||
k string
|
||||
v int
|
||||
}
|
||||
var ss []kv
|
||||
for k, v := range m {
|
||||
ss = append(ss, kv{k, v})
|
||||
}
|
||||
sort.Slice(ss, func(l, r int) bool {
|
||||
return ss[l].v > ss[r].v
|
||||
})
|
||||
|
||||
sorted := []string{}
|
||||
for _, v := range ss {
|
||||
sorted = append(sorted, v.k)
|
||||
}
|
||||
return sorted
|
||||
}
|
||||
|
||||
// -----------------------
|
||||
// upstreams configuration
|
||||
// -----------------------
|
||||
@@ -722,10 +577,6 @@ func registerControlHandlers() {
|
||||
httpRegister(http.MethodPost, "/control/test_upstream_dns", handleTestUpstreamDNS)
|
||||
httpRegister(http.MethodPost, "/control/i18n/change_language", handleI18nChangeLanguage)
|
||||
httpRegister(http.MethodGet, "/control/i18n/current_language", handleI18nCurrentLanguage)
|
||||
httpRegister(http.MethodGet, "/control/stats_top", handleStatsTop)
|
||||
httpRegister(http.MethodGet, "/control/stats", handleStats)
|
||||
httpRegister(http.MethodGet, "/control/stats_history", handleStatsHistory)
|
||||
httpRegister(http.MethodPost, "/control/stats_reset", handleStatsReset)
|
||||
http.HandleFunc("/control/version.json", postInstall(optionalAuth(handleGetVersionJSON)))
|
||||
httpRegister(http.MethodPost, "/control/update", handleUpdate)
|
||||
httpRegister(http.MethodPost, "/control/filtering/enable", handleFilteringEnable)
|
||||
@@ -760,6 +611,7 @@ func registerControlHandlers() {
|
||||
RegisterClientsHandlers()
|
||||
registerRewritesHandlers()
|
||||
RegisterBlockedServicesHandlers()
|
||||
RegisterStatsHandlers()
|
||||
|
||||
http.HandleFunc("/dns-query", postInstall(handleDOH))
|
||||
}
|
||||
|
||||
@@ -152,11 +152,6 @@ func startDNSServer() error {
|
||||
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
|
||||
}
|
||||
|
||||
top := config.dnsServer.GetStatsTop()
|
||||
for k := range top.Clients {
|
||||
beginAsyncRDNS(k)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user