graph.py revision 868fa2fe829687343ffae624259930155e16dbd8
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#!/usr/bin/env python 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)# 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved. 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)# found in the LICENSE file. 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import json 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import sys 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)from string import Template 10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)_HTML_TEMPLATE = """ 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)<html> 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) <head> 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) <script type='text/javascript' src='https://www.google.com/jsapi'></script> 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) <script type='text/javascript'> 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) google.load('visualization', '1', {packages:['corechart', 'table']}); 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) google.setOnLoadCallback(drawVisualization); 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) function drawVisualization() { 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) var data = google.visualization.arrayToDataTable( 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) $JSON_ARRAY 22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ); 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) var charOptions = { 25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) title: 'DMP Graph', 26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) vAxis: {title: 'Timestamp', titleTextStyle: {color: 'red'}}, 27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) isStacked : true 28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) }; 29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) var chart = new google.visualization.BarChart( 31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) document.getElementById('chart_div')); 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) chart.draw(data, charOptions); 33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) var table = new google.visualization.Table( 35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) document.getElementById('table_div')); 36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) table.draw(data); 37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) </script> 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) </head> 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) <body> 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) <div id='chart_div' style="width: 1024px; height: 800px;"></div> 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) <div id='table_div' style="width: 1024px; height: 640px;"></div> 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) </body> 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)</html> 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)""" 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)def _GenerateGraph(json_data, policy): 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) legends = list(json_data['policies'][policy]['legends']) 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1: 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) legends.index('UNTIL_HERE_FOR_TOTAL')] 51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) data = [] 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for snapshot in json_data['policies'][policy]['snapshots']: 53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) data.append([0] * len(legends)) 54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for k, v in snapshot.iteritems(): 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if k in legends: 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) data[-1][legends.index(k)] = v 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) print Template(_HTML_TEMPLATE).safe_substitute( 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) {'JSON_ARRAY': json.dumps([legends] + data)}) 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)def main(argv): 62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) _GenerateGraph(json.load(file(argv[1], 'r')), argv[2]) 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)if __name__ == '__main__': 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sys.exit(main(sys.argv)) 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 69