forked from gojp/goreportcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
high_scores.go
72 lines (60 loc) · 1.64 KB
/
high_scores.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package handlers
import (
"container/heap"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"time"
"github.com/boltdb/bolt"
"github.com/dustin/go-humanize"
)
func add(x, y int) int {
return x + y
}
func formatScore(x float64) string {
return fmt.Sprintf("%.2f", x)
}
// HighScoresHandler handles the stats page
func HighScoresHandler(w http.ResponseWriter, r *http.Request) {
// write to boltdb
db, err := bolt.Open(DBPath, 0755, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Println("Failed to open bolt database: ", err)
return
}
defer db.Close()
count, scores := 0, &scoreHeap{}
err = db.View(func(tx *bolt.Tx) error {
hsb := tx.Bucket([]byte(MetaBucket))
if hsb == nil {
return fmt.Errorf("high score bucket not found")
}
scoreBytes := hsb.Get([]byte("scores"))
if scoreBytes == nil {
scoreBytes, _ = json.Marshal([]scoreHeap{})
}
json.Unmarshal(scoreBytes, scores)
heap.Init(scores)
total := hsb.Get([]byte("total_repos"))
if total == nil {
count = 0
} else {
json.Unmarshal(total, &count)
}
return nil
})
if err != nil {
log.Println("ERROR: Failed to load high scores from bolt database: ", err)
http.Error(w, err.Error(), 500)
return
}
funcs := template.FuncMap{"add": add, "formatScore": formatScore}
t := template.Must(template.New("high_scores.html").Funcs(funcs).ParseFiles("templates/high_scores.html"))
sortedScores := make([]scoreItem, len(*scores))
for i := range sortedScores {
sortedScores[len(sortedScores)-i-1] = heap.Pop(scores).(scoreItem)
}
t.Execute(w, map[string]interface{}{"HighScores": sortedScores, "Count": humanize.Comma(int64(count))})
}