mirror of
https://github.com/OpenBankProject/API-Manager.git
synced 2026-02-06 10:59:00 +00:00
refactor/enhanced the performance and code clean
This commit is contained in:
parent
7030e3039a
commit
6f0a8187e4
@ -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'))
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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
|
||||
|
||||
@ -77,7 +77,7 @@ class APIMetricsForm(MetricsForm):
|
||||
|
||||
consumer_id = forms.ChoiceField(
|
||||
label=_('Consumer ID'),
|
||||
widget=forms.Select(
|
||||
widget=forms.TextInput(
|
||||
attrs={
|
||||
'class': FORM_CONTROL,
|
||||
}
|
||||
|
||||
@ -85,17 +85,17 @@
|
||||
<td>{% trans "Average response time (ms)" %}:</td>
|
||||
<td>{{ average_response_time }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Median time from consumer registration to first API call" %}:</td>
|
||||
<td>{{ median_time_to_first_api_call }}</td>
|
||||
</tr>
|
||||
{# <tr>#}
|
||||
{# <td>{% trans "Median time from consumer registration to first API call" %}:</td>#}
|
||||
{# <td>{{ median_time_to_first_api_call }}</td>#}
|
||||
{# </tr>#}
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct names" %}:</td>
|
||||
<td>{% trans "New apps with distinct names" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_app_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Distinct developer email addresses" %}:</td>
|
||||
<td>{% trans "New distinct developer email addresses" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_developer_email }}</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@ -94,17 +94,17 @@
|
||||
<td>{{ average_response_time }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Median time from consumer registration to first API call" %}:</td>
|
||||
<td>{{ median_time_to_first_api_call }}</td>
|
||||
</tr>
|
||||
{# <tr>#}
|
||||
{# <td>{% trans "Median time from consumer registration to first API call" %}:</td>#}
|
||||
{# <td>{{ median_time_to_first_api_call }}</td>#}
|
||||
{# </tr>#}
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct names" %}:</td>
|
||||
<td>{% trans "New apps with distinct names" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_app_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct developer email addresses" %}:</td>
|
||||
<td>{% trans "Apps with New distinct developer email addresses" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_developer_email }}</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@ -96,17 +96,17 @@
|
||||
<td>{{ average_response_time }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Median time from consumer registration to first API call" %}:</td>
|
||||
<td>{{ median_time_to_first_api_call }}</td>
|
||||
</tr>
|
||||
{# <tr>#}
|
||||
{# <td>{% trans "Median time from consumer registration to first API call" %}:</td>#}
|
||||
{# <td>{{ median_time_to_first_api_call }}</td>#}
|
||||
{# </tr>#}
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct names" %}:</td>
|
||||
<td>{% trans "New apps with distinct names" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_app_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct developer email addresses" %}:</td>
|
||||
<td>{% trans "Apps with New distinct developer email addresses" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_developer_email }}</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@ -87,17 +87,17 @@
|
||||
<td>{% trans "Average response time (ms)" %}:</td>
|
||||
<td>{{ average_response_time }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Median time from consumer registration to first API call" %}:</td>
|
||||
<td>{{ median_time_to_first_api_call }}</td>
|
||||
</tr>
|
||||
{# <tr>#}
|
||||
{# <td>{% trans "Median time from consumer registration to first API call" %}:</td>#}
|
||||
{# <td>{{ median_time_to_first_api_call }}</td>#}
|
||||
{# </tr>#}
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct names" %}:</td>
|
||||
<td>{% trans "New apps with distinct names" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_app_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Distinct developer email addresses" %}:</td>
|
||||
<td>{% trans "New distinct developer email addresses" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_developer_email }}</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@ -97,17 +97,17 @@
|
||||
<td>{{ average_response_time }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Median time from consumer registration to first API call" %}:</td>
|
||||
<td>{{ median_time_to_first_api_call }}</td>
|
||||
</tr>
|
||||
{# <tr>#}
|
||||
{# <td>{% trans "Median time from consumer registration to first API call" %}:</td>#}
|
||||
{# <td>{{ median_time_to_first_api_call }}</td>#}
|
||||
{# </tr>#}
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct names" %}:</td>
|
||||
<td>{% trans "New apps with distinct names" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_app_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct developer email addresses" %}:</td>
|
||||
<td>{% trans "Apps with New distinct developer email addresses" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_developer_email }}</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@ -93,17 +93,17 @@
|
||||
<td>{{ average_response_time }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Median time from consumer registration to first API call" %}:</td>
|
||||
<td>{{ median_time_to_first_api_call }}</td>
|
||||
</tr>
|
||||
{# <tr>#}
|
||||
{# <td>{% trans "Median time from consumer registration to first API call" %}:</td>#}
|
||||
{# <td>{{ median_time_to_first_api_call }}</td>#}
|
||||
{# </tr>#}
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct names" %}:</td>
|
||||
<td>{% trans "New apps with distinct names" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_app_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct developer email addresses" %}:</td>
|
||||
<td>{% trans "Apps with New distinct developer email addresses" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_developer_email }}</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@ -93,17 +93,17 @@
|
||||
<td>{{ average_response_time }}</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Median time from consumer registration to first API call" %}:</td>
|
||||
<td>{{ median_time_to_first_api_call }}</td>
|
||||
</tr>
|
||||
{# <tr>#}
|
||||
{# <td>{% trans "Median time from consumer registration to first API call" %}:</td>#}
|
||||
{# <td>{{ median_time_to_first_api_call }}</td>#}
|
||||
{# </tr>#}
|
||||
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct names" %}:</td>
|
||||
<td>{% trans "New apps with distinct names" %}:</td>
|
||||
<td>{{ number_of_apps_with_unique_app_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{% trans "Apps with distinct developer email addresses:" %}</td>
|
||||
<td>{% trans "Apps with New distinct developer email addresses:" %}</td>
|
||||
<td>{{ number_of_apps_with_unique_developer_email }}</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@ -22,8 +22,7 @@ from obp.api import API, APIError, LOGGER
|
||||
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 @@ class APIMetricsView(MetricsView):
|
||||
|
||||
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 @@ class APIMetricsView(MetricsView):
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
|
||||
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
#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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
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 @@ class MonthlyMetricsSummaryView(LoginRequiredMixin, TemplateView):
|
||||
'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
|
||||
|
||||
@ -160,13 +160,6 @@ class API(object):
|
||||
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"""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user