Skip to content

Commit

Permalink
optimize search.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cl117 committed Aug 14, 2024
1 parent e8e9b8a commit 74c5cd4
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 400 deletions.
56 changes: 50 additions & 6 deletions flask/explorer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3

from flask import Flask, request, jsonify, abort
from flask import Flask, request, jsonify, abort, render_template
from werkzeug.exceptions import HTTPException

import os
Expand All @@ -15,11 +15,44 @@

import threading
import time
from flask_debugtoolbar import DebugToolbarExtension
from flask_debugtoolbar_lineprofilerpanel.profile import line_profile


def profile_flask_app():
app.run(debug=True)

if __name__ == "__main__":
#profiler = profile.Profile()
#profiler.enable()
profile_flask_app()
#profiler.disable()
#profiler.print_stats(sort='time')

log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key' # Required for the debug toolbar
app.config['DEBUG'] = True
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
# Profiler configuration
app.config['DEBUG_TB_PROFILER_ENABLED'] = True
app.config['DEBUG_TB_PANELS'] = [
'flask_debugtoolbar.panels.versions.VersionDebugPanel',
'flask_debugtoolbar.panels.timer.TimerDebugPanel',
'flask_debugtoolbar.panels.headers.HeaderDebugPanel',
'flask_debugtoolbar.panels.request_vars.RequestVarsDebugPanel',
'flask_debugtoolbar.panels.config_vars.ConfigVarsDebugPanel',
'flask_debugtoolbar.panels.template.TemplateDebugPanel',
'flask_debugtoolbar.panels.logger.LoggingPanel',
'flask_debugtoolbar.panels.profiler.ProfilerDebugPanel',
'flask_debugtoolbar_lineprofilerpanel.panels.LineProfilerPanel'
]

# Initialize the debug toolbar
toolbar = DebugToolbarExtension(app)


@app.errorhandler(Exception)
def handle_error(e):
Expand All @@ -36,9 +69,9 @@ def startup():
def auto_update_index():
while True:
time.sleep(int(utils.get_config()['updateTimeInDays']) * 86400)
if utils.get_config()['autoUpdateIndex'] and utils.get_config()['updateTimeInDays'] > 0:
utils.log('Updating index automatically. To disable, set the \"autoUpdateIndex\" property in config.json to false.')
update()
# if utils.get_config()['autoUpdateIndex'] and utils.get_config()['updateTimeInDays'] > 0:
# utils.log('Updating index automatically. To disable, set the \"autoUpdateIndex\" property in config.json to false.')
# update()

# Thread for automatically updaing the index periodically
update_thread = threading.Thread(target=auto_update_index, daemon=True)
Expand All @@ -64,7 +97,6 @@ def handle_error(e):
utils.log('[ERROR] Returning error ' + str(e) + "\n Traceback:\n" + traceback.format_exc())
return jsonify(error=str(e)), 500


@app.route('/info', methods=['GET'])
def info():
utils.log('Explorer up!!! Virtutoso ' + str(query.memoized_query_sparql.cache_info()))
Expand Down Expand Up @@ -161,8 +193,13 @@ def incremental_remove_collection():
except:
raise

@app.route('/test', methods=['GET'])
@line_profile
def SBOLExplore_test_endpoint():
return render_template('index.html')

@app.route('/', methods=['GET'])
@line_profile
def sparql_search_endpoint():
try:
# make sure index is built, or throw exception
Expand All @@ -173,7 +210,13 @@ def sparql_search_endpoint():

if sparql_query is not None:
default_graph_uri = request.args.get('default-graph-uri')
response = jsonify(search.search(sparql_query, utils.get_uri2rank(), utils.get_clusters(), default_graph_uri))
response = jsonify(
search.search(
sparql_query,
utils.get_uri2rank(),
utils.get_clusters(),
default_graph_uri
))
return response
else:
return "<pre><h1>Welcome to SBOLExplorer! <br> <h2>The available indices in Elasticsearch are shown below:</h2></h1><br>"\
Expand All @@ -183,6 +226,7 @@ def sparql_search_endpoint():
+ "<br><br><br><br><a href=\"https://github.com/synbiodex/sbolexplorer\">Visit our GitHub repository!</a>"\
+ "<br><br>Any issues can be reported to our <a href=\"https://github.com/synbiodex/sbolexplorer/issues\">issue tracker.</a>"\
+ "<br><br>Used by <a href=\"https://github.com/synbiohub/synbiohub\">SynBioHub.</a>"
#return render_template('index.html')
except:
raise

Expand Down
1 change: 0 additions & 1 deletion flask/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import query
import json


def add_pagerank(parts_response, uri2rank):
"""
Adds the pagerank score for each part
Expand Down
5 changes: 3 additions & 2 deletions flask/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ ipaddress==1.0.22
itsdangerous==0.24
Jinja2
MarkupSafe==2.0.1
numpy==1.26.4
numpy
python-dateutil==2.7.3
requests==2.19.1
six==1.11.0
urllib3==1.23
Werkzeug==2.1.2
Werkzeug
apscheduler==3.10.4
Loading

0 comments on commit 74c5cd4

Please sign in to comment.