Skip to content

Commit

Permalink
make sure sql statements and timing are logged for all engines, plus …
Browse files Browse the repository at this point in the history
…tag engines with id and log those too, and superfluous user method cleanup
  • Loading branch information
melange396 committed Jun 13, 2023
1 parent d36e29a commit 03c27f2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/server/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from flask import Flask, g, request
from sqlalchemy import event
from sqlalchemy.engine import Connection
from sqlalchemy.engine import Connection, Engine
from werkzeug.exceptions import Unauthorized
from werkzeug.local import LocalProxy

Expand Down Expand Up @@ -85,12 +85,12 @@ def log_info_with_request_and_response(message, response, **kwargs):
**kwargs
)

@event.listens_for(engine, "before_cursor_execute")
@event.listens_for(Engine, "before_cursor_execute")
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
context._query_start_time = time.time()


@event.listens_for(engine, "after_cursor_execute")
@event.listens_for(Engine, "after_cursor_execute")
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
# this timing info may be suspect, at least in terms of dbms cpu time...
# it is likely that it includes that time as well as any overhead that
Expand All @@ -101,7 +101,8 @@ def after_cursor_execute(conn, cursor, statement, parameters, context, executema
# Convert to milliseconds
total_time *= 1000
get_structured_logger("server_api").info(
"Executed SQL", statement=statement, params=parameters, elapsed_time_ms=total_time
"Executed SQL", statement=statement, params=parameters, elapsed_time_ms=total_time,
engine_id=conn.get_execution_options().get('engine_id')
)


Expand Down
4 changes: 2 additions & 2 deletions src/server/_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
# previously `_common` imported from `_security` which imported from `admin.models`, which imported (back again) from `_common` for database connection objects


engine: Engine = create_engine(SQLALCHEMY_DATABASE_URI, **SQLALCHEMY_ENGINE_OPTIONS)
engine: Engine = create_engine(SQLALCHEMY_DATABASE_URI, **SQLALCHEMY_ENGINE_OPTIONS).execution_options(engine_id='default')
metadata = MetaData(bind=engine)
Session = sessionmaker(bind=engine)

if SQLALCHEMY_DATABASE_URI_PRIMARY:
write_engine: Engine = create_engine(SQLALCHEMY_DATABASE_URI_PRIMARY, **SQLALCHEMY_ENGINE_OPTIONS)
write_engine: Engine = create_engine(SQLALCHEMY_DATABASE_URI_PRIMARY, **SQLALCHEMY_ENGINE_OPTIONS).execution_options(engine_id='write_engine')
write_metadata = MetaData(bind=write_engine)
WriteSession = sessionmaker(bind=write_engine)
else:
Expand Down
17 changes: 4 additions & 13 deletions src/server/endpoints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,13 @@ def _require_admin():
return token


def _parse_roles(roles: List[str]) -> Set[str]:
return set(roles)


def _render(mode: str, token: str, flags: Dict, **kwargs):
template = (templates_dir / "index.html").read_text("utf8")
return render_template_string(
template, mode=mode, token=token, flags=flags, roles=UserRole.list_all_roles(), **kwargs
)


def user_exists(user_email: str = None, api_key: str = None):
user = User.find_user(user_email=user_email, api_key=api_key)
return True if user else False


# ~~~~ PUBLIC ROUTES ~~~~


Expand All @@ -69,11 +60,11 @@ def _index():
flags = dict()
if request.method == "POST":
# register a new user
if not user_exists(user_email=request.values["email"], api_key=request.values["api_key"]):
if not User.find_user(user_email=request.values["email"], api_key=request.values["api_key"]):
User.create_user(
request.values["api_key"],
request.values["email"],
_parse_roles(request.values.getlist("roles")),
set(request.values.getlist("roles")),
)
flags["banner"] = "Successfully Added"
else:
Expand Down Expand Up @@ -101,7 +92,7 @@ def _detail(user_id: int):
user=user,
api_key=request.values["api_key"],
email=request.values["email"],
roles=_parse_roles(request.values.getlist("roles")),
roles=set(request.values.getlist("roles")),
)
flags["banner"] = "Successfully Saved"
return _render("detail", token, flags, user=user.as_dict)
Expand All @@ -116,7 +107,7 @@ def _register():

user_api_key = body["user_api_key"]
user_email = body["user_email"]
if user_exists(user_email=user_email, api_key=user_api_key):
if User.find_user(user_email=user_email, api_key=user_api_key):
return make_response(
"User with email and/or API Key already exists, use different parameters or contact us for help",
409,
Expand Down

0 comments on commit 03c27f2

Please sign in to comment.