• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
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.
6
7import json
8import sys
9from string import Template
10
11
12_HTML_TEMPLATE = """<!DOCTYPE html>
13<script src="https://www.google.com/jsapi"></script>
14<script>
15var all_data = $ALL_DATA;
16google.load('visualization', '1', {packages:['corechart', 'table']});
17google.setOnLoadCallback(drawVisualization);
18function drawVisualization() {
19  // Apply policy 'l2' by default.
20  var default_policy = 'l2';
21  document.getElementById(default_policy).style.fontWeight = 'bold';
22  turnOn(default_policy);
23}
24
25function turnOn(policy) {
26  var data = google.visualization.arrayToDataTable(all_data[policy]);
27  var charOptions = {
28    title: 'DMP Graph (Policy: ' + policy + ')',
29    hAxis: {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}
39
40window.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';
48      }
49      this.style.fontWeight = 'bold';
50      turnOn(this.id);
51    }
52  }
53};
54</script>
55<style>
56#policies li {
57  display: inline-block;
58  padding: 5px 10px;
59}
60</style>
61Click 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>
65"""
66
67def _GenerateGraph(json_data):
68  policies = list(json_data['policies'])
69  policies = "".join(map(lambda x: '<li id="'+x+'">'+x+'</li>', policies))
70
71  all_data = {}
72  for policy in json_data['policies']:
73    legends = list(json_data['policies'][policy]['legends'])
74    legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1:
75                                     legends.index('UNTIL_HERE_FOR_TOTAL')]
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
84  print Template(_HTML_TEMPLATE).safe_substitute(
85      {'POLICIES': policies,
86       'ALL_DATA': json.dumps(all_data)})
87
88
89def main(argv):
90  _GenerateGraph(json.load(file(argv[1], 'r')))
91
92
93if __name__ == '__main__':
94  sys.exit(main(sys.argv))
95