OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import fileinput | |
7 import os | |
8 import sys | |
9 | |
10 | |
11 """A Module for manipulating trend graph with analyzer result history.""" | |
12 | |
13 DEFAULT_TREND_GRAPH_PATH = os.path.join('graph', 'graph.html') | |
14 | |
15 # The following is necesasry to decide the point to insert | |
dennis_jeffrey
2011/08/25 00:36:46
add period at end of line
imasaki1
2011/08/25 23:57:03
Done.
| |
16 LINE_INSERT_POINT_FOR_NUMBERS = r'// insert 1' | |
17 LINE_INSERT_POINT_FOR_PASSING_RATE = r'// insert 2' | |
18 | |
19 | |
20 class TrendGraph(object): | |
21 """A class to manage trend graph which is using Google Visualization APIs. | |
22 | |
23 Google Visualization API (http://code.google.com/apis/chart/interactive/docs/ | |
24 gallery/annotatedtimeline.html) is used to present the historical analyzer | |
25 result. Currently, data is directly written to JavaScript file using file | |
26 in-place replacement for simplicity. | |
27 | |
28 TODO(imasaki): use GoogleSpreadsheet to store the analyzer result. | |
29 """ | |
30 | |
31 def __init__(self, location=DEFAULT_TREND_GRAPH_PATH): | |
32 """Initialize this object with the location of trend graph.""" | |
33 self._location = location | |
34 | |
35 def Update(self, datetime_string, data_map): | |
36 """Update trend graphs using |datetime_string| and |data_map|. | |
37 | |
38 There are two kinds of graphs to be updated (one is for numbers and the | |
39 other is for passing rates) | |
dennis_jeffrey
2011/08/25 00:36:46
add period at end of sentence
imasaki1
2011/08/25 23:57:03
Done.
| |
40 | |
41 Args: | |
42 date_string: a timedate string (e.g., '2008,1,1,13,45,00)' | |
dennis_jeffrey
2011/08/25 00:36:46
'date_string' --> 'datetime_string'
dennis_jeffrey
2011/08/25 00:36:46
'timedate' --> 'datetime'
imasaki1
2011/08/25 23:57:03
Done.
imasaki1
2011/08/25 23:57:03
Done.
| |
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 has the following tuples (numbers, title, text) | |
dennis_jeffrey
2011/08/25 00:36:46
Remove this line; already stated on line 44 above.
imasaki1
2011/08/25 23:57:03
Done.
| |
48 """ | |
49 joined_str = '' | |
50 for key in ['whole', 'skip', 'nonskip']: | |
51 joined_str += ','.join(data_map[key]) + ',' | |
52 new_line_for_numbers = ' [new Date(%s),%s],\n' % (datetime_string, | |
53 joined_str) | |
dennis_jeffrey
2011/08/25 00:36:46
indent this line by 1 more space
imasaki1
2011/08/25 23:57:03
Done.
| |
54 new_line_for_numbers += ' %s\n' % ( | |
55 LINE_INSERT_POINT_FOR_NUMBERS) | |
56 self._ReplaceLine(LINE_INSERT_POINT_FOR_NUMBERS, new_line_for_numbers) | |
57 | |
58 joined_str = '%s,%s,%s' % ( | |
59 data_map['passingrate'][0], data_map['nonskip'][1], | |
60 data_map['nonskip'][2]) | |
61 new_line_for_passingrate = ' [new Date(%s),%s],\n' % ( | |
62 datetime_string, joined_str) | |
63 new_line_for_passingrate += ' %s\n' % ( | |
64 LINE_INSERT_POINT_FOR_PASSING_RATE) | |
65 self._ReplaceLine(LINE_INSERT_POINT_FOR_PASSING_RATE, | |
66 new_line_for_passingrate) | |
dennis_jeffrey
2011/08/25 00:36:46
indent this line by 1 more space
imasaki1
2011/08/25 23:57:03
Done.
| |
67 | |
68 def _ReplaceLine(self, search_exp, replace_line): | |
69 """Replace line which has |search_exp|, with |replace_line| | |
dennis_jeffrey
2011/08/25 00:36:46
remove the comma, and add a period at the end of t
imasaki1
2011/08/25 23:57:03
Done.
| |
70 | |
71 Args: | |
72 search_exp: search expression to find a line to be replaced. | |
dennis_jeffrey
2011/08/25 00:36:46
'to find a line' --> 'to find in a line'
imasaki1
2011/08/25 23:57:03
? I think this is correct...
dennis_jeffrey
2011/08/26 19:01:26
Yes, you're right. I was originally misinterpreti
| |
73 replace_line: the new line. | |
74 | |
75 Returns: | |
76 a boolean to indicate if the replacement actually happens. | |
77 """ | |
78 replaced = False | |
79 for line in fileinput.input(self._location, inplace=1): | |
80 if search_exp in line: | |
dennis_jeffrey
2011/08/25 00:36:46
Indent this line and the lines below by only 2 spa
imasaki1
2011/08/25 23:57:03
Done.
| |
81 replaced = True | |
82 line = replace_line | |
83 sys.stdout.write(line) | |
dennis_jeffrey
2011/08/25 00:36:46
Should it really be written to stdout? Also, we'r
imasaki1
2011/08/25 23:57:03
Yes. Also removed "Returns" from comments.
| |
OLD | NEW |