# Path: kotori/io/protocol/util.py
# def is_number(s):
#     """
#     Check string for being a numeric value.
#     http://pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/
#     """
#     try:
#         float(s)
#         return True
#     except ValueError:
#         pass
# 
#     try:
#         import unicodedata
#         unicodedata.numeric(s)
#         return True
#     except (TypeError, ValueError):
#         pass
# 
#     return False
# 
# Path: kotori/util/common.py
# def tdelta(input):
#     # https://blog.posativ.org/2011/parsing-human-readable-timedeltas-in-python/
#     keys = ["weeks", "days", "hours", "minutes", "seconds"]
#     regex = "".join(["((?P<%s>\d+)%s ?)?" % (k, k[0]) for k in keys])
#     kwargs = {}
#     for k,v in re.match(regex, input).groupdict(default="0").items():
#         kwargs[k] = int(v)
#     return timedelta(**kwargs)


import arrow
from arrow.parser import DateTimeParser
from datetime import datetime, timedelta
from twisted.logger import Logger
from kotori.io.protocol.util import is_number
from kotori.util.common import tdelta
        from pyinfluxql import Query
        from pyinfluxql.functions import Mean
# (c) 2016-2021 Andreas Motl <andreas@getkotori.org>

log = Logger()


class QueryTransformer(object):

    @classmethod
    def transform(cls, data):
        """
        Compute InfluxDB query expression from data in transformation dictionary.
        Also compute date range from query parameters "from" and "to".
        """


        measurement = data.measurement

        # Vanilla QL (v1)
        #expression = 'SELECT * FROM {measurement}'.format(measurement=measurement)

        # PyInfluxQL (v2)
        # https://github.com/jjmalina/pyinfluxql

        # Labs
        #time_begin = arrow.utcnow() - arrow.Arrow(hour=1)
        #expression = Query('*').from_(measurement).where(time__gt=datetime.utcnow() - timedelta(hours=1))
        #expression = Query(Mean('*')).from_(measurement).where(time__gt=datetime.now() - timedelta(1)).group_by(time=timedelta(hours=1))

        # Fix up "measurement" if starting with numeric value
        # TODO: Fix should go to pyinfluxql

# Path: kotori/io/protocol/util.py
# def is_number(s):
#     """
#     Check string for being a numeric value.
#     http://pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/
#     """
#     try:
#         float(s)
#         return True
#     except ValueError:
#         pass
# 
#     try:
#         import unicodedata
#         unicodedata.numeric(s)
#         return True
#     except (TypeError, ValueError):
#         pass
# 
#     return False
# 
# Path: kotori/util/common.py
# def tdelta(input):
#     # https://blog.posativ.org/2011/parsing-human-readable-timedeltas-in-python/
#     keys = ["weeks", "days", "hours", "minutes", "seconds"]
#     regex = "".join(["((?P<%s>\d+)%s ?)?" % (k, k[0]) for k in keys])
#     kwargs = {}
#     for k,v in re.match(regex, input).groupdict(default="0").items():
#         kwargs[k] = int(v)
#     return timedelta(**kwargs)


# Path: kotori/io/protocol/influx.py
import arrow
from arrow.parser import DateTimeParser
from datetime import datetime, timedelta
from twisted.logger import Logger
from kotori.io.protocol.util import is_number
from kotori.util.common import tdelta
        from pyinfluxql import Query
        from pyinfluxql.functions import Mean

# (c) 2016-2021 Andreas Motl <andreas@getkotori.org>

log = Logger()


class QueryTransformer(object):

    @classmethod
    def transform(cls, data):
        """
        Compute InfluxDB query expression from data in transformation dictionary.
        Also compute date range from query parameters "from" and "to".
        """


        measurement = data.measurement

        # Vanilla QL (v1)
        #expression = 'SELECT * FROM {measurement}'.format(measurement=measurement)

        # PyInfluxQL (v2)
        # https://github.com/jjmalina/pyinfluxql

        # Labs
        #time_begin = arrow.utcnow() - arrow.Arrow(hour=1)
        #expression = Query('*').from_(measurement).where(time__gt=datetime.utcnow() - timedelta(hours=1))
        #expression = Query(Mean('*')).from_(measurement).where(time__gt=datetime.now() - timedelta(1)).group_by(time=timedelta(hours=1))

        # Fix up "measurement" if starting with numeric value
        # TODO: Fix should go to pyinfluxql

        if is_number(measurement[0]):