From debd7da7046304cc87d34c22e6ccd9357c71182e Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 31 Dec 2018 19:31:45 +0000 Subject: [PATCH] Initial timeit deebug implementation --- webapp/titanembeds/blueprints/api/api.py | 8 ++++++- webapp/titanembeds/decorators.py | 30 +++++++++++++++++++++++- webapp/titanembeds/discordrest.py | 3 +++ webapp/titanembeds/redisqueue.py | 3 +++ webapp/titanembeds/utils.py | 4 ++++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index d13c8a0..3c13f85 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -1,5 +1,5 @@ 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.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild import titanembeds.constants as constants @@ -100,6 +100,7 @@ def filter_guild_channel(guild_id, channel_id, force_everyone=False): return chan return None +@timeit def get_online_discord_users(guild_id, embed): apimembers = redisqueue.list_guild_members(guild_id) apimembers_filtered = {} @@ -120,6 +121,7 @@ def get_online_discord_users(guild_id, embed): member["avatar_url"] = apimem["avatar_url"] return embed['members'] +@timeit def get_online_embed_users(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 [] @@ -144,9 +146,11 @@ def get_online_embed_users(guild_id): users['authenticated'].append(meta) return users +@timeit def get_guild_emojis(guild_id): return redisqueue.get_guild(guild_id)["emojis"] +@timeit def get_guild_roles(guild_id): return redisqueue.get_guild(guild_id)["roles"] @@ -428,11 +432,13 @@ def change_unauthenticated_username(): db.session.commit() return final_response +@timeit def get_guild_guest_icon(guild_id): 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') def process_query_guild(guild_id, visitor=False): + #print("process_query_guild") widget = discord_api.get_widget(guild_id) forced_role = get_forced_role(guild_id) channels = get_guild_channels(guild_id, visitor, forced_role=forced_role) diff --git a/webapp/titanembeds/decorators.py b/webapp/titanembeds/decorators.py index 830bf9e..27dd3e8 100644 --- a/webapp/titanembeds/decorators.py +++ b/webapp/titanembeds/decorators.py @@ -1,6 +1,7 @@ from functools import wraps from flask import url_for, redirect, session, jsonify, abort, request from titanembeds.database import list_disabled_guilds +from config import config def valid_session_required(api=False): def decorator(f): @@ -39,4 +40,31 @@ def abort_if_guild_disabled(*args): return ('', 423) return f(*args, **kwargs) return decorated_function - return decorator \ No newline at end of file + 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 + \ No newline at end of file diff --git a/webapp/titanembeds/discordrest.py b/webapp/titanembeds/discordrest.py index 19affe7..f301c48 100644 --- a/webapp/titanembeds/discordrest.py +++ b/webapp/titanembeds/discordrest.py @@ -6,6 +6,8 @@ import urllib from titanembeds.utils import redis_store from flask import request +from titanembeds.decorators import timeit + _DISCORD_API_BASE = "https://discordapp.com/api/v6" def json_or_text(response): @@ -148,6 +150,7 @@ class DiscordREST: # Widget Handler ##################### + @timeit def get_widget(self, guild_id): _endpoint = _DISCORD_API_BASE + "/servers/{guild_id}/widget.json".format(guild_id=guild_id) embed = self.get_guild_embed(guild_id) diff --git a/webapp/titanembeds/redisqueue.py b/webapp/titanembeds/redisqueue.py index fc70593..ac26ff5 100644 --- a/webapp/titanembeds/redisqueue.py +++ b/webapp/titanembeds/redisqueue.py @@ -2,6 +2,8 @@ from titanembeds.utils import redis_store import json import time +from titanembeds.decorators import timeit + class RedisQueue: def __init__(self): pass # Nothing really to initialize @@ -105,6 +107,7 @@ class RedisQueue: return self.get_guild_member(guild_id, guild_member_id["user_id"]) return None + @timeit def list_guild_members(self, guild_id): key = "/guilds/{}/members".format(guild_id) member_ids = self.get(key, "list_guild_members", {"guild_id": guild_id}, data_type="set") diff --git a/webapp/titanembeds/utils.py b/webapp/titanembeds/utils.py index 411d9ed..196cab6 100644 --- a/webapp/titanembeds/utils.py +++ b/webapp/titanembeds/utils.py @@ -14,6 +14,8 @@ import hashlib import time import json +from titanembeds.decorators import timeit + redis_store = FlaskRedis(charset="utf-8", decode_responses=True) from titanembeds.discordrest import DiscordREST @@ -205,6 +207,7 @@ def get_member_roles(guild_id, user_id): role_converted.append(str(role)) return role_converted +@timeit def get_guild_channels(guild_id, force_everyone=False, forced_role=0): if user_unauthenticated() or force_everyone: 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 return result +@timeit def get_forced_role(guild_id): dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() if not session.get("unauthenticated", True):