-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
executable file
·139 lines (114 loc) · 4.05 KB
/
Copy pathgraph.py
File metadata and controls
executable file
·139 lines (114 loc) · 4.05 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
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
#!/usr/bin/env python3
import os
import subprocess
import time
from contextlib import contextmanager
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from matplotlib.dates import num2date
DATA_FILE = "listen.ron"
GRAPH_FILE = "history.svg"
NEW_FORMAT_TIMESTAMP = 1736956035
CONTENT_COLOR = "#d5397b"
TEXT_COLOR = "#cccccc"
@contextmanager
def timer(name: str):
print(f"\x1b[1;32m{name}\x1b[0m start")
start = time.perf_counter()
yield
end = time.perf_counter()
elapsed = end - start
print(f"\x1b[1;32m{name}\x1b[0m: \x1b[1;35m{elapsed:.3f}\x1b[22ms\x1b[0m")
with timer(os.path.basename(__file__)):
with timer("data extraction"):
with timer("git log"):
datapoints = (
subprocess.run(
f"git log --format='%H %cs %ct' -- {DATA_FILE} | awk '!seen[$2]++ {{print $1, $2, $3, $4}}'",
shell=True,
text=True,
capture_output=True,
)
.stdout.strip()
.splitlines()[::-1]
)
with timer("line count"):
data = []
for datapoint in datapoints:
commit, date, timestamp = datapoint.split()
recording_count = (
int(
subprocess.run(
f"git show {commit}:{DATA_FILE} | wc -l",
shell=True,
text=True,
capture_output=True,
).stdout.strip()
)
- 2
)
if int(timestamp) <= NEW_FORMAT_TIMESTAMP:
recording_count //= 5
data.append((datetime.strptime(date, "%Y-%m-%d"), recording_count))
data.sort(key=lambda x: x[0])
with timer("graph plotting"):
x, y = zip(*data)
min_x, max_x = x[0], x[-1]
min_y, max_y = y[0], y[-1]
for text_type in [
"text.color",
"axes.labelcolor",
"xtick.color",
"ytick.color",
"axes.titlecolor",
]:
plt.rcParams[text_type] = TEXT_COLOR
plt.rcParams["font.family"] = "monospace"
plt.plot(x, y, color=CONTENT_COLOR, alpha=0.5)
# plt.plot(x, y, color=CONTENT_COLOR)
plt.ylabel("recording")
plt.title(f"recording count in {DATA_FILE} over time")
plt.gcf().autofmt_xdate(
rotation=-45,
ha="left",
)
plt.fill_between(x, y, color=CONTENT_COLOR, alpha=0.4)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.margins(x=0, y=0)
plt.xlim(min_x, max_x)
plt.ylim(min_y, max_y + 1)
min_x_edge = min_x.replace(tzinfo=None) + timedelta(days=15)
max_x_edge = max_x.replace(tzinfo=None) - timedelta(days=15)
xticks = []
for tick in plt.gca().get_xticks():
tick = num2date(tick).replace(tzinfo=None)
if tick > min_x_edge and tick < max_x_edge:
xticks.append(tick)
plt.xticks([min_x] + xticks + [max_x])
plt.yticks(
[tick for tick in plt.gca().get_yticks() if tick < max_y - 33] + [max_y]
)
plt.gca().xaxis.set_label_coords(-0.05, 0.5)
plt.gca().yaxis.tick_right()
plt.gca().yaxis.set_label_position("right")
today = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
total_days = (today - min_x).days
average = max_y / total_days if total_days > 0 else 0.0
plt.text(
0.1,
0.9,
f"days: {total_days}\n avg: {average:.2f}",
color=TEXT_COLOR,
transform=plt.gca().transAxes,
va="top",
family="monospace",
linespacing=1.5,
)
plt.savefig(
GRAPH_FILE,
format=GRAPH_FILE.split(".")[-1],
transparent=True,
bbox_inches="tight",
pad_inches=0,
)