perf: Extract Stream.get_client_data.

This function slims down the data that we get
from the database in order to create the
streams part of our client payload.

We also fix a typo.

We also clearly distinguish between queries
and lists here.
This commit is contained in:
Steve Howell
2020-02-29 20:51:06 +00:00
committed by Tim Abbott
parent 49b8218463
commit 94192395fb
2 changed files with 14 additions and 7 deletions

View File

@@ -5421,7 +5421,7 @@ def get_occupied_streams(realm: Realm) -> QuerySet:
def get_web_public_streams(realm: Realm) -> List[Dict[str, Any]]:
query = Stream.objects.filter(realm=realm, deactivated=False, is_web_public=True)
streams = [(row.to_dict()) for row in query]
streams = Stream.get_client_data(query)
return streams
def do_get_streams(
@@ -5436,7 +5436,9 @@ def do_get_streams(
# Start out with all streams in the realm with subscribers
query = get_occupied_streams(user_profile.realm)
if not include_all_active:
if include_all_active:
streams = Stream.get_client_data(query)
else:
# We construct a query as the or (|) of the various sources
# this user requested streams from.
query_filter = None # type: Optional[Q]
@@ -5464,12 +5466,13 @@ def do_get_streams(
if query_filter is not None:
query = query.filter(query_filter)
streams = Stream.get_client_data(query)
else:
# Don't bother doing to the database with no valid sources
query = []
# Don't bother going to the database with no valid sources
streams = []
streams = [(row.to_dict()) for row in query]
streams.sort(key=lambda elt: elt["name"])
if include_default:
is_default = {}
default_streams = get_default_streams_for_realm(user_profile.realm_id)

View File

@@ -1432,7 +1432,7 @@ class Stream(models.Model):
# * "email_token" is not realm-public and thus is not included here.
# * is_in_zephyr_realm is a backend-only optimization.
# * "deactivated" streams are filtered from the API entirely.
# * "realm" and "recipient" and not exposed to clients via the API.
# * "realm" and "recipient" are not exposed to clients via the API.
# * "date_created" should probably be added here, as it's useful information
# to subscribers and is needed to compute is_old_stream.
# * message_retention_days should be added here once the feature is
@@ -1449,7 +1449,11 @@ class Stream(models.Model):
"first_message_id",
]
# This is stream information that is sent to clients
@staticmethod
def get_client_data(query: QuerySet) -> List[Dict[str, Any]]:
query = query.only(*Stream.API_FIELDS)
return [row.to_dict() for row in query]
def to_dict(self) -> Dict[str, Any]:
result = {}
for field_name in self.API_FIELDS: