| 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 = """ | 12 _HTML_TEMPLATE = """<!DOCTYPE html> |
| 13 <html> | 13 <script src="https://www.google.com/jsapi"></script> |
| 14 <head> | 14 <script> |
| 15 <script type='text/javascript' src='https://www.google.com/jsapi'></script> | 15 var all_data = $ALL_DATA; |
| 16 <script type='text/javascript'> | 16 google.load('visualization', '1', {packages:['corechart', 'table']}); |
| 17 google.load('visualization', '1', {packages:['corechart', 'table']}); | 17 google.setOnLoadCallback(drawVisualization); |
| 18 google.setOnLoadCallback(drawVisualization); | 18 function drawVisualization() { |
| 19 function drawVisualization() { | 19 // Apply policy 'l2' by default. |
| 20 var data = google.visualization.arrayToDataTable( | 20 var default_policy = 'l2'; |
| 21 $JSON_ARRAY | 21 document.getElementById(default_policy).style.fontWeight = 'bold'; |
| 22 ); | 22 turnOn(default_policy); |
| 23 } |
| 23 | 24 |
| 24 var charOptions = { | 25 function turnOn(policy) { |
| 25 title: 'DMP Graph', | 26 var data = google.visualization.arrayToDataTable(all_data[policy]); |
| 26 vAxis: {title: 'Timestamp', titleTextStyle: {color: 'red'}}, | 27 var charOptions = { |
| 27 isStacked : true | 28 title: 'DMP Graph (Policy: ' + policy + ')', |
| 28 }; | 29 vAxis: {title: 'Timestamp', titleTextStyle: {color: 'red'}}, |
| 30 isStacked : true |
| 31 }; |
| 32 var chart = new google.visualization.AreaChart( |
| 33 document.getElementById('chart_div')); |
| 34 chart.draw(data, charOptions); |
| 35 var table = new google.visualization.Table( |
| 36 document.getElementById('table_div')); |
| 37 table.draw(data); |
| 38 } |
| 29 | 39 |
| 30 var chart = new google.visualization.BarChart( | 40 window.onload = function() { |
| 31 document.getElementById('chart_div')); | 41 var ul = document.getElementById('policies'); |
| 32 chart.draw(data, charOptions); | 42 for (var i = 0; i < ul.children.length; ++i) { |
| 33 | 43 var li = ul.children[i]; |
| 34 var table = new google.visualization.Table( | 44 li.onclick = function() { |
| 35 document.getElementById('table_div')); | 45 for (var j = 0; j < ul.children.length; ++j) { |
| 36 table.draw(data); | 46 var my_li = ul.children[j]; |
| 47 my_li.style.fontWeight = 'normal'; |
| 37 } | 48 } |
| 38 </script> | 49 this.style.fontWeight = 'bold'; |
| 39 </head> | 50 turnOn(this.id); |
| 40 <body> | 51 } |
| 41 <div id='chart_div' style="width: 1024px; height: 800px;"></div> | 52 } |
| 42 <div id='table_div' style="width: 1024px; height: 640px;"></div> | 53 }; |
| 43 </body> | 54 </script> |
| 44 </html> | 55 <style> |
| 56 #policies li { |
| 57 display: inline-block; |
| 58 padding: 5px 10px; |
| 59 } |
| 60 </style> |
| 61 Click to change an applied policy. |
| 62 <ul id="policies">$POLICIES</ul> |
| 63 <div id="chart_div" style="width: 1024px; height: 640px;"></div> |
| 64 <div id="table_div" style="width: 1024px; height: 640px;"></div> |
| 45 """ | 65 """ |
| 46 | 66 |
| 47 def _GenerateGraph(json_data, policy): | 67 def _GenerateGraph(json_data): |
| 48 legends = list(json_data['policies'][policy]['legends']) | 68 policies = list(json_data['policies']) |
| 49 legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1: | 69 policies = "".join(map(lambda x: '<li id="'+x+'">'+x+'</li>', policies)) |
| 50 legends.index('UNTIL_HERE_FOR_TOTAL')] | 70 |
| 51 data = [] | 71 all_data = {} |
| 52 for snapshot in json_data['policies'][policy]['snapshots']: | 72 for policy in json_data['policies']: |
| 53 data.append([0] * len(legends)) | 73 legends = list(json_data['policies'][policy]['legends']) |
| 54 for k, v in snapshot.iteritems(): | 74 legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1: |
| 55 if k in legends: | 75 legends.index('UNTIL_HERE_FOR_TOTAL')] |
| 56 data[-1][legends.index(k)] = v | 76 data = [] |
| 77 for snapshot in json_data['policies'][policy]['snapshots']: |
| 78 data.append([0] * len(legends)) |
| 79 for k, v in snapshot.iteritems(): |
| 80 if k in legends: |
| 81 data[-1][legends.index(k)] = v |
| 82 all_data[policy] = [legends] + data |
| 83 |
| 57 print Template(_HTML_TEMPLATE).safe_substitute( | 84 print Template(_HTML_TEMPLATE).safe_substitute( |
| 58 {'JSON_ARRAY': json.dumps([legends] + data)}) | 85 {'POLICIES': policies, |
| 86 'ALL_DATA': json.dumps(all_data)}) |
| 59 | 87 |
| 60 | 88 |
| 61 def main(argv): | 89 def main(argv): |
| 62 _GenerateGraph(json.load(file(argv[1], 'r')), argv[2]) | 90 _GenerateGraph(json.load(file(argv[1], 'r'))) |
| 63 | 91 |
| 64 | 92 |
| 65 if __name__ == '__main__': | 93 if __name__ == '__main__': |
| 66 sys.exit(main(sys.argv)) | 94 sys.exit(main(sys.argv)) |
| 67 | |
| 68 | |
| OLD | NEW |