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/2021/02/writing-academic-papers-in-markdown/", "rank": "43%" },
{ "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/", "rank": "20%" },
{ "url": "/post/2022/04/cool-things-people-do-with-their-blogs", "title": "Cool Things People Do With Their Blogs", "rank": "10%" }, { "url": "/post/2023/03/continuous-productivity-is-toxic/", "rank": "14%" },
{ "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/", "rank": "12%" },
{ "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/2022/04/analogue-pocket/", "rank": "11%" }
] ]

View File

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

View File

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