Sort template stats by usage

Template stats should show the most-used template first.

This commit:
- re-writes the `aggregate_usage` function to use `itertools.groupby`,
  which can do aggregation, and can return data in a structure that’s
  easy to sort on
- uses generators so that we’re not keeping lots of rows of template
  stats in memory

https://www.pivotaltracker.com/story/show/117348893
This commit is contained in:
Chris Hill-Scott
2016-04-12 11:42:45 +01:00
parent 85c996d09a
commit 4568c0e5ab
2 changed files with 33 additions and 13 deletions

View File

@@ -1,4 +1,6 @@
from datetime import date
from collections import namedtuple
from itertools import groupby
from flask import (
render_template,
@@ -90,12 +92,30 @@ def add_rates_to(delivery_statistics):
def aggregate_usage(template_statistics):
import collections
stats = collections.OrderedDict()
for item in template_statistics:
stat = stats.get(item['template']['id'])
if stat:
stat['usage_count'] = stat['usage_count'] + item['usage_count']
else:
stats[item['template']['id']] = item
return stats.values()
immutable_template = namedtuple('Template', ['template_type', 'name', 'id'])
# grouby requires the list to be sorted by template first
statistics_sorted_by_template = sorted(
(
(
immutable_template(**row['template']),
row['usage_count']
)
for row in template_statistics
),
key=lambda items: items[0]
)
# then group and sort the result by usage
return sorted(
(
{
'usage_count': sum(usage[1] for usage in usages),
'template': template
}
for template, usages in groupby(statistics_sorted_by_template, lambda items: items[0])
),
key=lambda row: row['usage_count'],
reverse=True
)