# Path: pystil/context.py
# MESSAGE_QUEUE = Queue()
# class Tracking(Thread):
# class Pystil(Application):
# class Hdr(RequestHandler):
# class url(object):
#     def __init__(self, db, log, *args, **kwargs):
#     def run(self):
#     def listen(self, *args, **kwargs):
#     def log(self):
#     def db(self):
#     def log(self):
#     def prepare(self):
#     def on_finish(self):
#     def __init__(self, url):
#     def __call__(self, cls):
# 
# Path: pystil/db.py
# class Visit(Base):
#     """This mapped class contains the visits"""
#     __tablename__ = 'visit'
# 
#     id = integer(pkey=True)
#     uuid = string()
#     browser_name = string()
#     hash = string()
#     host = string()
#     browser_version = string()
#     client_tz_offset = integer()
#     date = datetime()
#     last_visit = datetime()
#     ip = string()
#     language = string()
#     page = string()
#     platform = string()
#     query = string()
#     referrer = string()
#     pretty_referrer = string()
#     referrer_domain = string()
#     site = string()
#     size = string()
#     time = Column(Interval)
#     country = string()
#     country_code = string()
#     city = string()
#     lat = decimal()
#     lng = decimal()
#     asn = string()
# 
#     browser_name_version = string()
#     day = date_column()
#     hour = integer()
#     subdomain = string()
#     domain = string()
# 
#     spent_time = column_property(
#         case([
#             (time == None, None),
#             (time < timedelta(seconds=1), 0),
#             (time < timedelta(seconds=2), 1),
#             (time < timedelta(seconds=5), 2),
#             (time < timedelta(seconds=10), 3),
#             (time < timedelta(seconds=20), 4),
#             (time < timedelta(seconds=30), 5),
#             (time < timedelta(seconds=60), 6),
#             (time < timedelta(seconds=120), 7),
#             (time < timedelta(seconds=300), 8),
#             (time < timedelta(seconds=600), 9)
#         ], else_=10))


from datetime import timedelta, date
from math import floor
from urllib.parse import urlparse, parse_qs
from pystil.context import pystil
from pystil.db import Visit
from sqlalchemy.sql import literal, func
import re
                return referrer
            return "Local"

        query = up.query
        parsed = parse_qs(query)
        search = parsed.get('q', parsed.get('p', parsed.get('rdata', None)))
        if search:
            # TODO Yahoo variable encoding
            if with_query:
                return u"Organic: %s %s" % (netloc, try_decode(search[0]))
            return u"Organic: %s" % netloc
        if host_only:
            return netloc
        return referrer
    return "Direct"


def parse_domain(referrer):
    """Return the domain of the given referrer."""
    if referrer:
        up = urlparse(referrer)
        netloc = up.netloc
        return netloc or "Local"
    return "Direct"


def get_aggregate(criteria):
    """Returns a tuple in the form (model, count_column).
    Tries to look for an aggregate table suitable for the criteria
    """

# Path: pystil/context.py
# MESSAGE_QUEUE = Queue()
# class Tracking(Thread):
# class Pystil(Application):
# class Hdr(RequestHandler):
# class url(object):
#     def __init__(self, db, log, *args, **kwargs):
#     def run(self):
#     def listen(self, *args, **kwargs):
#     def log(self):
#     def db(self):
#     def log(self):
#     def prepare(self):
#     def on_finish(self):
#     def __init__(self, url):
#     def __call__(self, cls):
# 
# Path: pystil/db.py
# class Visit(Base):
#     """This mapped class contains the visits"""
#     __tablename__ = 'visit'
# 
#     id = integer(pkey=True)
#     uuid = string()
#     browser_name = string()
#     hash = string()
#     host = string()
#     browser_version = string()
#     client_tz_offset = integer()
#     date = datetime()
#     last_visit = datetime()
#     ip = string()
#     language = string()
#     page = string()
#     platform = string()
#     query = string()
#     referrer = string()
#     pretty_referrer = string()
#     referrer_domain = string()
#     site = string()
#     size = string()
#     time = Column(Interval)
#     country = string()
#     country_code = string()
#     city = string()
#     lat = decimal()
#     lng = decimal()
#     asn = string()
# 
#     browser_name_version = string()
#     day = date_column()
#     hour = integer()
#     subdomain = string()
#     domain = string()
# 
#     spent_time = column_property(
#         case([
#             (time == None, None),
#             (time < timedelta(seconds=1), 0),
#             (time < timedelta(seconds=2), 1),
#             (time < timedelta(seconds=5), 2),
#             (time < timedelta(seconds=10), 3),
#             (time < timedelta(seconds=20), 4),
#             (time < timedelta(seconds=30), 5),
#             (time < timedelta(seconds=60), 6),
#             (time < timedelta(seconds=120), 7),
#             (time < timedelta(seconds=300), 8),
#             (time < timedelta(seconds=600), 9)
#         ], else_=10))


# Path: pystil/utils.py
from datetime import timedelta, date
from math import floor
from urllib.parse import urlparse, parse_qs
from pystil.context import pystil
from pystil.db import Visit
from sqlalchemy.sql import literal, func
import re

                return referrer
            return "Local"

        query = up.query
        parsed = parse_qs(query)
        search = parsed.get('q', parsed.get('p', parsed.get('rdata', None)))
        if search:
            # TODO Yahoo variable encoding
            if with_query:
                return u"Organic: %s %s" % (netloc, try_decode(search[0]))
            return u"Organic: %s" % netloc
        if host_only:
            return netloc
        return referrer
    return "Direct"


def parse_domain(referrer):
    """Return the domain of the given referrer."""
    if referrer:
        up = urlparse(referrer)
        netloc = up.netloc
        return netloc or "Local"
    return "Direct"


def get_aggregate(criteria):
    """Returns a tuple in the form (model, count_column).
    Tries to look for an aggregate table suitable for the criteria
    """

    model = getattr(pystil.db, 'Agg_by_%s' % criteria, Visit)