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
5 # found in the LICENSE file.
9 from string
import Template
12 _HTML_TEMPLATE
= """<!DOCTYPE html>
13 <script src="https://www.google.com/jsapi"></script>
15 var all_data = $ALL_DATA;
16 google.load('visualization', '1', {packages:['corechart', 'table']});
17 google.setOnLoadCallback(drawVisualization);
18 function drawVisualization() {
19 // Apply policy 'l2' by default.
20 var default_policy = '$DEF_POLICY';
21 document.getElementById(default_policy).style.fontWeight = 'bold';
22 turnOn(default_policy);
25 function turnOn(policy) {
26 var data = google.visualization.arrayToDataTable(all_data[policy]);
28 title: 'DMP Graph (Policy: ' + policy + ')',
29 hAxis: {title: 'Timestamp', titleTextStyle: {color: 'red'}},
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'));
40 window.onload = function() {
41 var ul = document.getElementById('policies');
42 for (var i = 0; i < ul.children.length; ++i) {
43 var li = ul.children[i];
44 li.onclick = function() {
45 for (var j = 0; j < ul.children.length; ++j) {
46 var my_li = ul.children[j];
47 my_li.style.fontWeight = 'normal';
49 this.style.fontWeight = 'bold';
57 display: inline-block;
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>
67 def _GenerateGraph(json_data
):
68 policies
= list(json_data
['policies'])
71 if default_policy
not in policies
:
72 default_policy
= policies
[0]
74 policies
= "".join(map(lambda x
: '<li id="'+x
+'">'+x
+'</li>', policies
))
77 for policy
in json_data
['policies']:
78 legends
= list(json_data
['policies'][policy
]['legends'])
79 legends
= ['second'] + legends
[legends
.index('FROM_HERE_FOR_TOTAL') + 1:
80 legends
.index('UNTIL_HERE_FOR_TOTAL')]
82 for snapshot
in json_data
['policies'][policy
]['snapshots']:
83 data
.append([0] * len(legends
))
84 for k
, v
in snapshot
.iteritems():
86 data
[-1][legends
.index(k
)] = v
87 all_data
[policy
] = [legends
] + data
89 print Template(_HTML_TEMPLATE
).safe_substitute(
90 {'POLICIES': policies
,
91 'DEF_POLICY': default_policy
,
92 'ALL_DATA': json
.dumps(all_data
)})
96 _GenerateGraph(json
.load(file(argv
[1], 'r')))
99 if __name__
== '__main__':
100 sys
.exit(main(sys
.argv
))