Skip to content

Instantly share code, notes, and snippets.

@readevalprint
Created January 20, 2012 17:21
Show Gist options
  • Save readevalprint/1648531 to your computer and use it in GitHub Desktop.
Save readevalprint/1648531 to your computer and use it in GitHub Desktop.
def _remap_date_counts(**kwargs):
"""Remap the query result.
From: [{'count': 2085, 'month': 11, 'year': 2010},...]
To: {'<label>': 2085, 'date': '2010-11-01'}
"""
for label, qs in kwargs.iteritems():
yield dict((date(x['year'], x['month'], 1), {label: x['count']})
for x in qs)
def merge_results(**kwargs):
res_dict = reduce(_merge_results, _remap_date_counts(**kwargs))
res_list = [dict(date=k, **v) for k, v in res_dict.items()]
return sorted(res_list, key=itemgetter('date'), reverse=True)
def _merge_results(x, y):
"""Merge query results arrays into one array.
From:
[{"date": "2011-10-01", "votes": 3},...]
and
[{"date": "2011-10-01", "helpful": 7},...]
To:
[{"date": "2011-10-01", "votes": 3, "helpful": 7},...]
"""
return dict((s, dict(x.get(s, {}).items() + y.get(s, {}).items()))
for s in set(x.keys() + y.keys()))
diff --git a/apps/kpi/api.py b/apps/kpi/api.py
index e91a13a..a9e526a 100644
--- a/apps/kpi/api.py
+++ b/apps/kpi/api.py
@@ -24,6 +24,9 @@ class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
+ def __unicode__(self):
+ return unicode(self.__dict__)
+
class SolutionResource(Resource):
"""
@@ -104,15 +107,21 @@ def _start_date():
return date(year_ago.year, year_ago.month, 1)
-def _remap_date_counts(qs, label):
+def _remap_date_counts(**kwargs):
"""Remap the query result.
From: [{'count': 2085, 'month': 11, 'year': 2010},...]
To: {'<label>': 2085, 'date': '2010-11-01'}
"""
- return dict((date(x['year'], x['month'], 1), {label: x['count']})
- for x in qs)
+ for label, qs in kwargs.iteritems():
+ yield dict((date(x['year'], x['month'], 1), {label: x['count']})
+ for x in qs)
+
+def merge_results(**kwargs):
+ res_dict = reduce(_merge_results, _remap_date_counts(**kwargs))
+ res_list = [dict(date=k, **v) for k, v in res_dict.items()]
+ return sorted(res_list, key=itemgetter('date'), reverse=True)
def _merge_results(x, y):
"""Merge query results arrays into one array.
@@ -124,8 +133,6 @@ def _merge_results(x, y):
To:
[{"date": "2011-10-01", "votes": 3, "helpful": 7},...]
"""
- res_dict = dict((s, dict(x.get(s, {}).items() + y.get(s, {}).items()))
+ return dict((s, dict(x.get(s, {}).items() + y.get(s, {}).items()))
for s in set(x.keys() + y.keys()))
- res_list = [dict(date=k, **v) for k, v in res_dict.items()]
- return [Struct(**x) for x in sorted(res_list, key=itemgetter('date'),
- reverse=True)]
+
In [11]: qs_has_ff = qs.filter(title__icontains='firefox')
In [12]: merge_results(w=qs_with_solutions, wo=qs_without_solutions, ff=qs_has_ff)
Out[12]:
[{'date': datetime.date(2011, 10, 1), 'w': 1, 'wo': 5},
{'date': datetime.date(2011, 8, 1), 'ff': 3756, 'w': 707, 'wo': 8144},
{'date': datetime.date(2011, 7, 1), 'ff': 5434, 'w': 740, 'wo': 11676},
{'date': datetime.date(2011, 6, 1), 'ff': 6865, 'w': 735, 'wo': 14568},
{'date': datetime.date(2011, 5, 1), 'ff': 5763, 'w': 841, 'wo': 12164},
{'date': datetime.date(2011, 4, 1), 'ff': 4787, 'w': 798, 'wo': 10205},
{'date': datetime.date(2011, 3, 1), 'ff': 7439, 'w': 903, 'wo': 16329},
{'date': datetime.date(2011, 2, 1), 'ff': 2655, 'w': 330, 'wo': 6562},
{'date': datetime.date(2011, 1, 1), 'ff': 2611, 'w': 294, 'wo': 6284},
{'date': datetime.date(2010, 12, 1), 'ff': 2009, 'w': 232, 'wo': 4858},
{'date': datetime.date(2010, 11, 1), 'ff': 1837, 'w': 208, 'wo': 4320},
{'date': datetime.date(2010, 10, 1), 'ff': 2055, 'w': 259, 'wo': 4750},
{'date': datetime.date(2010, 9, 1), 'ff': 2172, 'w': 307, 'wo': 4808},
{'date': datetime.date(2010, 8, 1), 'ff': 1745, 'w': 197, 'wo': 3985},
{'date': datetime.date(2010, 7, 1), 'ff': 2292, 'w': 508, 'wo': 4919},
{'date': datetime.date(2010, 6, 1), 'ff': 1961, 'w': 540, 'wo': 4130},
{'date': datetime.date(2010, 5, 1), 'ff': 1763, 'w': 567, 'wo': 3861},
{'date': datetime.date(2010, 4, 1), 'ff': 19, 'w': 2, 'wo': 32},
{'date': datetime.date(2010, 3, 1), 'ff': 23, 'wo': 41},
{'date': datetime.date(2010, 2, 1), 'ff': 20, 'w': 4, 'wo': 41},
{'date': datetime.date(2010, 1, 1), 'ff': 3, 'w': 1, 'wo': 11},
{'date': datetime.date(2009, 12, 1), 'ff': 1, 'wo': 1}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment