Skip to content

Commit

Permalink
#62 Added ModelViewerUrlProvider, integrated into html renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
banko-marton committed Sep 30, 2021
1 parent 43d0fdf commit f34932c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
17 changes: 3 additions & 14 deletions source/incqueryserver-jupyter/iqs_jupyter/analysis_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def _monkey_patch_analysis_result_repr_html(self : iqs_client.AnalysisResult, he
param_headers = " ".join(["<th>{}</th> ".format(html.escape(param)) for param in pattern_params])
match_rows = "\n".join([
"<tr><th>{}</th><td>{}</td>{}</tr>\n".format(row_index, self.matches[row_index].message, " ".join([
" <td>{} <a href=\"{}\">LINK</a></td>".format(_cell_to_html(_dict_to_element(argument_value.value)), _create_model_viewer_link(argument_value.value['element'])) # copartmentURI és relativeElementID
" <td><a href=\"{}\">{}</a></td>".format(
_dict_to_element(argument_value.value).url,
_cell_to_html(_dict_to_element(argument_value.value)))
for argument_value in self.matches[row_index].matching_elements
])) for row_index in range(len(self.matches))
])
Expand All @@ -170,19 +172,6 @@ def _monkey_patch_analysis_result_repr_html(self : iqs_client.AnalysisResult, he
</div>
'''.format(header_report, style, param_headers, match_rows)

from iqs_jupyter.authentication import IQSConnectorWidget
import requests

def _create_model_viewer_link(element):
# iqs_host = self.configuration
# req = requests.get(iqs_host+"/api/web-console-config")
# start = req.json()["modelViewerBaseURL"]
start = "127.0.0.1:8080/"
compURI = element['compartmentURI']
relElementID = element['relativeElementID']

return start + '?compartmentURI='+compURI+'&elementId='+relElementID

def _do_monkey_patching():
iqs_client.AnalysisResults._repr_html_ = _monkey_patch_analysis_results_repr_html
iqs_client.AnalysisResult._repr_html_ = _monkey_patch_analysis_result_repr_html
Expand Down
38 changes: 38 additions & 0 deletions source/incqueryserver-jupyter/iqs_jupyter/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def connect(
configuration.access_token = token
return IQSClient(configuration, use_oidc)

import requests

class IQSClient:
def __init__(
Expand All @@ -54,6 +55,43 @@ def __init__(
api_composition.decorate_iqs_client(self, root_configuration, ApiClient)
self.jupyter_tools = ext_point.IQSJupyterTools(self)

# Acquiring Model Viewer base link from host through internal API
req = requests.get(f"http://{root_configuration.host}/api/web-console-config")

# Creating ModelViwer url holder to persist link
self.mv_url = ModelViewerUrlProvider(self, req.json()["modelViewerBaseURL"])

# Url provider registering, a function to provide Model Viewer links
ext_point.url_providers.append(self.mv_url.as_url_provider())
ext_point.url_providers.append(ModelViewerUrlProvider(self, req.json()["modelViewerBaseURL"]))

class ModelViewerUrlProvider:
def __init__(self, iqs, mv_address=None):
self.mv_address = mv_address
self.iqs = iqs

def __call__(self, element):
if "compartmentURI" in element and "relativeElementID" in element:
# Creating ModelViewer Link
compURI = element['compartmentURI']
relElementID = element['relativeElementID']
return f"{self.mv_address}?compartmentURI={compURI}&elementId={relElementID}"
else:
return None

def as_url_provider(self):
def url_provider(element_descriptor):
return self.element_api_link(element_descriptor)
return url_provider

def element_api_link(self, element):
if "compartmentURI" in element and "relativeElementID" in element:
# Creating ModelViewer Link
compURI = element['compartmentURI']
relElementID = element['relativeElementID']
return f"{self.mv_address}?compartmentURI={compURI}&elementId={relElementID}"
else:
return None

class IQSConnectorWidget:
def __init__(
Expand Down
4 changes: 1 addition & 3 deletions source/incqueryserver-jupyter/iqs_jupyter/core_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ def _recognize_typed_element_in_compartment_descriptor(
ext_point.element_dict_recognizers.append(_recognize_typed_element_in_compartment_descriptor)




## monkey patch section

def _monkey_patch_element_in_compartment_descriptor_repr_html(self):
Expand Down Expand Up @@ -250,7 +248,7 @@ def _monkey_patch_query_execution_response_to_html(self):
param_headers = " ".join(["<th>{}</th> ".format(html.escape(param)) for param in pattern_params])
match_rows = "\n".join([
"<tr><th>{}</th>{}</tr>\n".format(row_index," ".join([
" <td>{}</td>".format(_cell_to_html(match_list[row_index][param])) for param in pattern_params
" <td><a href=\"{}\">{}</a></td>".format(_cell_to_html(match_list[row_index][param]).url, _cell_to_html(match_list[row_index][param])) for param in pattern_params
])) for row_index in range(len(match_list))
])

Expand Down
22 changes: 18 additions & 4 deletions source/incqueryserver-jupyter/iqs_jupyter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,30 @@ def cell_to_html(cell):
else:
return str(cell)

def dict_to_element(dict_or_attribute_value, url_provider=None):
def dict_to_element(dict_or_attribute_value):
if isinstance(dict_or_attribute_value, dict):
for recognizer in ext_point.element_dict_recognizers:
recognized = recognizer(dict_or_attribute_value)
if recognized:
if url_provider is not None:
recognized.url = url_provider(recognized)
for url_provider in ext_point.url_providers:
if url_provider(dict_or_attribute_value['element']) is not None: # maybe we should set url param to None - else
setattr(recognized, 'url', url_provider(dict_or_attribute_value['element']))
return recognized
# not recognized as an element, treated as raw dict
# not recognized as an element, treated as raw dict
return dict_or_attribute_value
else:
return dict_or_attribute_value

# def dict_to_element(dict_or_attribute_value, url_provider=None):
# if isinstance(dict_or_attribute_value, dict):
# for recognizer in ext_point.element_dict_recognizers:
# recognized = recognizer(dict_or_attribute_value)
# if recognized:
# if url_provider is not None:
# recognized.url = url_provider(recognized)
# return recognized
# # not recognized as an element, treated as raw dict
# return dict_or_attribute_value
# else:
# return dict_or_attribute_value

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ def __init__(
self._iqs = iqs


# connector-specific extensions may add custom ways to convert a dict to an element representation;
# connector-specific extensions may add custom ways to convert a dict to an element representation;
# parse a dict and return the constructed element or None if not the right format
# default is ElementInCompartmentDescriptor
# default is ElementInCompartmentDescriptor
element_dict_recognizers : List[Callable[[dict], Optional[Any]]] = []


url_providers: List[Callable[[Any], Optional[Any]]] = []

0 comments on commit f34932c

Please sign in to comment.