2022-11-12 10:10:31 +01:00
|
|
|
|
|
|
|
import sqlite3
|
|
|
|
import sys
|
|
|
|
import os
|
2022-11-12 21:29:17 +01:00
|
|
|
import datetime
|
2022-11-12 10:10:31 +01:00
|
|
|
|
|
|
|
# see https://github.com/arp242/goatcounter/blob/master/db/query/hit_list.List-stats.sql
|
|
|
|
# date format: "2022-11-01 23:00:00"
|
|
|
|
q = """
|
|
|
|
select
|
|
|
|
path, title, hour,
|
|
|
|
sum(total) as total
|
|
|
|
from hit_counts
|
|
|
|
where
|
|
|
|
hit_counts.site = 1
|
2022-11-12 21:29:17 +01:00
|
|
|
and hour >= ?
|
2022-11-12 10:10:31 +01:00
|
|
|
and path like '/post/%'
|
|
|
|
group by path
|
|
|
|
order by total desc
|
|
|
|
limit 5
|
|
|
|
"""
|
|
|
|
|
2022-11-12 10:17:24 +01:00
|
|
|
dbpath = "/var/www/goatcounter/db/goatcounter.sqlite3"
|
2022-11-12 10:10:31 +01:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
dbpath = sys.argv[1]
|
|
|
|
|
|
|
|
if not os.path.exists(dbpath):
|
|
|
|
exit("DB file " + dbpath + " does not exist, quitting!")
|
|
|
|
|
2022-11-12 21:29:17 +01:00
|
|
|
with sqlite3.connect(dbpath) as connection:
|
|
|
|
cursor = connection.cursor()
|
2022-11-12 10:10:31 +01:00
|
|
|
|
2022-11-12 21:29:17 +01:00
|
|
|
a_month_ago = (datetime.date.today() - datetime.timedelta(days=31)).strftime("%Y-%m-%d")
|
2022-11-12 10:10:31 +01:00
|
|
|
|
2022-11-12 21:29:17 +01:00
|
|
|
cursor.execute(q, [a_month_ago])
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
total = sum(map(lambda r: int(r[3]), rows))
|
2022-11-12 10:10:31 +01:00
|
|
|
|
2022-11-12 21:29:17 +01:00
|
|
|
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)) + "%"
|
|
|
|
|
|
|
|
print('[')
|
|
|
|
print(',\n'.join(map(lambda r: "{ \"url\": \"" + url(r) + "\", \"title\": \"" + title(r) + "\", \"rank\": \"" + rank(r) + "\" }", rows)))
|
|
|
|
print(']')
|