-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
50 lines (41 loc) · 1.4 KB
/
Copy pathstats.py
File metadata and controls
50 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from datetime import datetime, date
import psycopg2
import psycopg2.extensions
def pastecount(cursor: psycopg2.extensions.cursor) -> None:
with cursor as cur:
cur.execute(
"UPDATE stats SET counter = counter + 1 WHERE metric = 'totalpastes';"
)
dailystats(cur, "pastecount", datetime.utcnow().date())
def pasteview(cursor: psycopg2.extensions.cursor) -> None:
with cursor as cur:
cur.execute(
"UPDATE stats SET counter = counter + 1 WHERE metric = 'totalviews';"
)
dailystats(cur, "pasteviews", datetime.utcnow().date())
def dailystats(
cursor: psycopg2.extensions.cursor, metric: str, today: date
) -> None:
cursor.execute(
"""INSERT INTO dailystats (date, {}) \
VALUES (%s, %s) \
ON CONFLICT (date) \
DO UPDATE SET {} = dailystats.{} + 1 \
WHERE dailystats.date = %s;""".format(
metric, metric, metric
),
(today, 1, today),
)
def getstats(cursor: psycopg2.extensions.cursor) -> dict:
stats = {}
with cursor as cur:
cur.execute(
"SELECT * FROM dailystats WHERE date = %s;", (datetime.utcnow().date(),)
)
stats["daily"] = cur.fetchone()
cur.execute("SELECT * FROM stats;")
totalstats = {}
for i in cur.fetchall():
totalstats[i[0]] = i[1]
stats["total"] = totalstats
return stats