40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
# retoor <retoor@molodetz.nl>
|
|||
|
|
from molodetz.database.core import db
|
||
|
|
|
||
|
|
|
||
|
|
def flush_visits(day, counts):
|
||
|
|
if not counts:
|
||
|
|
return
|
||
|
|
with db:
|
||
|
|
for (path, status), hits in counts.items():
|
||
|
|
db.query(
|
||
|
|
"INSERT INTO visit_stats (day, path, status, hits) VALUES (:day, :path, :status, :hits) "
|
||
|
|
"ON CONFLICT(day, path, status) DO UPDATE SET hits = hits + excluded.hits",
|
||
|
|
day=day,
|
||
|
|
path=path,
|
||
|
|
status=status,
|
||
|
|
hits=hits,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def top_paths(limit=25):
|
||
|
|
if "visit_stats" not in db.tables:
|
||
|
|
return []
|
||
|
|
return list(
|
||
|
|
db.query(
|
||
|
|
"SELECT path, SUM(hits) AS hits FROM visit_stats GROUP BY path ORDER BY hits DESC LIMIT :limit",
|
||
|
|
limit=limit,
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def daily_totals(limit=30):
|
||
|
|
if "visit_stats" not in db.tables:
|
||
|
|
return []
|
||
|
|
return list(
|
||
|
|
db.query(
|
||
|
|
"SELECT day, SUM(hits) AS hits FROM visit_stats GROUP BY day ORDER BY day DESC LIMIT :limit",
|
||
|
|
limit=limit,
|
||
|
|
)
|
||
|
|
)
|