summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2015-06-01 15:08:07 -0500
committerPaul Sokolovsky <paul.sokolovsky@linaro.org>2015-06-05 18:26:55 +0000
commit9e5b5f2be2f8c275d6ef4556b75b3e91df4ee28b (patch)
tree6dc4dbcef123fe0e7907891d032cf996f142082b
parent0d42b069c3047f69ad15e3505fe952c00f663312 (diff)
downloadlinaro-patchmetrics-9e5b5f2be2f8c275d6ef4556b75b3e91df4ee28b.tar.gz
fix performance of project report activity page
This page was really slow, but using the new optimized query can be done quite quickly Change-Id: I955a68be07fc8bc54af0e5e9695c0a31290aa6af
-rw-r--r--apps/patchmetrics/models.py6
-rw-r--r--apps/patchmetrics/views.py56
2 files changed, 33 insertions, 29 deletions
diff --git a/apps/patchmetrics/models.py b/apps/patchmetrics/models.py
index 22dcc72..6712064 100644
--- a/apps/patchmetrics/models.py
+++ b/apps/patchmetrics/models.py
@@ -326,17 +326,19 @@ def get_oldest_patches_waiting_for_feedback():
return patches.order_by('date')[:10]
-def patch_count_by_month(patch_set, num_months, state=None):
+def patch_count_by_month(patch_set, num_months, state=None, values=None):
# allow us to group patches by month
start = x_months_ago(num_months)
truncate_date = connection.ops.date_trunc_sql('month', 'date')
if state:
patch_set = patch_set.filter(state=state)
+ if not values:
+ values = ('month',)
return patch_set.filter(
date__gte=start
).extra(
{'month': truncate_date}
- ).values('month').annotate(Count('pk')).order_by('month')
+ ).values(*values).annotate(Count('pk')).order_by('month')
def patch_month_metrics(patch_set, num_months=6):
diff --git a/apps/patchmetrics/views.py b/apps/patchmetrics/views.py
index 4c4d470..123af9c 100644
--- a/apps/patchmetrics/views.py
+++ b/apps/patchmetrics/views.py
@@ -1,5 +1,5 @@
import calendar
-from datetime import datetime, timedelta
+from datetime import datetime
from dateutil import relativedelta
@@ -18,6 +18,7 @@ from patchmetrics.models import (
get_patches_per_month_up_to,
get_patches_waiting_for_feedback_by_age_range,
get_oldest_patches_waiting_for_feedback,
+ patch_count_by_month,
patch_month_metrics,
LinaroPatch,
PROJECTS_TO_IGNORE_IN_STATS,
@@ -278,37 +279,38 @@ def non_linaro_patches(request):
return render_to_response('patchmetrics/non-linaro.html', context)
-def report_project_activity(request):
- last_12 = datetime.now() - timedelta(days=365)
- last_6 = datetime.now() - timedelta(days=365/2)
- last_3 = datetime.now() - timedelta(days=365/4)
- last_1 = datetime.now() - timedelta(days=365/12)
+def _subtract_months(dt, months):
+ month = dt.month - months
+ if month > 0:
+ return dt.replace(month=month)
+ else:
+ return dt.replace(month=month + 12, year=dt.year - 1)
- # pull all linaro patches for the past 12 months
- # the select_related helps reduce the number of db hits we incurr
- patches = Patch.objects.exclude(
- state=State.objects.get(name='Superseded')
- ).filter(
- author__email__iendswith='@linaro.org',
- date__gt=last_12
- ).select_related(
- 'project__name', 'date'
- )
+
+def report_project_activity(request):
+ now = datetime.now()
+ last_full_month = datetime(now.year, now.month, 1)
+ last_12 = _subtract_months(last_full_month, 12)
+ last_6 = _subtract_months(last_full_month, 6)
+ last_3 = _subtract_months(last_full_month, 3)
+ last_1 = _subtract_months(last_full_month, 1)
summary = {}
- for p in patches:
+ patches = LinaroPatch.objects.filter(date__lte=last_full_month)
+ qs = patch_count_by_month(
+ patches, 12, values=('month', 'project__name'))
+ for x in qs:
e = summary.setdefault(
- p.project.name, {
+ x['project__name'], {
'last_12': 0, 'last_6': 0, 'last_3': 0, 'last_1': 0})
- if p.date > last_1:
- e['last_1'] += 1
- if p.date > last_3:
- e['last_3'] += 1
- if p.date > last_6:
- e['last_6'] += 1
- if p.date > last_12:
- e['last_12'] += 1
-
+ if x['month'] >= last_1:
+ e['last_1'] += x['pk__count']
+ if x['month'] >= last_3:
+ e['last_3'] += x['pk__count']
+ if x['month'] >= last_6:
+ e['last_6'] += x['pk__count']
+ if x['month'] >= last_12:
+ e['last_12'] += x['pk__count']
items = [(x, y['last_12'], y['last_6'], y['last_3'], y['last_1'])
for x, y in summary.iteritems()]
summary = sorted(items, key=lambda x: x[1], reverse=True)[:20]