Skip to content

Commit

Permalink
Merge pull request #61 from shanhuhai/master
Browse files Browse the repository at this point in the history
Fix the datetime problem in "KPI Dashboard" of "Metrics"
  • Loading branch information
simonredfern authored Oct 1, 2018
2 parents cdd943b + 8220547 commit bad89e8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 36 deletions.
5 changes: 5 additions & 0 deletions apimanager/consumers/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ApiConsumersForm(forms.Form):
'class': 'form-control',
}
),
initial=-1,
required=False,
)

Expand All @@ -29,6 +30,7 @@ class ApiConsumersForm(forms.Form):
'class': 'form-control',
}
),
initial=-1,
required=False,
)
per_day_call_limit = forms.IntegerField(
Expand All @@ -38,6 +40,7 @@ class ApiConsumersForm(forms.Form):
'class': 'form-control',
}
),
initial=-1,
required=False,
)
per_week_call_limit = forms.IntegerField(
Expand All @@ -47,6 +50,7 @@ class ApiConsumersForm(forms.Form):
'class': 'form-control',
}
),
initial=-1,
required=False,
)

Expand All @@ -57,5 +61,6 @@ class ApiConsumersForm(forms.Form):
'class': 'form-control',
}
),
initial=-1,
required=False,
)
37 changes: 17 additions & 20 deletions apimanager/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def to_django(self, metrics):
"""
for metric in metrics:
metric['date'] = datetime.datetime.strptime(
metric['date'], '%Y-%m-%dT%H:%M:%S.%fZ')
metric['date'], API_DATEFORMAT)
return metrics

def to_api(self, cleaned_data):
Expand Down Expand Up @@ -392,7 +392,7 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ):
if cleaned_data.get('include_obp_apps'):
while date_to <= to_date:
urlpath = '/management/aggregate-metrics?from_date={}&to_date={}'.format(
date_from.strftime(API_DATEFORMAT)[:-4].__add__("Z"), date_to.strftime(API_DATEFORMAT)[:-4].__add__("Z"))
date_from.strftime(API_DATEFORMAT), date_to.strftime(API_DATEFORMAT))
api = API(self.request.session.get('obp'))
try:
metrics = api.get(urlpath)
Expand All @@ -409,7 +409,7 @@ def calls_per_delta(self, cleaned_data, from_date, to_date, **delta ):
else:
while date_to <= to_date:
urlpath = '/management/aggregate-metrics?from_date={}&to_date={}&exclude_app_names={}'.format(
date_from.strftime(API_DATEFORMAT)[:-4].__add__("Z"), date_to.strftime(API_DATEFORMAT)[:-4].__add__("Z"), ",".join(EXCLUDE_APPS))
date_from.strftime(API_DATEFORMAT), date_to.strftime(API_DATEFORMAT), ",".join(EXCLUDE_APPS))
api = API(self.request.session.get('obp'))
try:
metrics = api.get(urlpath)
Expand Down Expand Up @@ -625,13 +625,10 @@ def get_top_apps_using_warehouse(self, from_date, to_date):
api = API(self.request.session.get('obp'))
try:
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)

top_apps_using_warehouse = top_apps_using_warehouse["top_consumers"][:2]



return top_apps_using_warehouse

def median_time_to_first_api_call(self, from_date, to_date):
Expand All @@ -650,7 +647,7 @@ def median_time_to_first_api_call(self, from_date, to_date):

for app in apps_list:
created_date = datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ')
created_date = created_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
created_date = created_date.strftime(API_DATEFORMAT)
created_date = datetime.datetime.strptime(created_date, API_DATEFORMAT)
if created_date >= datetime.datetime.strptime(from_date, API_DATEFORMAT):
new_apps_list.append(app)
Expand Down Expand Up @@ -684,9 +681,9 @@ def get_context_data(self, **kwargs):
form = self.get_form()

to_date = datetime.datetime.strptime(form.data['to_date'], '%Y-%m-%d %H:%M:%S')
to_date = to_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
to_date = to_date.strftime(API_DATEFORMAT)

from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(30)).strftime(API_DATEFORMAT)[:-4].__add__("Z")
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(30)).strftime(API_DATEFORMAT)
context = super(MetricsSummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date)
Expand Down Expand Up @@ -734,8 +731,8 @@ class YearlySummaryView(MetricsSummaryView):
def get_context_data(self, **kwargs):
form = self.get_form()
to_date = datetime.datetime.strptime(form.data['to_date'], '%Y-%m-%d %H:%M:%S')
to_date = to_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(365)).strftime(API_DATEFORMAT)[:-4].__add__("Z")
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(365)).strftime(API_DATEFORMAT)

context = super(YearlySummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
Expand Down Expand Up @@ -784,8 +781,8 @@ class QuarterlySummaryView(MetricsSummaryView):
def get_context_data(self, **kwargs):
form = self.get_form()
to_date = datetime.datetime.strptime(form.data['to_date'], '%Y-%m-%d %H:%M:%S')
to_date = to_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(90)).strftime(API_DATEFORMAT)[:-4].__add__("Z")
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(90)).strftime(API_DATEFORMAT)

context = super(QuarterlySummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
Expand Down Expand Up @@ -838,8 +835,8 @@ class WeeklySummaryView(MetricsSummaryView):
def get_context_data(self, **kwargs):
form = self.get_form()
to_date = datetime.datetime.strptime(form.data['to_date'], '%Y-%m-%d %H:%M:%S')
to_date = to_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(7)).strftime(API_DATEFORMAT)[:-4].__add__("Z")
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(7)).strftime(API_DATEFORMAT)

context = super(WeeklySummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
Expand Down Expand Up @@ -890,8 +887,8 @@ class DailySummaryView(MetricsSummaryView):
def get_context_data(self, **kwargs):
form = self.get_form()
to_date = datetime.datetime.strptime(form.data['to_date'], '%Y-%m-%d %H:%M:%S')
to_date = to_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(1)).strftime(API_DATEFORMAT)[:-4].__add__("Z")
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(1)).strftime(API_DATEFORMAT)

context = super(DailySummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
Expand Down Expand Up @@ -947,10 +944,10 @@ def get_context_data(self, **kwargs):
form = self.get_form()

to_date = datetime.datetime.strptime(form.data['to_date'], '%Y-%m-%d %H:%M:%S')
to_date = to_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
to_date = to_date.strftime(API_DATEFORMAT)

from_date = datetime.datetime.strptime(form.data['from_date_custom'], '%Y-%m-%d %H:%M:%S')
from_date = from_date.strftime(API_DATEFORMAT)[:-4].__add__("Z")
from_date = from_date.strftime(API_DATEFORMAT)
context = super(CustomSummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date)
Expand Down
11 changes: 11 additions & 0 deletions apimanager/users/templates/users/includes/filter_pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form class="form-inline" method="get">
<div class="form-group">
<label for="offset">Offset:</label>
<input type="number" class="form-control" name="offset" id="offset" placeholder="0" value="{{ offset }}">
</div>
<div class="form-group">
<label for="limit">Limit:</label>
<input type="number" class="form-control" name="limit" id="limit" placeholder="50" value="{{ limit }}">
</div>
<button type="submit" class="btn btn-default">Get user list</button>
</form>
9 changes: 9 additions & 0 deletions apimanager/users/templates/users/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ <h1>Users</h1>
</div>
</div>


<div class="row">
<div class="col-xs-12">

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

<h2>Statistics</h2>
<ul id="statistics">
<li>Total number of users: {{ statistics.users_num }}
Expand Down
46 changes: 32 additions & 14 deletions apimanager/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,62 @@ class IndexView(LoginRequiredMixin, TemplateView):
template_name = "users/index.html"

def get_users_rolenames(self, context):
users = []

api = API(self.request.session.get('obp'))
try:
urlpath = '/users'
users = api.get(urlpath)
urlpath = '/entitlements'
entitlements = api.get(urlpath)
except APIError as err:
messages.error(self.request, err)
return [], []

role_names = []
try:
for user in users['users']:
for entitlement in user['entitlements']['list']:
role_names.append(entitlement['role_name'])
for entitlement in entitlements['list']:
role_names.append(entitlement['role_name'])
# fail gracefully in case API provides new structure
except KeyError as err:
messages.error(self.request, 'KeyError: {}'.format(err))
return [], []

role_names = list(set(role_names))
role_names.sort()
users = FilterRoleName(context, self.request.GET)\
.apply(users['users'])
users = FilterEmail(context, self.request.GET)\
.apply(users)
users = FilterUsername(context, self.request.GET)\
.apply(users)
return users, role_names

return role_names

def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
users, role_names = self.get_users_rolenames(context)

api = API(self.request.session.get('obp'))
limit = self.request.GET.get('limit', 50)
offset = self.request.GET.get('offset', 0)
email = self.request.GET.get('email')
username = self.request.GET.get('username')

if email:
urlpath = '/users/email/{}/terminator'.format(email)
elif username:
urlpath = '/users/username/{}'.format(username)
else:
urlpath = '/users?limit={}&offset={}'.format(limit, offset)

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) \
.apply([users] if username else users['users'])
context.update({
'role_names': role_names,
'statistics': {
'users_num': len(users),
},
'users': users,
'limit': limit,
'offset': offset
})
return context

Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ gunicorn==19.6.0
matplotlib
django-bootstrap-datepicker-plus
django-mathfilters
django-bootstrap3
django-bootstrap-datepicker-plus
django-bootstrap3

0 comments on commit bad89e8

Please sign in to comment.