summaryrefslogtreecommitdiffstats
path: root/media/tools/layout_tests/trend_graph.py
blob: 8406d19e641c6f27a21e39abeaf9a9eaa1af81c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A module for manipulating trend graph with analyzer result history."""

import fileinput
import os
import sys


DEFAULT_TREND_GRAPH_PATH = os.path.join('graph', 'graph.html')

# The following is necesasry to decide the point to insert.
LINE_INSERT_POINT_FOR_NUMBERS = r'// insert 1'
LINE_INSERT_POINT_FOR_PASSING_RATE = r'// insert 2'


class TrendGraph(object):
  """A class to manage trend graph which is using Google Visualization APIs.

  Google Visualization API (http://code.google.com/apis/chart/interactive/docs/
  gallery/annotatedtimeline.html) is used to present the historical analyzer
  result. Currently, data is directly written to JavaScript file using file
  in-place replacement for simplicity.

  TODO(imasaki): use GoogleSpreadsheet to store the analyzer result.
  """

  def __init__(self, location=DEFAULT_TREND_GRAPH_PATH):
    """Initialize this object with the location of trend graph."""
    self._location = location

  def Update(self, datetime_string, data_map):
    """Update trend graphs using |datetime_string| and |data_map|.

    There are two kinds of graphs to be updated (one is for numbers and the
    other is for passing rates).

    Args:
        datetime_string: a datetime string delimited by ','
          (e.g., '2008,1,1,13,45,00)'. For example, in the case of the year
          2008, this ranges from '2008,1,1,0,0,00' to '2008,12,31,23,59,99'.
        data_map: a dictionary containing 'whole', 'skip' , 'nonskip',
          'passingrate' as its keys and (number, tile, text) string tuples
          as values for graph annotation.
    """
    joined_str = ''
    # For a date format in GViz, month is shifted (e.g., '2008,2,1' means
    # March 1, 2008). So, the input parameter |datetime_string| (before this
    # conversion) must be shifted in order to show the date properly on GViz.
    # After the below conversion, for example, in the case of the year 2008,
    # |datetime_string| ranges from '2008,0,1,0,0,00' to '2008,11,31,23,59,99'.
    str_list = datetime_string.split(',')
    str_list[1] = str(int(str_list[1])-1) # month
    datetime_string = ','.join(str_list)
    for key in ['whole', 'skip', 'nonskip']:
      joined_str += ','.join(data_map[key]) + ','
    new_line_for_numbers = '         [new Date(%s),%s],\n' % (datetime_string,
                                                              joined_str)
    new_line_for_numbers += '         %s\n' % (
        LINE_INSERT_POINT_FOR_NUMBERS)
    self._ReplaceLine(LINE_INSERT_POINT_FOR_NUMBERS, new_line_for_numbers)

    joined_str = '%s,%s,%s' % (
        data_map['passingrate'][0], data_map['nonskip'][1],
        data_map['nonskip'][2])
    new_line_for_passingrate = '         [new Date(%s),%s],\n' % (
        datetime_string, joined_str)
    new_line_for_passingrate += '         %s\n' % (
        LINE_INSERT_POINT_FOR_PASSING_RATE)
    self._ReplaceLine(LINE_INSERT_POINT_FOR_PASSING_RATE,
                      new_line_for_passingrate)

  def _ReplaceLine(self, search_exp, replace_line):
    """Replace line which has |search_exp| with |replace_line|.

    Args:
        search_exp: search expression to find a line to be replaced.
        replace_line: the new line.
    """
    for line in fileinput.input(self._location, inplace=1):
      if search_exp in line:
        line = replace_line
      sys.stdout.write(line)