Refactor to be more pythonic and convert float to int

- Implementation wasn't pythonic so updated it to be more inline with the
rest of the code for maintainability.
- The API is return a float in some cases for the month which is causing
the date to string method to fail.
This commit is contained in:
Richard Chapman
2017-11-17 14:35:25 +00:00
parent de9548bba2
commit 9eb544338d

View File

@@ -1,3 +1,5 @@
import calendar
from collections import defaultdict
from datetime import datetime from datetime import datetime
from functools import partial from functools import partial
from flask import ( from flask import (
@@ -116,31 +118,18 @@ def template_usage(service_id):
stats = sorted(stats, key=lambda x: (x['count']), reverse=True) stats = sorted(stats, key=lambda x: (x['count']), reverse=True)
stats_by_month = list() templates_by_month = defaultdict(list)
for stat in stats:
for month in get_months_for_financial_year(year, time_format='%Y-%m'): templates_by_month[int(stat['month'])].append({
long_month = yyyy_mm_to_datetime(month).strftime('%B') 'name': stat['name'],
'type': stat['type'],
template_used = list() 'requested_count': stat['count']
})
for stat in stats: months = [{
if long_month == datetime(1900, stat['month'], 1).strftime("%B"): 'name': calendar.month_name[k],
template_used.append( 'templates_used': v
{ } for k, v in templates_by_month.items()
'name': stat['name'], ]
'type': stat['type'],
'requested_count': stat['count']
}
)
stats_by_month.append(
{
'name': long_month,
'templates_used': template_used
}
)
months = stats_by_month
return render_template( return render_template(
'views/dashboard/all-template-statistics.html', 'views/dashboard/all-template-statistics.html',