From 8198b65f29f83e2ff51d4c0f89a7b7dcbe9c2fc1 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Fri, 7 Sep 2018 17:49:33 +0300 Subject: [PATCH] API /stats_top -- show only top entries for last 3 minutes --- control.go | 8 ++++++++ helpers.go | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/control.go b/control.go index 70cb9349..2552df53 100644 --- a/control.go +++ b/control.go @@ -409,6 +409,9 @@ func handleStatsTop(w http.ResponseWriter, r *http.Request) { domains := map[string]int{} blocked := map[string]int{} clients := map[string]int{} + now := time.Now() + timeWindow := time.Minute * 3 + notBefore := now.Add(timeWindow * -1) for _, value := range values { entry, ok := value.(map[string]interface{}) @@ -419,6 +422,11 @@ func handleStatsTop(w http.ResponseWriter, r *http.Request) { host := getHost(entry) reason := getReason(entry) client := getClient(entry) + time := getTime(entry) + if time.Before(notBefore) { + // skip if the entry is before specified cutoff + continue + } if len(host) > 0 { domains[host]++ } diff --git a/helpers.go b/helpers.go index a460f98f..d39c5e93 100644 --- a/helpers.go +++ b/helpers.go @@ -7,6 +7,7 @@ import ( "net/http" "sort" "strings" + "time" ) func clamp(value, low, high int) int { @@ -167,6 +168,22 @@ func getClient(entry map[string]interface{}) string { return client } +func getTime(entry map[string]interface{}) time.Time { + t, ok := entry["time"] + if !ok { + return time.Time{} + } + tstr, ok := t.(string) + if !ok { + return time.Time{} + } + value, err := time.Parse(time.RFC3339, tstr) + if err != nil { + return time.Time{} + } + return value +} + // ------------------------------------------------- // helper functions for parsing parameters from body // -------------------------------------------------