-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
statstool.py
executable file
·159 lines (134 loc) · 4.98 KB
/
statstool.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""
Command-line tool to dump statistics.
"""
import argparse
from collections import namedtuple
import datetime
from fxtumblr import config
STATS_DB_TYPE = config.get("stats_db_type", "sqlite")
if STATS_DB_TYPE == "sqlite":
import sqlite3
STATS_DB = config.get("stats_db", "stats.db")
elif STATS_DB_TYPE == "postgres":
import psycopg
PSQL_DSN = ' '.join([
f"host={config['stats_db_host']}",
f"port={config['stats_db_port']}",
f"user={config['stats_db_user']}",
f"password={config['stats_db_password']}",
f"dbname={config['stats_db_name']}",
])
else:
raise ValueError("stats_db_type must be one of: sqlite, postgres")
# Argument parsing
parser = argparse.ArgumentParser(
prog="statstool.py", description="Tools for parsing fxtumblr instance statistics"
)
subparsers = parser.add_subparsers(title="mode")
plot_parser = subparsers.add_parser("plot")
plot_parser.set_defaults(mode="plot")
plot_parser.add_argument(
"-d", "--days", type=int, help="How many days back to plot the data for"
)
plot_parser.add_argument(
"-s", "--start-date", help="Start date (inclusive) for the data (YYYY-MM-DD)"
)
plot_parser.add_argument(
"-e", "--end-date", help="End date (inclusive) for the data (YYYY-MM-DD)"
)
plot_parser.add_argument(
"-u",
"--unique",
help="Only count unique hits (i.e. multiple hits of the same post are discarded)",
action="store_true",
)
plot_parser.add_argument(
"-p",
"--print-only",
help="Instead of generating plot, print the data",
action="store_true",
)
plot_parser.add_argument(
"-m",
"--modifiers",
help="Only print cases where the provided modifiers (comma-separated) are used"
)
args = parser.parse_args()
try:
mode = args.mode
except:
print('No command provided! See "statstool.py --help" for more information.')
quit(1)
# Plot
if mode == "plot":
import matplotlib.pyplot as plt
if not args.days and not args.start_date:
raise ValueError("Must specify one of --days or --start-date")
now = datetime.datetime.now().replace(hour=0, minute=0, second=0)
if args.days:
start_date = now - datetime.timedelta(days=args.days)
elif args.start_date:
start_date = datetime.datetime.strptime(args.start_date, "%Y-%m-%d")
if args.end_date:
end_date = datetime.datetime.strptime(args.end_date, "%Y-%m-%d")
else:
end_date = now
if start_date > end_date:
raise ValueError("Start date is earlier than end date")
delta = end_date - start_date
start_date_epoch = int(start_date.strftime("%s"))
end_date_epoch = int((end_date + datetime.timedelta(days=1)).strftime("%s")) - 1
modifiers = []
if args.modifiers:
modifiers = args.modifiers.split(",")
hit_tuple = namedtuple("hit_tuple", "id time post modifiers failed")
hits = []
data = {}
for i in range(delta.days + 1):
checked_date = start_date + datetime.timedelta(days=i)
checked_date_start_epoch = int(checked_date.strftime("%s"))
checked_date_end_epoch = (
int((checked_date + datetime.timedelta(days=1)).strftime("%s")) - 1
)
hits_for_day = []
posts = set()
if STATS_DB_TYPE == "sqlite":
with sqlite3.connect(STATS_DB) as db:
fetch = db.execute(
f"SELECT * FROM fxtumblr_stats WHERE time BETWEEN {checked_date_start_epoch} AND {checked_date_end_epoch};"
).fetchall()
elif STATS_DB_TYPE == "postgres":
with psycopg.connect(PSQL_DSN) as conn:
with conn.cursor() as cur:
fetch = cur.execute(
f"SELECT * FROM fxtumblr_stats WHERE time BETWEEN {checked_date_start_epoch} AND {checked_date_end_epoch};"
).fetchall()
for hit in fetch:
hit_parsed = hit_tuple(*hit)._asdict()
if args.unique and hit_parsed["post"] in posts:
continue
if modifiers:
skip = False
for mod in modifiers:
if mod not in hit_parsed["modifiers"].split(","):
skip = True
break
if skip:
continue
posts.add(hit_parsed["post"])
hits_for_day.append(hit_parsed)
data[checked_date.strftime("%Y-%m-%d")] = len(hits_for_day)
if args.print_only:
for date, hits in data.items():
print(f"{date}: {hits} hits")
else:
fig, ax = plt.subplots()
ax.set_xlabel("date")
ax.set_ylabel("hits")
ax.plot(list(data.keys()), list(data.values()), linestyle="solid", marker="o")
for tick in ax.get_xticklabels():
tick.set_rotation(75)
fig.savefig("stats.png")
print("Done! Saved as stats.png")