diff --git a/seeq/addons/correlation/__init__.py b/seeq/addons/correlation/__init__.py index e3a0194..a7b84e9 100644 --- a/seeq/addons/correlation/__init__.py +++ b/seeq/addons/correlation/__init__.py @@ -1,5 +1,5 @@ from ._version import __version__ -from ._seeq_formulas import correlation_udfs, signals_from_formula +from ._seeq_formulas import correlation_udfs, signals_from_formula, create_udfs from ._preprocessor import _validate_df, default_preprocessing_wrapper from ._cross_correlations import cross_corr_matrix_raw, cross_corr_matrix_lagged, lags_coeffs from ._heatmap import _heatmap_plot, heatmap @@ -13,4 +13,4 @@ '_heatmap', 'create_worksheet', '_create_worksheet', 'get_workbook', 'worksheet_with_lagged_signals', 'create_lagged_signals', 'pairplot', 'worksheet_corrs_and_time_shifts', 'correlation_udfs', 'signals_from_formula', 'CorrelationHeatmap', '_validate_df', 'lags_coeffs', - 'default_preprocessing_wrapper', 'utils'] + 'default_preprocessing_wrapper', 'utils', 'create_udfs'] diff --git a/seeq/addons/correlation/__main__.py b/seeq/addons/correlation/__main__.py index 137b712..48e1ae0 100644 --- a/seeq/addons/correlation/__main__.py +++ b/seeq/addons/correlation/__main__.py @@ -11,14 +11,12 @@ # noinspection PyProtectedMember from seeq.spy import _url from ._copy import copy -from .utils import get_user, get_user_group, get_seeq_url -from . import correlation_udfs +from .utils import get_seeq_url, DEFAULT_GROUP, DEFAULT_USERS +from . import create_udfs NB_EXTENSIONS = ['widgetsnbextension', 'ipyvuetify', 'ipyvue'] DEPLOYMENT_FOLDER = 'deployment' CORRELATION_NOTEBOOK = "correlation_analysis_master.ipynb" -DEFAULT_GROUP = ['Everyone'] -DEFAULT_USERS = [] def install_app(sdl_url_, *, sort_key='a', permissions_group: list = None, permissions_users: list = None): @@ -121,52 +119,6 @@ def logging_attempts(_user): if count > allowed_attempts: raise RuntimeError("Number of login attempts exceeded") - -def create_udfs(api_client, *, permissions_groups: list = None, permissions_users: list = None): - """ - Creates the required Formula UDFs for the Correlation app - - Parameters - ---------- - api_client: seeq.sdk.api_client.ApiClient - The seeq.sdk API client that handles the client-server - communication - permissions_groups: list - Names of the Seeq groups that will have access to each tool - permissions_users: list - Names of Seeq users that will have access to each tool - Returns - -------- - -: None - The Correlation UDFs will be available in Seeq Workbench - """ - - permissions_groups = permissions_groups if permissions_groups else DEFAULT_GROUP - permissions_users = permissions_users if permissions_users else DEFAULT_USERS - print("\n\nCreating CrossCorrelation UDFs...") - user_groups_api = sdk.UserGroupsApi(api_client) - users_api = sdk.UsersApi(spy.client) - items_api = sdk.ItemsApi(api_client) - pkg_id = correlation_udfs(api_client) - - # assign group permissions - for group_name in permissions_groups: - group = get_user_group(group_name, user_groups_api) - if group: - ace_input = sdk.AceInputV1(identity_id=group.items[0].id, permissions=sdk.PermissionsV1(read=True)) - items_api.add_access_control_entry(id=pkg_id, body=ace_input) - - # assign user permissions - for user_name in permissions_users: - current_user = get_user(user_name, users_api) - if current_user: - ace_input = sdk.AceInputV1(identity_id=current_user.users[0].id, - permissions=sdk.PermissionsV1(read=True)) - items_api.add_access_control_entry(id=pkg_id, body=ace_input) - - print("DONE") - - def cli_interface(): """ Command line utility to install the Correlation Add-on Tool """ parser = argparse.ArgumentParser(description='Install Correlation as a Seeq Add-on Tool') diff --git a/seeq/addons/correlation/_seeq_formulas.py b/seeq/addons/correlation/_seeq_formulas.py index 84e72b9..e49d534 100644 --- a/seeq/addons/correlation/_seeq_formulas.py +++ b/seeq/addons/correlation/_seeq_formulas.py @@ -1,6 +1,7 @@ import textwrap -from seeq import sdk +from seeq import sdk, spy from seeq.sdk.rest import ApiException +from .utils import check_udf_package, get_user_group, get_user, DEFAULT_USERS, DEFAULT_GROUP pearson_formula = textwrap.dedent( """ @@ -163,16 +164,10 @@ def correlation_udfs(api_client): creator_name = 'Alberto Rivas' creator_contact_info = 'applied.research@seeq.com' formulas_api = sdk.FormulasApi(api_client) - try: - pkg = formulas_api.get_package(package_name=package_name) - if pkg.name == package_name: - print(f"Overwriting CrossCorrelation package") - formulas_api.delete_package(package_name=package_name) - except ApiException as e: - if 'not found' in e.reason.lower(): - pass - else: - raise e + found = check_udf_package(package_name, api_client) + if found: + print(f"Overwriting CrossCorrelation package") + formulas_api.delete_package(package_name=package_name) # Create the Formula Package package_input = sdk.FormulaPackageInputV1(creator_name=creator_name, creator_contact_info=creator_contact_info) @@ -290,3 +285,48 @@ def signals_from_formula(signal1_id, signal_ref_id, workbook_id, formula_type=No ) r = signals_api.create_signal_with_http_info(body=payload)[0] return r + + +def create_udfs(api_client, *, permissions_groups: list = None, permissions_users: list = None): + """ + Creates the required Formula UDFs for the Correlation app + + Parameters + ---------- + api_client: seeq.sdk.api_client.ApiClient + The seeq.sdk API client that handles the client-server + communication + permissions_groups: list + Names of the Seeq groups that will have access to each tool + permissions_users: list + Names of Seeq users that will have access to each tool + Returns + -------- + -: None + The Correlation UDFs will be available in Seeq Workbench + """ + + permissions_groups = permissions_groups if permissions_groups else DEFAULT_GROUP + permissions_users = permissions_users if permissions_users else DEFAULT_USERS + print("\n\nCreating CrossCorrelation UDFs...") + user_groups_api = sdk.UserGroupsApi(api_client) + users_api = sdk.UsersApi(spy.client) + items_api = sdk.ItemsApi(api_client) + pkg_id = correlation_udfs(api_client) + + # assign group permissions + for group_name in permissions_groups: + group = get_user_group(group_name, user_groups_api) + if group: + ace_input = sdk.AceInputV1(identity_id=group.items[0].id, permissions=sdk.PermissionsV1(read=True)) + items_api.add_access_control_entry(id=pkg_id, body=ace_input) + + # assign user permissions + for user_name in permissions_users: + current_user = get_user(user_name, users_api) + if current_user: + ace_input = sdk.AceInputV1(identity_id=current_user.users[0].id, + permissions=sdk.PermissionsV1(read=True)) + items_api.add_access_control_entry(id=pkg_id, body=ace_input) + + print("DONE") diff --git a/seeq/addons/correlation/_version.py b/seeq/addons/correlation/_version.py index f7c995b..bd5418d 100644 --- a/seeq/addons/correlation/_version.py +++ b/seeq/addons/correlation/_version.py @@ -1 +1 @@ -__version__ = '0.1.98' +__version__ = '0.1.99' diff --git a/seeq/addons/correlation/deployment_notebook/correlation_analysis_master.ipynb b/seeq/addons/correlation/deployment_notebook/correlation_analysis_master.ipynb index 85de219..9c0a7ba 100644 --- a/seeq/addons/correlation/deployment_notebook/correlation_analysis_master.ipynb +++ b/seeq/addons/correlation/deployment_notebook/correlation_analysis_master.ipynb @@ -8,23 +8,35 @@ }, "outputs": [], "source": [ - "from seeq.addons import correlation\n" + "from seeq.addons import correlation\n", + "from seeq import spy" ] }, { "cell_type": "code", "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "C = correlation.CorrelationHeatmap(sdl_notebook_url=jupyter_notebook_url)\n", - "C.run()\n" - ], + "found = correlation.utils.check_udf_package()\n", + "if not found:\n", + " correlation.create_udfs(spy.client)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } - } + }, + "outputs": [], + "source": [ + "C = correlation.CorrelationHeatmap(sdl_notebook_url=jupyter_notebook_url)\n", + "C.run()\n" + ] } ], "metadata": { @@ -48,4 +60,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} \ No newline at end of file +} diff --git a/seeq/addons/correlation/utils/__init__.py b/seeq/addons/correlation/utils/__init__.py index 54ff43d..469b959 100644 --- a/seeq/addons/correlation/utils/__init__.py +++ b/seeq/addons/correlation/utils/__init__.py @@ -1,10 +1,12 @@ from ._common import validate_argument_types, print_red, path_list_to_string, path_string_to_list from ._cache_management import clear_cache_all -from ._permissions import get_user, get_user_group -from ._sdl import pull_only_signals, get_worksheet_url, get_workbook_worksheet_workstep_ids, get_seeq_url +from ._permissions import get_user, get_user_group, DEFAULT_USERS, DEFAULT_GROUP +from ._sdl import (pull_only_signals, get_worksheet_url, get_workbook_worksheet_workstep_ids, get_seeq_url, + check_udf_package) from ._seeq_new_content import create_condition, create_workstep_signals __all__ = ['validate_argument_types', 'print_red', 'create_condition', 'create_workstep_signals', 'get_user', 'get_user_group', 'pull_only_signals', 'get_worksheet_url', 'get_workbook_worksheet_workstep_ids', - 'clear_cache_all', 'get_seeq_url', 'path_list_to_string', 'path_string_to_list'] + 'clear_cache_all', 'get_seeq_url', 'path_list_to_string', 'path_string_to_list', 'check_udf_package', + 'DEFAULT_USERS', 'DEFAULT_GROUP'] diff --git a/seeq/addons/correlation/utils/_permissions.py b/seeq/addons/correlation/utils/_permissions.py index 142aaee..3797538 100644 --- a/seeq/addons/correlation/utils/_permissions.py +++ b/seeq/addons/correlation/utils/_permissions.py @@ -2,6 +2,10 @@ from . import print_red +DEFAULT_GROUP = ['Everyone'] +DEFAULT_USERS = [] + + def get_user_group(group_name, user_groups_api): try: group = user_groups_api.get_user_groups(name_search=group_name) diff --git a/seeq/addons/correlation/utils/_sdl.py b/seeq/addons/correlation/utils/_sdl.py index 016f96d..740466d 100644 --- a/seeq/addons/correlation/utils/_sdl.py +++ b/seeq/addons/correlation/utils/_sdl.py @@ -1,5 +1,6 @@ import pandas as pd -from seeq import spy +from seeq import spy, sdk +from seeq.sdk.rest import ApiException from urllib.parse import urlparse, unquote, parse_qs import ipaddress @@ -95,3 +96,17 @@ def get_seeq_url(): return spy.session.private_url return None + +def check_udf_package(name="CrossCorrelations", api_client=None): + if api_client: + formulas_api = sdk.FormulasApi(api_client) + else: + formulas_api = sdk.FormulasApi(spy.client) + try: + formulas_api.get_package(package_name=name) + return True + except ApiException as e: + if 'not found' in e.reason.lower(): + return False + else: + raise e