# Path: protector/influxdb/time_expression.py
# class TimeExpression(object):
#     # Smallest possible system date.
#     # This is required for the calculation of the max duration between datetime objects.
#     # We use the release day of InfluxDB 0.8 as the epoch
#     # because it is the first official version supported by this tool.
#     INFLUXDB_EPOCH = datetime.strptime('2014-08-22', "%Y-%m-%d")
# 
#     # Full names of all supported relative time units.
#     # Useful for conversions of the internal InfluxDB format to the Python timedelta format.
#     UNIT_NAMES = {
#         'u': 'microseconds',
#         's': 'seconds',
#         'm': 'minutes',
#         'h': 'hours',
#         'd': 'days',
#         'w': 'weeks'
#     }
# 
#     # Conversions of all InfluxDB relative time units to seconds.
#     # Useful for datapoint calculations.
#     UNIT_CONVERSION = {'u': 0.001, 's': 1, 'm': 60, 'h': 3600, 'd': 86400, 'w': 604800}
# 
# Path: protector/influxdb/resolution.py
# class Resolution(object):
#     # Many metrics collectors like collectd send metrics every 10s
#     # so we use this as an estimate for the maximum data resolution
#     MAX_RESOLUTION = 10


import re
from protector.influxdb.time_expression import TimeExpression
from protector.influxdb.resolution import Resolution
    the time interval between two data points.
    The resolution will always be converted to seconds.
    """

    # Regex pattern to extract the value and unit of a group by time() statement
    # E.g. group by time(10s) => { 'value': 10, 'unit': s }
    GROUP_BY_TIME_PATTERN = re.compile(r'\s*(?:by)?\s*time\((\d+)([wdhmsu]|ms)\).*')

    def __init__(self):
        pass

    def parse(self, group_by_stmt):
        """
        Extract the data resolution of a query in seconds
        E.g. "group by time(99s)" => 99

        :param group_by_stmt: A raw InfluxDB group by statement
        """
        if not group_by_stmt:
            return Resolution.MAX_RESOLUTION

        m = self.GROUP_BY_TIME_PATTERN.match(group_by_stmt)
        if not m:
            return None

        value = int(m.group(1))
        unit = m.group(2)
        resolution = self.convert_to_seconds(value, unit)

        # We can't have a higher resolution than the max resolution

# Path: protector/influxdb/time_expression.py
# class TimeExpression(object):
#     # Smallest possible system date.
#     # This is required for the calculation of the max duration between datetime objects.
#     # We use the release day of InfluxDB 0.8 as the epoch
#     # because it is the first official version supported by this tool.
#     INFLUXDB_EPOCH = datetime.strptime('2014-08-22', "%Y-%m-%d")
# 
#     # Full names of all supported relative time units.
#     # Useful for conversions of the internal InfluxDB format to the Python timedelta format.
#     UNIT_NAMES = {
#         'u': 'microseconds',
#         's': 'seconds',
#         'm': 'minutes',
#         'h': 'hours',
#         'd': 'days',
#         'w': 'weeks'
#     }
# 
#     # Conversions of all InfluxDB relative time units to seconds.
#     # Useful for datapoint calculations.
#     UNIT_CONVERSION = {'u': 0.001, 's': 1, 'm': 60, 'h': 3600, 'd': 86400, 'w': 604800}
# 
# Path: protector/influxdb/resolution.py
# class Resolution(object):
#     # Many metrics collectors like collectd send metrics every 10s
#     # so we use this as an estimate for the maximum data resolution
#     MAX_RESOLUTION = 10


# Path: protector/parser/subparsers/resolution.py
import re
from protector.influxdb.time_expression import TimeExpression
from protector.influxdb.resolution import Resolution

    the time interval between two data points.
    The resolution will always be converted to seconds.
    """

    # Regex pattern to extract the value and unit of a group by time() statement
    # E.g. group by time(10s) => { 'value': 10, 'unit': s }
    GROUP_BY_TIME_PATTERN = re.compile(r'\s*(?:by)?\s*time\((\d+)([wdhmsu]|ms)\).*')

    def __init__(self):
        pass

    def parse(self, group_by_stmt):
        """
        Extract the data resolution of a query in seconds
        E.g. "group by time(99s)" => 99

        :param group_by_stmt: A raw InfluxDB group by statement
        """
        if not group_by_stmt:
            return Resolution.MAX_RESOLUTION

        m = self.GROUP_BY_TIME_PATTERN.match(group_by_stmt)
        if not m:
            return None

        value = int(m.group(1))
        unit = m.group(2)
        resolution = self.convert_to_seconds(value, unit)

        # We can't have a higher resolution than the max resolution

        return max(resolution, Resolution.MAX_RESOLUTION)