Report V
Writing a report and archiving it is all well and good, but what would be really amazing is if we could visualise history with plots and charts. So let's do that now.
image
For this example, I'll be using pygal so go ahead and install this if you would like to follow along. Reusing ArchiveValidators from the previous example, let's make some history.
1
import os
2
import random
3
import datetime
4
import pyblish.api
5
6
class FlipCoin(pyblish.api.ContextPlugin):
7
def process(self, context):
8
if random.random() > 0.5:
9
raise Exception("Failed")
10
11
class ArchiveValidation(pyblish.api.ContextPlugin):
12
...
13
14
pyblishOpi.register_plugin(FlipCoin)
15
pyblish.api.register_plugin(ArchiveValidation)
16
17
import pyblish.util
18
pyblish.util.publish()
Copied!
Run this at least 10 times so we have enough reports to work with. Next we will retrieve the last 10 publishes and draw a graph representing which time was successful and which was not.
Here is the graph-drawing source code.
1
import os
2
import pygal
3
4
input_path = os.path.join(os.path.expanduser("~"), "logs")
5
output_path = os.path.join(input_path, "graph.svg")
6
7
results = list()
8
for fname in os.listdir(input_path)[-10:]:
9
abspath = os.path.join(input_path, fname)
10
with open(abspath) as f:
11
lines = f.readlines()[2:] # Top two are headers
12
results.append([fname, any(line.startswith("0") for line in lines)])
13
14
chart = pygal.StackedLine(fill=True,
15
interpolate='cubic',
16
style=pygal.style.LightSolarizedStyle)
17
18
chart.title = 'Successful publishes over time'
19
chart.x_labels = [str(results.index(r)) for r in results]
20
chart.add("Publish", [r[1] for r in results])
21
chart.render_to_file(output_path)
Copied!
Putting it all together, here is the final source code.
1
import os
2
import random
3
import datetime
4
5
import pygal
6
import pyblish.api
7
8
class FlipCoin(pyblish.api.ContextPlugin):
9
def process(self, context):
10
if random.random() > 0.5:
11
raise Exception("Failed")
12
13
class ArchiveValidation(pyblish.api.ContextPlugin):
14
# Run after all validators have finished
15
order = pyblish.api.ValidatorOrder + 0.1
16
17
def process(self, context):
18
formatted_results = self.format_results(context)
19
20
# Compute output directory
21
date = datetime.datetime.today().strftime("%Y%m%d-%H%M%S")
22
output_dir = os.path.join(os.path.expanduser("~"), "logs")
23
output_path = os.path.join(output_dir, date + ".txt")
24
25
# Write to disk
26
if not os.path.exists(output_dir):
27
os.makedirs(output_dir)
28
29
with open(output_path, "w") as f:
30
# E.g. c:\users\marcus\Documents\logs\20150612-110000.txt
31
f.write(formatted_results)
32
33
# Print rather than log, as this plug-in
34
# won't be included in the results.
35
print("Outputted to: %s" % output_dir)
36
context.data["archiveDir"] = output_dir
37
38
def format_results(self, context):
39
header = "{:<10}{:<40} -> {}".format("Success", "Plug-in", "Instance")
40
result = "{success:<10}{plugin.__name__:<40} -> {instance}"
41
error = "{:<10}+-- EXCEPTION: {:<70}"
42
record = "{:<10}+-- {level}: {message:<70}"
43
44
results = list()
45
for r in context.data["results"]:
46
# Format summary
47
results.append(result.format(**r))
48
49
# Format log records
50
for lr in r["records"]:
51
results.append(record.format("", level=lr.levelname, message=lr.message))
52
53
# Format exception (if any)
54
if r["error"]:
55
results.append(error.format("", r["error"]))
56
57
report = """
58
{header}
59
{line}
60
{results}
61
"""
62
63
return report.format(
64
header=header,
65
results="\n".join(results),
66
line="-" * 70)
67
68
class PlotArchive(pyblish.api.ContextPlugin):
69
# Run after archival
70
order = pyblish.api.ValidatorOrder + 0.2
71
72
def process(self, context):
73
input_path = context.data["archiveDir"]
74
output_path = os.path.join(input_path, "graph.svg")
75
76
results = list()
77
for fname in os.listdir(input_path)[-10:]:
78
abspath = os.path.join(input_path, fname)
79
with open(abspath) as f:
80
lines = f.readlines()[2:] # Top two are headers
81
results.append([fname, any(line.startswith("0") for line in lines)])
82
83
chart = pygal.StackedLine(fill=True,
84
interpolate='cubic',
85
style=pygal.style.LightSolarizedStyle)
86
87
chart.title = 'Successful publishes over time'
88
chart.x_labels = [str(results.index(r)) for r in results]
89
chart.add("Publish", [r[1] for r in results])
90
chart.render_to_file(output_path)
91
92
93
pyblish.api.register_plugin(FlipCoin)
94
pyblish.api.register_plugin(ArchiveValidation)
95
pyblish.api.register_plugin(PlotArchive)
96
97
import pyblish.util
98
pyblish.util.publish()
99
# Outputted to: C:\Users\marcus\logs
Copied!
Now, for every publish, results are archived and the graph updated. Go ahead, publish a few times and keep on refreshing your browser whilst looking at the graph. Animation is an exercise left to the reader.
Last modified 1yr ago
Copy link