Skip to content

Commit

Permalink
Error handling for KPI Dashboard of API Manager if CanGetConsumers, C…
Browse files Browse the repository at this point in the history
…anGetAnyUser is not present OpenBankProject#65
  • Loading branch information
PengfeiLi0218 committed Jan 29, 2019
1 parent c515a82 commit e349d64
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apimanager/config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def get_context_data(self, **kwargs):
except APIError as err:
messages.error(self.request, err)
config = {}
except:
messages.error(self.request, "Unknown Error")
config = {}

context.update({
'config_json': json.dumps(config, indent=4),
Expand Down
9 changes: 9 additions & 0 deletions apimanager/consumers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def get_context_data(self, **kwargs):
consumers = self.scrub(consumers)
except APIError as err:
messages.error(self.request, err)
except:
messages.error(self.request, "Unknown")

sorted_consumers = sorted(
consumers, key=lambda consumer: consumer['created'], reverse=True)
Expand Down Expand Up @@ -128,6 +130,9 @@ def form_valid(self, form):
except APIError as err:
messages.error(self.request, err)
return super(DetailView, self).form_invalid(form)
except:
messages.error(self.request, "Unknown")
return super(DetailView, self).form_invalid(form)

msg = 'calls limit of consumer {} has been updated successfully.'.format(
data['consumer_id'])
Expand All @@ -154,6 +159,8 @@ def get_context_data(self, **kwargs):

except APIError as err:
messages.error(self.request, err)
except:
messages.error(self.request, "Unknown")

context.update({
'consumer': consumer
Expand All @@ -175,6 +182,8 @@ def get_redirect_url(self, *args, **kwargs):
messages.success(self.request, self.success)
except APIError as err:
messages.error(self.request, err)
except:
messages.error(self.request, "Unknown")

urlpath = self.request.POST.get('next', reverse('consumers-index'))
query = self.request.GET.urlencode()
Expand Down
5 changes: 5 additions & 0 deletions apimanager/customers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def get_form(self, *args, **kwargs):
fields['bank_id'].choices = self.api.get_bank_id_choices()
except APIError as err:
messages.error(self.request, err)
except:
messages.error(self.request, "Unknown Error")
fields['last_ok_date'].initial =\
datetime.datetime.now().strftime(settings.API_DATETIMEFORMAT)
return form
Expand Down Expand Up @@ -76,6 +78,9 @@ def form_valid(self, form):
except APIError as err:
messages.error(self.request, err)
return super(CreateView, self).form_invalid(form)
except:
messages.error(self.request, "Unknown Error")
return super(CreateView, self).form_invalid(form)
msg = 'Customer number {} for user {} has been created successfully!'.format( # noqa
result['customer_number'], data['username'])
messages.success(self.request, msg)
Expand Down
8 changes: 8 additions & 0 deletions apimanager/entitlementrequests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def get_context_data(self, **kwargs):
entitlement_requests = self.scrub(entitlement_requests)
except APIError as err:
messages.error(self.request, err)
except:
messages.error(self.request, "Unknown Error")

context.update({
'entitlementrequests': entitlement_requests,
Expand All @@ -60,6 +62,8 @@ def post(self, request, *args, **kwargs):
messages.success(request, msg)
except APIError as err:
messages.error(request, err)
except:
messages.error(self.request, "Unknown Error")

redirect_url = request.POST.get('next', reverse('entitlementrequests-index'))
return HttpResponseRedirect(redirect_url)
Expand All @@ -83,6 +87,8 @@ def post(self, request, *args, **kwargs):
messages.success(request, msg)
except APIError as err:
messages.error(request, err)
except:
messages.error(self.request, "Unknown Error")

try:
urlpath = '/entitlement-requests/{}'.format(request.POST.get('entitlement_request_id', '<undefined>'))
Expand All @@ -92,6 +98,8 @@ def post(self, request, *args, **kwargs):
messages.success(request, msg)
except APIError as err:
messages.error(request, err)
except:
messages.error(self.request, "Unknown Error")

redirect_url = request.POST.get('next', reverse('entitlementrequests-index'))
return HttpResponseRedirect(redirect_url)
30 changes: 30 additions & 0 deletions apimanager/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ def get_aggregate_metrics(self, cleaned_data, from_date, to_date):
average_response_time = metrics[0]["average_response_time"]
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

else:
urlpath = '/management/aggregate-metrics?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format(
Expand All @@ -277,6 +279,8 @@ def get_aggregate_metrics(self, cleaned_data, from_date, to_date):
average_response_time = metrics[0]["average_response_time"]
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')


to_date = datetime.datetime.strptime(to_date, API_DATEFORMAT)
Expand Down Expand Up @@ -324,6 +328,8 @@ def get_active_apps(self, cleaned_data, from_date, to_date):
active_apps_list = list(apps)
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')
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(EXCLUDE_APPS), ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN))
Expand All @@ -333,6 +339,8 @@ def get_active_apps(self, cleaned_data, from_date, to_date):
active_apps_list = list(apps)
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

return active_apps_list

Expand All @@ -349,6 +357,8 @@ def get_total_number_of_apps(self, cleaned_data, from_date, to_date):
apps_list = apps["list"]
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

for app in apps_list:
app_created_date = datetime.datetime.strptime(app["created"], '%Y-%m-%dT%H:%M:%SZ')
Expand Down Expand Up @@ -418,6 +428,8 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ):
sum += result
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

date_from = date_to
date_list.append(date_from)
Expand All @@ -435,6 +447,8 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ):
sum += result
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

date_from = date_to
date_list.append(date_from)
Expand Down Expand Up @@ -575,6 +589,8 @@ def get_users_cansearchwarehouse(self):
users = api.get(urlpath)
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

else:
try:
Expand All @@ -586,6 +602,8 @@ def get_users_cansearchwarehouse(self):
# fail gracefully in case API provides new structure
except KeyError as err:
messages.error(self.request, 'KeyError: {}'.format(err))
except:
error_once_only(self.request, 'Unknown Error')

user_email_cansearchwarehouse = dict(zip(users_with_cansearchwarehouse, email_with_cansearchwarehouse))
number_of_users_with_cansearchwarehouse = len(user_email_cansearchwarehouse)
Expand All @@ -601,6 +619,8 @@ def get_top_apis(self, cleaned_data, from_date, to_date):
top_apis = api.get(urlpath)['top_apis']
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')
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(EXCLUDE_APPS), ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN))
Expand All @@ -609,6 +629,8 @@ def get_top_apis(self, cleaned_data, from_date, to_date):
top_apis = api.get(urlpath)['top_apis']
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

for api in top_apis:
if api['Implemented_by_partial_function'] == "":
Expand All @@ -630,6 +652,8 @@ def get_top_warehouse_calls(self, cleaned_data, from_date, to_date):
top_warehouse_calls.append(api)
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')
return top_warehouse_calls

def get_top_apps_using_warehouse(self, from_date, to_date):
Expand All @@ -644,6 +668,8 @@ def get_top_apps_using_warehouse(self, from_date, to_date):
top_apps_using_warehouse = top_apps_using_warehouse["top_consumers"][:2]
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

return top_apps_using_warehouse

Expand All @@ -660,6 +686,8 @@ def median_time_to_first_api_call(self, from_date, to_date):
apps_list = apps["list"]
except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

for app in apps_list:
created_date = datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ')
Expand All @@ -684,6 +712,8 @@ def median_time_to_first_api_call(self, from_date, to_date):

except APIError as err:
error_once_only(self.request, err)
except:
error_once_only(self.request, 'Unknown Error')

if times_to_first_call:
median = statistics.median(times_to_first_call)
Expand Down
7 changes: 7 additions & 0 deletions apimanager/obp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def login_to_django(self):
data = api.get('/users/current')
except APIError as err:
messages.error(self.request, err)
except:
messages.error(self.request, 'Unknown Error')
return False
else:
userid = data['user_id'] or data['email']
Expand Down Expand Up @@ -66,6 +68,9 @@ def get_redirect_url(self, *args, **kwargs):
except AuthenticatorError as err:
messages.error(self.request, err)
return reverse('home')
except:
messages.error(self.request, 'Unknown Error')
return reverse('home')
else:
self.request.session['obp'] = {
'authenticator': 'obp.oauth.OAuthAuthenticator',
Expand All @@ -89,6 +94,8 @@ def get_redirect_url(self, *args, **kwargs):
authenticator.set_access_token(authorization_url)
except AuthenticatorError as err:
messages.error(self.request, err)
except:
messages.error(self.request, 'Unknown Error')
else:
session_data['authenticator_kwargs'] = {
'token': authenticator.token,
Expand Down

0 comments on commit e349d64

Please sign in to comment.