From 19e76b693803220dffcd0a1fb1fe1e654309a11a Mon Sep 17 00:00:00 2001
From: Eugene Bujak <hmage@hmage.net>
Date: Thu, 4 Oct 2018 14:25:59 +0300
Subject: [PATCH] Add API call to reset stats

---
 control.go | 11 +++++++++++
 stats.go   |  4 ++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/control.go b/control.go
index b16b6e22..d60c6509 100644
--- a/control.go
+++ b/control.go
@@ -401,6 +401,16 @@ func handleQueryLogDisable(w http.ResponseWriter, r *http.Request) {
 
 }
 
+func handleStatsReset(w http.ResponseWriter, r *http.Request) {
+	purgeStats()
+
+	_, err := fmt.Fprintf(w, "OK\n")
+	if err != nil {
+		httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err)
+		return
+	}
+}
+
 func handleStatsTop(w http.ResponseWriter, r *http.Request) {
 	resp, err := client.Get("http://127.0.0.1:8618/querylog")
 	if err != nil {
@@ -1489,6 +1499,7 @@ func registerControlHandlers() {
 	http.HandleFunc("/control/stats", optionalAuth(ensureGET(handleStats)))
 	http.HandleFunc("/control/stats_history", optionalAuth(ensureGET(handleStatsHistory)))
 	http.HandleFunc("/control/stats_top", optionalAuth(ensureGET(handleStatsTop)))
+	http.HandleFunc("/control/stats_reset", optionalAuth(ensurePOST(handleStatsReset)))
 	http.HandleFunc("/control/querylog", optionalAuth(ensureGET(handleQueryLog)))
 	http.HandleFunc("/control/querylog_enable", optionalAuth(ensurePOST(handleQueryLogEnable)))
 	http.HandleFunc("/control/querylog_disable", optionalAuth(ensurePOST(handleQueryLogDisable)))
diff --git a/stats.go b/stats.go
index 163559a8..aff78ee0 100644
--- a/stats.go
+++ b/stats.go
@@ -62,10 +62,10 @@ type stats struct {
 var statistics stats
 
 func initPeriodicStats(periodic *periodicStats) {
-	periodic.Entries = statsEntries{}
+	*periodic = periodicStats{}
 }
 
-func init() {
+func purgeStats() {
 	initPeriodicStats(&statistics.PerSecond)
 	initPeriodicStats(&statistics.PerMinute)
 	initPeriodicStats(&statistics.PerHour)