Home | History | Annotate | Download | only in layout_tests
      1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """A module for manipulating trend graph with analyzer result history."""
      6 
      7 import os
      8 
      9 import layouttest_analyzer_helpers
     10 
     11 DEFAULT_TREND_GRAPH_PATH = os.path.join('graph', 'graph.html')
     12 
     13 # The following is necesasry to decide the point to insert.
     14 LINE_INSERT_POINT_FOR_NUMBERS = r'// insert 1'
     15 LINE_INSERT_POINT_FOR_PASSING_RATE = r'// insert 2'
     16 
     17 
     18 class TrendGraph(object):
     19   """A class to manage trend graph which is using Google Visualization APIs.
     20 
     21   Google Visualization API (http://code.google.com/apis/chart/interactive/docs/
     22   gallery/annotatedtimeline.html) is used to present the historical analyzer
     23   result. Currently, data is directly written to JavaScript file using file
     24   in-place replacement for simplicity.
     25 
     26   TODO(imasaki): use GoogleSpreadsheet to store the analyzer result.
     27   """
     28 
     29   def __init__(self, location=DEFAULT_TREND_GRAPH_PATH):
     30     """Initialize this object with the location of trend graph."""
     31     self._location = location
     32 
     33   def Update(self, datetime_string, data_map):
     34     """Update trend graphs using |datetime_string| and |data_map|.
     35 
     36     There are two kinds of graphs to be updated (one is for numbers and the
     37     other is for passing rates).
     38 
     39     Args:
     40         datetime_string: a datetime string delimited by ','
     41           (e.g., '2008,1,1,13,45,00)'. For example, in the case of the year
     42           2008, this ranges from '2008,1,1,0,0,00' to '2008,12,31,23,59,99'.
     43         data_map: a dictionary containing 'whole', 'skip' , 'nonskip',
     44           'passingrate' as its keys and (number, tile, text) string tuples
     45           as values for graph annotation.
     46     """
     47     joined_str = ''
     48     # For a date format in GViz, month is shifted (e.g., '2008,2,1' means
     49     # March 1, 2008). So, the input parameter |datetime_string| (before this
     50     # conversion) must be shifted in order to show the date properly on GViz.
     51     # After the below conversion, for example, in the case of the year 2008,
     52     # |datetime_string| ranges from '2008,0,1,0,0,00' to '2008,11,31,23,59,99'.
     53     str_list = datetime_string.split(',')
     54     str_list[1] = str(int(str_list[1])-1)  # Month
     55     datetime_string = ','.join(str_list)
     56     for key in ['whole', 'skip', 'nonskip']:
     57       joined_str += str(len(data_map[key][0])) + ','
     58       joined_str += ','.join(data_map[key][1:]) + ','
     59     new_line_for_numbers = '         [new Date(%s),%s],\n' % (datetime_string,
     60                                                               joined_str)
     61     new_line_for_numbers += '         %s\n' % (
     62         LINE_INSERT_POINT_FOR_NUMBERS)
     63     layouttest_analyzer_helpers.ReplaceLineInFile(
     64         self._location, LINE_INSERT_POINT_FOR_NUMBERS,
     65         new_line_for_numbers)
     66 
     67     joined_str = '%s,%s,%s' % (
     68         str(data_map['passingrate'][0]), data_map['nonskip'][1],
     69         data_map['nonskip'][2])
     70     new_line_for_passingrate = '         [new Date(%s),%s],\n' % (
     71         datetime_string, joined_str)
     72     new_line_for_passingrate += '         %s\n' % (
     73         LINE_INSERT_POINT_FOR_PASSING_RATE)
     74     layouttest_analyzer_helpers.ReplaceLineInFile(
     75         self._location, LINE_INSERT_POINT_FOR_PASSING_RATE,
     76         new_line_for_passingrate)
     77