From 40ab415005e751b9b6b4b86a3bb0ac44e66a2575 Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Wed, 9 Jun 2021 17:16:12 +0530 Subject: [PATCH] activity: Show effective rate of realms in /activity page. --- analytics/views.py | 23 ++++++++++++++++---- corporate/lib/stripe.py | 8 +++++++ corporate/tests/test_stripe.py | 19 ++++++++++++++++ templates/analytics/realm_summary_table.html | 7 ++++++ zerver/tests/test_presence.py | 2 +- 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/analytics/views.py b/analytics/views.py index 6f8b1ae8f4..01db61ec77 100644 --- a/analytics/views.py +++ b/analytics/views.py @@ -80,10 +80,12 @@ if settings.BILLING_ENABLED: attach_discount_to_realm, downgrade_at_the_end_of_billing_cycle, downgrade_now_without_creating_additional_invoices, + estimate_annual_recurring_revenue_by_realm, get_current_plan_by_realm, get_customer_by_realm, get_discount_for_realm, get_latest_seat_count, + get_realms_to_default_discount_dict, make_end_of_cycle_updates_if_needed, update_billing_method_of_current_plan, update_sponsorship_status, @@ -761,12 +763,24 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str: # estimate annual subscription revenue total_amount = 0 if settings.BILLING_ENABLED: - from corporate.lib.stripe import estimate_annual_recurring_revenue_by_realm - estimated_arrs = estimate_annual_recurring_revenue_by_realm() + realms_to_default_discount = get_realms_to_default_discount_dict() + for row in rows: - if row["string_id"] in estimated_arrs: - row["amount"] = estimated_arrs[row["string_id"]] + string_id = row["string_id"] + + if string_id in estimated_arrs: + row["amount"] = estimated_arrs[string_id] + + if row["plan_type"] == Realm.STANDARD: + row["effective_rate"] = 100 - int(realms_to_default_discount.get(string_id, 0)) + elif row["plan_type"] == Realm.STANDARD_FREE: + row["effective_rate"] = 0 + elif row["plan_type"] == Realm.LIMITED and string_id in realms_to_default_discount: + row["effective_rate"] = 100 - int(realms_to_default_discount[string_id]) + else: + row["effective_rate"] = "" + total_amount += sum(estimated_arrs.values()) # augment data with realm_minutes @@ -808,6 +822,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str: string_id="Total", plan_type_string="", amount=total_amount, + effective_rate="", stats_link="", date_created_day="", realm_owner_emails="", diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 542e70732e..e0136259fa 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -895,6 +895,14 @@ def estimate_annual_recurring_revenue_by_realm() -> Dict[str, int]: # nocoverag return annual_revenue +def get_realms_to_default_discount_dict() -> Dict[str, Decimal]: + realms_to_default_discount = {} + customers = Customer.objects.exclude(default_discount=None).exclude(default_discount=0) + for customer in customers: + realms_to_default_discount[customer.realm.string_id] = customer.default_discount + return realms_to_default_discount + + # During realm deactivation we instantly downgrade the plan to Limited. # Extra users added in the final month are not charged. Also used # for the cancellation of Free Trial. diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index 868dae1c59..18d31900ad 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -32,6 +32,7 @@ from corporate.lib.stripe import ( get_discount_for_realm, get_latest_seat_count, get_price_per_license, + get_realms_to_default_discount_dict, invoice_plan, invoice_plans_as_needed, make_end_of_cycle_updates_if_needed, @@ -2950,6 +2951,24 @@ class BillingHelpersTest(ZulipTestCase): ) self.assertEqual(get_current_plan_by_realm(realm), plan) + def test_get_realms_to_default_discount_dict(self) -> None: + Customer.objects.create(realm=get_realm("zulip"), stripe_customer_id="cus_1") + lear_customer = Customer.objects.create(realm=get_realm("lear"), stripe_customer_id="cus_2") + lear_customer.default_discount = 30 + lear_customer.save(update_fields=["default_discount"]) + zephyr_customer = Customer.objects.create( + realm=get_realm("zephyr"), stripe_customer_id="cus_3" + ) + zephyr_customer.default_discount = 0 + zephyr_customer.save(update_fields=["default_discount"]) + + self.assertEqual( + get_realms_to_default_discount_dict(), + { + "lear": Decimal("30.0000"), + }, + ) + class LicenseLedgerTest(StripeTestCase): def test_add_plan_renewal_if_needed(self) -> None: diff --git a/templates/analytics/realm_summary_table.html b/templates/analytics/realm_summary_table.html index 1bb473c8cc..300d444e8c 100644 --- a/templates/analytics/realm_summary_table.html +++ b/templates/analytics/realm_summary_table.html @@ -24,6 +24,7 @@ Created (green if ≤12wk) Plan type ARR + Effective rate (%) DAU WAU @@ -60,6 +61,12 @@ {% endif %} + + {% if row.effective_rate != "" %} + {{ row.effective_rate }} + {% endif %} + + {% if not loop.first %} diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.py index 99277e2dea..b67d79521c 100644 --- a/zerver/tests/test_presence.py +++ b/zerver/tests/test_presence.py @@ -52,7 +52,7 @@ class ActivityTest(ZulipTestCase): result = self.client_get("/activity") self.assertEqual(result.status_code, 200) - self.assert_length(queries, 18) + self.assert_length(queries, 19) flush_per_request_caches() with queries_captured() as queries: