/
handle_history.go
48 lines (41 loc) · 1.25 KB
/
handle_history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package dashboard
import (
"encoding/json"
"net/http"
)
// HandleHistory returns an object in JSON with the following members.
//
// - "Clock": {number} is the current clock value (starting from 0).
//
// - "TotalHits": {number} is the total number of hits in the past 24
// hours.
//
// - "Directions": {array} is the collection of directions. Each item is
// an object with the following members:
//
// - "blocked-values": {array} is the collection of blocked values.
//
// - "name": {string} is the name of the direction.
func (s *Server) HandleHistory(w http.ResponseWriter, r *http.Request) {
type HistoryData struct {
Clock int32
TotalHits uint64
Directions []interface{}
}
data := HistoryData{
Clock: s.counter.Clock.GetTime(),
TotalHits: s.counter.Count.Count(),
Directions: make([]interface{}, 0, len(s.counter.Directions)),
}
for _, direction := range s.counter.Directions {
direction.Store.CleanUp(data.Clock)
dirData := map[string]interface{}{
"name": direction.Name,
"blocked-values": direction.BlockedValues(),
}
data.Directions = append(data.Directions, dirData)
}
json, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}