-
Notifications
You must be signed in to change notification settings - Fork 0
/
reports.go
272 lines (241 loc) · 6.69 KB
/
reports.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package controllers
import (
"fmt"
"strconv"
"time"
"github.com/jeff-blank/wg/app"
"github.com/jeff-blank/wg/app/routes"
"github.com/jeff-blank/wg/app/util"
"github.com/revel/revel"
)
const (
DATE_LIST_YEAR = 2000
DATE_LIST_LAYOUT = `2006-01-02`
)
type Reports struct {
*revel.Controller
}
func (c Reports) Index() revel.Result {
links := make(map[string]string)
links["master"] = routes.Reports.MasterStats()
links["calendar"] = routes.Reports.HitsCalendar()
links["first"] = routes.Reports.FirstHits()
links["last50"] = routes.Reports.Last50Counts()
links["topCounties"] = routes.Reports.TopCounties()
return c.Render(links)
}
func (c Reports) HitsCalendar() revel.Result {
// date list is the outer array to ease columnizing dates in template
var (
calendar [31]app.DayOfMonth
totalsRow app.DayOfMonth
missingRow app.DayOfMonth
monthTotal int
missing int
missingTotal int
)
dates := make(map[string]int)
rows, err := app.DB.Query(app.Q_HITS_CALENDAR)
if err != nil {
revel.AppLog.Errorf("query hits calendar: %#v", err)
return c.RenderError(err)
}
defer rows.Close()
for rows.Next() {
var date string
var count int
err = rows.Scan(&date, &count)
if err != nil {
revel.AppLog.Errorf("read hits calendar: %#v", err)
return c.RenderError(err)
}
dates[date] = count
}
checkDate := time.Date(DATE_LIST_YEAR, time.January, 1, 0, 0, 0, 0, time.UTC)
for checkDate.Year() == DATE_LIST_YEAR {
month0 := checkDate.Month()
monthDay := checkDate.Day()
dateStr := checkDate.Format(DATE_LIST_LAYOUT)[5:]
calendar[monthDay-1].Label = strconv.Itoa(monthDay)
if count, found := dates[dateStr]; found {
calendar[monthDay-1].Months[month0-1] = count
monthTotal += count
} else {
missing++
missingTotal++
}
checkDate = checkDate.AddDate(0, 0, 1)
month1 := checkDate.Month()
if month0 != month1 {
if monthDay < 31 {
for i := monthDay + 1; i <= 31; i++ {
calendar[i-1].Months[month0-1] = -1
}
}
totalsRow.Months[month0-1] = monthTotal
missingRow.Months[month0-1] = missing
monthTotal = 0
missing = 0
}
}
for d := 0; d < len(calendar); d++ {
monthDayTotal := 0
for m := 0; m < len(calendar[d].Months); m++ {
if calendar[d].Months[m] >= 0 {
monthDayTotal += calendar[d].Months[m]
}
}
calendar[d].Total = strconv.Itoa(monthDayTotal)
}
missingRow.Total = strconv.Itoa(missingTotal)
return c.Render(calendar, totalsRow, missingRow)
}
func (c Reports) MasterStats() revel.Result {
var tableIn []map[string]interface{}
tableIn = util.StatsData("table").([]map[string]interface{})
hitsLink := routes.Hits.Index() + "?year="
for m, monthData := range tableIn {
month := monthData["month"]
ents := monthData["monthBills"]
monthData["monthBills"] = map[string]interface{}{"month": month, "entries": ents}
tableIn[m] = monthData
}
// reverse table for display
tableOut := make([]map[string]interface{}, 0)
for m := len(tableIn) - 1; m >= 0; m-- {
tableOut = append(tableOut, tableIn[m])
}
entsLink := routes.Entries.Edit()
graphLinks := make(map[string]string)
graphLinks["score"] = routes.Charts.Grapher("score")
graphLinks["hits"] = routes.Charts.Grapher("hits")
graphLinks["bills"] = routes.Charts.Grapher("bills")
return c.Render(tableOut, entsLink, graphLinks, hitsLink)
}
func (c Reports) FirstHits() revel.Result {
return c.Render()
}
func (c Reports) TopCounties() revel.Result {
data := make([]app.DualRegionBrkEnt, 0)
query := `select cm.state, cm.county, count(1) from hits h, counties_master cm where country='US' and h.county_id=cm.id group by cm.state, cm.county order by count desc, cm.state, cm.county limit 25`
rows, err := app.DB.Query(query)
if err != nil {
revel.AppLog.Errorf("query top counties: %#v", err)
return c.RenderError(err)
}
rank := 1
for rows.Next() {
var (
state string
county string
count int
)
err := rows.Scan(&state, &county, &count)
if err != nil {
revel.AppLog.Errorf("read top counties: %#v", err)
return c.RenderError(err)
}
data = append(data, app.DualRegionBrkEnt{
Rank: rank,
State: state,
County: county,
Count: count,
})
rank++
}
data[0].RankStr = "1."
for i := range data {
if i == 0 {
continue
}
if data[i].Count == data[i-1].Count {
if data[i-1].RankStr[:2] != "T-" {
data[i-1].RankStr = fmt.Sprintf("T-%d.", data[i-1].Rank)
}
data[i].Rank = data[i-1].Rank
data[i].RankStr = data[i-1].RankStr
} else {
data[i].RankStr = strconv.Itoa(data[i].Rank) + "."
}
}
return c.Render(data)
}
func (c Reports) Last50Counts() revel.Result {
denomData := make([][2]int, 0)
seriesData := make([][2]interface{}, 0)
stateData := make([][2]interface{}, 0)
countyData := make([][3]interface{}, 0)
query := fmt.Sprintf(app.Q_LAST_50_DENOM_SERIES, "denomination", "denomination", "denomination")
rows, err := app.DB.Query(query)
if err != nil {
revel.AppLog.Errorf("query last 50 hits' denominations: %#v", err)
return c.RenderError(err)
}
for rows.Next() {
var (
denom int
count int
)
err := rows.Scan(&denom, &count)
if err != nil {
revel.AppLog.Errorf("read last 50 hits' denominations: %#v", err)
return c.RenderError(err)
}
denomData = append(denomData, [2]int{denom, count})
}
query = fmt.Sprintf(app.Q_LAST_50_DENOM_SERIES, "series", "series", "series") + " desc"
rows, err = app.DB.Query(query)
if err != nil {
revel.AppLog.Errorf("query last 50 hits' series: %#v", err)
return c.RenderError(err)
}
for rows.Next() {
var (
series string
count int
)
err := rows.Scan(&series, &count)
if err != nil {
revel.AppLog.Errorf("read last 50 hits' series: %#v", err)
return c.RenderError(err)
}
seriesData = append(seriesData, [2]interface{}{series, count})
}
rows, err = app.DB.Query(app.Q_LAST_50_STATES)
if err != nil {
revel.AppLog.Errorf("query last 50 hits' states: %#v", err)
return c.RenderError(err)
}
for rows.Next() {
var (
state string
count int
)
err := rows.Scan(&state, &count)
if err != nil {
revel.AppLog.Errorf("read last 50 hits' states: %#v", err)
return c.RenderError(err)
}
stateData = append(stateData, [2]interface{}{state, count})
}
rows, err = app.DB.Query(app.Q_LAST_50_COUNTIES)
if err != nil {
revel.AppLog.Errorf("query last 50 hits' counties: %#v", err)
return c.RenderError(err)
}
for rows.Next() {
var (
state string
county string
count int
)
err := rows.Scan(&state, &county, &count)
if err != nil {
revel.AppLog.Errorf("read last 50 hits' counties: %#v", err)
return c.RenderError(err)
}
countyData = append(countyData, [3]interface{}{state, county, count})
}
return c.Render(denomData, seriesData, stateData, countyData)
}
// vim:foldmethod=marker: