# Path: upribox_interface/traffic/ntopng.py
# class Metric(Enum):
#     PROTOCOLS = 'allprotocols_bps'
#     CATEGORIES = 'allcategories_bps'
#     TRAFFIC_PACKETS_PER_SECOND = 'traffic_pps'
#     TRAFFIC_BITS_PER_SECOND = 'traffic_bps'
#     TOTAL_BYTES = 'traffic_total_bytes'
#     TOTAL_PACKETS = 'traffic_total_packets'
# 
# def device_day_stats(date, host, sent_recv=True, metric=Metric.PROTOCOLS):
#     if not check_ip(host):
#         raise ValueError("host is not a valid IP address")
# 
#     if metric not in Metric:
#         raise ValueError("not a supported metric")
# 
#     res = {}
#     byte = 8.0  # bits
#     time_factor = 300 * 12  # seconds of one rrd interval (1 h) for intervals older than 24 hours
#     if datetime.date.today() == date:
#         time_factor = 300  # seconds of one rrd interval (5 min) for values of the last 24 hours
# 
#     data = _device_day_data(date, host, metric)
# 
#     for proto in data:
#         if sent_recv and metric == Metric.PROTOCOLS:
#             # separate values for sent and received of one protocol
#             key = " ".join(proto['target'].split(" ")[:2])
#         else:
#             # sum up values for send and received of one protocol
#             key = proto['target'].split(" ")[0]
# 
#         res.setdefault(key, 0)
#         # calculates bytes from given bps values
#         res[key] += sum((point[0] / byte) * time_factor for point in proto['datapoints'])
# 
#     final_result = {protocol: convert_to_megabytes(byte_count) for protocol, byte_count in res.items()}
# 
#     return final_result


import re
from .ntopng import Metric, device_day_stats
from lib.stats import get_week_days
from colour import Color
    from collections import Counter
            text.append('▲ {}'.format(value))
            amount += value
        if 'rcvd' in protocols[protocol] and float(protocols[protocol]['rcvd']) > 0:
            value = protocols[protocol]['rcvd']
            text.append('▼ {}'.format(value))
            amount += value
        amounts[protocol] = round(amount, 2)
        texts[protocol] = ' '.join(text)

    return amounts, texts


def parse_traffic_dict(traffic):
    result = dict()
    for datatype in traffic.keys():
        protocol, direction = re.match(r'^(.+)\ \((.+)\)$', datatype).group(1, 2)
        if protocol not in result:
            result[protocol] = dict()
        result[protocol][direction.lower()] = traffic[datatype]
    return result


def get_weekly_traffic(year, week, dev_ip):

    total_protocols = Counter()
    days = []
    total = 0
    colors = list()

    for date in get_week_days(year, week):

# Path: upribox_interface/traffic/ntopng.py
# class Metric(Enum):
#     PROTOCOLS = 'allprotocols_bps'
#     CATEGORIES = 'allcategories_bps'
#     TRAFFIC_PACKETS_PER_SECOND = 'traffic_pps'
#     TRAFFIC_BITS_PER_SECOND = 'traffic_bps'
#     TOTAL_BYTES = 'traffic_total_bytes'
#     TOTAL_PACKETS = 'traffic_total_packets'
# 
# def device_day_stats(date, host, sent_recv=True, metric=Metric.PROTOCOLS):
#     if not check_ip(host):
#         raise ValueError("host is not a valid IP address")
# 
#     if metric not in Metric:
#         raise ValueError("not a supported metric")
# 
#     res = {}
#     byte = 8.0  # bits
#     time_factor = 300 * 12  # seconds of one rrd interval (1 h) for intervals older than 24 hours
#     if datetime.date.today() == date:
#         time_factor = 300  # seconds of one rrd interval (5 min) for values of the last 24 hours
# 
#     data = _device_day_data(date, host, metric)
# 
#     for proto in data:
#         if sent_recv and metric == Metric.PROTOCOLS:
#             # separate values for sent and received of one protocol
#             key = " ".join(proto['target'].split(" ")[:2])
#         else:
#             # sum up values for send and received of one protocol
#             key = proto['target'].split(" ")[0]
# 
#         res.setdefault(key, 0)
#         # calculates bytes from given bps values
#         res[key] += sum((point[0] / byte) * time_factor for point in proto['datapoints'])
# 
#     final_result = {protocol: convert_to_megabytes(byte_count) for protocol, byte_count in res.items()}
# 
#     return final_result


# Path: upribox_interface/traffic/utils.py
import re
from .ntopng import Metric, device_day_stats
from lib.stats import get_week_days
from colour import Color
    from collections import Counter

            text.append('▲ {}'.format(value))
            amount += value
        if 'rcvd' in protocols[protocol] and float(protocols[protocol]['rcvd']) > 0:
            value = protocols[protocol]['rcvd']
            text.append('▼ {}'.format(value))
            amount += value
        amounts[protocol] = round(amount, 2)
        texts[protocol] = ' '.join(text)

    return amounts, texts


def parse_traffic_dict(traffic):
    result = dict()
    for datatype in traffic.keys():
        protocol, direction = re.match(r'^(.+)\ \((.+)\)$', datatype).group(1, 2)
        if protocol not in result:
            result[protocol] = dict()
        result[protocol][direction.lower()] = traffic[datatype]
    return result


def get_weekly_traffic(year, week, dev_ip):

    total_protocols = Counter()
    days = []
    total = 0
    colors = list()

    for date in get_week_days(year, week):

        traffic = device_day_stats(date, dev_ip, sent_recv=True, metric=Metric.PROTOCOLS)