From 31bc87d77094a34c1e263aa932bf1cb6d25f3053 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 8 Jan 2018 09:13:59 +0000 Subject: [PATCH] Add members count to admin panel --- webapp/titanembeds/blueprints/admin/admin.py | 11 +++++++++-- webapp/titanembeds/templates/admin_index.html.j2 | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/webapp/titanembeds/blueprints/admin/admin.py b/webapp/titanembeds/blueprints/admin/admin.py index 408db3f..8bd3e8b 100644 --- a/webapp/titanembeds/blueprints/admin/admin.py +++ b/webapp/titanembeds/blueprints/admin/admin.py @@ -1,10 +1,11 @@ from flask import Blueprint, url_for, redirect, session, render_template, abort, request, jsonify from flask_socketio import emit from functools import wraps -from titanembeds.database import db, get_administrators_list, Cosmetics, Guilds, UnauthenticatedUsers, UnauthenticatedBans, TitanTokens, TokenTransactions, get_titan_token, set_titan_token, list_disabled_guilds, DisabledGuilds, UserCSS +from titanembeds.database import db, get_administrators_list, Cosmetics, Guilds, UnauthenticatedUsers, UnauthenticatedBans, TitanTokens, TokenTransactions, get_titan_token, set_titan_token, list_disabled_guilds, DisabledGuilds, UserCSS, AuthenticatedUsers from titanembeds.oauth import generate_guild_icon_url import datetime import json +from sqlalchemy import func admin = Blueprint("admin", __name__) @@ -20,10 +21,16 @@ def is_admin(f): return decorated_function return decorator(f) +def get_online_users_count(): + time_past = (datetime.datetime.now() - datetime.timedelta(seconds = 15)).strftime('%Y-%m-%d %H:%M:%S') + unauths = db.session.query(func.count(UnauthenticatedUsers.id)).filter(UnauthenticatedUsers.last_timestamp > time_past, UnauthenticatedUsers.revoked == False).scalar() + auths = db.session.query(func.count(AuthenticatedUsers.id)).filter(AuthenticatedUsers.last_timestamp > time_past).scalar() + return {"authenticated": auths, "guest": unauths, "total": auths + unauths} + @admin.route("/") @is_admin def index(): - return render_template("admin_index.html.j2") + return render_template("admin_index.html.j2", count=get_online_users_count()) @admin.route("/cosmetics", methods=["GET"]) @is_admin diff --git a/webapp/titanembeds/templates/admin_index.html.j2 b/webapp/titanembeds/templates/admin_index.html.j2 index cff89b8..93ee921 100644 --- a/webapp/titanembeds/templates/admin_index.html.j2 +++ b/webapp/titanembeds/templates/admin_index.html.j2 @@ -6,6 +6,14 @@

Select an action.

+
+
+

Online Members Count - {{ count["total"] }}

+

Discord Users: {{ count["authenticated"] }}

+

Guest Users: {{ count["guest"] }}

+

(Excluding those utilizing visitor view)

+
+

Cosmetics