From c40d02c2306d54bf8536db75d88fd41bdfff35a7 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Thu, 13 Apr 2023 16:15:32 +0200 Subject: [PATCH] extra insurance for popular posts in case sqlite db is contaminated --- data/popularposts.json | 12 ++++++------ extras/popularposts.py | 16 ++++++++-------- layouts/index.html | 23 +++++++++++++++-------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/data/popularposts.json b/data/popularposts.json index c7d04694..27bf9d54 100644 --- a/data/popularposts.json +++ b/data/popularposts.json @@ -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%" } +] \ No newline at end of file diff --git a/extras/popularposts.py b/extras/popularposts.py index 7347a80c..6b9eaa1f 100644 --- a/extras/popularposts.py +++ b/extras/popularposts.py @@ -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(']') diff --git a/layouts/index.html b/layouts/index.html index 6940adf4..100d2443 100644 --- a/layouts/index.html +++ b/layouts/index.html @@ -42,14 +42,21 @@