From 15bfc12fe8ebbc13396ed12128be9ff7e61a4499 Mon Sep 17 00:00:00 2001 From: James Kent Date: Wed, 15 Nov 2023 13:46:55 -0600 Subject: [PATCH] add sqltap profiling agsi --- store/neurostore/core.py | 17 +++++++++++++++++ store/neurostore/models/data.py | 3 ++- store/neurostore/tests/api/test_studysets.py | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/store/neurostore/core.py b/store/neurostore/core.py index 804a4f8ee..2769ef760 100644 --- a/store/neurostore/core.py +++ b/store/neurostore/core.py @@ -11,11 +11,23 @@ from connexion.resolver import MethodResolver from flask_caching import Cache import sqltap.wsgi +import sqltap from .or_json import ORJSONDecoder, ORJSONEncoder from .database import init_db +class SQLTapMiddleware: + def __init__(self, app): + self.app = app + + async def __call__(self, scope, receive, send): + profiler = sqltap.start() + await self.app(scope, receive, send) + statistics = profiler.collect() + sqltap.report(statistics, "report.txt", report_format="text") + + connexion_app = connexion.FlaskApp(__name__, specification_dir="openapi/") app = connexion_app.app @@ -45,6 +57,11 @@ allow_headers=["*"], ) +# add sqltap +connexion_app.add_middleware( + SQLTapMiddleware, +) + connexion_app.add_api( openapi_file, base_path="/api", diff --git a/store/neurostore/models/data.py b/store/neurostore/models/data.py index da23f355f..c5733a2bc 100644 --- a/store/neurostore/models/data.py +++ b/store/neurostore/models/data.py @@ -150,7 +150,8 @@ class BaseStudy(BaseMixin, db.Model): user = relationship("User", backref=backref("base_studies")) # retrieve versions of same study versions = relationship( - "Study", backref=backref("base_study")) + "Study", backref=backref("base_study") + ) def update_has_images_and_points(self): # Calculate has_images and has_coordinates for the BaseStudy diff --git a/store/neurostore/tests/api/test_studysets.py b/store/neurostore/tests/api/test_studysets.py index a357c88bd..6ceb7c53c 100644 --- a/store/neurostore/tests/api/test_studysets.py +++ b/store/neurostore/tests/api/test_studysets.py @@ -23,7 +23,7 @@ def test_post_and_get_studysets(auth_client, ingest_neurosynth, session): == post_resp.json() ) -@add_event_listeners +# @add_event_listeners def test_add_many_studies_to_studyset(auth_client, ingest_neurosynth, session): existing_studies = Study.query.all() existing_study_ids = [s.id for s in existing_studies]