Skip to content

Commit

Permalink
Merge pull request #62 from shanhuhai/master
Browse files Browse the repository at this point in the history
Fix the 500 error when query an none exist username , removed the duplicated error message and updated the Readme.md and settings.py
  • Loading branch information
simonredfern authored Oct 2, 2018
2 parents bad89e8 + b7d7fd4 commit aed1194
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 21 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ Execute the same steps as for development, but do not run the app.
Edit `apimanager/apimanager/local_settings.py` for _additional_ changes to the development settings above:

```python

import os
# Disable debug
DEBUG = False
# Hosts allowed to access the app
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '<your public hostname here>']

# Directory to place static files in, defaults to `../static-collected` relative to this file
STATIC_ROOT = '<dirname>'
STATIC_ROOT = ''
# Admins to send e.g. error emails to
ADMINS = [
('Admin', '[email protected]')
Expand All @@ -103,6 +106,40 @@ SERVER_EMAIL = '[email protected]'
EMAIL_HOST = 'mail.example.com'
# Enable email security
EMAIL_TLS = True

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Used internally by Django, can be anything of your choice
SECRET_KEY = 'abc'
# API hostname, e.g. https://api.openbankproject.com
API_HOST = 'http://127.0.0.1:8080'
# Consumer key + secret to authenticate the _app_ against the API
OAUTH_CONSUMER_KEY = ''
OAUTH_CONSUMER_SECRET = ''
# Database filename, default is `../db.sqlite3` relative to this file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '..', '..', 'db.sqlite3'),
}
}

# Apps to exclude when request to OBP-API's api
EXCLUDE_APPS = []
# Functions to exclude when request to OBP-API's api
EXCLUDE_FUNCTIONS = []
# Url Patterns to exclude when reqeust to OBP-API's api
EXCLUDE_URL_PATTERN = []

# App Name to aggregate metrics
API_EXPLORER_APP_NAME = 'xxx'

#Map Java: yyyy-MM-dd'T'HH:mm'Z'
API_DATETIMEFORMAT = '%Y-%m-%dT%H:%M:%SZ'
#Map Java: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
API_DATEFORMAT = '%Y-%m-%dT%H:%M:%S.000Z'

```

## Static files
Expand Down
15 changes: 15 additions & 0 deletions apimanager/apimanager/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,18 @@
BOOTSTRAP4 = {
'include_jquery': True,
}

# Apps to exclude when request to OBP-API's api
EXCLUDE_APPS = []
# Functions to exclude when request to OBP-API's api
EXCLUDE_FUNCTIONS = []
# Url Patterns to exclude when reqeust to OBP-API's api
EXCLUDE_URL_PATTERN = []
# App Name to aggregate metrics
API_EXPLORER_APP_NAME = 'xxx'






2 changes: 1 addition & 1 deletion apimanager/consumers/templates/consumers/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h1>Consumers</h1>
</div>
</div>

<h2>Statistics</h2>
<h4>Statistics</h4>
<ul id="statistics">
<li>Total number of consumers: {{ statistics.consumers_num }}</li>
<li>Total number of unique developer email addresses: {{ statistics.unique_developer_email_num }}</li>
Expand Down
44 changes: 28 additions & 16 deletions apimanager/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
import statistics


def error_once_only(request, err):
"""
Just add the error once
:param request:
:param err:
:return:
"""
storage = messages.get_messages(request)
if str(err) not in [str(m.message) for m in storage]:
messages.error(request, err)


def get_random_color(to_hash):
hashed = str(int(hashlib.md5(to_hash.encode('utf-8')).hexdigest(), 16))
r = int(hashed[0:3]) % 255
Expand Down Expand Up @@ -130,7 +142,7 @@ def get_metrics(self, cleaned_data):
metrics = api.get(urlpath)
metrics = self.to_django(metrics['metrics'])
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)
return metrics

def get_context_data(self, **kwargs):
Expand Down Expand Up @@ -247,7 +259,7 @@ def get_aggregate_metrics(self, cleaned_data, from_date, to_date):
api_calls_total = metrics[0]["count"]
average_response_time = metrics[0]["average_response_time"]
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

else:
urlpath = '/management/aggregate-metrics?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format(
Expand All @@ -260,7 +272,7 @@ def get_aggregate_metrics(self, cleaned_data, from_date, to_date):
api_calls_total = metrics[0]["count"]
average_response_time = metrics[0]["average_response_time"]
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)


to_date = datetime.datetime.strptime(to_date, API_DATEFORMAT)
Expand All @@ -286,7 +298,7 @@ def get_aggregate_metrics_api_explorer(self, from_date, to_date):
api_calls_total = metrics[0]["count"]
average_response_time = metrics[0]["average_response_time"]
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)


to_date = datetime.datetime.strptime(to_date, API_DATEFORMAT)
Expand All @@ -307,7 +319,7 @@ def get_active_apps(self, cleaned_data, from_date, to_date):
apps = api.get(urlpath)
active_apps_list = list(apps)
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)
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 @@ -316,7 +328,7 @@ def get_active_apps(self, cleaned_data, from_date, to_date):
apps = api.get(urlpath)
active_apps_list = list(apps)
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

return active_apps_list

Expand All @@ -332,7 +344,7 @@ def get_total_number_of_apps(self, cleaned_data, from_date, to_date):
apps = api.get(urlpath)
apps_list = apps["list"]
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

for app in apps_list:
app_created_date = datetime.datetime.strptime(app["created"], '%Y-%m-%dT%H:%M:%SZ')
Expand Down Expand Up @@ -401,7 +413,7 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ):
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

date_from = date_to
date_list.append(date_from)
Expand All @@ -418,7 +430,7 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ):
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

date_from = date_to
date_list.append(date_from)
Expand Down Expand Up @@ -556,7 +568,7 @@ def get_users_cansearchwarehouse(self):
urlpath = '/users'
users = api.get(urlpath)
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

users_with_cansearchwarehouse = []
email_with_cansearchwarehouse = []
Expand Down Expand Up @@ -584,15 +596,15 @@ def get_top_apis(self, cleaned_data, from_date, to_date):
try:
top_apis = api.get(urlpath)['top_apis']
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)
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))
api = API(self.request.session.get('obp'))
try:
top_apis = api.get(urlpath)['top_apis']
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

for api in top_apis:
if api['Implemented_by_partial_function'] == "":
Expand All @@ -613,7 +625,7 @@ def get_top_warehouse_calls(self, cleaned_data, from_date, to_date):
if "elasticSearchWarehouse" in api['Implemented_by_partial_function']:
top_warehouse_calls.append(api)
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)
return top_warehouse_calls

def get_top_apps_using_warehouse(self, from_date, to_date):
Expand All @@ -627,7 +639,7 @@ def get_top_apps_using_warehouse(self, from_date, to_date):
top_apps_using_warehouse = api.get(urlpath)
top_apps_using_warehouse = top_apps_using_warehouse["top_consumers"][:2]
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

return top_apps_using_warehouse

Expand All @@ -643,7 +655,7 @@ def median_time_to_first_api_call(self, from_date, to_date):
apps = api.get(urlpath_consumers)
apps_list = apps["list"]
except APIError as err:
messages.error(self.request, err)
error_once_only(self.request, err)

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


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

if times_to_first_call:
median = statistics.median(times_to_first_call)
Expand Down
5 changes: 3 additions & 2 deletions apimanager/users/templates/users/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<h1>Users</h1>

<div id="users-filters">
<h4>Filters</h4>
<div class="hidden-xs">
{% include "users/includes/filter_role.html" %}
{% include "users/includes/filter_email.html" %}
Expand All @@ -32,12 +33,12 @@ <h1>Users</h1>
<div class="row">
<div class="col-xs-12">

<h2>Pagination</h2>
<h4>Pagination</h4>
{% include "users/includes/filter_pagination.html" %}
</div>
</div>

<h2>Statistics</h2>
<h4>Statistics</h4>
<ul id="statistics">
<li>Total number of users: {{ statistics.users_num }}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion apimanager/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def get_context_data(self, **kwargs):
else:
urlpath = '/users?limit={}&offset={}'.format(limit, offset)

users = []
try:
users = api.get(urlpath)
except APIError as err:
messages.error(self.request, err)
return [], []

role_names = self.get_users_rolenames(context)
users = FilterRoleName(context, self.request.GET) \
Expand Down

0 comments on commit aed1194

Please sign in to comment.