This repository has been archived by the owner on Nov 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_handler.go
145 lines (128 loc) · 3.39 KB
/
stats_handler.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/pilu/traffic"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
"google.golang.org/appengine/user"
)
type StatsData struct {
Posts int
WordsPerPost float64
PostsPerDay float64
WordsPerDay float64
DaysSince float64
IsAdmin bool
LinksPerPost float64
LinksPerDay float64
Years []string
YearData map[string][]float64
}
func StatsWorkHandler(w traffic.ResponseWriter, r *traffic.Request) {
c := appengine.NewContext(r.Request)
var data *StatsData
entries, err := AllPosts(c)
if err != nil {
log.Errorf(c, "Error loading posts: %+v", err)
http.Error(w, err.Error(), 500)
return
}
postCount := len(*entries)
oldestPost := (*entries)[postCount-1]
dayCount := time.Since(oldestPost.Datetime).Hours() / 24.0
oldestYear := oldestPost.Datetime.Year()
years := []string{}
yearData := make(map[string][]float64)
for i := time.Now().Year(); i >= oldestYear; i-- {
years = append(years, strconv.Itoa(i))
yearData[strconv.Itoa(i)] = []float64{0.0, 0.0, 0.0}
}
words := 0
for _, p := range *entries {
words += len(strings.Fields(p.Content))
words += len(strings.Fields(p.Title))
yearData[strconv.Itoa(p.Datetime.Year())][0] += 1
}
for _, y := range years {
yearData[y][1] = yearData[y][0] / 52.0
}
readLinks := 0
q := LinkQuery(c, 10000, true)
for t := q.Run(c); ; {
var l Link
_, err := t.Next(&l)
if err == datastore.Done {
break
}
if err != nil {
log.Errorf(c, "Error loading link: %+v", err)
} else {
readLinks += 1
yearPosted := l.Posted.Year()
// Don't want to record stats for years we don't have posts.
if yearPosted >= oldestYear {
yearData[strconv.Itoa(yearPosted)][2] += 1
}
}
}
data = &StatsData{
Posts: postCount,
PostsPerDay: float64(postCount) / dayCount,
WordsPerPost: float64(words) / float64(postCount),
WordsPerDay: float64(words) / dayCount,
DaysSince: dayCount,
LinksPerDay: float64(readLinks) / dayCount,
Years: years,
YearData: yearData,
}
b, err := json.Marshal(data)
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
return
}
item := &memcache.Item{
Key: "stats-data",
Value: b,
}
// Set the item, unconditionally
if err := memcache.Set(c, item); err != nil {
log.Errorf(c, "error setting item: %v", err)
}
}
func StatsHandler(w traffic.ResponseWriter, r *traffic.Request) {
c := appengine.NewContext(r.Request)
data := &StatsData{}
if json_data, err := memcache.Get(c, "stats-data"); err == memcache.ErrCacheMiss {
log.Errorf(c, "stats-data is empty")
} else if err != nil {
log.Errorf(c, "error getting item: %v", err)
} else {
err := json.Unmarshal(json_data.Value, &data)
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
return
}
}
data.IsAdmin = user.IsAdmin(c)
w.Render("stats", data)
}
func StatsHistoryJsonHandler(w traffic.ResponseWriter, r *traffic.Request) {
c := appengine.NewContext(r.Request)
entries, err := AllPostsJson(c)
if err != nil {
log.Errorf(c, "Error loading posts: %+v", err)
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteJSON(entries)
}