remote-activity: Prefetch LicenseLedger entry for current plan.

To estimate the annual recurring revenue for remote server and
remote realm CustomerPlans, we prefetch the current license
ledger as part of the CustomerPlan query.

Also adds a select related to the remote realm audit log query
so that we don't go to the database for the remote realm ID.

With the test added in the previous commit, the query count for
the remote activity view goes from 27 to 11, as we are no longer
hitting the database multiple times for every current plan or
for every remote realm with audit log data.

Refactors get_customer_plan_renewal_amount so that a license
ledger is always passed and make_end_of_cycle_updates_if_needed
does not need to be called.
This commit is contained in:
Lauryn Menard
2024-01-22 17:12:11 +01:00
committed by Tim Abbott
parent 842dcb6546
commit dcae35196c
4 changed files with 107 additions and 55 deletions

View File

@@ -187,7 +187,7 @@ class ActivityTest(ZulipTestCase):
event_time=timezone_now() - timedelta(days=1),
extra_data=extra_data,
)
with self.assert_database_query_count(11):
with self.assert_database_query_count(9):
result = self.client_get("/activity/remote")
self.assertEqual(result.status_code, 200)
@@ -362,6 +362,6 @@ class ActivityTest(ZulipTestCase):
add_audit_log_data(realm.server, remote_realm=realm, realm_id=None)
self.login("iago")
with self.assert_database_query_count(27):
with self.assert_database_query_count(11):
result = self.client_get("/activity/remote")
self.assertEqual(result.status_code, 200)