From 3f47a08a54f15af197fdb93c203dcc1a4af0d35e Mon Sep 17 00:00:00 2001 From: volpino Date: Tue, 27 Nov 2012 14:16:56 +0100 Subject: euscanwww: Improved query for aggregating handler's data Signed-off-by: volpino --- euscanwww/djeuscan/managers.py | 1 + euscanwww/djeuscan/templates/euscan/statistics.html | 4 ++-- euscanwww/djeuscan/views.py | 19 +++++-------------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/euscanwww/djeuscan/managers.py b/euscanwww/djeuscan/managers.py index 6c04b85..a55f38c 100644 --- a/euscanwww/djeuscan/managers.py +++ b/euscanwww/djeuscan/managers.py @@ -119,6 +119,7 @@ class PackageMixin(object): return packages.filter(version__handler=handler, version__overlay="").distinct() + class PackageQuerySet(models.query.QuerySet, PackageMixin): pass diff --git a/euscanwww/djeuscan/templates/euscan/statistics.html b/euscanwww/djeuscan/templates/euscan/statistics.html index 8372be0..b4fccad 100644 --- a/euscanwww/djeuscan/templates/euscan/statistics.html +++ b/euscanwww/djeuscan/templates/euscan/statistics.html @@ -58,8 +58,8 @@ {% for handler in handlers %} - - {{ handler.handler }} + + {{ handler.handler }} {{ handler.n }} diff --git a/euscanwww/djeuscan/views.py b/euscanwww/djeuscan/views.py index 1b128a4..b599d4c 100644 --- a/euscanwww/djeuscan/views.py +++ b/euscanwww/djeuscan/views.py @@ -380,21 +380,12 @@ def config(request): @render_to("euscan/statistics.html") def statistics(request): - # Didn't found a way to do: - # SELECT COUNT(*), AVG(confidence) FROM Versions - # GROUP BY handler - handlers = ( Version.objects.values("handler") - .filter(overlay="") - .annotate(n=models.Count("handler")) + handlers = ( + Version.objects.values("handler") + .filter(overlay="") + .annotate(n=models.Count("handler"), + avg_conf=models.Avg("confidence")) ) - for i in xrange(len(handlers)): - handler_id = handlers[i]['handler'] - avg = ( Version.objects.filter(handler=handler_id) - .aggregate(avg=models.Avg("confidence")) - ) - if not handler_id: - handlers[i]['handler'] = "None" - handlers[i]['avg_conf'] = avg['avg'] if 'avg' in avg else 0 return {"handlers": handlers} -- cgit v1.2.3-65-gdbad