# Path: suma/core/models/link.py
# class Link(Base, TimestampColumns):
#     hashid = Column(String, index=True, unique=True, nullable=False)
#     _url = Column('url', Unicode, index=True, nullable=False)
#     user_id = Column(BigInteger)
#     _hash = Column('hash', String, index=True, nullable=False)
#     _hash_netloc = Column('hash_netloc', String, nullable=False)
#     _hash_path = Column('hash_path', String, nullable=False)
#     meta = Column(MutableDict.as_mutable(JSONB), default={})
#     __table_args__ = (
#         Index('unique_url_and_user_id', _url, user_id, unique=True,
#               postgresql_where=(user_id != None)),
#         Index('unique_url_and_user_id_is_null', 'url', unique=True,
#               postgresql_where=(user_id == None))
#     )
# 
#     is_banned = column_property(
#         exists().where(
#             or_(
#                 BlockedLink.hash.in_([_hash, _hash_netloc, _hash_path])
#             )
#         )
#     )
# 
#     @property
#     def clicks(self):
#         if self._clicks:
#             return self._clicks.counter
#         return 0
#     
#     @hybrid_property
#     def url(self):
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._url = value
#         self._hash, self._hash_netloc, self._hash_path = create_url_hashes(value)
# 
# Path: suma/core/models/meta/base.py
# class Base(object):
#     @declared_attr
#     def __tablename__(cls):
#         return _camel_to_snake(cls.__name__)
# 
#     __table_args__ = {'schema': 'suma'}
# 
#     id = Column(Integer, primary_key=True)
# 
# Path: suma/core/models/meta/schema.py
# class CreatedColumn(object):
#     created = Column(DateTime, default=func.now())


from sqlalchemy import (
    Column,
    Unicode,
    String,
    BigInteger,
    ForeignKey,
    or_
)
from sqlalchemy.dialects.postgresql import INET
from .link import Link
from sqlalchemy.orm import relationship, backref
from suma.core.models.meta import (
    Base,
    CreatedColumn
)


class Stat(Base, CreatedColumn):
    link_id = Column(ForeignKey(Link.id), nullable=False)
    link = relationship(Link, backref=backref('stats'))
    referer = Column(Unicode)
    ip = Column(INET)

# The models belows, are denormalized models from Stat for quick lookup



# Path: suma/core/models/link.py
# class Link(Base, TimestampColumns):
#     hashid = Column(String, index=True, unique=True, nullable=False)
#     _url = Column('url', Unicode, index=True, nullable=False)
#     user_id = Column(BigInteger)
#     _hash = Column('hash', String, index=True, nullable=False)
#     _hash_netloc = Column('hash_netloc', String, nullable=False)
#     _hash_path = Column('hash_path', String, nullable=False)
#     meta = Column(MutableDict.as_mutable(JSONB), default={})
#     __table_args__ = (
#         Index('unique_url_and_user_id', _url, user_id, unique=True,
#               postgresql_where=(user_id != None)),
#         Index('unique_url_and_user_id_is_null', 'url', unique=True,
#               postgresql_where=(user_id == None))
#     )
# 
#     is_banned = column_property(
#         exists().where(
#             or_(
#                 BlockedLink.hash.in_([_hash, _hash_netloc, _hash_path])
#             )
#         )
#     )
# 
#     @property
#     def clicks(self):
#         if self._clicks:
#             return self._clicks.counter
#         return 0
#     
#     @hybrid_property
#     def url(self):
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._url = value
#         self._hash, self._hash_netloc, self._hash_path = create_url_hashes(value)
# 
# Path: suma/core/models/meta/base.py
# class Base(object):
#     @declared_attr
#     def __tablename__(cls):
#         return _camel_to_snake(cls.__name__)
# 
#     __table_args__ = {'schema': 'suma'}
# 
#     id = Column(Integer, primary_key=True)
# 
# Path: suma/core/models/meta/schema.py
# class CreatedColumn(object):
#     created = Column(DateTime, default=func.now())


# Path: suma/core/models/stat.py
from sqlalchemy import (
    Column,
    Unicode,
    String,
    BigInteger,
    ForeignKey,
    or_
)
from sqlalchemy.dialects.postgresql import INET
from .link import Link
from sqlalchemy.orm import relationship, backref
from suma.core.models.meta import (
    Base,
    CreatedColumn
)



class Stat(Base, CreatedColumn):
    link_id = Column(ForeignKey(Link.id), nullable=False)
    link = relationship(Link, backref=backref('stats'))
    referer = Column(Unicode)
    ip = Column(INET)

# The models belows, are denormalized models from Stat for quick lookup



class ClickCounter(Base):