OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 import json | 7 import json |
8 import sys | 8 import sys |
9 from string import Template | 9 from string import Template |
10 | 10 |
11 | 11 |
12 _HTML_TEMPLATE = """<!DOCTYPE html> | 12 _HTML_TEMPLATE = """<!DOCTYPE html> |
13 <script src="https://www.google.com/jsapi"></script> | 13 <script src="https://www.google.com/jsapi"></script> |
14 <script> | 14 <script> |
15 var all_data = $ALL_DATA; | 15 var all_data = $ALL_DATA; |
16 google.load('visualization', '1', {packages:['corechart', 'table']}); | 16 google.load('visualization', '1', {packages:['corechart', 'table']}); |
17 google.setOnLoadCallback(drawVisualization); | 17 google.setOnLoadCallback(drawVisualization); |
18 function drawVisualization() { | 18 function drawVisualization() { |
19 // Apply policy 'l2' by default. | 19 // Apply policy 'l2' by default. |
20 var default_policy = 'l2'; | 20 var default_policy = '$DEF_POLICY'; |
21 document.getElementById(default_policy).style.fontWeight = 'bold'; | 21 document.getElementById(default_policy).style.fontWeight = 'bold'; |
22 turnOn(default_policy); | 22 turnOn(default_policy); |
23 } | 23 } |
24 | 24 |
25 function turnOn(policy) { | 25 function turnOn(policy) { |
26 var data = google.visualization.arrayToDataTable(all_data[policy]); | 26 var data = google.visualization.arrayToDataTable(all_data[policy]); |
27 var charOptions = { | 27 var charOptions = { |
28 title: 'DMP Graph (Policy: ' + policy + ')', | 28 title: 'DMP Graph (Policy: ' + policy + ')', |
29 hAxis: {title: 'Timestamp', titleTextStyle: {color: 'red'}}, | 29 hAxis: {title: 'Timestamp', titleTextStyle: {color: 'red'}}, |
30 isStacked : true | 30 isStacked : true |
(...skipping 28 matching lines...) Expand all Loading... |
59 } | 59 } |
60 </style> | 60 </style> |
61 Click to change an applied policy. | 61 Click to change an applied policy. |
62 <ul id="policies">$POLICIES</ul> | 62 <ul id="policies">$POLICIES</ul> |
63 <div id="chart_div" style="width: 1024px; height: 640px;"></div> | 63 <div id="chart_div" style="width: 1024px; height: 640px;"></div> |
64 <div id="table_div" style="width: 1024px; height: 640px;"></div> | 64 <div id="table_div" style="width: 1024px; height: 640px;"></div> |
65 """ | 65 """ |
66 | 66 |
67 def _GenerateGraph(json_data): | 67 def _GenerateGraph(json_data): |
68 policies = list(json_data['policies']) | 68 policies = list(json_data['policies']) |
| 69 |
| 70 default_policy = "l2" |
| 71 if default_policy not in policies: |
| 72 default_policy = policies[0] |
| 73 |
69 policies = "".join(map(lambda x: '<li id="'+x+'">'+x+'</li>', policies)) | 74 policies = "".join(map(lambda x: '<li id="'+x+'">'+x+'</li>', policies)) |
70 | 75 |
71 all_data = {} | 76 all_data = {} |
72 for policy in json_data['policies']: | 77 for policy in json_data['policies']: |
73 legends = list(json_data['policies'][policy]['legends']) | 78 legends = list(json_data['policies'][policy]['legends']) |
74 legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1: | 79 legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1: |
75 legends.index('UNTIL_HERE_FOR_TOTAL')] | 80 legends.index('UNTIL_HERE_FOR_TOTAL')] |
76 data = [] | 81 data = [] |
77 for snapshot in json_data['policies'][policy]['snapshots']: | 82 for snapshot in json_data['policies'][policy]['snapshots']: |
78 data.append([0] * len(legends)) | 83 data.append([0] * len(legends)) |
79 for k, v in snapshot.iteritems(): | 84 for k, v in snapshot.iteritems(): |
80 if k in legends: | 85 if k in legends: |
81 data[-1][legends.index(k)] = v | 86 data[-1][legends.index(k)] = v |
82 all_data[policy] = [legends] + data | 87 all_data[policy] = [legends] + data |
83 | 88 |
84 print Template(_HTML_TEMPLATE).safe_substitute( | 89 print Template(_HTML_TEMPLATE).safe_substitute( |
85 {'POLICIES': policies, | 90 {'POLICIES': policies, |
| 91 'DEF_POLICY': default_policy, |
86 'ALL_DATA': json.dumps(all_data)}) | 92 'ALL_DATA': json.dumps(all_data)}) |
87 | 93 |
88 | 94 |
89 def main(argv): | 95 def main(argv): |
90 _GenerateGraph(json.load(file(argv[1], 'r'))) | 96 _GenerateGraph(json.load(file(argv[1], 'r'))) |
91 | 97 |
92 | 98 |
93 if __name__ == '__main__': | 99 if __name__ == '__main__': |
94 sys.exit(main(sys.argv)) | 100 sys.exit(main(sys.argv)) |
OLD | NEW |