website-theme/layouts/_default/stats.html

130 lines
4.6 KiB
HTML
Raw Normal View History

2022-05-29 14:23:59 -04:00
{{ define "main" }}
<!-- Adapted from Luke Harris https://www.lkhrs.com/ -->
{{ partial "header.html" . }}
<main class="container">
<article class="row justify-content-center">
<div class="col-md-8">
{{ .Content }}
{{ $tags := .Site.Taxonomies.tags }}
2022-05-29 14:23:59 -04:00
{{ $posts := where site.RegularPages "Type" "blog" }}
{{ $postCount := len $posts }}
2022-05-29 14:23:59 -04:00
{{ $scratch := newScratch }}
{{ $scratch.Set "longCount" 0 }}
{{ range $posts }}
<!-- Sum words for average calculation later -->
{{ $scratch.Add "wordcount" .WordCount }}
<!-- Find longest post -->
{{ if ge .WordCount ($scratch.Get "longCount") }}
{{ $scratch.Set "longestPost" . }}
{{ $scratch.Set "longCount" .WordCount }}
{{ end }}
{{ end }}
2022-12-02 00:39:44 -05:00
<!-- Most Popular tag -->
{{ $mostPopularTag := index .Site.Taxonomies.tags.ByCount 0 }}
2022-05-29 14:23:59 -04:00
{{ $wordCount := $scratch.Get "wordcount" }}
{{ $avgPostLength := div $wordCount $postCount }}
<table class="table">
<tr>
{{ range last 1 $posts }}
{{ $numYears := div (sub now.Unix .Date.Unix) 31536000 }}
<td width="40%">Years Blogging</td>
<td width="60%">{{ $numYears | lang.NumFmt 0 }}</td>
{{ end }}
</tr>
<tr>
<td>Total Posts Published</td>
<td>{{ $postCount | lang.NumFmt 0 }}</td>
2022-05-29 14:23:59 -04:00
</tr>
<tr>
<td>Total Words Written</td>
<td>{{ $wordCount | lang.NumFmt 0 }}</td>
</tr>
<tr>
<td>Average Words per Post</td>
<td>{{ $avgPostLength | lang.NumFmt 0 }}</td>
</tr>
<tr>
<td>Number of Topics</td>
<td>{{ len $tags | lang.NumFmt 0 }}</td>
</tr>
2022-12-02 00:39:44 -05:00
<tr>
<td>Most Popular Topic</td>
<td>
{{ with $mostPopularTag }}
<a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> with {{ .Count }} posts
{{ end }}
</td>
</tr>
2022-05-29 14:23:59 -04:00
<tr>
<td>Longest Post Published</td>
<td>
{{ with $scratch.Get "longestPost" }}
<a href="{{ .Permalink }}">{{ .Title }}</a> with {{ .WordCount }} words
{{ end }}
</td>
</tr>
2024-01-01 19:39:27 -05:00
<tr>
<td>Total Wikipedia Edits</td>
<td>
{{ $data := getJSON "https://en.wikipedia.org/w/api.php?action=query&list=users&ususers=BrandonRozek&usprop=editcount&format=json" }}
{{ range first 1 $data.query.users }}
{{ .editcount }}
{{ end }}
</td>
</tr>
2022-05-29 14:23:59 -04:00
</table>
{{ $grouped := $posts.GroupByDate "2006" }}
<h2 class="post-stats-hd">Posts Per Year</h2>
<div style="width: 100%;height: 300px;margin: 30px auto">
<canvas id="yearchart"></canvas>
<noscript>
<table class="table">
<tr>
<th width="30%">Year</td>
<th width="70%">Number of Posts</td>
</tr>
{{ range $grouped.Reverse }}
{{ $yearPostCount := len (where .Pages "Type" "blog") }}
<tr>
<td>{{ .Key }}</td>
<td>{{ $yearPostCount | lang.NumFmt 0 }}</td>
</tr>
{{ $scratch.Add "yearKeys" "'" }}
{{ $scratch.Add "yearKeys" .Key }}
{{ $scratch.Add "yearKeys" "'," }}
{{ $scratch.Add "yearCount" (string $yearPostCount) }}
{{ $scratch.Add "yearCount" "," }}
{{ end }}
</table>
</noscript>
</div>
{{ $chartPre := "{type: 'line',data: {labels: [" }}
{{ $chartYearKeys := trim ($scratch.Get "yearKeys") ","}}
{{ $chartYearCount := trim ($scratch.Get "yearCount") ","}}
{{ $chartMid := "],datasets: [{label: 'Posts Published',data: [" }}
{{ $chartPost := "],backgroundColor: 'rgb(54, 128, 56)',borderColor: 'rgb(54, 128, 56)',borderWidth: 3}]},options: {maintainAspectRatio: false, animation: false, scales: {y: {ticks: {beginAtZero: true}}}}}"
}}
{{ $chartData := printf "%s" $chartPost | printf "%s%s" $chartYearCount | printf "%s%s" $chartMid | printf "%s%s" $chartYearKeys | printf "%s%s" $chartPre | printf "%s" }}
2022-12-05 13:17:02 -05:00
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.0.1/dist/chart.umd.min.js" type="text/javascript"></script>
<script>
2022-05-29 14:23:59 -04:00
var ctx = document.getElementById('yearchart').getContext('2d');
var options = {{ $chartData | safeJS }};
new Chart(ctx, options);
2022-12-05 13:17:02 -05:00
</script>
2022-05-29 14:23:59 -04:00
</div>
</article>
</main>
{{ end }}