simplify stats code #319

This commit is contained in:
rimu 2024-09-06 16:00:09 +12:00
parent f284be0b39
commit 7d276136ed
2 changed files with 10 additions and 8 deletions

View file

@ -84,19 +84,21 @@ class Instance(db.Model):
def votes_are_public(self):
return self.software.lower() == 'lemmy' or self.software.lower() == 'mbin' or self.software.lower() == 'kbin'
# the db execute returns a cursorresult. the all() returns a list with one item in it.
# the [0] gets the one sqlalchemy Row object, the Row.count then is the number we show
def post_count(self):
return db.session.execute(text(f'SELECT count(*) FROM post WHERE instance_id = {self.id}')).all()[0].count
return db.session.execute(text('SELECT COUNT(id) as c FROM "post" WHERE instance_id = :instance_id'),
{'instance_id': self.id}).scalar()
def post_replies_count(self):
return db.session.execute(text(f'SELECT count(*) FROM post_reply WHERE instance_id = {self.id}')).all()[0].count
return db.session.execute(text('SELECT COUNT(id) as c FROM "post_reply" WHERE instance_id = :instance_id'),
{'instance_id': self.id}).scalar()
def known_communities_count(self):
return db.session.execute(text(f'SELECT count(*) FROM community WHERE instance_id = {self.id}')).all()[0].count
return db.session.execute(text('SELECT COUNT(id) as c FROM "community" WHERE instance_id = :instance_id'),
{'instance_id': self.id}).scalar()
def known_users_count(self):
return db.session.execute(text(f'SELECT count(*) FROM "user" WHERE instance_id = {self.id}')).all()[0].count
return db.session.execute(text('SELECT COUNT(id) as c FROM "user" WHERE instance_id = :instance_id'),
{'instance_id': self.id}).scalar()
def __repr__(self):
return '<Instance {}>'.format(self.domain)

View file

@ -206,7 +206,6 @@
<ul class="dropdown-menu">
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_site' }}" href="{{ url_for('admin.admin_site') }}">{{ _('Site profile') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_misc' }}" href="{{ url_for('admin.admin_misc') }}">{{ _('Misc settings') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_instances' }}" href="{{ url_for('admin.admin_instances') }}">{{ _('Instances') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_communities' }}" href="{{ url_for('admin.admin_communities') }}">{{ _('Communities') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_topics' }}" href="{{ url_for('admin.admin_topics') }}">{{ _('Topics') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_users' }}" href="{{ url_for('admin.admin_users', local_remote='local') }}">{{ _('Users') }}</a></li>
@ -219,6 +218,7 @@
{% endif %}
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_reports' }}" href="{{ url_for('admin.admin_reports') }}">{{ _('Moderation') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_federation' }}" href="{{ url_for('admin.admin_federation') }}">{{ _('Federation') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_instances' }}" href="{{ url_for('admin.admin_instances') }}">{{ _('Instances') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_newsletter' }}" href="{{ url_for('admin.newsletter') }}">{{ _('Newsletter') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_activities' }}" href="{{ url_for('admin.admin_activities') }}">{{ _('Activities') }}</a></li>
<li><a class="dropdown-item{{ ' active' if active_child == 'admin_permissions' }}" href="{{ url_for('admin.admin_permissions') }}">{{ _('Permissions') }}</a></li>