From 47ddcc402841e899648007a4736877398af4c35c Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Fri, 23 Apr 2021 20:52:12 +0200 Subject: [PATCH] Blogstats with months (on click) --- blogstats.go | 54 ++++++++++++++++++++++++++------ check.go | 12 +++---- templates/assets/js/blogstats.js | 11 +++++++ templates/blogstats.gohtml | 23 ++++++++++---- templates/strings/de.yaml | 1 + templates/strings/default.yaml | 1 + utils.go | 2 +- 7 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 templates/assets/js/blogstats.js diff --git a/blogstats.go b/blogstats.go index 828cc5a..07b9a90 100644 --- a/blogstats.go +++ b/blogstats.go @@ -2,15 +2,17 @@ package main import ( "net/http" + "strconv" ) func serveBlogStats(w http.ResponseWriter, r *http.Request) { blog := r.Context().Value(blogContextKey).(string) // Build query - query, params := buildPostsQuery(&postsRequestConfig{ + prq := &postsRequestConfig{ blog: blog, status: statusPublished, - }) + } + query, params := buildPostsQuery(prq) // Count total posts row, err := appDbQueryRow("select count(distinct path) from ("+query+")", params...) if err != nil { @@ -28,21 +30,55 @@ func serveBlogStats(w http.ResponseWriter, r *http.Request) { serveError(w, r, err.Error(), http.StatusInternalServerError) return } - var years, counts []int + var years []stringPair for rows.Next() { - var year, count int + var year, count string if err = rows.Scan(&year, &count); err == nil { - years = append(years, year) - counts = append(counts, count) + 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 + } } } render(w, r, templateBlogStats, &renderData{ BlogString: blog, Canonical: blogPath(blog) + appConfig.Blogs[blog].BlogStats.Path, Data: map[string]interface{}{ - "total": totalCount, - "years": years, - "counts": counts, + "total": totalCount, + "years": years, + "withoutdate": noDateCount, + "months": months, }, }) } diff --git a/check.go b/check.go index 9dfa3e4..36a5c16 100644 --- a/check.go +++ b/check.go @@ -36,10 +36,10 @@ func checkAllExternalLinks() { wg.Add(1) for postLinkPair := range linkChan { rm.RLock() - _, ok := responses[postLinkPair.second] + _, ok := responses[postLinkPair.Second] rm.RUnlock() if !ok { - req, err := http.NewRequest(http.MethodGet, postLinkPair.second, nil) + req, err := http.NewRequest(http.MethodGet, postLinkPair.Second, nil) if err != nil { fmt.Println(err.Error()) continue @@ -50,19 +50,19 @@ func checkAllExternalLinks() { req.Header.Set("Accept-Language", "en-US,en;q=0.5") resp, err := client.Do(req) if err != nil { - fmt.Println(postLinkPair.second+" ("+postLinkPair.first+"):", err.Error()) + fmt.Println(postLinkPair.Second+" ("+postLinkPair.First+"):", err.Error()) continue } status := resp.StatusCode _, _ = io.Copy(io.Discard, resp.Body) resp.Body.Close() rm.Lock() - responses[postLinkPair.second] = status + responses[postLinkPair.Second] = status rm.Unlock() } rm.RLock() - if response, ok := responses[postLinkPair.second]; ok && !checkSuccessStatus(response) { - fmt.Println(postLinkPair.second+" ("+postLinkPair.first+"):", response) + if response, ok := responses[postLinkPair.Second]; ok && !checkSuccessStatus(response) { + fmt.Println(postLinkPair.Second+" ("+postLinkPair.First+"):", response) } rm.RUnlock() } diff --git a/templates/assets/js/blogstats.js b/templates/assets/js/blogstats.js new file mode 100644 index 0000000..5f4e37e --- /dev/null +++ b/templates/assets/js/blogstats.js @@ -0,0 +1,11 @@ +(function () { + Array.from(document.getElementsByClassName('statsyear')).forEach(element => { + element.addEventListener('click', function () { + Array.from(document.getElementsByClassName('statsmonth')).forEach(c => { + if (element.dataset.year == c.dataset.year) { + c.classList.contains('hide') ? c.classList.remove('hide') : c.classList.add('hide') + } + }) + }) + }) +})() \ No newline at end of file diff --git a/templates/blogstats.gohtml b/templates/blogstats.gohtml index 67c2beb..7e787f9 100644 --- a/templates/blogstats.gohtml +++ b/templates/blogstats.gohtml @@ -14,19 +14,30 @@ - {{ $counts := .Data.counts }} - {{ range $i, $year := .Data.years }} - - {{ $year }} - {{ index $counts $i }} + {{ $months := .Data.months }} + {{ range $year := .Data.years }} + + {{ $year.First }} + {{ $year.Second }} + {{ range $month := (index $months $year.First) }} + + {{ $month.First }} + {{ $month.Second }} + + {{ end }} {{ end }} - {{ string .Blog.Lang "total" }} + {{ string .Blog.Lang "withoutdate" }} + {{ .Data.withoutdate }} + + + {{ string .Blog.Lang "total" }} {{ .Data.total }} + {{ end }} diff --git a/templates/strings/de.yaml b/templates/strings/de.yaml index 8228a8b..b3d804c 100644 --- a/templates/strings/de.yaml +++ b/templates/strings/de.yaml @@ -29,4 +29,5 @@ update: "Aktualisieren" updatedon: "Aktualisiert am" upload: "Hochladen" view: "Anschauen" +withoutdate: "Ohne Datum" year: "Jahr" \ No newline at end of file diff --git a/templates/strings/default.yaml b/templates/strings/default.yaml index ab6127c..c7af5f4 100644 --- a/templates/strings/default.yaml +++ b/templates/strings/default.yaml @@ -47,4 +47,5 @@ verified: "Verified" view: "View" webmentions: "Webmentions" websiteopt: "Website (optional)" +withoutdate: "Without date" year: "Year" \ No newline at end of file diff --git a/utils.go b/utils.go index 912c75c..08a1986 100644 --- a/utils.go +++ b/utils.go @@ -162,5 +162,5 @@ func dateFormat(date string, format string) string { } type stringPair struct { - first, second string + First, Second string }