-
Notifications
You must be signed in to change notification settings - Fork 1
/
apb-bible-trend.go
69 lines (58 loc) · 1.48 KB
/
apb-bible-trend.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
package apiary
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
// APBBibleTrendHandler returns the rates of quotation per year for a verse.
func (s *Server) APBBibleTrendHandler() http.HandlerFunc {
query := `
SELECT
year,
n,
SUM(n) OVER (ORDER BY year ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) / SUM(wordcount) OVER (ORDER BY year ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) * 1000000 AS q_rate_smoothed
FROM
(SELECT series.year,
COALESCE(n, 0) as n,
wordcount
FROM
(SELECT generate_series($2::int, $3::int) AS year) series
LEFT JOIN
(SELECT
year,
n,
wordcount
FROM apb.rate_quotations_bible
WHERE corpus = $1) AS q
ON series.year = q.year
ORDER BY series.year) res
`
return func(w http.ResponseWriter, r *http.Request) {
corpus := "chronam"
minYear, maxYear := 1836, 1922
results := make([]VerseTrend, 0, 87) // Preallocate slice capacity
var row VerseTrend
rows, err := s.DB.Query(context.TODO(), query, corpus, minYear, maxYear)
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&row.Year, &row.N, &row.QuotationRateSmooth)
if err != nil {
log.Println(err)
}
results = append(results, row)
}
err = rows.Err()
if err != nil {
log.Println(err)
}
wrapper := VerseTrendResponse{Reference: "bible", Corpus: corpus, Trend: results}
response, _ := json.Marshal(wrapper)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(response))
}
}