-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcharts.py
190 lines (153 loc) · 5.49 KB
/
charts.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from datetime import datetime
from datetime import timedelta
from opencolor import oc
import json
import os
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
VERSION_DIR = os.path.join(ROOT_DIR, "version")
TEMPLATE_FILE = os.path.join(ROOT_DIR, "template.html")
INDEX_FILE = os.path.join(ROOT_DIR, "index.html")
MAX_DAYS = 7
def chart_colors(index):
return [
oc["grape"][index],
oc["red"][index],
oc["orange"][index],
oc["yellow"][index],
oc["lime"][index],
oc["green"][index],
oc["teal"][index],
oc["cyan"][index],
oc["blue"][index],
oc["indigo"][index],
oc["violet"][index],
]
chart_colors_bg = chart_colors(1)
chart_colors_text = chart_colors(9)
empty_color = oc["gray"][1]
usage_colors = [
oc["lime"][0],
oc["lime"][1],
oc["lime"][2],
oc["lime"][3],
oc["lime"][5],
oc["lime"][6],
]
def parse_day(filename):
filename = filename.replace(".json", "")
file_date = datetime.strptime(filename, "%Y-%m-%d")
today = datetime.today()
return file_date > today + timedelta(days=-MAX_DAYS)
def string2date(string):
return datetime.strptime(string, "%Y-%m-%d").strftime("%d %b")
def string2day(string):
return datetime.strptime(string, "%Y-%m-%d").strftime("%a")
def string2weekday(string):
return int(datetime.strptime(string, "%Y-%m-%d").strftime("%w"))
def render_cell(value, max):
color_id = round((value / max) * (len(usage_colors) - 1))
if value:
return "<td style='background-color: %s'>%2.1f%%</td>" % (
usage_colors[color_id],
value * 100,
)
return "<td style='background-color: %s'>-</td>" % (empty_color)
def main():
with open(TEMPLATE_FILE, "r") as template:
data = template.read()
_, _, filenames = next(os.walk(VERSION_DIR))
days = {}
versions = set()
for filename in filenames:
if not parse_day(filename):
continue
with open(os.path.join(VERSION_DIR, filename), "r") as day_json:
day = json.load(day_json)
days[filename.replace(".json", "")] = day
for key in day.keys():
versions.add(key)
sorted_days = sorted(days.items())
sorted_versions = sorted(versions)
# find max_value
max_value = 0
for day in sorted_days:
for version in day[1]:
max_value = max(max_value, day[1][version])
chart_rows = [["Day"]]
for version in sorted_versions:
chart_rows[len(chart_rows) - 1].append(
version[-7:] if len(version) == 28 else "Older"
)
for day in sorted_days:
chart_rows.append([string2date(day[0])])
for version in sorted_versions:
if version in day[1]:
chart_rows[len(chart_rows) - 1].append((day[1][version]))
else:
chart_rows[len(chart_rows) - 1].append(0)
report = {}
for version in sorted(sorted_versions, reverse=True):
report[version] = {}
for day in sorted_days:
report[version][day[0]] = 0
if version in day[1]:
report[version][day[0]] = day[1][version]
version_head = "<tr><th>Version</th><th>Commit</th><th style='background-color: {}'></th>".format(
empty_color
)
for day in sorted_days:
version_head += "<th style='background-color: %s'>%s<br>%s</th>" % (
oc["red"][0] if string2weekday(day[0]) in [6, 0] else oc["white"],
string2day(day[0]),
string2date(day[0]),
)
version_head += "</tr>"
version_body = ""
current_version_date = ""
for index, row in enumerate(report):
version_date = row[:10]
version_datetime = row[:16].replace("T", " ")
version_hash = row[-7:]
color_bg = chart_colors_bg[
(index - len(sorted_versions)) % len(chart_colors_bg)
]
color_text = chart_colors_text[
(index - len(sorted_versions)) % len(chart_colors_text)
]
if version_date != current_version_date:
version_body += (
"<tr><td style='background-color: {}' colspan='{}'></td></tr>".format(
empty_color, 3 + len(report[row])
)
)
version_body += "<tr><td style='background-color: {}; color: {};'><code>{}</code></td>".format(
color_bg,
color_text,
version_datetime,
)
# older version
if len(row) == 20:
version_body += "<td style='background-color: {};'></td><td style='background-color: {}'></td>".format(
color_bg,
empty_color,
)
else:
version_body += "<td style='background-color: {};'><code><a style='color: {};' href='https://github.com/excalidraw/excalidraw/commit/{}'>{}</a></code></td><td style='background-color: {}'></td>".format(
color_bg,
color_text,
version_hash,
version_hash,
empty_color,
)
for day in report[row]:
version_body += render_cell(report[row][day], max_value)
version_body += "</tr>\n"
current_version_date = version_date
data = data.replace("{ data }", "%r" % chart_rows)
data = data.replace("{ version_head }", version_head)
data = data.replace("{ version_body }", version_body)
with open(INDEX_FILE, "w") as index:
index.write(data)
print("Charts updated")
if __name__ == "__main__":
main()