-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbom-bills.go
627 lines (563 loc) · 16.4 KB
/
bom-bills.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package apiary
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/jackc/pgx/v4"
)
// ParishByYear describes a parish's canoncial name, count type, total count, start day,
// start month, end day, end month, year, week number, and week ID.
type ParishByYear struct {
CanonicalName string `json:"name"`
BillType string `json:"bill_type"`
CountType string `json:"count_type"`
Count NullInt64 `json:"count"`
StartDay NullInt64 `json:"start_day"`
StartMonth NullString `json:"start_month"`
EndDay NullInt64 `json:"end_day"`
EndMonth NullString `json:"end_month"`
Year NullInt64 `json:"year"`
SplitYear string `json:"split_year"`
WeekNo int `json:"week_no"`
WeekID string `json:"week_id"`
Missing *bool `json:"missing"`
Illegible *bool `json:"illegible"`
Source NullString `json:"source"`
UniqueIdentifier NullString `json:"unique_identifier"`
TotalRecords int `json:"totalrecords"`
}
type APIParameters struct {
StartYear int
EndYear int
Parish []int
BillType string
CountType string
Sort string
Limit int
Offset int
Page int
}
type QueryOptions struct {
Limit int
Offset int
}
// TotalBills returns to the total number of records in the database. We need this
// number to get pagination working.
type TotalBills struct {
TotalRecords NullInt64 `json:"total_records"`
}
// BillsHandler returns the bills for a given range of years. It expects a start year and
// an end year. It returns a JSON array of ParishByYear objects.
func (s *Server) BillsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Parse and validate query parameters
apiParams, err := parseAPIParameters(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("Error parsing API parameters: %v", err)
return
}
// Build query
query, err := buildBillsQuery(apiParams)
if err != nil {
http.Error(w, "Error building query", http.StatusInternalServerError)
log.Printf("Error building query: %v", err)
return
}
// Execute query
rows, err := s.DB.Query(context.TODO(), query)
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
log.Printf("Error executing query: %v", err)
return
}
defer rows.Close()
// Process results
results := []ParishByYear{}
for rows.Next() {
var result ParishByYear
err := rows.Scan(
&result.CanonicalName,
&result.BillType,
&result.CountType,
&result.Count,
&result.StartDay,
&result.StartMonth,
&result.EndDay,
&result.EndMonth,
&result.Year,
&result.SplitYear,
&result.WeekNo,
&result.WeekID,
&result.Missing,
&result.Illegible,
&result.Source,
&result.UniqueIdentifier,
&result.TotalRecords,
)
if err != nil {
http.Error(w, "Error processing results", http.StatusInternalServerError)
log.Printf("Error scanning row: %v", err)
return
}
results = append(results, result)
}
if err = rows.Err(); err != nil {
http.Error(w, "Error processing results", http.StatusInternalServerError)
log.Printf("Error iterating through rows: %v", err)
return
}
// Return results (empty array if no results found)
w.Header().Set("Content-Type", "application/json")
response, err := json.Marshal(results)
if err != nil {
http.Error(w, "Error encoding response", http.StatusInternalServerError)
log.Printf("Error marshaling JSON: %v", err)
return
}
w.Write(response)
}
}
func (p *APIParameters) GetQueryOptions() QueryOptions {
if p.Page > 0 {
// Default to 25 items per page if using page parameter
return QueryOptions{
Limit: 25,
Offset: (p.Page - 1) * 25,
}
}
return QueryOptions{
Limit: p.Limit,
Offset: p.Offset,
}
}
// Helper function to parse and validate query parameters
func parseAPIParameters(r *http.Request) (APIParameters, error) {
params := APIParameters{
StartYear: 1648, // Default values
EndYear: 1750,
Parish: []int{},
Sort: "year, week_no, canonical_name",
}
// Parse start year
if startYear := r.URL.Query().Get("start-year"); startYear != "" {
startYearInt, err := strconv.Atoi(startYear)
if err != nil {
return params, fmt.Errorf("invalid start year: %v", err)
}
params.StartYear = startYearInt
}
// Parse end year
if endYear := r.URL.Query().Get("end-year"); endYear != "" {
endYearInt, err := strconv.Atoi(endYear)
if err != nil {
return params, fmt.Errorf("invalid end year: %v", err)
}
params.EndYear = endYearInt
}
// Parse parish IDs
if parish := r.URL.Query().Get("parish"); parish != "" {
parishList := strings.Split(parish, ",")
for _, p := range parishList {
parishInt, err := strconv.Atoi(strings.TrimSpace(p))
if err != nil {
return params, fmt.Errorf("invalid parish ID: %v", err)
}
params.Parish = append(params.Parish, parishInt)
}
}
// Parse bill type
if billType := r.URL.Query().Get("bill-type"); billType != "" {
if !isValidBillType(billType) {
return params, fmt.Errorf("invalid bill type: %s", billType)
}
params.BillType = billType
}
// Parse count type
if countType := r.URL.Query().Get("count-type"); countType != "" {
if !isValidCountType(countType) {
return params, fmt.Errorf("invalid count type: %s", countType)
}
params.CountType = countType
}
// Parse pagination parameters first
if limit := r.URL.Query().Get("limit"); limit != "" {
limitInt, err := strconv.Atoi(limit)
if err != nil {
return params, fmt.Errorf("invalid limit: %v", err)
}
params.Limit = limitInt
}
if offset := r.URL.Query().Get("offset"); offset != "" {
offsetInt, err := strconv.Atoi(offset)
if err != nil {
return params, fmt.Errorf("invalid offset: %v", err)
}
params.Offset = offsetInt
}
// Handle page parameter last since it may override limit/offset
if page := r.URL.Query().Get("page"); page != "" {
pageInt, err := strconv.Atoi(page)
if err != nil {
return params, fmt.Errorf("invalid page number: %v", err)
}
params.Page = pageInt
}
// Parse sorting
if sort := r.URL.Query().Get("sort"); sort != "" {
params.Sort = sort
}
return params, nil
}
// Helper function to build the SQL query
func buildBillsQuery(params APIParameters) (string, error) {
baseQuery := `
SELECT
p.canonical_name,
b.bill_type,
b.count_type,
b.count,
w.start_day,
w.start_month,
w.end_day,
w.end_month,
b.year,
w.split_year,
w.week_no,
b.week_id,
b.missing,
b.illegible,
b.source,
b.unique_identifier,
COUNT(*) OVER() AS totalrecords
FROM
bom.bill_of_mortality b
JOIN
bom.parishes p ON p.id = b.parish_id
JOIN
bom.year y ON y.year = b.year
JOIN
bom.week w ON w.joinid = b.week_id
WHERE 1=1`
// Build WHERE clause
var conditions []string
if params.StartYear != 0 {
conditions = append(conditions, fmt.Sprintf("b.year >= %d", params.StartYear))
}
if params.EndYear != 0 {
conditions = append(conditions, fmt.Sprintf("b.year <= %d", params.EndYear))
}
if len(params.Parish) > 0 {
conditions = append(conditions, fmt.Sprintf("b.parish_id IN (%s)",
strings.Trim(strings.Join(strings.Fields(fmt.Sprint(params.Parish)), ","), "[]")))
}
if params.BillType != "" {
conditions = append(conditions, fmt.Sprintf("b.bill_type = '%s'", params.BillType))
}
if params.CountType != "" {
conditions = append(conditions, fmt.Sprintf("b.count_type = '%s'", params.CountType))
}
// Add conditions to query
if len(conditions) > 0 {
baseQuery += " AND " + strings.Join(conditions, " AND ")
}
// Add sorting
if params.Sort != "" {
baseQuery += " ORDER BY " + params.Sort + " ASC"
}
// Handle pagination
if params.Page > 0 {
limit := 25
offset := (params.Page - 1) * limit
baseQuery += fmt.Sprintf(" LIMIT %d OFFSET %d", limit, offset)
} else if params.Limit > 0 {
baseQuery += fmt.Sprintf(" LIMIT %d", params.Limit)
if params.Offset > 0 {
baseQuery += fmt.Sprintf(" OFFSET %d", params.Offset)
}
}
return baseQuery, nil
}
// Helper function to validate bill types
func isValidBillType(billType string) bool {
validTypes := map[string]bool{
"Weekly": true,
"General": true,
"Total": true,
}
return validTypes[billType]
}
// Helper function to validate count types
func isValidCountType(countType string) bool {
validTypes := map[string]bool{
"Buried": true,
"Plague": true,
}
return validTypes[countType]
}
// TotalBillsHandler returns the total number of bills in the database.
// This number is required for pagination in the web application.
func (s *Server) TotalBillsHandler() http.HandlerFunc {
queryWeekly := `
SELECT
COUNT(*)
FROM
bom.bill_of_mortality
WHERE
bill_type = 'Weekly';
`
queryGeneral := `
SELECT
COUNT(*)
FROM
bom.bill_of_mortality
WHERE
bill_type = 'General';
`
queryChristenings := `
SELECT
COUNT(*)
FROM
bom.christenings;
`
queryCauses := `
SELECT
COUNT(*)
FROM
bom.causes_of_death;
`
return func(w http.ResponseWriter, r *http.Request) {
totalValues := r.URL.Query().Get("type")
if totalValues == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
results := make([]TotalBills, 0)
var row TotalBills
var rows pgx.Rows
var err error
switch {
case totalValues == "Weekly":
rows, err = s.DB.Query(context.TODO(), queryWeekly)
case totalValues == "General":
rows, err = s.DB.Query(context.TODO(), queryGeneral)
case totalValues == "Christenings":
rows, err = s.DB.Query(context.TODO(), queryChristenings)
case totalValues == "Causes":
rows, err = s.DB.Query(context.TODO(), queryCauses)
}
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&row.TotalRecords)
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
results = append(results, row)
}
response, _ := json.Marshal(results)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(response))
}
}
// Statistics
type YearlySummary struct {
Year int `json:"year"`
WeeksCompleted int `json:"weeksCompleted"`
RowsCount int `json:"rowsCount"`
TotalCount int `json:"totalCount"`
}
type WeeklySummary struct {
Year int `json:"year"`
WeekNo int `json:"weekNo"`
RowsCount int `json:"rowsCount"`
}
// ParishYearlySummary represents total counts by parish and year for small multiple visualizations
type ParishYearlySummary struct {
Year int `json:"year"`
ParishName string `json:"parish_name"`
TotalBuried int `json:"total_buried"`
TotalPlague *int `json:"total_plague"`
}
func buildYearlyStatsQuery() string {
query := `
WITH year_range AS (
SELECT generate_series(1636, 1754) AS year
),
weekly_stats AS (
SELECT
b.year as year,
COUNT(DISTINCT b.week_id) as weeks_completed,
COUNT(*) as rows_count
FROM bom.bill_of_mortality b
WHERE b.bill_type = 'Weekly'
GROUP BY b.year
)
SELECT
yr.year,
COALESCE(ws.weeks_completed, 0) as weeks_completed,
COALESCE(ws.rows_count, 0) as rows_count,
53 as total_count
FROM year_range yr
LEFT JOIN weekly_stats ws ON yr.year = ws.year
ORDER BY yr.year;
`
return query
}
func buildWeeklyStatsQuery() string {
query := `
WITH year_week_range AS (
SELECT
y.year,
w.number as week_no
FROM generate_series(1636, 1754) y(year)
CROSS JOIN generate_series(1, 53) w(number)
),
weekly_stats AS (
SELECT
b.year as year,
w.week_no,
COUNT(*) as rows_count
FROM bom.bill_of_mortality b
JOIN bom.week w ON w.joinid = b.week_id
WHERE b.bill_type = 'Weekly'
GROUP BY b.year, w.week_no
)
SELECT
yr.year,
yr.week_no,
COALESCE(ws.rows_count, 0) as rows_count
FROM year_week_range yr
LEFT JOIN weekly_stats ws ON yr.year = ws.year AND yr.week_no = ws.week_no
ORDER BY yr.year, yr.week_no;
`
return query
}
func buildParishYearlyStatsQuery(parishName string) string {
query := `
SELECT
b.year,
p.canonical_name as parish_name,
SUM(CASE WHEN b.count_type = 'Buried' THEN COALESCE(b.count, 0) ELSE 0 END) as total_buried,
NULLIF(SUM(CASE WHEN b.count_type = 'Plague' THEN COALESCE(b.count, 0) ELSE 0 END), 0) as total_plague
FROM bom.bill_of_mortality b
JOIN bom.parishes p ON p.id = b.parish_id
WHERE b.bill_type = 'Weekly'
`
if parishName != "" {
query += fmt.Sprintf(" AND p.canonical_name = '%s'", parishName)
}
query += `
GROUP BY b.year, p.canonical_name
ORDER BY p.canonical_name, b.year
`
return query
}
func (s *Server) StatisticsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
statType := r.URL.Query().Get("type")
parishName := r.URL.Query().Get("parish")
var query string
switch statType {
case "weekly":
query = buildWeeklyStatsQuery()
case "yearly":
query = buildYearlyStatsQuery()
case "parish-yearly":
query = buildParishYearlyStatsQuery(parishName)
default:
http.Error(w, "Invalid type parameter. Must be 'weekly', 'yearly', or 'parish-yearly'", http.StatusBadRequest)
return
}
rows, err := s.DB.Query(context.TODO(), query)
if err != nil {
log.Printf("Database error executing query: %v", err)
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
defer rows.Close()
switch statType {
case "weekly":
stats := []WeeklySummary{}
for rows.Next() {
var summary WeeklySummary
err := rows.Scan(&summary.Year, &summary.WeekNo, &summary.RowsCount)
if err != nil {
log.Printf("Error scanning weekly summary: %v", err)
http.Error(w, fmt.Sprintf("Error processing results: %v", err), http.StatusInternalServerError)
return
}
stats = append(stats, summary)
}
// Check for errors from iterating over rows
if err = rows.Err(); err != nil {
log.Printf("Error iterating over rows: %v", err)
http.Error(w, fmt.Sprintf("Error processing results: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(stats); err != nil {
log.Printf("Error encoding JSON response: %v", err)
}
case "yearly":
stats := []YearlySummary{}
for rows.Next() {
var summary YearlySummary
err := rows.Scan(&summary.Year, &summary.WeeksCompleted,
&summary.RowsCount, &summary.TotalCount)
if err != nil {
log.Printf("Error scanning yearly summary: %v", err)
http.Error(w, fmt.Sprintf("Error processing results: %v", err), http.StatusInternalServerError)
return
}
stats = append(stats, summary)
}
// Check for errors from iterating over rows
if err = rows.Err(); err != nil {
log.Printf("Error iterating over rows: %v", err)
http.Error(w, fmt.Sprintf("Error processing results: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(stats); err != nil {
log.Printf("Error encoding JSON response: %v", err)
}
case "parish-yearly":
stats := []ParishYearlySummary{}
for rows.Next() {
var summary ParishYearlySummary
err := rows.Scan(
&summary.Year,
&summary.ParishName,
&summary.TotalBuried,
&summary.TotalPlague,
)
if err != nil {
log.Printf("Error scanning parish-yearly summary: %v", err)
http.Error(w, fmt.Sprintf("Error processing results: %v", err), http.StatusInternalServerError)
return
}
stats = append(stats, summary)
}
// Check for errors from iterating over rows
if err = rows.Err(); err != nil {
log.Printf("Error iterating over rows: %v", err)
http.Error(w, fmt.Sprintf("Error processing results: %v", err), http.StatusInternalServerError)
return
}
log.Printf("Returning %d parish-yearly summary records", len(stats))
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(stats); err != nil {
log.Printf("Error encoding JSON response: %v", err)
}
}
}
}