def generate_credit_chart(traffic_data, inline=True, max_credit=(63 * 1024 ** 2)): """Create a graph object from the input traffic data with pygal. If inline is set, the chart is being passed the option to not add an xml declaration header to the beginning of the `render()` output, so it can be directly included in HTML code (wrapped by a `<figure>`) :param traffic_data: The traffic data as given by `user.traffic_history` :param inline: Determines the option `disable_xml_declaration` :return: The graph object """ raw_max = max_credit divisions = max_divisions(raw_max) max = reduce_by_base(raw_max, divisions) credit_chart = default_chart( pygal.Line, gettext("Credit (GiB)"), inline, value_formatter=lambda value: format_as_traffic(value, divisions, divide=False), ) credit_chart.range = (0, max) credit_chart.x_labels = (get_weekday(day['day']) for day in traffic_data) credit_chart.add(gettext("Credit"), [reduce_by_base(day['credit'], divisions=divisions) for day in traffic_data]) credit_chart.add(gettext("Maximum"), [max]*len(traffic_data), stroke_style={'dasharray': '7', 'width': '2'}, fill=False, show_dots=False) return credit_chart
def generate_credit_chart(traffic_data, inline=True): """Create a graph object from the input traffic data with pygal. If inline is set, the chart is being passed the option to not add an xml declaration header to the beginning of the `render()` output, so it can be directly included in HTML code (wrapped by a `<figure>`) :param traffic_data: The traffic data as given by `user.traffic_history` :param inline: Determines the option `disable_xml_declaration` :return: The graph object """ raw_max = 63*1024*1024 divisions = max_divisions(raw_max) max = reduce_by_base(raw_max, divisions) credit_chart = default_chart( pygal.Line, gettext("Credit (GiB)"), inline, value_formatter=lambda value: format_as_traffic(value, divisions, divide=False), ) credit_chart.range = (0, max) credit_chart.x_labels = (get_weekday(day['day']) for day in traffic_data) credit_chart.add(gettext("Credit"), [reduce_by_base(day['credit'], divisions=divisions) for day in traffic_data]) credit_chart.add(gettext("Maximum"), [max]*len(traffic_data), stroke_style={'dasharray': '7', 'width': '2'}, fill=False, show_dots=False) return credit_chart
def test_max_divisions(self): """Test `max_divisions()` for basic cases""" for base in self.BASES: with self.subTest(base=base): example = [(1, 0), (base / 2, 0), (base, 1), (2 * base, 1), (base**2, 2)] for num, expected_division in example: with self.subTest(num=num): self.assertEqual(max_divisions(num, base=base), expected_division)
def test_max_divisions(self): """Test `max_divisions()` for basic cases""" for base in self.BASES: with self.subTest(base=base): example = [(1, 0), (base / 2, 0), (base, 1), (2 * base, 1), (base**2, 2)] for num, expected_division in example: with self.subTest(num=num): self.assertEqual(max_divisions(num, base=base), expected_division)
def generate_traffic_chart(traffic_data, inline=True): """Create a graph object from the input traffic data with pygal. If inline is set, the chart is being passed the option to not add an xml declaration header to the beginning of the `render()` output, so it can be directly included in HTML code (wrapped by a `<figure>`) :param traffic_data: The traffic data as given by `user.traffic_history` :param inline: Determines the option `disable_xml_declaration` :return: The graph object """ # choose unit according to maximum of `throughput` divisions = max_divisions(max(day['throughput'] for day in traffic_data)) traffic_data = [{key: (reduce_by_base(val, divisions=divisions) if key in ['input', 'output', 'throughput'] else val) for key, val in entry.items() } for entry in traffic_data] traffic_chart = default_chart( pygal.Bar, gettext("Traffic (MiB)"), inline, # don't divide, since the raw values already have been prepared. # `divide=False` effectively just appends the according unit. value_formatter=lambda value: format_as_traffic(value, divisions, divide=False), ) traffic_chart.x_labels = (get_weekday(day['day']) for day in traffic_data) traffic_chart.add(gettext("Eingehend"), [day['input'] for day in traffic_data], stroke_style={'dasharray': '5'}) traffic_chart.add(gettext("Ausgehend"), [day['output'] for day in traffic_data], stroke_style={'dasharray': '5'}) traffic_chart.add(gettext("Gesamt"), [day['throughput'] for day in traffic_data], stroke_style={'width': '2'}) return traffic_chart
def generate_traffic_chart(traffic_data, inline=True): """Create a graph object from the input traffic data with pygal. If inline is set, the chart is being passed the option to not add an xml declaration header to the beginning of the `render()` output, so it can be directly included in HTML code (wrapped by a `<figure>`) :param traffic_data: The traffic data as given by `user.traffic_history` :param inline: Determines the option `disable_xml_declaration` :return: The graph object """ # choose unit according to maximum of `throughput` divisions = max_divisions(max(day['throughput'] for day in traffic_data)) traffic_data = [{ key: (reduce_by_base(val, divisions=divisions) if key in ['input', 'output', 'throughput'] else val) for key, val in entry.items() } for entry in traffic_data] traffic_chart = default_chart( pygal.Bar, gettext("Traffic (MiB)"), inline, # don't divide, since the raw values already have been prepared. # `divide=False` effectively just appends the according unit. value_formatter=lambda value: format_as_traffic( value, divisions, divide=False), ) traffic_chart.x_labels = (get_weekday(day['day']) for day in traffic_data) traffic_chart.add(gettext("Eingehend"), [day['input'] for day in traffic_data], stroke_style={'dasharray': '5'}) traffic_chart.add(gettext("Ausgehend"), [day['output'] for day in traffic_data], stroke_style={'dasharray': '5'}) traffic_chart.add(gettext("Gesamt"), [day['throughput'] for day in traffic_data], stroke_style={'width': '2'}) return traffic_chart