Skip to content

Commit 4501cd6

Browse files
committed
Speed up a couple of the stats queries
- Legacy-Id: 12861
1 parent 9492e1e commit 4501cd6

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

ietf/stats/views.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from ietf.group.models import Role, Group
2525
from ietf.person.models import Person
2626
from ietf.name.models import ReviewRequestStateName, ReviewResultName, CountryName
27-
from ietf.doc.models import DocAlias, Document
27+
from ietf.doc.models import DocAlias, Document, State
2828
from ietf.stats.utils import get_aliased_affiliations, get_aliased_countries
2929
from ietf.ietfauth.utils import has_role
3030

@@ -147,10 +147,11 @@ def build_document_stats_url(stats_type_override=Ellipsis, get_overrides={}):
147147
# filter documents
148148
docalias_qs = DocAlias.objects.filter(document__type="draft")
149149

150+
rfc_state = State.objects.get(type="draft", slug="rfc")
150151
if document_type == "rfc":
151-
docalias_qs = docalias_qs.filter(document__states__type="draft", document__states__slug="rfc")
152+
docalias_qs = docalias_qs.filter(document__states=rfc_state)
152153
elif document_type == "draft":
153-
docalias_qs = docalias_qs.exclude(document__states__type="draft", document__states__slug="rfc")
154+
docalias_qs = docalias_qs.exclude(document__states=rfc_state)
154155

155156
if from_time:
156157
# this is actually faster than joining in the database,
@@ -326,10 +327,11 @@ def generate_canonical_names(docalias_qs):
326327
person_filters = Q(documentauthor__document__type="draft")
327328

328329
# filter persons
330+
rfc_state = State.objects.get(type="draft", slug="rfc")
329331
if document_type == "rfc":
330-
person_filters &= Q(documentauthor__document__states__type="draft", documentauthor__document__states__slug="rfc")
332+
person_filters &= Q(documentauthor__document__states=rfc_state)
331333
elif document_type == "draft":
332-
person_filters &= ~Q(documentauthor__document__states__type="draft", documentauthor__document__states__slug="rfc")
334+
person_filters &= ~Q(documentauthor__document__states=rfc_state)
333335

334336
if from_time:
335337
# this is actually faster than joining in the database,
@@ -351,18 +353,14 @@ def generate_canonical_names(docalias_qs):
351353
else:
352354
doc_label = "document"
353355

354-
total_persons = person_qs.distinct().count()
355-
356356
def prune_unknown_bin_with_known(bins):
357357
# remove from the unknown bin all authors within the
358358
# named/known bins
359359
all_known = set(n for b, names in bins.iteritems() if b for n in names)
360-
unknown = []
361-
for name in bins[""]:
362-
if name not in all_known:
363-
unknown.append(name)
364-
bins[""] = unknown
360+
bins[""] = [name for name in bins[""] if name not in all_known]
365361

362+
def count_bins(bins):
363+
return len(set(n for b, names in bins.iteritems() if b for n in names))
366364

367365
if stats_type == "author/documents":
368366
stats_title = "Number of {}s per author".format(doc_label)
@@ -372,6 +370,8 @@ def prune_unknown_bin_with_known(bins):
372370
for name, document_count in person_qs.values_list("name").annotate(Count("documentauthor")):
373371
bins[document_count].append(name)
374372

373+
total_persons = count_bins(bins)
374+
375375
series_data = []
376376
for document_count, names in sorted(bins.iteritems(), key=lambda t: t[0]):
377377
percentage = len(names) * 100.0 / total_persons
@@ -401,6 +401,7 @@ def prune_unknown_bin_with_known(bins):
401401
bins[aliases.get(affiliation, affiliation)].append(name)
402402

403403
prune_unknown_bin_with_known(bins)
404+
total_persons = count_bins(bins)
404405

405406
series_data = []
406407
for affiliation, names in sorted(bins.iteritems(), key=lambda t: t[0].lower()):
@@ -447,6 +448,7 @@ def prune_unknown_bin_with_known(bins):
447448
bins[eu_name].append(name)
448449

449450
prune_unknown_bin_with_known(bins)
451+
total_persons = count_bins(bins)
450452

451453
series_data = []
452454
for country, names in sorted(bins.iteritems(), key=lambda t: t[0].lower()):
@@ -486,6 +488,7 @@ def prune_unknown_bin_with_known(bins):
486488
bins[continent_name].append(name)
487489

488490
prune_unknown_bin_with_known(bins)
491+
total_persons = count_bins(bins)
489492

490493
series_data = []
491494
for continent, names in sorted(bins.iteritems(), key=lambda t: t[0].lower()):

0 commit comments

Comments
 (0)