2021-01-04 19:29:49 +00:00
package main
import (
"net/http"
2021-04-23 18:52:12 +00:00
"strconv"
2021-01-04 19:29:49 +00:00
)
2021-03-22 07:20:56 +00:00
func serveBlogStats ( w http . ResponseWriter , r * http . Request ) {
blog := r . Context ( ) . Value ( blogContextKey ) . ( string )
// Build query
2021-04-23 18:52:12 +00:00
prq := & postsRequestConfig {
2021-03-22 07:20:56 +00:00
blog : blog ,
status : statusPublished ,
2021-04-23 18:52:12 +00:00
}
query , params := buildPostsQuery ( prq )
2021-03-22 07:20:56 +00:00
// 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
}
2021-04-23 18:52:12 +00:00
var years [ ] stringPair
2021-03-22 07:20:56 +00:00
for rows . Next ( ) {
2021-04-23 18:52:12 +00:00
var year , count string
2021-03-22 07:20:56 +00:00
if err = rows . Scan ( & year , & count ) ; err == nil {
2021-04-23 18:52:12 +00:00
years = append ( years , stringPair { year , count } )
} else {
serveError ( w , r , err . Error ( ) , http . StatusInternalServerError )
return
}
}
// Count posts without date
row , err = appDbQueryRow ( "select count(distinct path) from (" + query + ") where published = ''" , params ... )
if err != nil {
serveError ( w , r , err . Error ( ) , http . StatusInternalServerError )
return
}
var noDateCount int
if err = row . Scan ( & noDateCount ) ; err != nil {
serveError ( w , r , err . Error ( ) , http . StatusInternalServerError )
return
}
// Count posts per month per year
months := map [ string ] [ ] stringPair { }
for _ , year := range years {
prq . publishedYear , _ = strconv . Atoi ( year . First )
query , params = buildPostsQuery ( prq )
rows , err = appDbQuery ( "select substr(published, 6, 2) as month, count(distinct path) as count from (" + query + ") where published != '' group by month order by month desc" , params ... )
if err != nil {
serveError ( w , r , err . Error ( ) , http . StatusInternalServerError )
return
}
for rows . Next ( ) {
var month , count string
if err = rows . Scan ( & month , & count ) ; err == nil {
months [ year . First ] = append ( months [ year . First ] , stringPair { month , count } )
} else {
serveError ( w , r , err . Error ( ) , http . StatusInternalServerError )
return
}
2021-01-04 19:29:49 +00:00
}
}
2021-03-22 07:20:56 +00:00
render ( w , r , templateBlogStats , & renderData {
BlogString : blog ,
Canonical : blogPath ( blog ) + appConfig . Blogs [ blog ] . BlogStats . Path ,
Data : map [ string ] interface { } {
2021-04-23 18:52:12 +00:00
"total" : totalCount ,
"years" : years ,
"withoutdate" : noDateCount ,
"months" : months ,
2021-03-22 07:20:56 +00:00
} ,
} )
2021-01-04 19:29:49 +00:00
}