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.

subscription_info: Fetch recent traffic for all streams.

Shubham Padia91bd4b222fd4
diff --git a/zerver/lib/subscription_info.py b/zerver/lib/subscription_info.py
index 64fa854d250..045bfda5508 100644
--- a/zerver/lib/subscription_info.py
+++ b/zerver/lib/subscription_info.py
@@ -847,8 +847,7 @@ def gather_subscriptions_helper(
def get_stream_id(sub_dict: RawSubscriptionDict) -> int:
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(realm=realm, stream_ids=traffic_stream_ids)
+ recent_traffic = get_streams_traffic(realm=realm)
# Okay, now we finally get to populating our main results, which
# will be these three lists.
diff --git a/zerver/tests/test_channel_fetch.py b/zerver/tests/test_channel_fetch.py
index 4a9aebc6f83..8953fbc489c 100644
--- a/zerver/tests/test_channel_fetch.py
+++ b/zerver/tests/test_channel_fetch.py
@@ -1,10 +1,13 @@
+from datetime import timedelta
from typing import TYPE_CHECKING, Any
import orjson
from django.conf import settings
from django.test import override_settings
+from django.utils.timezone import now as timezone_now
from typing_extensions import override
+from analytics.models import StreamCount
from zerver.actions.streams import (
do_change_stream_group_based_setting,
do_change_stream_permission,
@@ -1262,6 +1265,50 @@ class GetSubscribersTest(ZulipTestCase):
# Guest user only get data about never subscribed web-public streams
self.assert_length(neversubs, 1)
+ def test_stream_weekly_traffic(self) -> None:
+ realm = self.user_profile.realm
+
+ subscribed_stream = self.make_stream("subscribed", realm=realm)
+ unsubscribed_stream = self.make_stream("unsubscribed", realm=realm)
+ never_subscribed_stream = self.make_stream("never_subscribed", realm=realm)
+
+ self.subscribe(self.user_profile, subscribed_stream.name)
+ self.subscribe(self.user_profile, unsubscribed_stream.name)
+ self.unsubscribe(self.user_profile, unsubscribed_stream.name)
+
+ end_time = timezone_now()
+ for stream in [subscribed_stream, unsubscribed_stream, never_subscribed_stream]:
+ stream.date_created = end_time - timedelta(days=7, minutes=1)
+ stream.save(update_fields=["date_created"])
+ StreamCount.objects.create(
+ realm=realm,
+ stream=stream,
+ property="messages_in_stream:is_bot:day",
+ end_time=end_time,
+ value=999,
+ )
+
+ helper_result = gather_subscriptions_helper(self.user_profile)
+ self.verify_sub_fields(helper_result)
+
+ [subscribed_entry] = [
+ sub for sub in helper_result.subscriptions if sub["name"] == subscribed_stream.name
+ ]
+ [unsubscribed_entry] = [
+ sub for sub in helper_result.unsubscribed if sub["name"] == unsubscribed_stream.name
+ ]
+ [never_subscribed_entry] = [
+ sub
+ for sub in helper_result.never_subscribed
+ if sub["name"] == never_subscribed_stream.name
+ ]
+
+ # Stream traffic values are rounded to two significant digits
+ # in get_average_weekly_stream_traffic, so 999 is converted to 1000.
+ self.assertEqual(subscribed_entry["stream_weekly_traffic"], 1000)
+ self.assertEqual(unsubscribed_entry["stream_weekly_traffic"], 1000)
+ self.assertEqual(never_subscribed_entry["stream_weekly_traffic"], 1000)
+
def test_api_fields_present(self) -> None:
user = self.example_user("cordelia")