From 600f8f6ef7332676e9a45ab977f202512ea86aaa Mon Sep 17 00:00:00 2001 From: Hamcha Date: Sun, 14 Feb 2016 10:45:45 +0100 Subject: [PATCH] stats: Some attempt at a filter to make the dictionary lighter --- stats/web.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/stats/web.go b/stats/web.go index 6bb3c4c..84bae57 100644 --- a/stats/web.go +++ b/stats/web.go @@ -7,6 +7,7 @@ import ( ) func webStats(rw http.ResponseWriter, req *http.Request) { + rw.Header().Set("Content-Type", "application/json") err := json.NewEncoder(rw).Encode(stats) if err != nil { log.Println("[webStats] JSON Encoding error: " + err.Error()) @@ -14,14 +15,32 @@ func webStats(rw http.ResponseWriter, req *http.Request) { } func webUsers(rw http.ResponseWriter, req *http.Request) { + rw.Header().Set("Content-Type", "application/json") err := json.NewEncoder(rw).Encode(users) if err != nil { log.Println("[webUsers] JSON Encoding error: " + err.Error()) } } +const USAGE_THRESHOLD = 3 + func webWords(rw http.ResponseWriter, req *http.Request) { - err := json.NewEncoder(rw).Encode(words) + // Filter words under a certain usage + filtered := make(map[string]UserCount) + for word, usage := range words { + total := 0 + for _, count := range usage { + total += count + } + + if total < USAGE_THRESHOLD { + continue + } + filtered[word] = usage + } + + rw.Header().Set("Content-Type", "application/json") + err := json.NewEncoder(rw).Encode(filtered) if err != nil { log.Println("[webWords] JSON Encoding error: " + err.Error()) }