From 22e1378e795ef8e7c470de9ece5281cbe536ce0d Mon Sep 17 00:00:00 2001 From: Hongwei Date: Wed, 6 Dec 2023 15:01:16 +0100 Subject: [PATCH] refactor/removed the duplicated rest calls --- apimanager/metrics/views.py | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/apimanager/metrics/views.py b/apimanager/metrics/views.py index fc4fa29f..d99f331d 100644 --- a/apimanager/metrics/views.py +++ b/apimanager/metrics/views.py @@ -389,30 +389,19 @@ def get_active_apps(self, from_date, to_date): apps = [] form = self.get_form() active_apps_list = [] - urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}'.format(from_date, to_date) + urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( + from_date, to_date, ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) api = API(self.request.session.get('obp')) try: apps = api.get(urlpath) if apps is not None and 'code' in apps and apps['code']==403: error_once_only(self.request, apps['message']) else: - active_apps_list = list(apps) + active_apps_list = list(apps['top_consumers']) except APIError as err: error_once_only(self.request, err) except Exception as err: error_once_only(self.request, err) - else: - urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( - from_date, to_date, ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) - api = API(self.request.session.get('obp')) - try: - apps = api.get(urlpath) - active_apps_list = list(apps['top_consumers']) - except APIError as err: - error_once_only(self.request, err) - except Exception as err: - error_once_only(self.request, err) - return active_apps_list @@ -718,12 +707,7 @@ def _api_data(self, urlpath, data_key): def get_top_apis(self, cleaned_data, from_date, to_date): top_apis = [] - #if cleaned_data.get('include_obp_apps'): - # urlpath = '/management/metrics/top-apis?from_date={}&to_date={}'.format(from_date, to_date) - #else: - # urlpath = '/management/metrics/top-apis?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( - # from_date, to_date, ",".join(local_settings.EXCLUDE_APPS), ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) - urlpath = '/management/metrics/top-apis?from_date={}&to_date={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( + urlpath = '/management/metrics/top-apis?limit=10&from_date={}&to_date={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( from_date, to_date, ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) top_apis = self._api_data(urlpath, 'top_apis') @@ -739,12 +723,7 @@ def get_top_apis(self, cleaned_data, from_date, to_date): def get_top_consumers(self, cleaned_data, from_date, to_date): top_consumers = [] - #if cleaned_data.get('include_obp_apps'): - # urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}'.format(from_date, to_date) - #else: - # urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( - # from_date, to_date, ",".join(local_settings.EXCLUDE_APPS), ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) - urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( + urlpath = '/management/metrics/top-consumers?limit=10&from_date={}&to_date={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( from_date, to_date, ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN)) top_consumers = self._api_data(urlpath, 'top_consumers') @@ -752,7 +731,6 @@ def get_top_consumers(self, cleaned_data, from_date, to_date): if consumer['app_name'] == "": top_consumers.remove(consumer) - top_consumers = top_consumers[:10] top_consumers = reversed(top_consumers) return top_consumers