Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RACK UI endpoint to optimize triplestore #856

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion rack-ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import dash_bootstrap_components as dbc
from pages import home, load, verify
from pages.helper import *
from flask import Flask
import json
import platform

# diskcache for non-production apps when developing locally (fine for our Docker application). Needed for @dash.callback with background=True
cache = diskcache.Cache(get_temp_dir() + "/cache")
background_callback_manager = DiskcacheManager(cache)

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], background_callback_manager=background_callback_manager)
server = Flask(__name__)
app = Dash(server=server, external_stylesheets=[dbc.themes.BOOTSTRAP], background_callback_manager=background_callback_manager)
app.title = 'RACK UI'

# menu
Expand Down Expand Up @@ -64,5 +68,23 @@ def display_page(pathname):
else:
return '404'


# endpoint to run triplestore optimization script
# (runs as part of the Dash app, alongside but separate from the UI)
@server.route('/optimize')
def optimize():
try:
if platform.system() == "Windows":
raise Exception("RACK UI triplestore optimization endpoint is not supported on Windows")
command = "sudo ../cli/optimize.sh"
completed_process = run_subprocess(command, get_temp_dir_unique("optimize"))
if completed_process.returncode == 0:
return json.dumps({'success': True})
else:
raise Exception(f"Optimize script returned exit code {completed_process.returncode}")
except Exception as e:
return json.dumps({'success': False, 'message': get_error_message(e)})


if __name__ == '__main__':
app.run_server(host="0.0.0.0", debug=False)
12 changes: 12 additions & 0 deletions rack-ui/pages/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import uuid
import semtk3
import rack
import subprocess

# configuration
BASE_URL = "http://localhost"
Expand All @@ -30,6 +31,10 @@ def get_error_trace(e) -> str:
trace = traceback.format_exception(None, e, e.__traceback__)
return trace[-1]

def get_error_message(e) -> str:
""" Get error message """
return str(e)

def get_trigger():
"""
Get the input that triggered a callback
Expand All @@ -47,3 +52,10 @@ def get_graph_info():
conn_str = rack.sparql_connection(BASE_URL, None, None, [], TRIPLE_STORE, TRIPLE_STORE_TYPE)
graph_info_table = semtk3.get_graph_info(conn_str, True, False) # True to exclude internal SemTK graphs, False to get counts too
return graph_info_table

def run_subprocess(command, status_filepath):
""" Launch a process using a given command, pipe output to a given file """
print("Running '" + command + "', sending output to " + status_filepath)
completed_process = subprocess.run(command + " > " + status_filepath + " 2>&1", shell=True, capture_output=True) # make this a helper function, consolidate with verify
print(completed_process) # useful to see exit code
return completed_process
5 changes: 2 additions & 3 deletions rack-ui/pages/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import time
import platform
import subprocess
from dash import html, dcc, callback, Input, Output, State
import dash_bootstrap_components as dbc
import semtk3
Expand Down Expand Up @@ -108,8 +107,8 @@ def run_assist(status_filepath):
if platform.system() == "Windows":
raise Exception("Not yet supported on Windows. (PROLOG checking is available through LINUX/Docker.)")
else:
# runs on all graphs in the triple store, minus an exclusion list of internal SemTK graphs (e.g. demo data)
subprocess.call("../assist/bin/check -v -m " + TRIPLE_STORE_BASE_URL + "/ > " + status_filepath + " 2>&1", shell=True)
command = f"../assist/bin/check -v -m {TRIPLE_STORE_BASE_URL}/" # ASSIST tool. Runs on all graphs, minus exclusion list of internal SemTK graphs
run_subprocess(command, status_filepath) # TODO returns error code 1 even though seems successful
time.sleep(1)

return [dcc.Markdown("Completed ASSIST verification.")], False
Expand Down