8341: Use "time since job start", not "time since task start", as X axis.
[arvados.git] / tools / crunchstat-summary / crunchstat_summary / chartjs.py
1 from __future__ import print_function
2
3 import cgi
4 import json
5 import pkg_resources
6
7 from crunchstat_summary import logger
8
9
10 class ChartJS(object):
11     JSLIB = 'https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.min.js'
12
13     def __init__(self, label, summarizers):
14         self.label = label
15         self.summarizers = summarizers
16
17     def html(self):
18         return '''<!doctype html><html><head>
19         <title>{} stats</title>
20         <script type="text/javascript" src="{}"></script>
21         <script type="text/javascript">{}</script>
22         </head><body></body></html>
23         '''.format(cgi.escape(self.label), self.JSLIB, self.js())
24
25     def js(self):
26         return 'var sections = {};\n{}'.format(
27             json.dumps(self.sections()),
28             pkg_resources.resource_string('crunchstat_summary', 'chartjs.js'))
29
30     def sections(self):
31         return [
32             {
33                 'label': s.long_label(),
34                 'charts': self.charts(s.label, s.tasks),
35             }
36             for s in self.summarizers]
37
38     def charts(self, label, tasks):
39         return [
40             {
41                 'axisY': {
42                     'minimum': 0,
43                 },
44                 'data': [
45                     {
46                         'type': 'line',
47                         'markerType': 'none',
48                         'dataPoints': self._datapoints(
49                             label=uuid, task=task, series=task.series[stat]),
50                     }
51                     for uuid, task in tasks.iteritems()
52                 ],
53                 'title': {
54                     'text': '{}: {} {}'.format(label, stat[0], stat[1]),
55                 },
56                 'zoomEnabled': True,
57             }
58             for stat in (('cpu', 'user+sys__rate'),
59                          ('mem', 'rss'),
60                          ('net:eth0', 'tx+rx__rate'),
61                          ('net:keep0', 'tx+rx__rate'))]
62
63     def _datapoints(self, label, task, series):
64         points = [
65             {'x': pt[0].total_seconds(), 'y': pt[1]}
66             for pt in series]
67         if len(points) > 0:
68             points[-1]['markerType'] = 'cross'
69             points[-1]['markerSize'] = 12
70         return points