extra insurance for popular posts in case sqlite db is contaminated

This commit is contained in:
Wouter Groeneveld 2023-04-13 16:15:32 +02:00
parent 1e60501810
commit c40d02c230
3 changed files with 29 additions and 22 deletions

View File

@ -1,7 +1,7 @@
[
{ "url": "/post/2022/10/a-visual-studio-6-nostalgia-trip", "title": "A Visual Studio 6.0 Nostalgia Trip", "rank": "57%" },
{ "url": "/post/2022/10/guilt-and-flexible-working-hours", "title": "Guilt And Flexible Working Hours", "rank": "15%" },
{ "url": "/post/2022/04/cool-things-people-do-with-their-blogs", "title": "Cool Things People Do With Their Blogs", "rank": "10%" },
{ "url": "/post/2021/02/writing-academic-papers-in-markdown", "title": "How to write academic papers in Markdown", "rank": "10%" },
{ "url": "/post/2022/02/how-to-setup-pi-hole-on-synology-nas", "title": "How to setup Pi-Hole on a Synology NAS", "rank": "8%" }
{ "url": "/post/2021/02/writing-academic-papers-in-markdown/", "rank": "43%" },
{ "url": "/post/2022/04/cool-things-people-do-with-their-blogs/", "rank": "20%" },
{ "url": "/post/2023/03/continuous-productivity-is-toxic/", "rank": "14%" },
{ "url": "/post/2022/02/how-to-setup-pi-hole-on-synology-nas/", "rank": "12%" },
{ "url": "/post/2022/04/analogue-pocket/", "rank": "11%" }
]

View File

@ -8,13 +8,14 @@ import datetime
# date format: "2022-11-01 23:00:00"
q = """
select
path, title, hour,
path, hour,
sum(total) as total
from hit_counts
where
hit_counts.site = 1
and hour >= ?
and path like '/post/%'
and path like '/post/20%'
and path not like '%/?%'
group by path
order by total desc
limit 5
@ -34,13 +35,12 @@ with sqlite3.connect(dbpath) as connection:
cursor.execute(q, [a_month_ago])
rows = cursor.fetchall()
total = sum(map(lambda r: int(r[3]), rows))
total = sum(map(lambda r: int(r[2]), rows))
title = lambda r: r[1].split('|')[0].strip()
url = lambda r: r[0]
date = lambda r: datetime.datetime.strptime(r[2], "%Y-%m-%d %H:%M:%S").strftime("%d %b %Y")
rank = lambda r: str(round((int(r[3]) / total) * 100)) + "%"
url = lambda r: r[0] if r[0].endswith("/") else r[0] + '/'
date = lambda r: datetime.datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S").strftime("%d %b %Y")
rank = lambda r: str(round((int(r[2]) / total) * 100)) + "%"
print('[')
print(',\n'.join(map(lambda r: "{ \"url\": \"" + url(r) + "\", \"title\": \"" + title(r) + "\", \"rank\": \"" + rank(r) + "\" }", rows)))
print(',\n'.join(map(lambda r: "{ \"url\": \"" + url(r) + "\", \"rank\": \"" + rank(r) + "\" }", rows)))
print(']')

View File

@ -42,14 +42,21 @@
<ul class="small">
{{ range first $itms (.Site.Data.popularposts) }}
<li class="flexgrid flexgrid-8020">
<h3>
<a href="{{ .url }}">{{ .title }}</a>
</h3>
<small>
{{ .rank }} hits
</small>
</li>
{{ $rank := .rank }}
{{ $url := .url }}
<!-- this is silly but extra insurance in case the data is contaminated, which it was... Take title from Hugo -->
{{ range where $.Site.Pages "Section" "post" }}
{{ if eq .RelPermalink $url }}
<li class="flexgrid flexgrid-8020">
<h3>
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</h3>
<small>
{{ $rank }} hits
</small>
</li>
{{ end }}
{{ end }}
{{ end }}
</ul>
</article>