blob: 1263a64ecda30875efda6e2a3046be7763755a07 [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 Portnova4ef6e9d2015-04-15 12:14:10 +030061 dataset = bars_data + bars_dev_top + bars_dev_bottom + \
62 [l[0] for l in lines]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020063
Yulia Portnova8ca20572015-04-14 14:09:39 +030064 bar.dataset(dataset, series=len(bars_data))
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020065 bar.axes.type('xyy')
Yulia Portnova407ca952015-04-10 10:38:15 +030066 bar.axes.label(2, None, label_x)
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +030067
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020068 if scale_x:
69 bar.axes.label(0, *scale_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020070
Yulia Portnova8ca20572015-04-14 14:09:39 +030071 max_value = (max([max(l) for l in dataset[:2]]))
Yulia Portnova919f3be2015-02-06 12:49:22 +020072 bar.axes.range(1, 0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020073 bar.axes.style(1, 'N*s*')
Yulia Portnovaffec7602015-02-12 11:16:11 +020074 bar.axes.style(2, '000000', '13')
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020075
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +030076
Yulia Portnova8ca20572015-04-14 14:09:39 +030077 bar.scale(*[0, max_value] * 3)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020078
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020079 bar.bar('r', '.1', '1')
Yulia Portnova407ca952015-04-10 10:38:15 +030080 for i in range(1):
Yulia Portnova8ca20572015-04-14 14:09:39 +030081 bar.marker('E', '000000', '%s:%s' % ((len(bars_data) + i*2), i),
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020082 '', '1:10')
Yulia Portnova407ca952015-04-10 10:38:15 +030083 bar.color(*COLORS)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020084 bar.size(width, height)
85
Yulia Portnova407ca952015-04-10 10:38:15 +030086 axes_type = "xyy"
87
Yulia Portnova8ca20572015-04-14 14:09:39 +030088 scale = [0, max_value] * len(bars_dev_top + bars_dev_bottom + bars_data)
Yulia Portnova407ca952015-04-10 10:38:15 +030089 if lines:
90 line_n = 0
91 for data, label, axe, leg in lines:
Yulia Portnova8ca20572015-04-14 14:09:39 +030092 bar.marker('D', COLORS[len(bars_data) + line_n],
93 (len(bars_data + bars_dev_top + bars_dev_bottom))
94 + line_n, 0, 3)
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +030095 # max_val_l = max(data)
Yulia Portnova407ca952015-04-10 10:38:15 +030096 if axe:
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +030097 max_val_l = max(data)
Yulia Portnova407ca952015-04-10 10:38:15 +030098 bar.axes.type(axes_type + axe)
99 bar.axes.range(len(axes_type), 0, max_val_l)
100 bar.axes.style(len(axes_type), 'N*s*')
101 bar.axes.label(len(axes_type) + 1, None, label)
102 bar.axes.style(len(axes_type) + 1, '000000', '13')
103 axes_type += axe
104 line_n += 1
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300105 scale += [0, max_val_l]
106 else:
107 scale += [0, max_value]
Yulia Portnova407ca952015-04-10 10:38:15 +0300108 legend.append(leg)
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300109 # scale += [0, max_val_l]
Yulia Portnova407ca952015-04-10 10:38:15 +0300110
111 bar.legend(*legend)
112 bar.scale(*scale)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200113 img_name = hashlib.md5(str(bar)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300114 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300115
Yulia Portnovae3a49982015-02-16 16:06:09 +0200116 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300117 bar.save(img_path)
118
119 return get_top_top_dir(img_path)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +0200120
121
Yulia Portnova919f3be2015-02-06 12:49:22 +0200122def render_lines(title, legend, dataset, scale_x, width=700, height=400):
123 line = Line([], encoding="text")
124 line.title(title)
125 line.dataset(dataset)
126
127 line.axes('xy')
128 max_value = (max([max(l) for l in dataset]))
129 line.axes.range(1, 0, max_value)
130 line.scale(0, max_value)
131 line.axes.label(0, *scale_x)
132 line.legend(*legend)
133 line.color(*COLORS[:len(legend)])
134 line.size(width, height)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200135
Yulia Portnovae3a49982015-02-16 16:06:09 +0200136 img_name = hashlib.md5(str(line)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300137 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200138 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300139 line.save(img_path)
140
141 return get_top_top_dir(img_path)