corporate: Fix misuse of timezone_now() as parameter default.

Python parameter defaults are only evaluated once at the function
definition site, not at each call site.  So these defaults were
incorrectly evaluating to the server’s startup time rather than the
current time.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-11-10 16:48:13 -08:00 committed by Tim Abbott
parent 70fb590466
commit 9e9e951a4a
3 changed files with 13 additions and 13 deletions

View File

@ -366,13 +366,13 @@ def get_plan_data_by_remote_realm() -> dict[int, dict[int, RemoteActivityPlanDat
def get_remote_realm_user_counts( def get_remote_realm_user_counts(
event_time: datetime = timezone_now(), event_time: datetime | None = None,
) -> dict[int, RemoteCustomerUserCount]: # nocoverage ) -> dict[int, RemoteCustomerUserCount]: # nocoverage
user_counts_by_realm: dict[int, RemoteCustomerUserCount] = {} user_counts_by_realm: dict[int, RemoteCustomerUserCount] = {}
for log in ( for log in (
RemoteRealmAuditLog.objects.filter( RemoteRealmAuditLog.objects.filter(
event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS, event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS,
event_time__lte=event_time, event_time__lte=timezone_now() if event_time is None else event_time,
remote_realm__isnull=False, remote_realm__isnull=False,
) )
# Important: extra_data is empty for some pre-2020 audit logs # Important: extra_data is empty for some pre-2020 audit logs
@ -393,13 +393,13 @@ def get_remote_realm_user_counts(
def get_remote_server_audit_logs( def get_remote_server_audit_logs(
event_time: datetime = timezone_now(), event_time: datetime | None = None,
) -> dict[int, list[RemoteRealmAuditLog]]: ) -> dict[int, list[RemoteRealmAuditLog]]:
logs_per_server: dict[int, list[RemoteRealmAuditLog]] = defaultdict(list) logs_per_server: dict[int, list[RemoteRealmAuditLog]] = defaultdict(list)
for log in ( for log in (
RemoteRealmAuditLog.objects.filter( RemoteRealmAuditLog.objects.filter(
event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS, event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS,
event_time__lte=event_time, event_time__lte=timezone_now() if event_time is None else event_time,
) )
# Important: extra_data is empty for some pre-2020 audit logs # Important: extra_data is empty for some pre-2020 audit logs
# prior to the introduction of realm_user_count_by_role # prior to the introduction of realm_user_count_by_role

View File

@ -733,7 +733,7 @@ class BillingSession(ABC):
pass pass
@abstractmethod @abstractmethod
def current_count_for_billed_licenses(self, event_time: datetime = timezone_now()) -> int: def current_count_for_billed_licenses(self, event_time: datetime | None = None) -> int:
pass pass
@abstractmethod @abstractmethod
@ -3660,7 +3660,7 @@ class BillingSession(ABC):
customer: Customer, customer: Customer,
tier: int, tier: int,
licenses: int | None = None, licenses: int | None = None,
event_time: datetime = timezone_now(), event_time: datetime | None = None,
) -> int: ) -> int:
if licenses is not None and customer.exempt_from_license_number_check: if licenses is not None and customer.exempt_from_license_number_check:
return licenses return licenses
@ -3927,7 +3927,7 @@ class RealmBillingSession(BillingSession):
return self.user.delivery_email return self.user.delivery_email
@override @override
def current_count_for_billed_licenses(self, event_time: datetime = timezone_now()) -> int: def current_count_for_billed_licenses(self, event_time: datetime | None = None) -> int:
return get_latest_seat_count(self.realm) return get_latest_seat_count(self.realm)
@override @override
@ -4292,7 +4292,7 @@ class RemoteRealmBillingSession(BillingSession):
return self.remote_billing_user.email return self.remote_billing_user.email
@override @override
def current_count_for_billed_licenses(self, event_time: datetime = timezone_now()) -> int: def current_count_for_billed_licenses(self, event_time: datetime | None = None) -> int:
if has_stale_audit_log(self.remote_realm.server): if has_stale_audit_log(self.remote_realm.server):
raise MissingDataError raise MissingDataError
remote_realm_counts = get_remote_realm_guest_and_non_guest_count( remote_realm_counts = get_remote_realm_guest_and_non_guest_count(
@ -4737,7 +4737,7 @@ class RemoteServerBillingSession(BillingSession):
return self.remote_billing_user.email return self.remote_billing_user.email
@override @override
def current_count_for_billed_licenses(self, event_time: datetime = timezone_now()) -> int: def current_count_for_billed_licenses(self, event_time: datetime | None = None) -> int:
if has_stale_audit_log(self.remote_server): if has_stale_audit_log(self.remote_server):
raise MissingDataError raise MissingDataError
remote_server_counts = get_remote_server_guest_and_non_guest_count( remote_server_counts = get_remote_server_guest_and_non_guest_count(

View File

@ -523,7 +523,7 @@ def get_remote_customer_user_count(
def get_remote_server_guest_and_non_guest_count( def get_remote_server_guest_and_non_guest_count(
server_id: int, event_time: datetime = timezone_now() server_id: int, event_time: datetime | None = None
) -> RemoteCustomerUserCount: ) -> RemoteCustomerUserCount:
# For each realm hosted on the server, find the latest audit log # For each realm hosted on the server, find the latest audit log
# entry indicating the number of active users in that realm. # entry indicating the number of active users in that realm.
@ -531,7 +531,7 @@ def get_remote_server_guest_and_non_guest_count(
RemoteRealmAuditLog.objects.filter( RemoteRealmAuditLog.objects.filter(
server_id=server_id, server_id=server_id,
event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS, event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS,
event_time__lte=event_time, event_time__lte=timezone_now() if event_time is None else event_time,
) )
# Important: extra_data is empty for some pre-2020 audit logs # Important: extra_data is empty for some pre-2020 audit logs
# prior to the introduction of realm_user_count_by_role # prior to the introduction of realm_user_count_by_role
@ -553,13 +553,13 @@ def get_remote_server_guest_and_non_guest_count(
def get_remote_realm_guest_and_non_guest_count( def get_remote_realm_guest_and_non_guest_count(
remote_realm: RemoteRealm, event_time: datetime = timezone_now() remote_realm: RemoteRealm, event_time: datetime | None = None
) -> RemoteCustomerUserCount: ) -> RemoteCustomerUserCount:
latest_audit_log = ( latest_audit_log = (
RemoteRealmAuditLog.objects.filter( RemoteRealmAuditLog.objects.filter(
remote_realm=remote_realm, remote_realm=remote_realm,
event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS, event_type__in=RemoteRealmAuditLog.SYNCED_BILLING_EVENTS,
event_time__lte=event_time, event_time__lte=timezone_now() if event_time is None else event_time,
) )
# Important: extra_data is empty for some pre-2020 audit logs # Important: extra_data is empty for some pre-2020 audit logs
# prior to the introduction of realm_user_count_by_role # prior to the introduction of realm_user_count_by_role