Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

activity: Add filter parameter to installation activity view.

Lauryn Menarda3f101e93d0b
diff --git a/corporate/views/installation_activity.py b/corporate/views/installation_activity.py
index 20b013b27f0..02d510d65af 100644
--- a/corporate/views/installation_activity.py
+++ b/corporate/views/installation_activity.py
@@ -1,5 +1,5 @@
from collections import defaultdict
-from typing import Any
+from typing import Any, Literal
from django.conf import settings
from django.db import connection
@@ -90,13 +90,63 @@ def get_realm_day_counts() -> dict[str, dict[str, Markup]]:
return result
-def realm_summary_table(export: bool) -> str:
+InstalationActivityFilter = Literal["all", "demo", "sponsored", "free", "paid"]
+
+
+def get_where_clause_by_filter(filter: InstalationActivityFilter) -> SQL:
+ if filter == "demo":
+ return SQL(
+ """
+ WHERE
+ _14day_active_humans IS NOT NULL
+ AND realm.demo_organization_scheduled_deletion_date IS NOT NULL
+ """
+ )
+ elif filter == "sponsored":
+ return SQL(
+ """
+ WHERE
+ _14day_active_humans IS NOT NULL
+ AND realm.plan_type = 4
+ """
+ )
+ elif filter == "free":
+ return SQL(
+ """
+ WHERE
+ _14day_active_humans IS NOT NULL
+ AND realm.demo_organization_scheduled_deletion_date IS NULL
+ AND realm.plan_type = 2
+ """
+ )
+ elif filter == "paid":
+ return SQL(
+ """
+ WHERE
+ realm.plan_type IN (3, 10)
+ """
+ )
+ # Default to realms with a paid plan or with one active user in the last
+ # two weeks, i.e., "all" filter.
+ return SQL(
+ """
+ WHERE
+ _14day_active_humans IS NOT NULL
+ OR realm.plan_type IN (3, 10)
+ """
+ )
+
+
+def realm_summary_table(export: bool, filter: InstalationActivityFilter) -> str:
from corporate.lib.stripe import cents_to_dollar_string
now = timezone_now()
- query = SQL(
- """
+ where_clause = get_where_clause_by_filter(filter)
+
+ query = (
+ SQL(
+ """
SELECT
realm.string_id,
realm.demo_organization_scheduled_deletion_date,
@@ -185,13 +235,16 @@ def realm_summary_table(export: bool) -> str:
AND is_active=True
AND role IN %(admin_roles)s
) as realm_admin_user ON realm.id = realm_admin_user.realm_id
- WHERE
- _14day_active_humans IS NOT NULL
- or realm.plan_type = 3
+ """
+ )
+ + where_clause
+ + SQL(
+ """
ORDER BY
dau_count DESC,
string_id ASC
"""
+ )
)
cursor = connection.cursor()
@@ -234,8 +287,13 @@ def realm_summary_table(export: bool) -> str:
total_bot_count = 0
total_wau_count = 0
if settings.BILLING_ENABLED:
- estimated_arrs, plan_rates = get_estimated_arr_and_rate_by_realm()
- total_arr = sum(estimated_arrs.values())
+ estimated_arrs: dict[str, int] = {}
+ plan_rates: dict[str, str] = {}
+ if filter in ["all", "paid"]:
+ estimated_arrs, plan_rates = get_estimated_arr_and_rate_by_realm()
+ total_arr = sum(estimated_arrs.values())
+ else:
+ total_arr = 0
for row in rows:
realm_string_id = row.pop("string_id")
@@ -338,6 +396,7 @@ def realm_summary_table(export: bool) -> str:
utctime=now.isoformat(" ", "minutes"),
billing_enabled=settings.BILLING_ENABLED,
export=export,
+ current_filter=filter,
),
)
return content
@@ -345,8 +404,13 @@ def realm_summary_table(export: bool) -> str:
@require_server_admin
@typed_endpoint
-def get_installation_activity(request: HttpRequest, *, export: Json[bool] = False) -> HttpResponse:
- content: str = realm_summary_table(export)
+def get_installation_activity(
+ request: HttpRequest,
+ *,
+ export: Json[bool] = False,
+ filter: InstalationActivityFilter = "all",
+) -> HttpResponse:
+ content: str = realm_summary_table(export, filter)
title = "Installation activity"
return render(
diff --git a/templates/corporate/activity/installation_activity_table.html b/templates/corporate/activity/installation_activity_table.html
index 3831ab4e31c..ef55e7713e0 100644
--- a/templates/corporate/activity/installation_activity_table.html
+++ b/templates/corporate/activity/installation_activity_table.html
@@ -34,6 +34,18 @@
<li><strong>Human message</strong> - message sent by non-bot user, and not with known-bot client</li>
</ul>
+<div class="installation-activity-filter-container">
+ <h4>Current filter: {{ current_filter }}</h4>
+ <p>
+ <b>Filters:</b>
+ <a href="/activity?filter=all">All</a> |
+ <a href="/activity?filter=demo">Demo</a> |
+ <a href="/activity?filter=sponsored">100% sponsored</a> |
+ <a href="/activity?filter=free">On free plan</a> |
+ <a href="/activity?filter=paid">On paid plan</a>
+ </p>
+</div>
+
<table class="table summary-table sortable table-striped table-bordered">
<thead class="activity-head">
diff --git a/web/styles/portico/activity.css b/web/styles/portico/activity.css
index a018db22eb9..80ce8bb277f 100644
--- a/web/styles/portico/activity.css
+++ b/web/styles/portico/activity.css
@@ -31,6 +31,15 @@
}
}
+.installation-activity-filter-container {
+ width: fit-content;
+ border-radius: 4px;
+ background-color: hsl(0deg 0% 92%);
+ border: 1px solid hsl(0deg 0% 68%);
+ padding: 0 8px;
+ margin: 8px;
+}
+
.summary-table,
.analytics-table {
border: 1px solid hsl(0deg 0% 87%);