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)
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)_HTML_TEMPLATE = """<!DOCTYPE html>
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)<script src="https://www.google.com/jsapi"></script>
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)<script>
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)var all_data = $ALL_DATA;
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)google.load('visualization', '1', {packages:['corechart', 'table']});
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)google.setOnLoadCallback(drawVisualization);
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)function drawVisualization() {
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Apply policy 'l2' by default.
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  var default_policy = '$DEF_POLICY';
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  document.getElementById(default_policy).style.fontWeight = 'bold';
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  turnOn(default_policy);
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)function turnOn(policy) {
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  var data = google.visualization.arrayToDataTable(all_data[policy]);
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  var charOptions = {
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    title: 'DMP Graph (Policy: ' + policy + ')',
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    hAxis: {title: 'Timestamp',  titleTextStyle: {color: 'red'}},
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    isStacked : true
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  var chart = new google.visualization.AreaChart(
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      document.getElementById('chart_div'));
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  chart.draw(data, charOptions);
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  var table = new google.visualization.Table(
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      document.getElementById('table_div'));
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  table.draw(data);
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)window.onload = function() {
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  var ul = document.getElementById('policies');
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (var i = 0; i < ul.children.length; ++i) {
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    var li = ul.children[i];
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    li.onclick = function() {
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      for (var j = 0; j < ul.children.length; ++j) {
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        var my_li = ul.children[j];
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        my_li.style.fontWeight = 'normal';
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      this.style.fontWeight = 'bold';
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      turnOn(this.id);
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)</script>
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)<style>
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#policies li {
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  display: inline-block;
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  padding: 5px 10px;
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)</style>
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)Click to change an applied policy.
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)<ul id="policies">$POLICIES</ul>
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)<div id="chart_div" style="width: 1024px; height: 640px;"></div>
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)<div id="table_div" style="width: 1024px; height: 640px;"></div>
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)"""
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def _GenerateGraph(json_data):
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  policies = list(json_data['policies'])
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  default_policy = "l2"
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if default_policy not in policies:
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    default_policy = policies[0]
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  policies = "".join(map(lambda x: '<li id="'+x+'">'+x+'</li>', policies))
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  all_data = {}
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for policy in json_data['policies']:
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    legends = list(json_data['policies'][policy]['legends'])
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1:
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                     legends.index('UNTIL_HERE_FOR_TOTAL')]
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    data = []
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    for snapshot in json_data['policies'][policy]['snapshots']:
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      data.append([0] * len(legends))
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      for k, v in snapshot.iteritems():
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if k in legends:
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          data[-1][legends.index(k)] = v
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    all_data[policy] = [legends] + data
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  print Template(_HTML_TEMPLATE).safe_substitute(
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      {'POLICIES': policies,
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       'DEF_POLICY': default_policy,
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)       'ALL_DATA': json.dumps(all_data)})
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)def main(argv):
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  _GenerateGraph(json.load(file(argv[1], 'r')))
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)if __name__ == '__main__':
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  sys.exit(main(sys.argv))
101