Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stream_traffic: Make stream_ids optional when fetching info for realm.

Shubham Padia1172390d258d
diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py
index f8656c57aac..5fae93597b3 100644
--- a/zerver/actions/streams.py
+++ b/zerver/actions/streams.py
@@ -286,7 +286,7 @@ def do_unarchive_stream(stream: Stream, new_name: str, *, acting_user: UserProfi
event_time=timezone_now(),
)
- recent_traffic = get_streams_traffic({stream.id}, realm)
+ recent_traffic = get_streams_traffic(realm, {stream.id})
notify_user_ids = list(can_access_stream_metadata_user_ids(stream))
@@ -414,7 +414,7 @@ def send_subscription_add_events(
info_by_user[sub_info.user.id].append(sub_info)
stream_ids = {sub_info.stream.id for sub_info in sub_info_list}
- recent_traffic = get_streams_traffic(stream_ids=stream_ids, realm=realm)
+ recent_traffic = get_streams_traffic(realm=realm, stream_ids=stream_ids)
# We generally only have a few streams, so we compute subscriber
# data in its own loop.
@@ -542,7 +542,7 @@ def send_stream_creation_events_for_previously_inaccessible_streams(
users_with_metadata_access_via_permission_groups: dict[int, set[int]] | None = None,
) -> None:
stream_ids = set(altered_user_dict.keys())
- recent_traffic = get_streams_traffic(stream_ids, realm)
+ recent_traffic = get_streams_traffic(realm, stream_ids)
streams = [stream_dict[stream_id] for stream_id in stream_ids]
anonymous_group_membership: dict[int, UserGroupMembersData] | None = None
@@ -1360,7 +1360,7 @@ def do_change_stream_permission(
- user_ids_with_metadata_access_via_permission_groups
)
- recent_traffic = get_streams_traffic({stream.id}, realm)
+ recent_traffic = get_streams_traffic(realm, {stream.id})
anonymous_group_membership = get_anonymous_group_membership_dict_for_streams([stream])
send_stream_creation_event(
realm,
@@ -1853,7 +1853,7 @@ def do_change_stream_group_based_setting(
)
if len(user_ids_gaining_metadata_access) > 0:
- recent_traffic = get_streams_traffic({stream.id}, stream.realm)
+ recent_traffic = get_streams_traffic(stream.realm, {stream.id})
anonymous_group_membership = get_anonymous_group_membership_dict_for_streams([stream])
send_stream_creation_event(
stream.realm,
diff --git a/zerver/actions/user_groups.py b/zerver/actions/user_groups.py
index 99aa2398950..ac16fc176c6 100644
--- a/zerver/actions/user_groups.py
+++ b/zerver/actions/user_groups.py
@@ -349,7 +349,7 @@ def bulk_add_members_to_user_groups(
subscriber_ids_for_streams = get_user_ids_for_streams({stream.id for stream in streams})
new_stream_metadata_user_ids = bulk_can_access_stream_metadata_user_ids(streams)
- recent_traffic = get_streams_traffic({stream.id for stream in streams}, realm)
+ recent_traffic = get_streams_traffic(realm, {stream.id for stream in streams})
anonymous_group_membership = get_anonymous_group_membership_dict_for_streams(streams)
for user_group in user_groups:
@@ -493,7 +493,7 @@ def add_subgroups_to_user_group(
subscriber_ids_for_streams = get_user_ids_for_streams({stream.id for stream in streams})
new_stream_metadata_user_ids = bulk_can_access_stream_metadata_user_ids(streams)
- recent_traffic = get_streams_traffic({stream.id for stream in streams}, realm)
+ recent_traffic = get_streams_traffic(realm, {stream.id for stream in streams})
anonymous_group_membership = get_anonymous_group_membership_dict_for_streams(streams)
do_send_subgroups_update_event("add_subgroups", user_group, subgroup_ids)
diff --git a/zerver/actions/users.py b/zerver/actions/users.py
index dc8cd58d02f..7b5abc3f052 100644
--- a/zerver/actions/users.py
+++ b/zerver/actions/users.py
@@ -565,7 +565,7 @@ def send_stream_events_for_role_update(
now_accessible_stream_ids = current_accessible_stream_ids - old_accessible_stream_ids
if now_accessible_stream_ids:
- recent_traffic = get_streams_traffic(now_accessible_stream_ids, user_profile.realm)
+ recent_traffic = get_streams_traffic(user_profile.realm, now_accessible_stream_ids)
now_accessible_streams = [
stream
diff --git a/zerver/lib/stream_traffic.py b/zerver/lib/stream_traffic.py
index b9ddf7a9c57..d3312cf297d 100644
--- a/zerver/lib/stream_traffic.py
+++ b/zerver/lib/stream_traffic.py
@@ -8,17 +8,20 @@ from analytics.models import StreamCount
from zerver.models import Realm
-def get_streams_traffic(stream_ids: set[int], realm: Realm) -> dict[int, int] | None:
+def get_streams_traffic(realm: Realm, stream_ids: set[int] | None = None) -> dict[int, int] | None:
stat = COUNT_STATS["messages_in_stream:is_bot:day"]
traffic_from = timezone_now() - timedelta(days=28)
- query = StreamCount.objects.filter(
+ filter_kwargs = dict(
# The realm_id is important, as it makes this significantly better-indexed
realm_id=realm.id,
- stream_id__in=stream_ids,
property=stat.property,
end_time__gt=traffic_from,
)
+ if stream_ids is not None:
+ filter_kwargs["stream_id__in"] = stream_ids
+
+ query = StreamCount.objects.filter(**filter_kwargs)
traffic_list = query.values("stream_id").annotate(sum_value=Sum("value"))
traffic_dict = {}
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index 9f64cb78f8b..6e120932753 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -1976,7 +1976,7 @@ def do_get_streams(
)
stream_ids = {stream.id for stream in streams}
- recent_traffic = get_streams_traffic(stream_ids, user_profile.realm)
+ recent_traffic = get_streams_traffic(user_profile.realm, stream_ids)
if anonymous_group_membership is None:
anonymous_group_membership = get_anonymous_group_membership_dict_for_streams(streams)
diff --git a/zerver/lib/subscription_info.py b/zerver/lib/subscription_info.py
index d025536c3fa..64fa854d250 100644
--- a/zerver/lib/subscription_info.py
+++ b/zerver/lib/subscription_info.py
@@ -848,7 +848,7 @@ def gather_subscriptions_helper(
return recip_id_to_stream_id[sub_dict["recipient_id"]]
traffic_stream_ids = {get_stream_id(sub_dict) for sub_dict in sub_dicts}
- recent_traffic = get_streams_traffic(stream_ids=traffic_stream_ids, realm=realm)
+ recent_traffic = get_streams_traffic(realm=realm, stream_ids=traffic_stream_ids)
# Okay, now we finally get to populating our main results, which
# will be these three lists.
diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py
index 470a284be78..3b4c9711427 100644
--- a/zerver/tests/test_audit_log.py
+++ b/zerver/tests/test_audit_log.py
@@ -388,11 +388,10 @@ class TestRealmAuditLog(ZulipTestCase):
def test_get_streams_traffic(self) -> None:
realm = get_realm("zulip")
- stream_name = "whatever"
- stream = self.make_stream(stream_name, realm)
- stream_ids = {stream.id}
+ stream = self.make_stream("whatever", realm)
+ stream_2 = self.make_stream("whatever_2", realm)
- result = get_streams_traffic(stream_ids, realm)
+ result = get_streams_traffic(realm)
self.assertEqual(result, {})
StreamCount.objects.create(
@@ -402,10 +401,20 @@ class TestRealmAuditLog(ZulipTestCase):
end_time=timezone_now(),
value=999,
)
+ StreamCount.objects.create(
+ realm=realm,
+ stream=stream_2,
+ property="messages_in_stream:is_bot:day",
+ end_time=timezone_now(),
+ value=23,
+ )
- result = get_streams_traffic(stream_ids, realm)
+ result = get_streams_traffic(realm, {stream.id})
self.assertEqual(result, {stream.id: 999})
+ all_streams_traffic = get_streams_traffic(realm)
+ self.assertEqual(all_streams_traffic, {stream.id: 999, stream_2.id: 23})
+
def test_subscriptions(self) -> None:
now = timezone_now()
diff --git a/zerver/views/streams.py b/zerver/views/streams.py
index a7e01512e85..795189f6f85 100644
--- a/zerver/views/streams.py
+++ b/zerver/views/streams.py
@@ -1147,7 +1147,7 @@ def get_stream_backend(
) -> HttpResponse:
(stream, _sub) = access_stream_by_id(user_profile, stream_id, require_content_access=False)
- recent_traffic = get_streams_traffic({stream.id}, user_profile.realm)
+ recent_traffic = get_streams_traffic(user_profile.realm, {stream.id})
anonymous_group_membership = get_anonymous_group_membership_dict_for_streams([stream])
return json_success(