Files
upstream-zulip/zerver/management/commands/update_subscriber_counts.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

122 lines
4.8 KiB
Python

import argparse
import logging
from datetime import timedelta
from typing import Any
from django.conf import settings
from django.db import transaction
from django.db.models import F, QuerySet
from django.utils.timezone import now as timezone_now
from typing_extensions import override
from zerver.lib.logging_util import log_to_file
from zerver.lib.management import ZulipBaseCommand, abort_unless_locked
from zerver.models import RealmAuditLog, Stream, Subscription
from zerver.models.realm_audit_logs import AuditLogEventType
## Logging setup ##
logger = logging.getLogger(__name__)
log_to_file(logger, settings.DIGEST_LOG_PATH)
class Command(ZulipBaseCommand):
help = """Update the `Stream.subscriber_count` field based on current subscribers.
There may be race conditions with keeping the cached subscriber count
accurate; this command is run as a daily cron job to ensure the number is accurate.
"""
@override
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--since",
type=int,
help="Only examine channels with changed subscribers in this many hours",
)
self.add_realm_args(parser, help="The optional name of the realm to limit to")
@override
@abort_unless_locked
def handle(self, *args: Any, **options: Any) -> None:
realm = self.get_realm(options)
streams = Stream.objects.all()
if options["since"]:
since_time = timezone_now() - timedelta(hours=options["since"])
# Two ways the count can change -- via a subscription
# being changed, or via a user being (de)activated.
changed_subs = RealmAuditLog.objects.filter(
event_type__in=(
AuditLogEventType.SUBSCRIPTION_CREATED,
AuditLogEventType.SUBSCRIPTION_ACTIVATED,
AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
),
event_time__gte=since_time,
)
if realm:
changed_subs = changed_subs.filter(realm=realm)
# Find all users changed in the time period, join those to
# their subscriptions and distinct recipients, and thence
# to streams.
changed_users = RealmAuditLog.objects.filter(
event_type__in=(
AuditLogEventType.USER_CREATED,
AuditLogEventType.USER_DEACTIVATED,
AuditLogEventType.USER_ACTIVATED,
AuditLogEventType.USER_REACTIVATED,
),
event_time__gte=since_time,
)
if realm:
changed_users = changed_users.filter(realm=realm)
changed_user_ids = (
changed_users.values_list("modified_user_id", flat=True)
.distinct()
.order_by("modified_user_id")
)
changed_user_subs = (
Subscription.objects.filter(user_profile_id__in=changed_user_ids)
.values_list("recipient_id", flat=True)
.distinct()
.order_by("recipient_id")
)
streams_from_users = Stream.objects.filter(recipient_id__in=changed_user_subs)
if realm:
streams_from_users = streams_from_users.filter(realm=realm)
stream_ids: QuerySet[Any, int] = (
changed_subs.distinct("modified_stream_id")
.order_by("modified_stream_id")
.annotate(stream_id=F("modified_stream_id"))
.union(streams_from_users.annotate(stream_id=F("id")))
.values_list("stream_id", flat=True)
)
elif realm := self.get_realm(options):
stream_ids = streams.filter(realm=realm).values_list("id", flat=True)
else:
stream_ids = streams.all().values_list("id", flat=True)
for stream_id in stream_ids.iterator():
with transaction.atomic(durable=True):
stream = Stream.objects.select_for_update(no_key=True).get(id=stream_id)
actual_subscriber_count = Subscription.objects.filter(
active=True,
recipient__type=2,
recipient__type_id=stream_id,
is_user_active=True,
).count()
db_count = stream.subscriber_count
if actual_subscriber_count == db_count:
continue
stream.subscriber_count = actual_subscriber_count
stream.save(update_fields=["subscriber_count"])
logging.info(
"Updated subscriber count of %s, #%s: from %d to %d",
stream.realm.string_id,
stream.name,
db_count,
actual_subscriber_count,
)