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.

populate_db: Fix -o flag not spreading messages across days.

apoorvapendsedc787feda586
diff --git a/zerver/tests/test_populate_db.py b/zerver/tests/test_populate_db.py
index d50c1cd4f8c..1a96b4a54d2 100644
--- a/zerver/tests/test_populate_db.py
+++ b/zerver/tests/test_populate_db.py
@@ -1,5 +1,6 @@
from collections import defaultdict
from datetime import timedelta
+from math import floor
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_ids
from zerver.lib.test_classes import ZulipTestCase
@@ -16,8 +17,9 @@ class TestChoosePubDate(ZulipTestCase):
being distributed across the span of several days.
"""
tot_messages = 1000000
+ oldest_message_days = 10
datetimes_list = [
- choose_date_sent(i, tot_messages, 5, 1)
+ choose_date_sent(i, tot_messages, oldest_message_days, 1)
for i in range(1, tot_messages, tot_messages // 100)
]
@@ -25,6 +27,14 @@ class TestChoosePubDate(ZulipTestCase):
for i in range(1, len(datetimes_list)):
self.assertTrue(datetimes_list[i] - datetimes_list[i - 1] > timedelta(minutes=5))
+ # Verify messages are split across the oldest_message_days
+ idx_for_last_message_in_old_message_days = floor(0.8 * len(datetimes_list)) - 1
+ # The first chunk should span roughly (oldest_message_days - 2) days.
+ expected_span_seconds = (oldest_message_days - 2) * 24 * 60 * 60
+ actual_span = datetimes_list[idx_for_last_message_in_old_message_days] - datetimes_list[0]
+ # Assert the span is at least, say, 90% of the intended window to account for randomness
+ self.assertGreaterEqual(actual_span.total_seconds(), expected_span_seconds * 0.9)
+
class TestUserTimeZones(ZulipTestCase):
def test_timezones_assigned_to_users(self) -> None:
diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py
index ad4420dce9d..fc5b2f7d12a 100644
--- a/zilencer/management/commands/populate_db.py
+++ b/zilencer/management/commands/populate_db.py
@@ -1472,14 +1472,14 @@ def choose_date_sent(
# (2) there are some >24hr gaps between adjacent messages, and
# (3) a decent bulk of messages in the last day so you see adjacent messages with the same date.
# So we distribute 80% of messages starting from oldest_message_days days ago, over a period
- # of the first min(oldest_message_days-2, 1) of those days. Then, distributes remaining messages
+ # of the first max(oldest_message_days-2, 1) of those days. Then, distributes remaining messages
# over the past 24 hours.
amount_in_first_chunk = int(tot_messages * 0.8)
amount_in_second_chunk = tot_messages - amount_in_first_chunk
if num_messages < amount_in_first_chunk:
spoofed_date = timezone_now() - timedelta(days=oldest_message_days)
- num_days_for_first_chunk = min(oldest_message_days - 2, 1)
+ num_days_for_first_chunk = max(oldest_message_days - 2, 1)
interval_size = num_days_for_first_chunk * 24 * 60 * 60 / amount_in_first_chunk
lower_bound = interval_size * num_messages
upper_bound = interval_size * (num_messages + 1)