stats: Some attempt at a filter to make the dictionary lighter

This commit is contained in:
Hamcha 2016-02-14 10:45:45 +01:00
parent bf7f2b6b55
commit 600f8f6ef7

View file

@ -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())
}