# Path: suma/core/services/interfaces/stat.py
# class IStatService(Interface):
#     def add_click(self, link_id, ip=None, referer=None):
#         pass
# 
#     def get_counter_by_link_id(self, link_id):
#         pass
# 
# Path: suma/core/models/stat.py
# class Stat(Base, CreatedColumn):
#     link_id = Column(ForeignKey(Link.id), nullable=False)
#     link = relationship(Link, backref=backref('stats'))
#     referer = Column(Unicode)
#     ip = Column(INET)
# 
# class ClickCounter(Base):
#     id = Column(ForeignKey(Link.id), primary_key=True, nullable=False)
#     link = relationship(Link, backref=backref('_clicks', uselist=False, lazy='joined'))
#     counter = Column(BigInteger, default=1)


from types import IntType, LongType
from suma.core.services.interfaces import IStatService
from zope.interface import implementer
from suma.core.models import Stat, ClickCounter
from structlog import get_logger
from sqlalchemy.exc import IntegrityError
        assert type(link_id) in [IntType, LongType], "link_id is not an integer: %r" % link_id

        lcounter = self.get_counter_by_link_id(link_id)
        if lcounter:
            lcounter.counter = lcounter.counter + 1
            self.dbsession.add(lcounter)
        if not lcounter:
            lcounter = ClickCounter(
                id=link_id,
                counter=1
            )
            self.dbsession.begin_nested()
            try:
                self.dbsession.add(lcounter)
                self.dbsession.commit()
            except IntegrityError:
                self.dbsession.rollback()
                lcounter = self.get_counter_by_link_id(link_id)
                lcounter.counter = lcounter.counter + 1
                self.dbsession.add(lcounter)

        stat = Stat(
            link_id=link_id,
            ip=ip,
            referer=referer
        )
        self.dbsession.add(stat)
        return lcounter, stat

    def get_counter_by_link_id(self, link_id):

# Path: suma/core/services/interfaces/stat.py
# class IStatService(Interface):
#     def add_click(self, link_id, ip=None, referer=None):
#         pass
# 
#     def get_counter_by_link_id(self, link_id):
#         pass
# 
# Path: suma/core/models/stat.py
# class Stat(Base, CreatedColumn):
#     link_id = Column(ForeignKey(Link.id), nullable=False)
#     link = relationship(Link, backref=backref('stats'))
#     referer = Column(Unicode)
#     ip = Column(INET)
# 
# class ClickCounter(Base):
#     id = Column(ForeignKey(Link.id), primary_key=True, nullable=False)
#     link = relationship(Link, backref=backref('_clicks', uselist=False, lazy='joined'))
#     counter = Column(BigInteger, default=1)


# Path: suma/core/services/stat.py
from types import IntType, LongType
from suma.core.services.interfaces import IStatService
from zope.interface import implementer
from suma.core.models import Stat, ClickCounter
from structlog import get_logger
from sqlalchemy.exc import IntegrityError

        assert type(link_id) in [IntType, LongType], "link_id is not an integer: %r" % link_id

        lcounter = self.get_counter_by_link_id(link_id)
        if lcounter:
            lcounter.counter = lcounter.counter + 1
            self.dbsession.add(lcounter)
        if not lcounter:
            lcounter = ClickCounter(
                id=link_id,
                counter=1
            )
            self.dbsession.begin_nested()
            try:
                self.dbsession.add(lcounter)
                self.dbsession.commit()
            except IntegrityError:
                self.dbsession.rollback()
                lcounter = self.get_counter_by_link_id(link_id)
                lcounter.counter = lcounter.counter + 1
                self.dbsession.add(lcounter)

        stat = Stat(
            link_id=link_id,
            ip=ip,
            referer=referer
        )
        self.dbsession.add(stat)
        return lcounter, stat

    def get_counter_by_link_id(self, link_id):

        return self.dbsession.query(ClickCounter).get(link_id)