2021-01-04 19:29:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func serveBlogStats(blog, statsPath string) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-01-15 20:56:46 +00:00
|
|
|
// Build query
|
|
|
|
query, params := buildQuery(&postsRequestConfig{
|
|
|
|
blog: blog,
|
|
|
|
status: statusPublished,
|
|
|
|
})
|
|
|
|
// Count total posts
|
|
|
|
row, err := appDbQueryRow("select count(distinct path) from ("+query+")", params...)
|
2021-01-04 19:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-01-15 20:56:46 +00:00
|
|
|
var totalCount int
|
2021-01-04 19:29:49 +00:00
|
|
|
err = row.Scan(&totalCount)
|
|
|
|
if err != nil {
|
|
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-01-15 20:56:46 +00:00
|
|
|
// Count posts per year
|
|
|
|
rows, err := appDbQuery(`select substr(published, 1, 4) as year, count(distinct path) as count from (`+query+`)
|
|
|
|
where published != '' group by year order by year desc`, params...)
|
2021-01-04 19:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-01-15 20:56:46 +00:00
|
|
|
var years, counts []int
|
2021-01-04 19:29:49 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var year, count int
|
|
|
|
rows.Scan(&year, &count)
|
|
|
|
years = append(years, year)
|
|
|
|
counts = append(counts, count)
|
|
|
|
}
|
|
|
|
render(w, templateBlogStats, &renderData{
|
2021-01-17 11:53:07 +00:00
|
|
|
BlogString: blog,
|
2021-01-04 19:29:49 +00:00
|
|
|
Canonical: statsPath,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"total": totalCount,
|
|
|
|
"years": years,
|
|
|
|
"counts": counts,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|