Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support getting count without querying #28895

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions corehq/apps/dump_reload/sql/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def get_filters(self, domain_name):
of the others."""
raise NotImplementedError()

def count(self, domain_name):
return None


class SimpleFilter(DomainFilter):
def __init__(self, filter_kwarg):
Expand Down Expand Up @@ -40,6 +43,9 @@ class UsernameFilter(DomainFilter):
def __init__(self, usernames=None):
self.usernames = usernames

def count(self, domain_name):
return len(self.usernames) if self.usernames is not None else None

def get_filters(self, domain_name):
"""
:return: A generator of filters each filtering for at most 500 users.
Expand All @@ -61,6 +67,9 @@ def __init__(self, field, ids):
self.field = field
self.ids = ids

def count(self, domain_name):
return len(self.get_ids(domain_name))

def get_ids(self, domain_name):
return self.ids

Expand Down Expand Up @@ -97,7 +106,10 @@ def _base_queryset(self):
return objects.using(self.db_alias).order_by(self.model_class._meta.pk.name)

def querysets(self):
return self._base_queryset()
yield self._base_queryset()

def count(self):
return sum(q.count() for q in self.querysets())

def iterators(self):
for queryset in self.querysets():
Expand All @@ -115,11 +127,17 @@ def __init__(self, model_label, filter):
def build(self, domain, model_class, db_alias):
return self.__class__(self.model_label, self.filter).prepare(domain, model_class, db_alias)

def count(self):
count = self.filter.count(self.domain)
if count is not None:
return count
return super(FilteredModelIteratorBuilder, self).count()

def querysets(self):
queryset = self._base_queryset()
filters = self.filter.get_filters(self.domain)
for filter in filters:
yield queryset.filter(filter)
for filter_ in filters:
yield queryset.filter(filter_)


class UniqueFilteredModelIteratorBuilder(FilteredModelIteratorBuilder):
Expand Down