Merge branch '21121-cluster-activity' refs #21121
[arvados.git] / tools / crunchstat-summary / crunchstat_summary / dygraphs.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 import json
6 import pkg_resources
7 from arvados._internal.report_template import ReportTemplate
8
9 class DygraphsChart(ReportTemplate):
10     """Crunchstat report using dygraphs for charting.
11     """
12
13     CSS = 'https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.min.css'
14     JSLIB = 'https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.min.js'
15     JSASSETS = ['synchronizer.js','dygraphs.js']
16
17     def __init__(self, label, summarizers, beforechart, afterchart):
18         super().__init__(label)
19         self.summarizers = summarizers
20         self.beforechart = beforechart
21         self.afterchart = afterchart
22
23     def html(self):
24         self.cards.extend(self.beforechart)
25         self.cards.append("""
26                 <h2>Graph</h2>
27                 <div id="chart"></div>
28             """)
29         self.cards.extend(self.afterchart)
30
31         return super().html()
32
33     def js(self):
34         return '''
35         <script type="text/javascript" src="{jslib}"></script>
36         <script type="text/javascript">
37         var chartdata = {chartdata};\n{jsassets}
38         </script>'''.format(
39             jslib=self.JSLIB,
40             chartdata=json.dumps(self.sections()),
41             jsassets='\n'.join([pkg_resources.resource_string('crunchstat_summary', jsa).decode('utf-8') for jsa in self.JSASSETS]))
42
43     def sections(self):
44         return [
45             {
46                 'label': s.long_label(),
47                 'charts': [
48                     self.chartdata(s.label, s.tasks, stat)
49                     for stat in (('cpu', ['user+sys__rate', 'user__rate', 'sys__rate']),
50                                  ('mem', ['rss']),
51                                  ('net:eth0', ['tx+rx__rate','rx__rate','tx__rate']),
52                                  ('net:keep0', ['tx+rx__rate','rx__rate','tx__rate']),
53                                  ('statfs', ['used', 'total']),
54                                  )
55                     ],
56             }
57             for s in self.summarizers]
58
59     def chartdata(self, label, tasks, stats):
60         '''For Crunch2, label is the name of container request,
61         tasks is the top level container and
62         stats is index by a tuple of (category, metric).
63         '''
64         return {
65             'data': self._collate_data(tasks, stats),
66             'options': {
67                 'legend': 'always',
68                 'connectSeparatedPoints': True,
69                 'labels': ['elapsed'] +  stats[1],
70                 'includeZero': True,
71                 'title': '{}: {}'.format(label, stats[0]) if label else stats[0],
72             },
73         }
74
75     def _collate_data(self, tasks, stats):
76         data = []
77         nulls = []
78         # uuid is category for crunch2
79         for uuid, task in tasks.items():
80             # All stats in a category are assumed to have the same time base and same number of samples
81             category = stats[0]
82             series_names = stats[1]
83             sn0 = series_names[0]
84             series = task.series[(category,sn0)]
85             for i in range(len(series)):
86                 pt = series[i]
87                 vals = [task.series[(category,stat)][i][1] for stat in series_names[1:]]
88                 data.append([pt[0].total_seconds()] + nulls + [pt[1]] + vals)
89             nulls.append(None)
90         return sorted(data)
91
92     def style(self):
93         return '\n'.join((super().style(),
94                          '<link rel="stylesheet" href="{}">\n'.format(self.CSS)))