790b08da87f1b7894fc4e47511c63d44eda9604e
[arvados.git] / tools / crunchstat-summary / crunchstat_summary / webchart.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 import cgi
6 import json
7 import pkg_resources
8
9
10 class WebChart(object):
11     """Base class for a web chart.
12
13     Subclasses must assign JSLIB and JSASSET, and override the
14     chartdata() method.
15     """
16     JSLIB = None
17     JSASSET = None
18
19     def __init__(self, label, summarizers):
20         self.label = label
21         self.summarizers = summarizers
22
23     def html(self):
24         return '''<!doctype html><html><head>
25         <title>{} stats</title>
26         <script type="text/javascript" src="{}"></script>
27         <script type="text/javascript">{}</script>
28         {}
29         </head><body></body></html>
30         '''.format(cgi.escape(self.label),
31                    self.JSLIB, self.js(), self.headHTML())
32
33     def js(self):
34         return 'var chartdata = {};\n{}'.format(
35             json.dumps(self.sections()),
36             pkg_resources.resource_string('crunchstat_summary', self.JSASSET))
37
38     def sections(self):
39         return [
40             {
41                 'label': s.long_label(),
42                 'charts': [
43                     self.chartdata(s.label, s.tasks, stat)
44                     for stat in (('cpu', 'user+sys__rate'),
45                                  ('mem', 'rss'),
46                                  ('net:eth0', 'tx+rx__rate'),
47                                  ('net:keep0', 'tx+rx__rate'))],
48             }
49             for s in self.summarizers]
50
51     def chartdata(self, label, tasks, stat):
52         """Return chart data for the given tasks.
53
54         The returned value will be available on the client side as an
55         element of the "chartdata" array.
56         """
57         raise NotImplementedError()
58
59     def headHTML(self):
60         """Return extra HTML text to include in HEAD."""
61         return ''