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.
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.
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.
import osimport pygalinput_path = os.path.join(os.path.expanduser("~"), "logs")output_path = os.path.join(input_path, "graph.svg")results =list()for fname in os.listdir(input_path)[-10:]: abspath = os.path.join(input_path, fname)withopen(abspath)as f: lines = f.readlines()[2:] # Top two are headers results.append([fname, any(line.startswith("0") for line in lines)])chart = pygal.StackedLine(fill=True, interpolate='cubic', style=pygal.style.LightSolarizedStyle)chart.title ='Successful publishes over time'chart.x_labels = [str(results.index(r))for r in results]chart.add("Publish", [r[1] for r in results])chart.render_to_file(output_path)
Putting it all together, here is the final source code.
import osimport randomimport datetimeimport pygalimport pyblish.apiclassFlipCoin(pyblish.api.ContextPlugin):defprocess(self,context):if random.random()>0.5:raiseException("Failed")classArchiveValidation(pyblish.api.ContextPlugin):# Run after all validators have finished order = pyblish.api.ValidatorOrder +0.1defprocess(self,context): formatted_results = self.format_results(context)# Compute output directory date = datetime.datetime.today().strftime("%Y%m%d-%H%M%S") output_dir = os.path.join(os.path.expanduser("~"), "logs") output_path = os.path.join(output_dir, date +".txt")# Write to diskifnot os.path.exists(output_dir): os.makedirs(output_dir)withopen(output_path, "w")as f:# E.g. c:\users\marcus\Documents\logs\20150612-110000.txt f.write(formatted_results)# Print rather than log, as this plug-in# won't be included in the results.print("Outputted to: %s"% output_dir) context.data["archiveDir"]= output_dirdefformat_results(self,context): header ="{:<10}{:<40} -> {}".format("Success", "Plug-in", "Instance") result ="{success:<10}{plugin.__name__:<40} -> {instance}" error ="{:<10}+-- EXCEPTION: {:<70}" record ="{:<10}+-- {level}: {message:<70}" results =list()for r in context.data["results"]:# Format summary results.append(result.format(**r))# Format log recordsfor lr in r["records"]: results.append(record.format("", level=lr.levelname, message=lr.message))# Format exception (if any)if r["error"]: results.append(error.format("", r["error"])) report ="""{header}{line}{results} """return report.format( header=header, results="\n".join(results), line="-"*70)classPlotArchive(pyblish.api.ContextPlugin):# Run after archival order = pyblish.api.ValidatorOrder +0.2defprocess(self,context): input_path = context.data["archiveDir"] output_path = os.path.join(input_path, "graph.svg") results =list()for fname in os.listdir(input_path)[-10:]: abspath = os.path.join(input_path, fname)withopen(abspath)as f: lines = f.readlines()[2:] # Top two are headers results.append([fname, any(line.startswith("0") for line in lines)]) chart = pygal.StackedLine(fill=True, interpolate='cubic', style=pygal.style.LightSolarizedStyle) chart.title ='Successful publishes over time' chart.x_labels = [str(results.index(r))for r in results] chart.add("Publish", [r[1] for r in results]) chart.render_to_file(output_path)pyblish.api.register_plugin(FlipCoin)pyblish.api.register_plugin(ArchiveValidation)pyblish.api.register_plugin(PlotArchive)import pyblish.utilpyblish.util.publish()# Outputted to: C:\Users\marcus\logs
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.