mirror of
https://github.com/zulip/zulip.git
synced 2025-11-07 15:33:30 +00:00
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:
@@ -5421,7 +5421,7 @@ def get_occupied_streams(realm: Realm) -> QuerySet:
|
|||||||
|
|
||||||
def get_web_public_streams(realm: Realm) -> List[Dict[str, Any]]:
|
def get_web_public_streams(realm: Realm) -> List[Dict[str, Any]]:
|
||||||
query = Stream.objects.filter(realm=realm, deactivated=False, is_web_public=True)
|
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
|
return streams
|
||||||
|
|
||||||
def do_get_streams(
|
def do_get_streams(
|
||||||
@@ -5436,7 +5436,9 @@ def do_get_streams(
|
|||||||
# Start out with all streams in the realm with subscribers
|
# Start out with all streams in the realm with subscribers
|
||||||
query = get_occupied_streams(user_profile.realm)
|
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
|
# We construct a query as the or (|) of the various sources
|
||||||
# this user requested streams from.
|
# this user requested streams from.
|
||||||
query_filter = None # type: Optional[Q]
|
query_filter = None # type: Optional[Q]
|
||||||
@@ -5464,12 +5466,13 @@ def do_get_streams(
|
|||||||
|
|
||||||
if query_filter is not None:
|
if query_filter is not None:
|
||||||
query = query.filter(query_filter)
|
query = query.filter(query_filter)
|
||||||
|
streams = Stream.get_client_data(query)
|
||||||
else:
|
else:
|
||||||
# Don't bother doing to the database with no valid sources
|
# Don't bother going to the database with no valid sources
|
||||||
query = []
|
streams = []
|
||||||
|
|
||||||
streams = [(row.to_dict()) for row in query]
|
|
||||||
streams.sort(key=lambda elt: elt["name"])
|
streams.sort(key=lambda elt: elt["name"])
|
||||||
|
|
||||||
if include_default:
|
if include_default:
|
||||||
is_default = {}
|
is_default = {}
|
||||||
default_streams = get_default_streams_for_realm(user_profile.realm_id)
|
default_streams = get_default_streams_for_realm(user_profile.realm_id)
|
||||||
|
|||||||
@@ -1432,7 +1432,7 @@ class Stream(models.Model):
|
|||||||
# * "email_token" is not realm-public and thus is not included here.
|
# * "email_token" is not realm-public and thus is not included here.
|
||||||
# * is_in_zephyr_realm is a backend-only optimization.
|
# * is_in_zephyr_realm is a backend-only optimization.
|
||||||
# * "deactivated" streams are filtered from the API entirely.
|
# * "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
|
# * "date_created" should probably be added here, as it's useful information
|
||||||
# to subscribers and is needed to compute is_old_stream.
|
# to subscribers and is needed to compute is_old_stream.
|
||||||
# * message_retention_days should be added here once the feature is
|
# * message_retention_days should be added here once the feature is
|
||||||
@@ -1449,7 +1449,11 @@ class Stream(models.Model):
|
|||||||
"first_message_id",
|
"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]:
|
def to_dict(self) -> Dict[str, Any]:
|
||||||
result = {}
|
result = {}
|
||||||
for field_name in self.API_FIELDS:
|
for field_name in self.API_FIELDS:
|
||||||
|
|||||||
Reference in New Issue
Block a user