Initial timeit deebug implementation

This commit is contained in:
Jeremy Zhang 2018-12-31 19:31:45 +00:00
parent 149dbe3128
commit debd7da704
5 changed files with 46 additions and 2 deletions

View File

@ -1,5 +1,5 @@
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, get_administrators_list, get_badges, DiscordBotsOrgTransactions from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, get_administrators_list, get_badges, DiscordBotsOrgTransactions
from titanembeds.decorators import valid_session_required, discord_users_only, abort_if_guild_disabled from titanembeds.decorators import valid_session_required, discord_users_only, abort_if_guild_disabled, timeit
from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles, get_online_embed_user_keys, redis_store, redisqueue, get_forced_role from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles, get_online_embed_user_keys, redis_store, redisqueue, get_forced_role
from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild
import titanembeds.constants as constants import titanembeds.constants as constants
@ -100,6 +100,7 @@ def filter_guild_channel(guild_id, channel_id, force_everyone=False):
return chan return chan
return None return None
@timeit
def get_online_discord_users(guild_id, embed): def get_online_discord_users(guild_id, embed):
apimembers = redisqueue.list_guild_members(guild_id) apimembers = redisqueue.list_guild_members(guild_id)
apimembers_filtered = {} apimembers_filtered = {}
@ -120,6 +121,7 @@ def get_online_discord_users(guild_id, embed):
member["avatar_url"] = apimem["avatar_url"] member["avatar_url"] = apimem["avatar_url"]
return embed['members'] return embed['members']
@timeit
def get_online_embed_users(guild_id): def get_online_embed_users(guild_id):
usrs = get_online_embed_user_keys(guild_id) usrs = get_online_embed_user_keys(guild_id)
unauths = db.session.query(UnauthenticatedUsers).filter(UnauthenticatedUsers.user_key.in_(usrs["UnauthenticatedUsers"]), UnauthenticatedUsers.revoked == False, UnauthenticatedUsers.guild_id == guild_id).all() if usrs["UnauthenticatedUsers"] else [] unauths = db.session.query(UnauthenticatedUsers).filter(UnauthenticatedUsers.user_key.in_(usrs["UnauthenticatedUsers"]), UnauthenticatedUsers.revoked == False, UnauthenticatedUsers.guild_id == guild_id).all() if usrs["UnauthenticatedUsers"] else []
@ -144,9 +146,11 @@ def get_online_embed_users(guild_id):
users['authenticated'].append(meta) users['authenticated'].append(meta)
return users return users
@timeit
def get_guild_emojis(guild_id): def get_guild_emojis(guild_id):
return redisqueue.get_guild(guild_id)["emojis"] return redisqueue.get_guild(guild_id)["emojis"]
@timeit
def get_guild_roles(guild_id): def get_guild_roles(guild_id):
return redisqueue.get_guild(guild_id)["roles"] return redisqueue.get_guild(guild_id)["roles"]
@ -428,11 +432,13 @@ def change_unauthenticated_username():
db.session.commit() db.session.commit()
return final_response return final_response
@timeit
def get_guild_guest_icon(guild_id): def get_guild_guest_icon(guild_id):
guest_icon = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().guest_icon guest_icon = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().guest_icon
return guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png') return guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')
def process_query_guild(guild_id, visitor=False): def process_query_guild(guild_id, visitor=False):
#print("process_query_guild")
widget = discord_api.get_widget(guild_id) widget = discord_api.get_widget(guild_id)
forced_role = get_forced_role(guild_id) forced_role = get_forced_role(guild_id)
channels = get_guild_channels(guild_id, visitor, forced_role=forced_role) channels = get_guild_channels(guild_id, visitor, forced_role=forced_role)

View File

@ -1,6 +1,7 @@
from functools import wraps from functools import wraps
from flask import url_for, redirect, session, jsonify, abort, request from flask import url_for, redirect, session, jsonify, abort, request
from titanembeds.database import list_disabled_guilds from titanembeds.database import list_disabled_guilds
from config import config
def valid_session_required(api=False): def valid_session_required(api=False):
def decorator(f): def decorator(f):
@ -40,3 +41,30 @@ def abort_if_guild_disabled(*args):
return f(*args, **kwargs) return f(*args, **kwargs)
return decorated_function return decorated_function
return decorator return decorator
import time
import logging
logger = logging.getLogger('myapp')
hdlr = logging.FileHandler(config["app-location"] + "/timeit.log")
formatter = logging.Formatter('%(asctime)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.CRITICAL)
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
# print('%r %2.2f ms' % \
# (method.__name__, (te - ts) * 1000))
logger.critical('%r %2.2f ms' % \
(method.__name__, (te - ts) * 1000) + " " + str(session) + " " + str(request))
return result
return timed

View File

@ -6,6 +6,8 @@ import urllib
from titanembeds.utils import redis_store from titanembeds.utils import redis_store
from flask import request from flask import request
from titanembeds.decorators import timeit
_DISCORD_API_BASE = "https://discordapp.com/api/v6" _DISCORD_API_BASE = "https://discordapp.com/api/v6"
def json_or_text(response): def json_or_text(response):
@ -148,6 +150,7 @@ class DiscordREST:
# Widget Handler # Widget Handler
##################### #####################
@timeit
def get_widget(self, guild_id): def get_widget(self, guild_id):
_endpoint = _DISCORD_API_BASE + "/servers/{guild_id}/widget.json".format(guild_id=guild_id) _endpoint = _DISCORD_API_BASE + "/servers/{guild_id}/widget.json".format(guild_id=guild_id)
embed = self.get_guild_embed(guild_id) embed = self.get_guild_embed(guild_id)

View File

@ -2,6 +2,8 @@ from titanembeds.utils import redis_store
import json import json
import time import time
from titanembeds.decorators import timeit
class RedisQueue: class RedisQueue:
def __init__(self): def __init__(self):
pass # Nothing really to initialize pass # Nothing really to initialize
@ -105,6 +107,7 @@ class RedisQueue:
return self.get_guild_member(guild_id, guild_member_id["user_id"]) return self.get_guild_member(guild_id, guild_member_id["user_id"])
return None return None
@timeit
def list_guild_members(self, guild_id): def list_guild_members(self, guild_id):
key = "/guilds/{}/members".format(guild_id) key = "/guilds/{}/members".format(guild_id)
member_ids = self.get(key, "list_guild_members", {"guild_id": guild_id}, data_type="set") member_ids = self.get(key, "list_guild_members", {"guild_id": guild_id}, data_type="set")

View File

@ -14,6 +14,8 @@ import hashlib
import time import time
import json import json
from titanembeds.decorators import timeit
redis_store = FlaskRedis(charset="utf-8", decode_responses=True) redis_store = FlaskRedis(charset="utf-8", decode_responses=True)
from titanembeds.discordrest import DiscordREST from titanembeds.discordrest import DiscordREST
@ -205,6 +207,7 @@ def get_member_roles(guild_id, user_id):
role_converted.append(str(role)) role_converted.append(str(role))
return role_converted return role_converted
@timeit
def get_guild_channels(guild_id, force_everyone=False, forced_role=0): def get_guild_channels(guild_id, force_everyone=False, forced_role=0):
if user_unauthenticated() or force_everyone: if user_unauthenticated() or force_everyone:
member_roles = [guild_id] #equivilant to @everyone role member_roles = [guild_id] #equivilant to @everyone role
@ -313,6 +316,7 @@ def get_channel_permission(channel, guild_id, guild_owner, guild_roles, member_r
result["attach_files"] = False result["attach_files"] = False
return result return result
@timeit
def get_forced_role(guild_id): def get_forced_role(guild_id):
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
if not session.get("unauthenticated", True): if not session.get("unauthenticated", True):