From 6f0a8187e4409bf894ce40e84e2a4d71139ad62f Mon Sep 17 00:00:00 2001 From: Hongwei Date: Wed, 6 Dec 2023 23:35:37 +0100 Subject: [PATCH] refactor/enhanced the performance and code clean --- apimanager/base/views.py | 12 - apimanager/locale/de/LC_MESSAGES/django.po | 6 +- apimanager/locale/es/LC_MESSAGES/django.po | 6 +- apimanager/locale/hi/LC_MESSAGES/django.po | 6 +- apimanager/metrics/forms.py | 2 +- .../templates/metrics/custom_summary.html | 12 +- .../templates/metrics/daily_summary.html | 12 +- .../templates/metrics/hourly_summary.html | 12 +- .../templates/metrics/monthly_summary.html | 12 +- .../templates/metrics/quarterly_summary.html | 12 +- .../templates/metrics/weekly_summary.html | 12 +- .../templates/metrics/yearly_summary.html | 12 +- apimanager/metrics/views.py | 228 ++++++------------ apimanager/obp/api.py | 7 - 14 files changed, 129 insertions(+), 222 deletions(-) diff --git a/apimanager/base/views.py b/apimanager/base/views.py index 058b70d9..59911e79 100644 --- a/apimanager/base/views.py +++ b/apimanager/base/views.py @@ -21,18 +21,6 @@ def get_banks(request): except APIError as err: messages.error(request, err) return [] -def get_consumers(request): - api = API(request.session.get('obp')) - try: - urlpath = '/management/consumers' - result = api.get(urlpath) - if 'consumers' in result: - return [consumer['consumer_id'] for consumer in sorted(result['consumers'], key=lambda d: d['consumer_id'])] - else: - return [] - except APIError as err: - messages.error(request, err) - return [] def get_api_versions(request): api = API(request.session.get('obp')) diff --git a/apimanager/locale/de/LC_MESSAGES/django.po b/apimanager/locale/de/LC_MESSAGES/django.po index 03ede848..481f951e 100644 --- a/apimanager/locale/de/LC_MESSAGES/django.po +++ b/apimanager/locale/de/LC_MESSAGES/django.po @@ -1075,12 +1075,12 @@ msgstr "Medianzeit von der Verbraucherregistrierung bis zum ersten API-Aufruf" #: metrics/templates/metrics/quarterly_summary.html:108 #: metrics/templates/metrics/weekly_summary.html:106 #: metrics/templates/metrics/yearly_summary.html:106 -msgid "Apps with distinct names" +msgid "New apps with distinct names" msgstr "Apps mit eindeutigen Namen" #: metrics/templates/metrics/custom_summary.html:115 #: metrics/templates/metrics/monthly_summary.html:118 -msgid "Distinct developer email addresses" +msgid "New distinct developer email addresses" msgstr "Unterschiedliche Entwickler-E-Mail-Adressen" #: metrics/templates/metrics/custom_summary.html:120 @@ -1192,7 +1192,7 @@ msgstr "Durchschnittliche Anzahl der Anrufe pro Stunde" #: metrics/templates/metrics/hourly_summary.html:110 #: metrics/templates/metrics/quarterly_summary.html:112 #: metrics/templates/metrics/weekly_summary.html:110 -msgid "Apps with distinct developer email addresses" +msgid "Apps with New distinct developer email addresses" msgstr "Apps mit unterschiedlichen Entwickler-E-Mail-Adressen" #: metrics/templates/metrics/hourly_summary.html:20 diff --git a/apimanager/locale/es/LC_MESSAGES/django.po b/apimanager/locale/es/LC_MESSAGES/django.po index a2ce611f..0e8b9a34 100644 --- a/apimanager/locale/es/LC_MESSAGES/django.po +++ b/apimanager/locale/es/LC_MESSAGES/django.po @@ -1412,12 +1412,12 @@ msgstr "" #: metrics/templates/metrics/quarterly_summary.html:115 #: metrics/templates/metrics/weekly_summary.html:111 #: metrics/templates/metrics/yearly_summary.html:113 -msgid "Apps with distinct names" +msgid "New apps with distinct names" msgstr "Aplicaciones con nombres distintos" #: metrics/templates/metrics/custom_summary.html:113 #: metrics/templates/metrics/monthly_summary.html:110 -msgid "Distinct developer email addresses" +msgid "New distinct developer email addresses" msgstr "Distintas direcciones de correo electrónico para desarrolladores" #: metrics/templates/metrics/custom_summary.html:118 @@ -1502,7 +1502,7 @@ msgstr "Número promedio de llamadas" #: metrics/templates/metrics/hourly_summary.html:117 #: metrics/templates/metrics/quarterly_summary.html:119 #: metrics/templates/metrics/weekly_summary.html:115 -msgid "Apps with distinct developer email addresses" +msgid "Apps with New distinct developer email addresses" msgstr "" "Aplicaciones con distintas direcciones de correo electrónico de los " "desarrolladores" diff --git a/apimanager/locale/hi/LC_MESSAGES/django.po b/apimanager/locale/hi/LC_MESSAGES/django.po index f28b8c80..53d974a6 100644 --- a/apimanager/locale/hi/LC_MESSAGES/django.po +++ b/apimanager/locale/hi/LC_MESSAGES/django.po @@ -1054,12 +1054,12 @@ msgstr "MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NE #: metrics/templates/metrics/quarterly_summary.html:108 #: metrics/templates/metrics/weekly_summary.html:106 #: metrics/templates/metrics/yearly_summary.html:106 -msgid "Apps with distinct names" +msgid "New apps with distinct names" msgstr "MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 00 HOURS 04 MINUTES 16 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE" #: metrics/templates/metrics/custom_summary.html:115 #: metrics/templates/metrics/monthly_summary.html:118 -msgid "Distinct developer email addresses" +msgid "New distinct developer email addresses" msgstr "MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 00 HOURS 04 MINUTES 16 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE" #: metrics/templates/metrics/custom_summary.html:120 @@ -1171,7 +1171,7 @@ msgstr "MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NE #: metrics/templates/metrics/hourly_summary.html:110 #: metrics/templates/metrics/quarterly_summary.html:112 #: metrics/templates/metrics/weekly_summary.html:110 -msgid "Apps with distinct developer email addresses" +msgid "Apps with New distinct developer email addresses" msgstr "MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 00 HOURS 04 MINUTES 02 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE" #: metrics/templates/metrics/hourly_summary.html:20 diff --git a/apimanager/metrics/forms.py b/apimanager/metrics/forms.py index 4a370912..53fd84ac 100644 --- a/apimanager/metrics/forms.py +++ b/apimanager/metrics/forms.py @@ -77,7 +77,7 @@ class APIMetricsForm(MetricsForm): consumer_id = forms.ChoiceField( label=_('Consumer ID'), - widget=forms.Select( + widget=forms.TextInput( attrs={ 'class': FORM_CONTROL, } diff --git a/apimanager/metrics/templates/metrics/custom_summary.html b/apimanager/metrics/templates/metrics/custom_summary.html index 0313f5c3..77e31152 100644 --- a/apimanager/metrics/templates/metrics/custom_summary.html +++ b/apimanager/metrics/templates/metrics/custom_summary.html @@ -85,17 +85,17 @@

{% trans "Custom Date Range " %}

{% trans "Average response time (ms)" %}: {{ average_response_time }} - - {% trans "Median time from consumer registration to first API call" %}: - {{ median_time_to_first_api_call }} - + {# #} +{# {% trans "Median time from consumer registration to first API call" %}:#} +{# {{ median_time_to_first_api_call }}#} +{# #} - {% trans "Apps with distinct names" %}: + {% trans "New apps with distinct names" %}: {{ number_of_apps_with_unique_app_name }} - {% trans "Distinct developer email addresses" %}: + {% trans "New distinct developer email addresses" %}: {{ number_of_apps_with_unique_developer_email }} diff --git a/apimanager/metrics/templates/metrics/daily_summary.html b/apimanager/metrics/templates/metrics/daily_summary.html index 17053b6a..4547c777 100644 --- a/apimanager/metrics/templates/metrics/daily_summary.html +++ b/apimanager/metrics/templates/metrics/daily_summary.html @@ -94,17 +94,17 @@

{% if include_app_names != "" %} {{ include_app_names }} : {% endif %}{{ fro {{ average_response_time }} - - {% trans "Median time from consumer registration to first API call" %}: - {{ median_time_to_first_api_call }} - + {# #} +{# {% trans "Median time from consumer registration to first API call" %}:#} +{# {{ median_time_to_first_api_call }}#} +{# #} - {% trans "Apps with distinct names" %}: + {% trans "New apps with distinct names" %}: {{ number_of_apps_with_unique_app_name }} - {% trans "Apps with distinct developer email addresses" %}: + {% trans "Apps with New distinct developer email addresses" %}: {{ number_of_apps_with_unique_developer_email }} diff --git a/apimanager/metrics/templates/metrics/hourly_summary.html b/apimanager/metrics/templates/metrics/hourly_summary.html index be0d6416..85963d43 100644 --- a/apimanager/metrics/templates/metrics/hourly_summary.html +++ b/apimanager/metrics/templates/metrics/hourly_summary.html @@ -96,17 +96,17 @@

{% if include_app_names != "" %} {{ include_app_names }} : {% endif %}{{ fro {{ average_response_time }} - - {% trans "Median time from consumer registration to first API call" %}: - {{ median_time_to_first_api_call }} - + {# #} +{# {% trans "Median time from consumer registration to first API call" %}:#} +{# {{ median_time_to_first_api_call }}#} +{# #} - {% trans "Apps with distinct names" %}: + {% trans "New apps with distinct names" %}: {{ number_of_apps_with_unique_app_name }} - {% trans "Apps with distinct developer email addresses" %}: + {% trans "Apps with New distinct developer email addresses" %}: {{ number_of_apps_with_unique_developer_email }} diff --git a/apimanager/metrics/templates/metrics/monthly_summary.html b/apimanager/metrics/templates/metrics/monthly_summary.html index d5d2724d..e39c7996 100644 --- a/apimanager/metrics/templates/metrics/monthly_summary.html +++ b/apimanager/metrics/templates/metrics/monthly_summary.html @@ -87,17 +87,17 @@

{% if include_app_names != "" %} {{ include_app_names }} : {% endif %}{{ fro {% trans "Average response time (ms)" %}: {{ average_response_time }} - - {% trans "Median time from consumer registration to first API call" %}: - {{ median_time_to_first_api_call }} - + {# #} +{# {% trans "Median time from consumer registration to first API call" %}:#} +{# {{ median_time_to_first_api_call }}#} +{# #} - {% trans "Apps with distinct names" %}: + {% trans "New apps with distinct names" %}: {{ number_of_apps_with_unique_app_name }} - {% trans "Distinct developer email addresses" %}: + {% trans "New distinct developer email addresses" %}: {{ number_of_apps_with_unique_developer_email }} diff --git a/apimanager/metrics/templates/metrics/quarterly_summary.html b/apimanager/metrics/templates/metrics/quarterly_summary.html index d65b1868..6c239eee 100644 --- a/apimanager/metrics/templates/metrics/quarterly_summary.html +++ b/apimanager/metrics/templates/metrics/quarterly_summary.html @@ -97,17 +97,17 @@

{% if include_app_names != "" %} {{ include_app_names }} : {% endif %}{{ fro {{ average_response_time }} - - {% trans "Median time from consumer registration to first API call" %}: - {{ median_time_to_first_api_call }} - + {# #} +{# {% trans "Median time from consumer registration to first API call" %}:#} +{# {{ median_time_to_first_api_call }}#} +{# #} - {% trans "Apps with distinct names" %}: + {% trans "New apps with distinct names" %}: {{ number_of_apps_with_unique_app_name }} - {% trans "Apps with distinct developer email addresses" %}: + {% trans "Apps with New distinct developer email addresses" %}: {{ number_of_apps_with_unique_developer_email }} diff --git a/apimanager/metrics/templates/metrics/weekly_summary.html b/apimanager/metrics/templates/metrics/weekly_summary.html index 78095708..077ae698 100644 --- a/apimanager/metrics/templates/metrics/weekly_summary.html +++ b/apimanager/metrics/templates/metrics/weekly_summary.html @@ -93,17 +93,17 @@

{% if include_app_names != "" %} {{ include_app_names }} : {% endif %}{{ fro {{ average_response_time }} - - {% trans "Median time from consumer registration to first API call" %}: - {{ median_time_to_first_api_call }} - +{# #} +{# {% trans "Median time from consumer registration to first API call" %}:#} +{# {{ median_time_to_first_api_call }}#} +{# #} - {% trans "Apps with distinct names" %}: + {% trans "New apps with distinct names" %}: {{ number_of_apps_with_unique_app_name }} - {% trans "Apps with distinct developer email addresses" %}: + {% trans "Apps with New distinct developer email addresses" %}: {{ number_of_apps_with_unique_developer_email }} diff --git a/apimanager/metrics/templates/metrics/yearly_summary.html b/apimanager/metrics/templates/metrics/yearly_summary.html index acc45e04..b2ce28a6 100644 --- a/apimanager/metrics/templates/metrics/yearly_summary.html +++ b/apimanager/metrics/templates/metrics/yearly_summary.html @@ -93,17 +93,17 @@

{% if include_app_names != "" %} {{ include_app_names }} : {% endif %}{{ fro {{ average_response_time }} - - {% trans "Median time from consumer registration to first API call" %}: - {{ median_time_to_first_api_call }} - + {# #} +{# {% trans "Median time from consumer registration to first API call" %}:#} +{# {{ median_time_to_first_api_call }}#} +{# #} - {% trans "Apps with distinct names" %}: + {% trans "New apps with distinct names" %}: {{ number_of_apps_with_unique_app_name }} - {% trans "Apps with distinct developer email addresses:" %} + {% trans "Apps with New distinct developer email addresses:" %} {{ number_of_apps_with_unique_developer_email }} diff --git a/apimanager/metrics/views.py b/apimanager/metrics/views.py index 1bac1c46..008a2647 100644 --- a/apimanager/metrics/views.py +++ b/apimanager/metrics/views.py @@ -22,8 +22,7 @@ from .forms import APIMetricsForm, ConnectorMetricsForm, MonthlyMetricsSummaryForm, CustomSummaryForm from pylab import * from django.core.cache import cache -from base.views import get_consumers, get_api_versions -import traceback + try: # Python 2 import cStringIO @@ -182,11 +181,9 @@ def dispatch(self, request, *args, **kwargs): def get_form(self, *args, **kwargs): form = super(APIMetricsView, self).get_form(*args, **kwargs) - # Cannot add api in constructor: super complains about unknown kwarg form.api = self.api fields = form.fields try: - fields['consumer_id'].choices = self.api.get_consumer_id_choices() fields['implemented_in_version'].choices = self.api.get_api_version_choices() except APIError as err: messages.error(self.request, err) @@ -194,14 +191,6 @@ def get_form(self, *args, **kwargs): messages.error(self.request, err) return form - def get_context_data(self, **kwargs): - context = super(APIMetricsView, self).get_context_data(**kwargs) - context.update({ - 'consumer_id': get_consumers(self.request), - 'API_VERSION': get_api_versions(self.request) - }) - return context - def get_metric_last_endpoint(request): to_date = datetime.datetime.now().strftime(settings.API_DATE_FORMAT_WITH_MILLISECONDS) urlpath = "/management/metrics?limit=1&to_date="+to_date @@ -313,13 +302,9 @@ def get_app_name_parameters(self, include_app_names): result = "" if input_string != "": input_list = input_string.strip().split(",") - #print("input_list is:", input_list) cleaned_list = [item.strip() for item in input_list] - #print("cleaned_list is: ", cleaned_list) cleaned_string=', '.join([str(item) for item in cleaned_list]) - #print("cleaned_string is:", cleaned_string) url_encoded_string = urllib.parse.quote(cleaned_string) - #print("url_encoded_string is:", url_encoded_string) if len(cleaned_list) == 0: result = "" elif len(cleaned_list) == 1: @@ -327,6 +312,7 @@ def get_app_name_parameters(self, include_app_names): else: result = "&include_app_names={}".format(url_encoded_string) return result + def get_aggregate_metrics(self, from_date, to_date, include_app_names): """ Gets the metrics from the API, using given parameters, @@ -334,44 +320,20 @@ def get_aggregate_metrics(self, from_date, to_date, include_app_names): only_show_api_explorer_metrics has the default value False, because it is just used for app = API_Explorer. """ try: - print("get_app_name_parameters is: ", self.get_app_name_parameters(include_app_names)) api_calls_total = 0 average_response_time = 0 - url_path = '/management/aggregate-metrics' - #if only_show_api_explorer_metrics: - # urlpath = urlpath + '?from_date={}&to_date={}&app_name={}'.format(from_date, to_date) - #elif (not only_show_api_explorer_metrics): - # urlpath = urlpath + '?from_date={}&to_date={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format( - # from_date, to_date, ",".join(local_settings.EXCLUDE_FUNCTIONS), ",".join(local_settings.EXCLUDE_URL_PATTERN)) - # - #else: - url_path = url_path + '?from_date={}&to_date={}{}'.format(from_date, to_date, self.get_app_name_parameters(include_app_names)) - #print("get_app_name_parameters(include_app_names) is:", self.get_app_name_parameters(include_app_names)) - #print("url_path is: ", url_path) - cache_key = get_cache_key_for_current_call(self.request, url_path) - api_cache = None - try: - api_cache = cache.get(cache_key) - except Exception as err: - api_cache = None - if not api_cache is None: - metrics = api_cache - else: - api = API(self.request.session.get('obp')) - metrics = api.get(url_path) - api_cache = cache.set(cache_key, metrics) - LOGGER.warning('{0}: {1}'.format(CACHE_SETTING_URL_MSG, url_path)) - LOGGER.warning('{0}: {1}'.format(CACHE_SETTING_KEY_MSG, cache_key)) - + url_path = '/management/aggregate-metrics?from_date={}&to_date={}&include_app_names={}'.format(from_date, to_date, self.get_app_name_parameters(include_app_names)) + api = API(self.request.session.get('obp')) + metrics = api.get(url_path) api_calls_total, average_calls_per_day, average_response_time = self.get_internal_api_call_metrics( - api_calls_total, average_response_time, cache_key, from_date, metrics, to_date, url_path) + api_calls_total, average_response_time, from_date, metrics, to_date, url_path) return api_calls_total, average_response_time, int(average_calls_per_day) except APIError as err: error_once_only(self.request, err) except Exception as err: error_once_only(self.request, err) - def get_internal_api_call_metrics(self, api_calls_total, average_response_time, cache_key, from_date, metrics, + def get_internal_api_call_metrics(self, api_calls_total, average_response_time, from_date, metrics, to_date, urlpath): api_calls_total = metrics[0]["count"] average_response_time = metrics[0]["average_response_time"] @@ -406,30 +368,14 @@ def get_active_apps(self, from_date, to_date): def get_total_number_of_apps(self, cleaned_data, from_date, to_date): - apps = [] - from_date = datetime.datetime.strptime(from_date, API_DATE_FORMAT_WITH_MILLISECONDS) - to_date = datetime.datetime.strptime(to_date, API_DATE_FORMAT_WITH_MILLISECONDS) - apps_list = self.get_all_consumers() - - for app in apps_list: - app_created_date = datetime.datetime.strptime(app["created"], API_DATE_FORMAT_WITH_SECONDS ) - - if app_created_date < from_date and app_created_date > to_date: - apps_list.remove(app) - + apps_list = self.get_all_consumers(from_date, to_date) app_names = [] + apps = [] for apps in apps_list: app_names.append(apps["app_name"]) - # If include OBP Apps is selected - #if not cleaned_data.get('include_obp_apps'): - # for app in app_names: - # if app in local_settings.EXCLUDE_APPS: - # app_names.remove(app) - app_names = list(filter(None, app_names)) - unique_app_names = list(set(app_names)) developer_emails = [] @@ -444,28 +390,16 @@ def get_total_number_of_apps(self, cleaned_data, from_date, to_date): return unique_app_names, number_of_apps_with_unique_app_name, number_of_apps_with_unique_developer_email - def get_all_consumers(self): - urlpath = '/management/consumers' + def get_all_consumers(self, from_date, to_date): + urlpath = '/management/consumers?limit=1000000&from_date={}&to_date={}'.format(from_date, to_date) api = API(self.request.session.get('obp')) - cache_key = get_cache_key_for_current_call(self.request, urlpath) - api_cache = None try: - api_cache = cache.get(cache_key) + apps = api.get(urlpath) + apps_list = apps["consumers"] + except APIError as err: + error_once_only(self.request, err) except Exception as err: - api_cache = None - if api_cache is not None: - apps_list = api_cache - else: - try: - apps = api.get(urlpath) - apps_list = apps["consumers"] - cache.set(cache_key, apps_list, 60 * 60) # for the consumers we cache for 1 hour, consumers may be increased - LOGGER.warning('{0}: {1}'.format(CACHE_SETTING_URL_MSG, urlpath)) - LOGGER.warning('{0}: {1}'.format(CACHE_SETTING_KEY_MSG, cache_key)) - except APIError as err: - error_once_only(self.request, err) - except Exception as err: - error_once_only(self.request, err) + error_once_only(self.request, err) return apps_list def calls_per_delta(self, from_date_string, to_date_string, include_app_names, **delta ): @@ -499,7 +433,6 @@ def calls_per_delta(self, from_date_string, to_date_string, include_app_names, * from_datetime_object = time_delta_in_loop time_delta_in_loop = time_delta_in_loop + timedelta(**delta) - print("time_delta_in_loop in **delta is", time_delta_in_loop) return (result_list, result_list_pure, date_list) @@ -768,76 +701,71 @@ def get_top_apps_using_warehouse(self, from_date, to_date): return top_apps_using_warehouse - def median_time_to_first_api_call(self, from_date, to_date): - return 0 #TODO this cost too much time, do not use this at the moment. - form = self.get_form() - form = self.get_form() - new_apps_list = [] - apps = [] - apps_list = self.get_all_consumers() - - for app in apps_list: - created_date = datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ') - created_date = created_date.strftime(API_DATE_FORMAT_WITH_MILLISECONDS) - created_date = datetime.datetime.strptime(created_date, API_DATE_FORMAT_WITH_MILLISECONDS) - if created_date >= datetime.datetime.strptime(from_date, API_DATE_FORMAT_WITH_MILLISECONDS): - new_apps_list.append(app) - - times_to_first_call = [] - - strfrom_date=datetime.datetime.strptime(from_date, API_DATE_FORMAT_WITH_MILLISECONDS) - strto_date=datetime.datetime.strptime(to_date, API_DATE_FORMAT_WITH_MILLISECONDS) - for app in new_apps_list: - urlpath_metrics = '/management/metrics?from_date={}&to_date={}&consumer_id={}&sort_by={}&direction={}&limit={}'.format( - from_date, to_date, app['consumer_id'], 'date', 'asc', '1') - cache_key = get_cache_key_for_current_call(self.request, urlpath_metrics) - api = API(self.request.session.get('obp')) - try: - api_cache=None - try: - api_cache=cache.get(cache_key) - except Exception as err: - api_cache=None - metrics=[] - if not api_cache is None: - metrics=api_cache - else: - metrics = api.get(urlpath_metrics) - - if metrics is not None and 'code' in metrics and metrics['code'] == 403: - error_once_only(self.request, metrics['message']) - if(metrics['message'].startswith('OBP-20006')): - break - metrics = [] - else: - metrics = list(metrics['metrics']) - cache.set(cache_key, metrics) - LOGGER.warning('The cache is setting, url is: {}'.format(urlpath_metrics)) - LOGGER.warning('The cache is setting key is: {}'.format(cache_key)) - if metrics: - time_difference = datetime.datetime.strptime(metrics[0]['date'], '%Y-%m-%dT%H:%M:%S.%fZ') - datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ') - times_to_first_call.append(time_difference.total_seconds()) - - - except APIError as err: - error_once_only(self.request, err) - except Exception as err: - error_once_only(self.request, 'Unknown Error. {}'.format(err)) - - if times_to_first_call: - median = statistics.median(times_to_first_call) - delta = datetime.timedelta(seconds=median) - else: - delta = 0 - - return delta + # def median_time_to_first_api_call(self, from_date, to_date): + # return 0 #TODO this cost too much time, do not use this at the moment. + # strfrom_date=datetime.datetime.strptime(from_date, API_DATE_FORMAT_WITH_MILLISECONDS) + # strto_date=datetime.datetime.strptime(to_date, API_DATE_FORMAT_WITH_MILLISECONDS) + # new_apps_list = [] + # apps_list = self.get_all_consumers(strfrom_date,strto_date) + # + # for app in apps_list: + # created_date = datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ') + # if created_date >= strfrom_date: + # new_apps_list.append(app) + # + # times_to_first_call = [] + # + # + # for app in new_apps_list: + # urlpath_metrics = '/management/metrics?from_date={}&to_date={}&consumer_id={}&sort_by={}&direction={}&limit={}'.format( + # from_date, to_date, app['consumer_id'], 'date', 'asc', '1') + # cache_key = get_cache_key_for_current_call(self.request, urlpath_metrics) + # api = API(self.request.session.get('obp')) + # try: + # api_cache=None + # try: + # api_cache=cache.get(cache_key) + # except Exception as err: + # api_cache=None + # metrics=[] + # if not api_cache is None: + # metrics=api_cache + # else: + # metrics = api.get(urlpath_metrics) + # + # if metrics is not None and 'code' in metrics and metrics['code'] == 403: + # error_once_only(self.request, metrics['message']) + # if(metrics['message'].startswith('OBP-20006')): + # break + # metrics = [] + # else: + # metrics = list(metrics['metrics']) + # cache.set(cache_key, metrics) + # LOGGER.warning('The cache is setting, url is: {}'.format(urlpath_metrics)) + # LOGGER.warning('The cache is setting key is: {}'.format(cache_key)) + # if metrics: + # time_difference = datetime.datetime.strptime(metrics[0]['date'], '%Y-%m-%dT%H:%M:%S.%fZ') - datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ') + # times_to_first_call.append(time_difference.total_seconds()) + # + # + # except APIError as err: + # error_once_only(self.request, err) + # except Exception as err: + # error_once_only(self.request, 'Unknown Error. {}'.format(err)) + # + # if times_to_first_call: + # median = statistics.median(times_to_first_call) + # delta = datetime.timedelta(seconds=median) + # else: + # delta = 0 + # + # return delta def get_context_data(self, **kwargs): return self.prepare_general_context(SummaryType.MONTHLY) def prepare_general_context(self, web_page_type, **kwargs): try: form = self.get_form() - print("form from get_form", form) per_day_chart=[] calls_per_month_list=[] per_month_chart=[] @@ -849,9 +777,7 @@ def prepare_general_context(self, web_page_type, **kwargs): #if exclude_app_names not in local_settings.EXCLUDE_APPS: # error_once_only(self.request, "Invalid Exclude App Name, Please select" + str(local_settings.EXCLUDE_APPS) + "Anyone of these") form_to_date_string = form.data['to_date'] - print(form.data, "Form data") to_date = convert_form_date_to_obpapi_datetime_format(form_to_date_string) - print("to_date", to_date) if (web_page_type == SummaryType.DAILY): # for one day, the from_date is 1 day ago. from_date = return_to_days_ago(to_date, 0) @@ -895,7 +821,7 @@ def prepare_general_context(self, web_page_type, **kwargs): api_host_name = API_HOST top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date) user_email_cansearchwarehouse, number_of_users_with_cansearchwarehouse = self.get_users_cansearchwarehouse() - median_time_to_first_api_call = self.median_time_to_first_api_call(from_date, to_date) + # median_time_to_first_api_call = self.median_time_to_first_api_call(from_date, to_date) top_apis = self.get_top_apis(form.cleaned_data, from_date, to_date) top_apis_bar_chart = self.plot_bar_chart(top_apis) @@ -932,7 +858,7 @@ def prepare_general_context(self, web_page_type, **kwargs): 'top_apis': top_apis, 'top_apis_bar_chart': top_apis_bar_chart, 'top_consumers_bar_chart': top_consumers_bar_chart, - 'median_time_to_first_api_call': median_time_to_first_api_call, + # 'median_time_to_first_api_call': median_time_to_first_api_call, #'excluded_apps':[exclude_app_names if exclude_app_names in local_settings.EXCLUDE_APPS else "null"], }) return context diff --git a/apimanager/obp/api.py b/apimanager/obp/api.py index 4f2b7aa8..3c75a3fa 100644 --- a/apimanager/obp/api.py +++ b/apimanager/obp/api.py @@ -160,13 +160,6 @@ def get_bank_id_choices(self): for bank in sorted(result['banks'], key=lambda d: d['id']) : choices.append((bank['id'], bank['id'])) return choices - def get_consumer_id_choices(self): - """Gets a list of Consumer ids and consumer ids as used by form choices""" - choices = [('', _('Choose ...'))] - result = self.get('/management/consumers') - for consumer in sorted(result['consumers'], key=lambda d: d['consumer_id']) : - choices.append((consumer['consumer_id'], consumer['consumer_id'])) - return choices def get_api_version_choices(self): """Gets a list of APIs Version and APIs Version as used by form choices"""