blob: 7c49abd37c7df848f36d667835c5441d9459bc34 [file] [log] [blame]
Yulia Portnovae3a49982015-02-16 16:06:09 +02001import os
koder aka kdanilovcee43342015-04-14 22:52:53 +03002import hashlib
Yulia Portnovae3a49982015-02-16 16:06:09 +02003import threading
4
Yulia Portnova6d72d7f2015-02-04 16:48:50 +02005from GChartWrapper import VerticalBarGroup
6from GChartWrapper import Line
7from GChartWrapper import constants
8
Yulia Portnova407ca952015-04-10 10:38:15 +03009from config import cfg_dict
Yulia Portnovae3a49982015-02-16 16:06:09 +020010
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020011
Yulia Portnova407ca952015-04-10 10:38:15 +030012COLORS = ["1569C7", "81D8D0", "307D7E", "5CB3FF", "0040FF", "81DAF5"]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020013constants.MARKERS += 'E' # append E marker to available markers
14
15
koder aka kdanilovcee43342015-04-14 22:52:53 +030016def get_top_top_dir(path):
17 top_top_dir = os.path.dirname(os.path.dirname(path))
18 return path[len(top_top_dir) + 1:]
Yulia Portnovae3a49982015-02-16 16:06:09 +020019
20
Yulia Portnova8ca20572015-04-14 14:09:39 +030021def render_vertical_bar(title, legend, bars_data, bars_dev_top,
22 bars_dev_bottom,
23 width=700, height=400,
Yulia Portnova407ca952015-04-10 10:38:15 +030024 scale_x=None, scale_y=None, label_x=None,
25 label_y=None, lines=()):
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020026 """
27 Renders vertical bar group chart
28
29 :param legend - list of legend values.
30 Example: ['bar1', 'bar2', 'bar3']
31 :param dataset - list of values for each type (value, deviation)
32 Example:
33 [
Yulia Portnova407ca952015-04-10 10:38:15 +030034 [(10,1), (11, 2), (10,1)], # bar1 values
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020035 [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values
36 [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values
37 ]
38 :param width - width of chart
39 :param height - height of chart
40 :param scale_x - x ace scale
41 :param scale_y - y ace scale
42
43 :returns url to chart
44
45 dataset example:
46 {
47 'relese_1': {
48 'randr': (1, 0.1),
49 'randwr': (2, 0.2)
50 }
51 'release_2': {
52 'randr': (3, 0.3),
53 'randwr': (4, 0.4)
54 }
55 }
56 """
koder aka kdanilov66839a92015-04-11 13:22:31 +030057
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020058 bar = VerticalBarGroup([], encoding='text')
59 bar.title(title)
60
Yulia Portnova8ca20572015-04-14 14:09:39 +030061 dataset = bars_data + bars_dev_top + bars_dev_bottom + [lines[0][0]]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020062
Yulia Portnova8ca20572015-04-14 14:09:39 +030063 bar.dataset(dataset, series=len(bars_data))
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020064 bar.axes.type('xyy')
Yulia Portnova407ca952015-04-10 10:38:15 +030065 bar.axes.label(2, None, label_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020066 if scale_x:
67 bar.axes.label(0, *scale_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020068
Yulia Portnova8ca20572015-04-14 14:09:39 +030069 max_value = (max([max(l) for l in dataset[:2]]))
Yulia Portnova919f3be2015-02-06 12:49:22 +020070 bar.axes.range(1, 0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020071 bar.axes.style(1, 'N*s*')
Yulia Portnovaffec7602015-02-12 11:16:11 +020072 bar.axes.style(2, '000000', '13')
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020073
Yulia Portnova8ca20572015-04-14 14:09:39 +030074 bar.scale(*[0, max_value] * 3)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020075
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020076 bar.bar('r', '.1', '1')
Yulia Portnova407ca952015-04-10 10:38:15 +030077 for i in range(1):
Yulia Portnova8ca20572015-04-14 14:09:39 +030078 bar.marker('E', '000000', '%s:%s' % ((len(bars_data) + i*2), i),
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020079 '', '1:10')
Yulia Portnova407ca952015-04-10 10:38:15 +030080 bar.color(*COLORS)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020081 bar.size(width, height)
82
Yulia Portnova407ca952015-04-10 10:38:15 +030083 axes_type = "xyy"
84
Yulia Portnova8ca20572015-04-14 14:09:39 +030085 scale = [0, max_value] * len(bars_dev_top + bars_dev_bottom + bars_data)
Yulia Portnova407ca952015-04-10 10:38:15 +030086 if lines:
87 line_n = 0
88 for data, label, axe, leg in lines:
Yulia Portnova8ca20572015-04-14 14:09:39 +030089 bar.marker('D', COLORS[len(bars_data) + line_n],
90 (len(bars_data + bars_dev_top + bars_dev_bottom))
91 + line_n, 0, 3)
Yulia Portnova407ca952015-04-10 10:38:15 +030092 max_val_l = max(data)
93 if axe:
94 bar.axes.type(axes_type + axe)
95 bar.axes.range(len(axes_type), 0, max_val_l)
96 bar.axes.style(len(axes_type), 'N*s*')
97 bar.axes.label(len(axes_type) + 1, None, label)
98 bar.axes.style(len(axes_type) + 1, '000000', '13')
99 axes_type += axe
100 line_n += 1
101 legend.append(leg)
102 scale += [0, max_val_l]
103
104 bar.legend(*legend)
105 bar.scale(*scale)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200106 img_name = hashlib.md5(str(bar)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300107 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300108
Yulia Portnovae3a49982015-02-16 16:06:09 +0200109 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300110 bar.save(img_path)
111
112 return get_top_top_dir(img_path)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +0200113
114
Yulia Portnova919f3be2015-02-06 12:49:22 +0200115def render_lines(title, legend, dataset, scale_x, width=700, height=400):
116 line = Line([], encoding="text")
117 line.title(title)
118 line.dataset(dataset)
119
120 line.axes('xy')
121 max_value = (max([max(l) for l in dataset]))
122 line.axes.range(1, 0, max_value)
123 line.scale(0, max_value)
124 line.axes.label(0, *scale_x)
125 line.legend(*legend)
126 line.color(*COLORS[:len(legend)])
127 line.size(width, height)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200128
Yulia Portnovae3a49982015-02-16 16:06:09 +0200129 img_name = hashlib.md5(str(line)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300130 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200131 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300132 line.save(img_path)
133
134 return get_top_top_dir(img_path)