2023-12-02 17:55:39 +01:00
|
|
|
from dataclasses import dataclass
|
2024-01-05 22:22:44 +01:00
|
|
|
from datetime import datetime, timedelta
|
2024-01-17 13:55:25 +01:00
|
|
|
from typing import Optional, TypedDict, Union
|
2021-09-29 19:51:55 +02:00
|
|
|
|
2024-01-05 22:22:44 +01:00
|
|
|
from django.db.models import Sum
|
2023-12-02 17:55:39 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2021-09-29 19:51:55 +02:00
|
|
|
|
2023-12-15 18:04:08 +01:00
|
|
|
from corporate.lib.stripe import (
|
|
|
|
BillingSession,
|
2024-02-23 16:51:51 +01:00
|
|
|
PushNotificationsEnabledStatus,
|
2024-03-07 18:06:14 +01:00
|
|
|
RealmBillingSession,
|
2023-12-15 18:04:08 +01:00
|
|
|
RemoteRealmBillingSession,
|
|
|
|
RemoteServerBillingSession,
|
2024-01-31 14:10:45 +01:00
|
|
|
get_configured_fixed_price_plan_offer,
|
2024-08-26 18:01:20 +02:00
|
|
|
get_guest_user_count,
|
|
|
|
get_non_guest_user_count,
|
2024-05-06 06:12:15 +02:00
|
|
|
get_price_per_license,
|
2024-02-23 16:51:51 +01:00
|
|
|
get_push_status_for_remote_request,
|
2024-01-19 19:40:37 +01:00
|
|
|
start_of_next_billing_cycle,
|
2023-12-15 18:04:08 +01:00
|
|
|
)
|
2023-12-13 18:23:05 +01:00
|
|
|
from corporate.models import (
|
|
|
|
Customer,
|
|
|
|
CustomerPlan,
|
2024-01-17 13:55:25 +01:00
|
|
|
CustomerPlanOffer,
|
2023-12-13 18:23:05 +01:00
|
|
|
ZulipSponsorshipRequest,
|
|
|
|
get_current_plan_by_customer,
|
|
|
|
)
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models import Realm
|
2024-09-06 17:29:04 +02:00
|
|
|
from zerver.models.realm_audit_logs import AuditLogEventType
|
2024-11-04 21:01:04 +01:00
|
|
|
from zerver.models.realms import get_org_type_display_name
|
2023-12-06 18:16:20 +01:00
|
|
|
from zilencer.lib.remote_counts import MissingDataError
|
2023-12-15 18:04:08 +01:00
|
|
|
from zilencer.models import (
|
|
|
|
RemoteCustomerUserCount,
|
2024-01-05 22:22:44 +01:00
|
|
|
RemoteInstallationCount,
|
|
|
|
RemotePushDeviceToken,
|
2024-02-05 19:08:30 +01:00
|
|
|
RemoteRealm,
|
2024-01-05 22:22:44 +01:00
|
|
|
RemoteRealmCount,
|
2024-02-05 19:08:30 +01:00
|
|
|
RemoteZulipServer,
|
2023-12-28 19:28:36 +01:00
|
|
|
RemoteZulipServerAuditLog,
|
2023-12-15 18:04:08 +01:00
|
|
|
get_remote_realm_guest_and_non_guest_count,
|
|
|
|
get_remote_server_guest_and_non_guest_count,
|
2024-02-21 19:07:01 +01:00
|
|
|
has_stale_audit_log,
|
2023-12-15 18:04:08 +01:00
|
|
|
)
|
2021-09-29 19:51:55 +02:00
|
|
|
|
2024-02-21 19:07:01 +01:00
|
|
|
USER_DATA_STALE_WARNING = "Recent audit log data missing: No information for used licenses"
|
|
|
|
|
2021-09-29 19:51:55 +02:00
|
|
|
|
2023-12-13 18:23:05 +01:00
|
|
|
class SponsorshipRequestDict(TypedDict):
|
|
|
|
org_type: str
|
|
|
|
org_website: str
|
|
|
|
org_description: str
|
|
|
|
total_users: str
|
|
|
|
paid_users: str
|
|
|
|
paid_users_description: str
|
|
|
|
requested_plan: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SponsorshipData:
|
|
|
|
sponsorship_pending: bool = False
|
2024-08-07 20:53:08 +02:00
|
|
|
has_discount: bool = False
|
2024-07-12 02:30:23 +02:00
|
|
|
monthly_discounted_price: int | None = None
|
|
|
|
annual_discounted_price: int | None = None
|
|
|
|
original_monthly_plan_price: int | None = None
|
|
|
|
original_annual_plan_price: int | None = None
|
|
|
|
minimum_licenses: int | None = None
|
|
|
|
required_plan_tier: int | None = None
|
|
|
|
latest_sponsorship_request: SponsorshipRequestDict | None = None
|
2023-12-13 18:23:05 +01:00
|
|
|
|
|
|
|
|
2024-02-21 16:11:02 +01:00
|
|
|
@dataclass
|
|
|
|
class NextPlanData:
|
|
|
|
plan: Union["CustomerPlan", "CustomerPlanOffer", None] = None
|
2024-07-12 02:30:23 +02:00
|
|
|
estimated_revenue: int | None = None
|
2024-02-21 16:11:02 +01:00
|
|
|
|
|
|
|
|
2023-12-02 17:55:39 +01:00
|
|
|
@dataclass
|
|
|
|
class PlanData:
|
|
|
|
customer: Optional["Customer"] = None
|
|
|
|
current_plan: Optional["CustomerPlan"] = None
|
2024-01-17 13:55:25 +01:00
|
|
|
next_plan: Union["CustomerPlan", "CustomerPlanOffer", None] = None
|
2024-07-12 02:30:23 +02:00
|
|
|
licenses: int | None = None
|
|
|
|
licenses_used: int | None = None
|
|
|
|
next_billing_cycle_start: datetime | None = None
|
2023-12-06 18:09:34 +01:00
|
|
|
is_legacy_plan: bool = False
|
2023-12-11 17:09:36 +01:00
|
|
|
has_fixed_price: bool = False
|
2024-02-02 13:04:41 +01:00
|
|
|
is_current_plan_billable: bool = False
|
2024-07-12 02:30:23 +02:00
|
|
|
stripe_customer_url: str | None = None
|
|
|
|
warning: str | None = None
|
|
|
|
annual_recurring_revenue: int | None = None
|
|
|
|
estimated_next_plan_revenue: int | None = None
|
2023-12-02 17:55:39 +01:00
|
|
|
|
|
|
|
|
2024-01-05 22:22:44 +01:00
|
|
|
@dataclass
|
|
|
|
class MobilePushData:
|
2024-02-06 14:31:22 +01:00
|
|
|
total_mobile_users: int
|
2024-02-23 16:51:51 +01:00
|
|
|
push_notification_status: PushNotificationsEnabledStatus
|
2024-07-12 02:30:23 +02:00
|
|
|
uncategorized_mobile_users: int | None = None
|
|
|
|
mobile_pushes_forwarded: int | None = None
|
2024-02-16 14:12:00 +01:00
|
|
|
last_mobile_push_sent: str = ""
|
2024-01-05 22:22:44 +01:00
|
|
|
|
|
|
|
|
2023-12-13 18:23:05 +01:00
|
|
|
@dataclass
|
2024-03-07 18:06:14 +01:00
|
|
|
class RemoteSupportData:
|
2023-12-28 19:28:36 +01:00
|
|
|
date_created: datetime
|
2024-03-08 12:55:59 +01:00
|
|
|
has_stale_audit_log: bool
|
2023-12-13 18:23:05 +01:00
|
|
|
plan_data: PlanData
|
|
|
|
sponsorship_data: SponsorshipData
|
2023-12-15 18:04:08 +01:00
|
|
|
user_data: RemoteCustomerUserCount
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_push_data: MobilePushData
|
2023-12-13 18:23:05 +01:00
|
|
|
|
|
|
|
|
2024-08-26 18:01:20 +02:00
|
|
|
@dataclass
|
|
|
|
class UserData:
|
|
|
|
guest_user_count: int
|
|
|
|
non_guest_user_count: int
|
|
|
|
|
|
|
|
|
2024-03-07 18:06:14 +01:00
|
|
|
@dataclass
|
|
|
|
class CloudSupportData:
|
|
|
|
plan_data: PlanData
|
|
|
|
sponsorship_data: SponsorshipData
|
2024-08-26 18:01:20 +02:00
|
|
|
user_data: UserData
|
2024-03-07 18:06:14 +01:00
|
|
|
|
|
|
|
|
2024-05-21 20:14:13 +02:00
|
|
|
def get_stripe_customer_url(stripe_id: str) -> str:
|
|
|
|
return f"https://dashboard.stripe.com/customers/{stripe_id}" # nocoverage
|
|
|
|
|
|
|
|
|
2024-08-26 18:01:20 +02:00
|
|
|
def get_realm_user_data(realm: Realm) -> UserData:
|
|
|
|
non_guests = get_non_guest_user_count(realm)
|
|
|
|
guests = get_guest_user_count(realm)
|
|
|
|
return UserData(
|
|
|
|
guest_user_count=guests,
|
|
|
|
non_guest_user_count=non_guests,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-13 18:23:05 +01:00
|
|
|
def get_customer_sponsorship_data(customer: Customer) -> SponsorshipData:
|
|
|
|
pending = customer.sponsorship_pending
|
2023-12-14 19:55:38 +01:00
|
|
|
licenses = customer.minimum_licenses
|
2024-01-10 17:20:08 +01:00
|
|
|
plan_tier = customer.required_plan_tier
|
2024-08-07 20:53:08 +02:00
|
|
|
has_discount = False
|
2023-12-13 18:23:05 +01:00
|
|
|
sponsorship_request = None
|
2024-05-06 06:12:15 +02:00
|
|
|
monthly_discounted_price = None
|
|
|
|
annual_discounted_price = None
|
|
|
|
original_monthly_plan_price = None
|
|
|
|
original_annual_plan_price = None
|
|
|
|
if customer.monthly_discounted_price:
|
2024-08-07 20:53:08 +02:00
|
|
|
has_discount = True
|
2024-05-06 06:12:15 +02:00
|
|
|
monthly_discounted_price = customer.monthly_discounted_price
|
|
|
|
if customer.annual_discounted_price:
|
2024-08-07 20:53:08 +02:00
|
|
|
has_discount = True
|
2024-05-06 06:12:15 +02:00
|
|
|
annual_discounted_price = customer.annual_discounted_price
|
|
|
|
if plan_tier is not None:
|
|
|
|
original_monthly_plan_price = get_price_per_license(
|
|
|
|
plan_tier, CustomerPlan.BILLING_SCHEDULE_MONTHLY
|
|
|
|
)
|
|
|
|
original_annual_plan_price = get_price_per_license(
|
|
|
|
plan_tier, CustomerPlan.BILLING_SCHEDULE_ANNUAL
|
|
|
|
)
|
2023-12-13 18:23:05 +01:00
|
|
|
if pending:
|
|
|
|
last_sponsorship_request = (
|
|
|
|
ZulipSponsorshipRequest.objects.filter(customer=customer).order_by("id").last()
|
|
|
|
)
|
|
|
|
if last_sponsorship_request is not None:
|
|
|
|
org_type_name = get_org_type_display_name(last_sponsorship_request.org_type)
|
|
|
|
if (
|
|
|
|
last_sponsorship_request.org_website is None
|
|
|
|
or last_sponsorship_request.org_website == ""
|
|
|
|
):
|
|
|
|
website = "No website submitted"
|
|
|
|
else:
|
|
|
|
website = last_sponsorship_request.org_website
|
|
|
|
sponsorship_request = SponsorshipRequestDict(
|
|
|
|
org_type=org_type_name,
|
|
|
|
org_website=website,
|
|
|
|
org_description=last_sponsorship_request.org_description,
|
|
|
|
total_users=last_sponsorship_request.expected_total_users,
|
|
|
|
paid_users=last_sponsorship_request.paid_users_count,
|
|
|
|
paid_users_description=last_sponsorship_request.paid_users_description,
|
|
|
|
requested_plan=last_sponsorship_request.requested_plan,
|
|
|
|
)
|
|
|
|
|
|
|
|
return SponsorshipData(
|
|
|
|
sponsorship_pending=pending,
|
2024-08-07 20:53:08 +02:00
|
|
|
has_discount=has_discount,
|
2024-05-06 06:12:15 +02:00
|
|
|
monthly_discounted_price=monthly_discounted_price,
|
|
|
|
annual_discounted_price=annual_discounted_price,
|
|
|
|
original_monthly_plan_price=original_monthly_plan_price,
|
|
|
|
original_annual_plan_price=original_annual_plan_price,
|
2023-12-14 19:55:38 +01:00
|
|
|
minimum_licenses=licenses,
|
2024-01-10 17:20:08 +01:00
|
|
|
required_plan_tier=plan_tier,
|
2023-12-13 18:23:05 +01:00
|
|
|
latest_sponsorship_request=sponsorship_request,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-21 20:53:54 +01:00
|
|
|
def get_annual_invoice_count(billing_schedule: int) -> int:
|
|
|
|
if billing_schedule == CustomerPlan.BILLING_SCHEDULE_MONTHLY:
|
|
|
|
return 12
|
2024-05-22 16:47:29 +02:00
|
|
|
else: # nocoverage
|
2023-12-21 20:53:54 +01:00
|
|
|
return 1
|
|
|
|
|
|
|
|
|
2024-02-21 16:11:02 +01:00
|
|
|
def get_next_plan_data(
|
|
|
|
billing_session: BillingSession,
|
|
|
|
customer: Customer,
|
2024-07-12 02:30:23 +02:00
|
|
|
current_plan: CustomerPlan | None = None,
|
2024-02-21 16:11:02 +01:00
|
|
|
) -> NextPlanData:
|
2024-07-12 02:30:23 +02:00
|
|
|
plan_offer: CustomerPlanOffer | None = None
|
2024-02-21 16:11:02 +01:00
|
|
|
|
|
|
|
# A customer can have a CustomerPlanOffer with or without a current plan.
|
|
|
|
if customer.required_plan_tier:
|
|
|
|
plan_offer = get_configured_fixed_price_plan_offer(customer, customer.required_plan_tier)
|
|
|
|
|
|
|
|
if plan_offer is not None:
|
|
|
|
next_plan_data = NextPlanData(plan=plan_offer)
|
|
|
|
elif current_plan is not None:
|
|
|
|
next_plan_data = NextPlanData(plan=billing_session.get_next_plan(current_plan))
|
|
|
|
else:
|
|
|
|
next_plan_data = NextPlanData()
|
|
|
|
|
|
|
|
if next_plan_data.plan is not None:
|
|
|
|
if next_plan_data.plan.fixed_price is not None:
|
|
|
|
next_plan_data.estimated_revenue = next_plan_data.plan.fixed_price
|
|
|
|
return next_plan_data
|
|
|
|
|
|
|
|
if current_plan is not None:
|
|
|
|
licenses_at_next_renewal = current_plan.licenses_at_next_renewal()
|
|
|
|
if licenses_at_next_renewal is not None:
|
|
|
|
assert type(next_plan_data.plan) is CustomerPlan
|
|
|
|
assert next_plan_data.plan.price_per_license is not None
|
|
|
|
invoice_count = get_annual_invoice_count(next_plan_data.plan.billing_schedule)
|
|
|
|
next_plan_data.estimated_revenue = (
|
|
|
|
next_plan_data.plan.price_per_license * licenses_at_next_renewal * invoice_count
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
next_plan_data.estimated_revenue = 0 # nocoverage
|
|
|
|
return next_plan_data
|
|
|
|
|
|
|
|
return next_plan_data
|
|
|
|
|
|
|
|
|
2024-02-21 19:07:01 +01:00
|
|
|
def get_plan_data_for_support_view(
|
2024-07-12 02:30:23 +02:00
|
|
|
billing_session: BillingSession, user_count: int | None = None, stale_user_data: bool = False
|
2024-02-21 19:07:01 +01:00
|
|
|
) -> PlanData:
|
2023-12-02 17:55:39 +01:00
|
|
|
customer = billing_session.get_customer()
|
|
|
|
plan = None
|
|
|
|
if customer is not None:
|
|
|
|
plan = get_current_plan_by_customer(customer)
|
|
|
|
plan_data = PlanData(
|
|
|
|
customer=customer,
|
|
|
|
current_plan=plan,
|
|
|
|
)
|
2024-01-31 14:10:45 +01:00
|
|
|
|
2023-12-02 17:55:39 +01:00
|
|
|
if plan is not None:
|
|
|
|
new_plan, last_ledger_entry = billing_session.make_end_of_cycle_updates_if_needed(
|
|
|
|
plan, timezone_now()
|
|
|
|
)
|
|
|
|
if last_ledger_entry is not None:
|
|
|
|
if new_plan is not None:
|
|
|
|
plan_data.current_plan = new_plan # nocoverage
|
|
|
|
plan_data.licenses = last_ledger_entry.licenses
|
2024-02-21 19:07:01 +01:00
|
|
|
assert plan_data.current_plan is not None # for mypy
|
|
|
|
|
|
|
|
# If we already have user count data, we use that
|
|
|
|
# instead of querying the database again to get
|
|
|
|
# the number of currently used licenses.
|
|
|
|
if stale_user_data:
|
|
|
|
plan_data.warning = USER_DATA_STALE_WARNING
|
|
|
|
elif user_count is None:
|
2023-12-06 18:16:20 +01:00
|
|
|
try:
|
|
|
|
plan_data.licenses_used = billing_session.current_count_for_billed_licenses()
|
|
|
|
except MissingDataError: # nocoverage
|
2024-02-21 19:07:01 +01:00
|
|
|
plan_data.warning = USER_DATA_STALE_WARNING
|
|
|
|
else: # nocoverage
|
|
|
|
assert user_count is not None
|
|
|
|
plan_data.licenses_used = user_count
|
2023-12-21 20:53:54 +01:00
|
|
|
|
2024-01-19 19:40:37 +01:00
|
|
|
if plan_data.current_plan.status in (
|
|
|
|
CustomerPlan.FREE_TRIAL,
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL,
|
|
|
|
): # nocoverage
|
|
|
|
assert plan_data.current_plan.next_invoice_date is not None
|
|
|
|
plan_data.next_billing_cycle_start = plan_data.current_plan.next_invoice_date
|
|
|
|
else:
|
|
|
|
plan_data.next_billing_cycle_start = start_of_next_billing_cycle(
|
|
|
|
plan_data.current_plan, timezone_now()
|
|
|
|
)
|
|
|
|
|
2023-12-06 18:09:34 +01:00
|
|
|
plan_data.is_legacy_plan = (
|
|
|
|
plan_data.current_plan.tier == CustomerPlan.TIER_SELF_HOSTED_LEGACY
|
|
|
|
)
|
2023-12-11 17:09:36 +01:00
|
|
|
plan_data.has_fixed_price = plan_data.current_plan.fixed_price is not None
|
2024-02-02 13:04:41 +01:00
|
|
|
plan_data.is_current_plan_billable = billing_session.check_plan_tier_is_billable(
|
|
|
|
plan_tier=plan_data.current_plan.tier
|
|
|
|
)
|
2024-01-22 17:12:11 +01:00
|
|
|
if last_ledger_entry is not None:
|
|
|
|
plan_data.annual_recurring_revenue = (
|
2024-05-22 16:47:29 +02:00
|
|
|
billing_session.get_annual_recurring_revenue_for_support_data(
|
2024-01-22 17:12:11 +01:00
|
|
|
plan_data.current_plan, last_ledger_entry
|
|
|
|
)
|
2023-12-21 20:53:54 +01:00
|
|
|
)
|
2024-01-22 17:12:11 +01:00
|
|
|
else:
|
|
|
|
plan_data.annual_recurring_revenue = 0 # nocoverage
|
2023-12-02 17:55:39 +01:00
|
|
|
|
2024-02-21 16:11:02 +01:00
|
|
|
# Check for a non-active/scheduled CustomerPlan or CustomerPlanOffer
|
|
|
|
if customer is not None:
|
|
|
|
next_plan_data = get_next_plan_data(billing_session, customer, plan_data.current_plan)
|
|
|
|
plan_data.next_plan = next_plan_data.plan
|
|
|
|
plan_data.estimated_next_plan_revenue = next_plan_data.estimated_revenue
|
|
|
|
|
2024-05-21 20:14:13 +02:00
|
|
|
# If customer has a stripe ID, add link to stripe customer dashboard
|
|
|
|
if customer is not None and customer.stripe_customer_id is not None:
|
|
|
|
plan_data.stripe_customer_url = get_stripe_customer_url(
|
|
|
|
customer.stripe_customer_id
|
|
|
|
) # nocoverage
|
|
|
|
|
2023-12-02 17:55:39 +01:00
|
|
|
return plan_data
|
2023-12-13 18:23:05 +01:00
|
|
|
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def get_mobile_push_data(remote_entity: RemoteZulipServer | RemoteRealm) -> MobilePushData:
|
2024-02-05 19:08:30 +01:00
|
|
|
if isinstance(remote_entity, RemoteZulipServer):
|
2024-02-06 14:31:22 +01:00
|
|
|
total_users = (
|
2024-02-05 19:08:30 +01:00
|
|
|
RemotePushDeviceToken.objects.filter(server=remote_entity)
|
2024-01-05 22:22:44 +01:00
|
|
|
.distinct("user_id", "user_uuid")
|
|
|
|
.count()
|
|
|
|
)
|
2024-02-06 14:31:22 +01:00
|
|
|
uncategorized_users = (
|
|
|
|
RemotePushDeviceToken.objects.filter(server=remote_entity, remote_realm__isnull=True)
|
|
|
|
.distinct("user_id", "user_uuid")
|
|
|
|
.count()
|
|
|
|
)
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_pushes = RemoteInstallationCount.objects.filter(
|
2024-02-05 19:08:30 +01:00
|
|
|
server=remote_entity,
|
2024-01-05 22:22:44 +01:00
|
|
|
property="mobile_pushes_forwarded::day",
|
2024-10-02 19:40:59 +02:00
|
|
|
subgroup=None,
|
2024-01-05 22:22:44 +01:00
|
|
|
end_time__gte=timezone_now() - timedelta(days=7),
|
|
|
|
).aggregate(total_forwarded=Sum("value", default=0))
|
2024-02-05 19:08:30 +01:00
|
|
|
latest_remote_server_push_forwarded_count = RemoteInstallationCount.objects.filter(
|
|
|
|
server=remote_entity,
|
2024-10-02 19:40:59 +02:00
|
|
|
subgroup=None,
|
2024-02-05 19:08:30 +01:00
|
|
|
property="mobile_pushes_forwarded::day",
|
|
|
|
).last()
|
|
|
|
if latest_remote_server_push_forwarded_count is not None: # nocoverage
|
2024-02-16 14:12:00 +01:00
|
|
|
# mobile_pushes_forwarded is a CountStat with a day frequency,
|
|
|
|
# so we want to show the start of the latest day interval.
|
|
|
|
push_forwarded_interval_start = (
|
|
|
|
latest_remote_server_push_forwarded_count.end_time - timedelta(days=1)
|
|
|
|
).strftime("%Y-%m-%d")
|
|
|
|
else:
|
|
|
|
push_forwarded_interval_start = "None"
|
2024-02-23 16:51:51 +01:00
|
|
|
push_notification_status = get_push_status_for_remote_request(
|
|
|
|
remote_server=remote_entity, remote_realm=None
|
|
|
|
)
|
2024-02-05 19:08:30 +01:00
|
|
|
return MobilePushData(
|
2024-02-06 14:31:22 +01:00
|
|
|
total_mobile_users=total_users,
|
2024-02-23 16:51:51 +01:00
|
|
|
push_notification_status=push_notification_status,
|
2024-02-06 14:31:22 +01:00
|
|
|
uncategorized_mobile_users=uncategorized_users,
|
2024-02-05 19:08:30 +01:00
|
|
|
mobile_pushes_forwarded=mobile_pushes["total_forwarded"],
|
2024-02-16 14:12:00 +01:00
|
|
|
last_mobile_push_sent=push_forwarded_interval_start,
|
2024-01-05 22:22:44 +01:00
|
|
|
)
|
2023-12-19 16:20:18 +01:00
|
|
|
else:
|
2024-02-05 19:08:30 +01:00
|
|
|
assert isinstance(remote_entity, RemoteRealm)
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_users = (
|
2024-02-05 19:08:30 +01:00
|
|
|
RemotePushDeviceToken.objects.filter(remote_realm=remote_entity)
|
2024-01-05 22:22:44 +01:00
|
|
|
.distinct("user_id", "user_uuid")
|
|
|
|
.count()
|
|
|
|
)
|
|
|
|
mobile_pushes = RemoteRealmCount.objects.filter(
|
2024-02-05 19:08:30 +01:00
|
|
|
remote_realm=remote_entity,
|
2024-01-05 22:22:44 +01:00
|
|
|
property="mobile_pushes_forwarded::day",
|
2024-10-02 19:40:59 +02:00
|
|
|
subgroup=None,
|
2024-01-05 22:22:44 +01:00
|
|
|
end_time__gte=timezone_now() - timedelta(days=7),
|
|
|
|
).aggregate(total_forwarded=Sum("value", default=0))
|
2024-02-05 19:08:30 +01:00
|
|
|
latest_remote_realm_push_forwarded_count = RemoteRealmCount.objects.filter(
|
|
|
|
remote_realm=remote_entity,
|
2024-10-02 19:40:59 +02:00
|
|
|
subgroup=None,
|
2024-02-05 19:08:30 +01:00
|
|
|
property="mobile_pushes_forwarded::day",
|
|
|
|
).last()
|
|
|
|
if latest_remote_realm_push_forwarded_count is not None: # nocoverage
|
2024-02-16 14:12:00 +01:00
|
|
|
# mobile_pushes_forwarded is a CountStat with a day frequency,
|
|
|
|
# so we want to show the start of the latest day interval.
|
|
|
|
push_forwarded_interval_start = (
|
|
|
|
latest_remote_realm_push_forwarded_count.end_time - timedelta(days=1)
|
|
|
|
).strftime("%Y-%m-%d")
|
|
|
|
else:
|
|
|
|
push_forwarded_interval_start = "None"
|
2024-02-23 16:51:51 +01:00
|
|
|
push_notification_status = get_push_status_for_remote_request(
|
|
|
|
remote_entity.server, remote_entity
|
|
|
|
)
|
2024-02-05 19:08:30 +01:00
|
|
|
return MobilePushData(
|
2024-02-06 14:31:22 +01:00
|
|
|
total_mobile_users=mobile_users,
|
2024-02-23 16:51:51 +01:00
|
|
|
push_notification_status=push_notification_status,
|
2024-02-06 14:31:22 +01:00
|
|
|
uncategorized_mobile_users=None,
|
2024-02-05 19:08:30 +01:00
|
|
|
mobile_pushes_forwarded=mobile_pushes["total_forwarded"],
|
2024-02-16 14:12:00 +01:00
|
|
|
last_mobile_push_sent=push_forwarded_interval_start,
|
2024-01-05 22:22:44 +01:00
|
|
|
)
|
2024-02-05 19:08:30 +01:00
|
|
|
|
|
|
|
|
2024-03-07 18:06:14 +01:00
|
|
|
def get_data_for_remote_support_view(billing_session: BillingSession) -> RemoteSupportData:
|
2024-02-05 19:08:30 +01:00
|
|
|
if isinstance(billing_session, RemoteServerBillingSession):
|
|
|
|
user_data = get_remote_server_guest_and_non_guest_count(billing_session.remote_server.id)
|
2024-02-21 19:07:01 +01:00
|
|
|
stale_audit_log_data = has_stale_audit_log(billing_session.remote_server)
|
2024-02-05 19:08:30 +01:00
|
|
|
date_created = RemoteZulipServerAuditLog.objects.get(
|
2024-09-06 17:29:04 +02:00
|
|
|
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
|
2024-02-05 19:08:30 +01:00
|
|
|
server__id=billing_session.remote_server.id,
|
|
|
|
).event_time
|
|
|
|
mobile_data = get_mobile_push_data(billing_session.remote_server)
|
|
|
|
else:
|
|
|
|
assert isinstance(billing_session, RemoteRealmBillingSession)
|
|
|
|
user_data = get_remote_realm_guest_and_non_guest_count(billing_session.remote_realm)
|
2024-02-21 19:07:01 +01:00
|
|
|
stale_audit_log_data = has_stale_audit_log(billing_session.remote_realm.server)
|
2024-02-05 19:08:30 +01:00
|
|
|
date_created = billing_session.remote_realm.realm_date_created
|
|
|
|
mobile_data = get_mobile_push_data(billing_session.remote_realm)
|
2024-02-21 19:07:01 +01:00
|
|
|
user_count = user_data.guest_user_count + user_data.non_guest_user_count
|
|
|
|
plan_data = get_plan_data_for_support_view(billing_session, user_count, stale_audit_log_data)
|
2024-02-21 16:11:02 +01:00
|
|
|
if plan_data.customer is not None:
|
|
|
|
sponsorship_data = get_customer_sponsorship_data(plan_data.customer)
|
2023-12-13 18:23:05 +01:00
|
|
|
else:
|
|
|
|
sponsorship_data = SponsorshipData()
|
|
|
|
|
2024-03-07 18:06:14 +01:00
|
|
|
return RemoteSupportData(
|
2023-12-28 19:28:36 +01:00
|
|
|
date_created=date_created,
|
2024-03-08 12:55:59 +01:00
|
|
|
has_stale_audit_log=stale_audit_log_data,
|
2023-12-13 18:23:05 +01:00
|
|
|
plan_data=plan_data,
|
|
|
|
sponsorship_data=sponsorship_data,
|
2023-12-15 18:04:08 +01:00
|
|
|
user_data=user_data,
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_push_data=mobile_data,
|
2023-12-13 18:23:05 +01:00
|
|
|
)
|
2024-03-07 18:06:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_data_for_cloud_support_view(billing_session: BillingSession) -> CloudSupportData:
|
|
|
|
assert isinstance(billing_session, RealmBillingSession)
|
2024-08-26 18:01:20 +02:00
|
|
|
user_data = get_realm_user_data(billing_session.realm)
|
2024-03-07 18:06:14 +01:00
|
|
|
plan_data = get_plan_data_for_support_view(billing_session)
|
|
|
|
if plan_data.customer is not None:
|
|
|
|
sponsorship_data = get_customer_sponsorship_data(plan_data.customer)
|
|
|
|
else:
|
|
|
|
sponsorship_data = SponsorshipData()
|
|
|
|
|
|
|
|
return CloudSupportData(
|
|
|
|
plan_data=plan_data,
|
|
|
|
sponsorship_data=sponsorship_data,
|
2024-08-26 18:01:20 +02:00
|
|
|
user_data=user_data,
|
2024-03-07 18:06:14 +01:00
|
|
|
)
|