Skip to content

Commit

Permalink
Merge pull request #147 from pagreene/log-queries
Browse files Browse the repository at this point in the history
Log usage of queries/endpoints.
  • Loading branch information
pagreene committed Nov 16, 2020
2 parents af05081 + 49e7633 commit de1d7ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion indra_db/util/constructors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ['get_primary_db', 'get_db', 'get_ro']
__all__ = ['get_primary_db', 'get_db', 'get_ro', 'get_ro_host']

import logging

Expand Down Expand Up @@ -106,3 +106,11 @@ def get_ro(ro_label):
return
ro.grab_session()
return ro


def get_ro_host(ro_label):
"""Get the host of the current readonly database."""
ro = get_ro(ro_label)
if not ro:
return None
return ro.url.host
12 changes: 12 additions & 0 deletions rest_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
from indra_db.exceptions import BadHashError
from indra_db.client.principal.curation import *
from indra_db.client.readonly import AgentJsonExpander
from indra_db.util.constructors import get_ro_host

from indralab_auth_tools.auth import auth, resolve_auth, config_auth
from indralab_auth_tools.log import note_in_log, set_log_service_name, \
user_log_endpoint

from rest_api.config import *
from rest_api.call_handlers import *
Expand All @@ -30,6 +33,8 @@
logger = logging.getLogger("db rest api")
logger.setLevel(logging.INFO)

set_log_service_name(f"db-rest-api-{DEPLOYMENT}")


class MyFlask(Flask):
def route(self, url, *args, **kwargs):
Expand Down Expand Up @@ -113,6 +118,7 @@ def ground():

@app.route('/search', methods=['GET'])
@jwt_nontest_optional
@user_log_endpoint
def search():
stmt_types = {c.__name__ for c in get_all_descendants(Statement)}
stmt_types -= {'Influence', 'Event', 'Unresolved'}
Expand Down Expand Up @@ -186,6 +192,7 @@ def serve_stages(stage):

@app.route('/statements', methods=['GET'])
@jwt_nontest_optional
@user_log_endpoint
def old_search():
# Create a template object from the template file, load once
url_base = request.url_root
Expand All @@ -200,8 +207,11 @@ def old_search():

@app.route('/<result_type>/<path:method>', methods=['GET', 'POST'])
@app.route('/metadata/<result_type>/<path:method>', methods=['GET', 'POST'])
@user_log_endpoint
def get_statements(result_type, method):
"""Get some statements constrained by query."""
note_in_log(method=method, result_type=result_type)
note_in_log(db_host=get_ro_host('primary'))

if method == 'from_agents' and request.method == 'GET':
call = FromAgentsApiCall(env)
Expand Down Expand Up @@ -312,7 +322,9 @@ def expand_meta_row():


@app.route('/query/<result_type>', methods=['GET', 'POST'])
@user_log_endpoint
def get_statements_by_query_json(result_type):
note_in_log(result_type=result_type)
return FallbackQueryApiCall(env).run(result_type)


Expand Down
5 changes: 5 additions & 0 deletions rest_api/call_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from indra_db.client.readonly import *
from indra_db.client.principal.curation import *
from indralab_auth_tools.log import note_in_log, is_log_running

from rest_api.config import MAX_STMTS, REDACT_MESSAGE, TITLE, TESTING, \
jwt_nontest_optional
Expand Down Expand Up @@ -146,6 +147,10 @@ def get_db_query(self):
.count(HasAgent.__name__))
self.db_query &= HasNumAgents((num_agents,))

# Note the query in the log, if one is running.
if is_log_running():
note_in_log(query=self.db_query.to_json())

logger.info(f"Constructed query \"{self.db_query}\":\n"
f"{json.dumps(self.db_query.to_json(), indent=2)}")
return self.db_query
Expand Down

0 comments on commit de1d7ee

Please sign in to comment.