예제 #1
0
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
예제 #2
0
파일: graph_utils.py 프로젝트: gwTumm/sipa
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
예제 #3
0
파일: test_units.py 프로젝트: agdsn/sipa
    def test_unit_in_formatted_string(self):
        num, divisions = 1024, 1

        formatted = format_as_traffic(num, divisions, divide=False)
        # Checking num was passed isn't really testable, since
        # it could've been formatted ANY possible way.
        # Calling the format string itself is a stupid test,
        # on the other hand, basically reprogramming the
        # method.  Therefore, it is only tested that the
        # correct unit is appended.
        self.assertIn(UNIT_LIST[divisions], formatted)
예제 #4
0
    def test_unit_in_formatted_string(self):
        num, divisions = 1024, 1

        formatted = format_as_traffic(num, divisions, divide=False)
        # Checking num was passed isn't really testable, since
        # it could've been formatted ANY possible way.
        # Calling the format string itself is a stupid test,
        # on the other hand, basically reprogramming the
        # method.  Therefore, it is only tested that the
        # correct unit is appended.
        self.assertIn(UNIT_LIST[divisions], formatted)
예제 #5
0
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
예제 #6
0
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