Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zilencer: Add `get_remote_realm_guest_and_non_guest_count`.
Prakhar Pratyush40621478cbb4
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 4d06fb1a45a..e2b0da4e806 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -62,6 +62,7 @@ from zilencer.models import (RemoteRealmBillingUser,RemoteZulipServer,RemoteZulipServerAuditLog,+ get_remote_realm_guest_and_non_guest_count,get_remote_server_guest_and_non_guest_count,)from zproject.config import get_secret@@ -2801,8 +2802,8 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage@overridedef current_count_for_billed_licenses(self) -> int:- # TODO: Do the proper calculation here.- return 10+ remote_realm_counts = get_remote_realm_guest_and_non_guest_count(self.remote_realm)+ return remote_realm_counts.non_guest_user_count + remote_realm_counts.guest_user_count@overridedef get_audit_log_event(self, event_type: AuditLogEventType) -> int:diff --git a/zilencer/models.py b/zilencer/models.pyindex 124294e86d0..c245d77661d 100644--- a/zilencer/models.py+++ b/zilencer/models.py@@ -395,3 +395,38 @@ def get_remote_server_guest_and_non_guest_count(return RemoteCustomerUserCount(non_guest_user_count=non_guest_count, guest_user_count=guest_count)+++def get_remote_realm_guest_and_non_guest_count(+ remote_realm: RemoteRealm, event_time: datetime = timezone_now()+) -> RemoteCustomerUserCount: # nocoverage+ latest_audit_log = (+ RemoteRealmAuditLog.objects.filter(+ remote_realm=remote_realm,+ event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS,+ event_time__lte=event_time,+ )+ # Important: extra_data is empty for some pre-2020 audit logs+ # prior to the introduction of realm_user_count_by_role+ # logging. Meanwhile, modern Zulip servers using+ # bulk_create_users to create the users in the system bot+ # realm also generate such audit logs. Such audit logs should+ # never be the latest in a normal realm.+ .exclude(extra_data={}).last()+ )++ guest_count = 0+ non_guest_count = 0+ if latest_audit_log is not None:+ humans_count_dict = latest_audit_log.extra_data[RemoteRealmAuditLog.ROLE_COUNT][+ RemoteRealmAuditLog.ROLE_COUNT_HUMANS+ ]+ for role_type in UserProfile.ROLE_TYPES:+ if role_type == UserProfile.ROLE_GUEST:+ guest_count += humans_count_dict.get(str(role_type), 0)+ else:+ non_guest_count += humans_count_dict.get(str(role_type), 0)++ return RemoteCustomerUserCount(+ non_guest_user_count=non_guest_count, guest_user_count=guest_count+ )