47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
|
|
import sqlite3
|
|
import sys
|
|
import os
|
|
import datetime
|
|
|
|
# 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, hour,
|
|
sum(total) as total
|
|
from hit_counts
|
|
where
|
|
hit_counts.site = 1
|
|
and hour >= ?
|
|
and path like '/post/20%'
|
|
and path not like '%/?%'
|
|
group by path
|
|
order by total desc
|
|
limit 5
|
|
"""
|
|
|
|
dbpath = "/var/www/goatcounter/db/goatcounter.sqlite3"
|
|
if len(sys.argv) > 1:
|
|
dbpath = sys.argv[1]
|
|
|
|
if not os.path.exists(dbpath):
|
|
exit("DB file " + dbpath + " does not exist, quitting!")
|
|
|
|
with sqlite3.connect(dbpath) as connection:
|
|
cursor = connection.cursor()
|
|
|
|
a_month_ago = (datetime.date.today() - datetime.timedelta(days=31)).strftime("%Y-%m-%d")
|
|
|
|
cursor.execute(q, [a_month_ago])
|
|
rows = cursor.fetchall()
|
|
total = sum(map(lambda r: int(r[2]), rows))
|
|
|
|
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) + "\", \"rank\": \"" + rank(r) + "\" }", rows)))
|
|
print(']')
|