mirror of https://github.com/jlelse/GoBlog
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
func serveBlogStats(blog, statsPath string) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Build query
|
|
query, params := buildPostsQuery(&postsRequestConfig{
|
|
blog: blog,
|
|
status: statusPublished,
|
|
})
|
|
// Count total posts
|
|
row, err := appDbQueryRow("select count(distinct path) from ("+query+")", params...)
|
|
if err != nil {
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
var totalCount int
|
|
if err = row.Scan(&totalCount); err != nil {
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// 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...)
|
|
if err != nil {
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
var years, counts []int
|
|
for rows.Next() {
|
|
var year, count int
|
|
if err = rows.Scan(&year, &count); err == nil {
|
|
years = append(years, year)
|
|
counts = append(counts, count)
|
|
}
|
|
}
|
|
render(w, r, templateBlogStats, &renderData{
|
|
BlogString: blog,
|
|
Canonical: statsPath,
|
|
Data: map[string]interface{}{
|
|
"total": totalCount,
|
|
"years": years,
|
|
"counts": counts,
|
|
},
|
|
})
|
|
}
|
|
}
|