# Path: ngcrowd/models.py
# class JsonifyMixin:
# class Application(Base, JsonifyMixin):
# class User(Base):
# class Entity(Base, JsonifyMixin):
# class EntityProperty(Base, JsonifyMixin):
# class EntityPropertyValue(Base):
# class ReferenceBookValue(Base):
# class EntityVersions(Base):
#     def __init__(self):
#     def as_json_dict(self, **init):
#     def password_hash(cls, password, salt):
#     def as_dict(self, **addon):
#     def to_dict(self):
#     def to_json_binary_dump(self, json_object):


from ngcrowd.models import DBSession, EntityVersions, User, Entity
from pyramid.view import view_config
from sqlalchemy import func
from sqlalchemy.sql.expression import desc
# -*- coding: utf-8 -*-
__author__ = 'karavanjo'


@view_config(route_name='logs', request_method='GET', renderer='users.mako')
def get_logs(context, request):
    session = DBSession()
    user_entities_count_sbq = session \
        .query(EntityVersions.user_id.label('user_id'), func.count(EntityVersions.entity_id.distinct()).label('count_entities')) \
        .group_by(EntityVersions.user_id) \
        .subquery()

    user_entities_logs = session.query(User, user_entities_count_sbq.c.count_entities) \
        .outerjoin(user_entities_count_sbq, User.id == user_entities_count_sbq.c.user_id) \
        .order_by(desc(user_entities_count_sbq.c.count_entities))

    # count_editable_uiks = session.query(func.count(UikVersions.uik_id.distinct())).scalar()
    count_approved_entities = session.query(func.count(Entity.id)).filter(Entity.approved == True).scalar()

# Path: ngcrowd/models.py
# class JsonifyMixin:
# class Application(Base, JsonifyMixin):
# class User(Base):
# class Entity(Base, JsonifyMixin):
# class EntityProperty(Base, JsonifyMixin):
# class EntityPropertyValue(Base):
# class ReferenceBookValue(Base):
# class EntityVersions(Base):
#     def __init__(self):
#     def as_json_dict(self, **init):
#     def password_hash(cls, password, salt):
#     def as_dict(self, **addon):
#     def to_dict(self):
#     def to_json_binary_dump(self, json_object):


# Path: ngcrowd/views/logs.py
from ngcrowd.models import DBSession, EntityVersions, User, Entity
from pyramid.view import view_config
from sqlalchemy import func
from sqlalchemy.sql.expression import desc

# -*- coding: utf-8 -*-
__author__ = 'karavanjo'


@view_config(route_name='logs', request_method='GET', renderer='users.mako')
def get_logs(context, request):
    session = DBSession()
    user_entities_count_sbq = session \
        .query(EntityVersions.user_id.label('user_id'), func.count(EntityVersions.entity_id.distinct()).label('count_entities')) \
        .group_by(EntityVersions.user_id) \
        .subquery()

    user_entities_logs = session.query(User, user_entities_count_sbq.c.count_entities) \
        .outerjoin(user_entities_count_sbq, User.id == user_entities_count_sbq.c.user_id) \
        .order_by(desc(user_entities_count_sbq.c.count_entities))

    # count_editable_uiks = session.query(func.count(UikVersions.uik_id.distinct())).scalar()
    count_approved_entities = session.query(func.count(Entity.id)).filter(Entity.approved == True).scalar()

    count_all_entities = session.query(func.count(Entity.id)).scalar()