2023-10-05 13:25:45 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.shortcuts import render
|
|
|
|
from psycopg2.sql import SQL
|
|
|
|
|
2024-01-29 13:47:19 +01:00
|
|
|
from corporate.lib.activity import (
|
2023-11-28 13:55:09 +01:00
|
|
|
fix_rows,
|
2024-02-13 21:55:37 +01:00
|
|
|
format_datetime_as_date,
|
2024-01-03 19:28:34 +01:00
|
|
|
format_none_as_zero,
|
2024-02-05 19:07:13 +01:00
|
|
|
format_optional_datetime,
|
2024-01-19 17:06:10 +01:00
|
|
|
get_plan_data_by_remote_realm,
|
|
|
|
get_plan_data_by_remote_server,
|
2024-01-29 13:47:19 +01:00
|
|
|
get_query_data,
|
2024-01-19 17:06:10 +01:00
|
|
|
get_remote_realm_user_counts,
|
|
|
|
get_remote_server_audit_logs,
|
2024-01-29 13:47:19 +01:00
|
|
|
make_table,
|
|
|
|
remote_installation_stats_link,
|
|
|
|
remote_installation_support_link,
|
2024-01-19 17:06:10 +01:00
|
|
|
)
|
2023-10-05 13:25:45 +02:00
|
|
|
from zerver.decorator import require_server_admin
|
2024-01-17 22:27:14 +01:00
|
|
|
from zerver.models.realms import get_org_type_display_name
|
2024-01-19 17:06:10 +01:00
|
|
|
from zilencer.models import get_remote_customer_user_count
|
2023-10-05 13:25:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@require_server_admin
|
|
|
|
def get_remote_server_activity(request: HttpRequest) -> HttpResponse:
|
2024-09-24 23:27:28 +02:00
|
|
|
from corporate.lib.stripe import cents_to_dollar_string
|
|
|
|
|
2023-10-05 13:25:45 +02:00
|
|
|
title = "Remote servers"
|
|
|
|
|
|
|
|
query = SQL(
|
|
|
|
"""
|
2024-01-18 11:20:51 +01:00
|
|
|
with mobile_push_forwarded_count as (
|
2023-11-29 15:46:41 +01:00
|
|
|
select
|
|
|
|
server_id,
|
|
|
|
sum(coalesce(value, 0)) as push_forwarded_count
|
|
|
|
from zilencer_remoteinstallationcount
|
|
|
|
where
|
|
|
|
property = 'mobile_pushes_forwarded::day'
|
|
|
|
and end_time >= current_timestamp(0) - interval '7 days'
|
|
|
|
group by server_id
|
|
|
|
),
|
2023-10-05 13:25:45 +02:00
|
|
|
remote_push_devices as (
|
2023-12-04 19:37:49 +01:00
|
|
|
select
|
|
|
|
server_id,
|
|
|
|
count(distinct(user_id, user_uuid)) as push_user_count
|
|
|
|
from zilencer_remotepushdevicetoken
|
2023-10-05 13:25:45 +02:00
|
|
|
group by server_id
|
2024-01-17 22:27:14 +01:00
|
|
|
),
|
2024-02-13 21:55:37 +01:00
|
|
|
remote_server_audit_log as (
|
|
|
|
select
|
|
|
|
server_id,
|
|
|
|
event_time as server_created
|
|
|
|
from zilencer_remotezulipserverauditlog
|
|
|
|
where
|
|
|
|
event_type = 10215
|
|
|
|
group by server_id, event_time
|
|
|
|
),
|
2024-01-17 22:27:14 +01:00
|
|
|
remote_realms as (
|
|
|
|
select
|
|
|
|
server_id,
|
|
|
|
id as realm_id,
|
|
|
|
name as realm_name,
|
2024-01-22 18:00:04 +01:00
|
|
|
org_type as realm_type,
|
2024-02-13 21:55:37 +01:00
|
|
|
host as realm_host,
|
|
|
|
realm_date_created as realm_created
|
2024-01-17 22:27:14 +01:00
|
|
|
from zilencer_remoterealm
|
|
|
|
where
|
|
|
|
is_system_bot_realm = False
|
|
|
|
and realm_deactivated = False
|
|
|
|
group by server_id, id, name, org_type
|
2023-10-05 13:25:45 +02:00
|
|
|
)
|
|
|
|
select
|
|
|
|
rserver.id,
|
2024-01-22 18:00:04 +01:00
|
|
|
realm_id,
|
2024-02-13 21:55:37 +01:00
|
|
|
server_created,
|
|
|
|
realm_created,
|
2024-01-22 18:00:04 +01:00
|
|
|
realm_name,
|
2023-10-05 13:25:45 +02:00
|
|
|
rserver.hostname,
|
2024-01-22 18:00:04 +01:00
|
|
|
realm_host,
|
2023-10-05 13:25:45 +02:00
|
|
|
rserver.contact_email,
|
2023-11-28 13:58:30 +01:00
|
|
|
rserver.last_version,
|
2024-01-03 19:32:04 +01:00
|
|
|
rserver.last_audit_log_update,
|
2023-10-05 13:25:45 +02:00
|
|
|
push_user_count,
|
2024-01-17 22:27:14 +01:00
|
|
|
push_forwarded_count,
|
|
|
|
realm_type
|
2023-10-05 13:25:45 +02:00
|
|
|
from zilencer_remotezulipserver rserver
|
2023-11-29 15:46:41 +01:00
|
|
|
left join mobile_push_forwarded_count on mobile_push_forwarded_count.server_id = rserver.id
|
2023-10-05 13:25:45 +02:00
|
|
|
left join remote_push_devices on remote_push_devices.server_id = rserver.id
|
2024-01-17 22:27:14 +01:00
|
|
|
left join remote_realms on remote_realms.server_id = rserver.id
|
2024-02-13 21:55:37 +01:00
|
|
|
left join remote_server_audit_log on remote_server_audit_log.server_id = rserver.id
|
2023-12-13 05:10:58 +01:00
|
|
|
where not deactivated
|
2024-01-18 11:20:51 +01:00
|
|
|
order by push_user_count DESC NULLS LAST
|
2023-10-05 13:25:45 +02:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
cols = [
|
2024-01-31 21:59:03 +01:00
|
|
|
"Links",
|
2024-01-22 18:00:04 +01:00
|
|
|
"IDs",
|
2024-02-13 21:55:37 +01:00
|
|
|
"Date created",
|
2024-01-22 18:00:04 +01:00
|
|
|
"Realm name",
|
|
|
|
"Realm host or server hostname",
|
2024-01-19 17:06:10 +01:00
|
|
|
"Server contact email",
|
|
|
|
"Server Zulip version",
|
2024-02-05 19:07:13 +01:00
|
|
|
"Server last audit log update (UTC)",
|
2024-01-17 22:27:14 +01:00
|
|
|
"Server mobile users",
|
|
|
|
"Server mobile pushes",
|
2024-01-19 17:06:10 +01:00
|
|
|
"Realm organization type",
|
2023-12-12 19:48:15 +01:00
|
|
|
"Plan name",
|
|
|
|
"Plan status",
|
|
|
|
"ARR",
|
2024-01-31 21:06:53 +01:00
|
|
|
"Rate",
|
2024-01-17 22:27:14 +01:00
|
|
|
"Total users",
|
2023-12-05 09:09:29 +01:00
|
|
|
"Guest users",
|
2023-10-05 13:25:45 +02:00
|
|
|
]
|
|
|
|
|
2024-01-22 18:00:04 +01:00
|
|
|
# If the query or column order above changes, update the constants below
|
2024-02-13 21:55:37 +01:00
|
|
|
# Query constants:
|
2024-01-22 18:00:04 +01:00
|
|
|
SERVER_AND_REALM_IDS = 0
|
2024-02-13 21:55:37 +01:00
|
|
|
SERVER_CREATED = 1
|
|
|
|
REALM_CREATED = 2
|
2024-02-16 14:07:08 +01:00
|
|
|
SERVER_HOST = 3
|
|
|
|
REALM_HOST = 4
|
2024-02-13 21:55:37 +01:00
|
|
|
|
|
|
|
# Column constants:
|
|
|
|
DATE_CREATED = 2
|
|
|
|
LAST_AUDIT_LOG_DATE = 7
|
|
|
|
MOBILE_USER_COUNT = 8
|
|
|
|
MOBILE_PUSH_COUNT = 9
|
|
|
|
ORG_TYPE = 10
|
|
|
|
ARR = 13
|
|
|
|
TOTAL_USER_COUNT = 15
|
|
|
|
GUEST_COUNT = 16
|
2024-01-18 11:40:25 +01:00
|
|
|
|
2023-11-28 13:55:09 +01:00
|
|
|
rows = get_query_data(query)
|
2023-12-12 19:48:15 +01:00
|
|
|
plan_data_by_remote_server = get_plan_data_by_remote_server()
|
2024-01-17 22:27:14 +01:00
|
|
|
plan_data_by_remote_server_and_realm = get_plan_data_by_remote_realm()
|
2024-01-19 17:06:10 +01:00
|
|
|
audit_logs_by_remote_server = get_remote_server_audit_logs()
|
|
|
|
remote_realm_user_counts = get_remote_realm_user_counts()
|
2024-01-17 22:27:14 +01:00
|
|
|
|
|
|
|
total_row = []
|
2024-01-19 17:06:10 +01:00
|
|
|
remote_server_mobile_data_counted = set()
|
2024-01-17 22:27:14 +01:00
|
|
|
total_revenue = 0
|
|
|
|
total_mobile_users = 0
|
|
|
|
total_pushes = 0
|
2023-12-12 19:48:15 +01:00
|
|
|
|
2023-11-28 13:55:09 +01:00
|
|
|
for row in rows:
|
2024-01-22 18:00:04 +01:00
|
|
|
# Create combined IDs column with server and realm IDs
|
|
|
|
server_id = row.pop(SERVER_AND_REALM_IDS)
|
|
|
|
realm_id = row.pop(SERVER_AND_REALM_IDS)
|
|
|
|
if realm_id is not None:
|
|
|
|
ids_string = f"{server_id}/{realm_id}"
|
|
|
|
else:
|
|
|
|
ids_string = f"{server_id}"
|
|
|
|
row.insert(SERVER_AND_REALM_IDS, ids_string)
|
|
|
|
|
2024-02-13 21:55:37 +01:00
|
|
|
# Set date created data
|
|
|
|
# For remote realm row, remove server created value;
|
|
|
|
# for remote server row, remove realm created value
|
|
|
|
if realm_id is not None:
|
|
|
|
row.pop(SERVER_CREATED)
|
|
|
|
else:
|
|
|
|
row.pop(REALM_CREATED)
|
|
|
|
|
2024-01-22 18:00:04 +01:00
|
|
|
# Get server_host for support link
|
|
|
|
# For remote realm row, remove server hostname value;
|
|
|
|
# for remote server row, remove None realm host value
|
|
|
|
if realm_id is not None:
|
|
|
|
server_host = row.pop(SERVER_HOST)
|
|
|
|
else:
|
|
|
|
row.pop(REALM_HOST)
|
|
|
|
server_host = row[SERVER_HOST]
|
|
|
|
|
2024-01-31 21:59:03 +01:00
|
|
|
# Add server links
|
|
|
|
stats = remote_installation_stats_link(server_id)
|
|
|
|
support = remote_installation_support_link(server_host)
|
|
|
|
links = stats + " " + support
|
|
|
|
row.insert(0, links)
|
|
|
|
|
2024-01-17 22:27:14 +01:00
|
|
|
# Count mobile users and pushes forwarded, once per server
|
2024-01-22 18:00:04 +01:00
|
|
|
if server_id not in remote_server_mobile_data_counted:
|
2024-01-17 22:27:14 +01:00
|
|
|
if row[MOBILE_USER_COUNT] is not None:
|
|
|
|
total_mobile_users += row[MOBILE_USER_COUNT] # nocoverage
|
|
|
|
if row[MOBILE_PUSH_COUNT] is not None:
|
|
|
|
total_pushes += row[MOBILE_PUSH_COUNT] # nocoverage
|
2024-01-22 18:00:04 +01:00
|
|
|
remote_server_mobile_data_counted.add(server_id)
|
|
|
|
|
|
|
|
# Get plan, revenue and user count data for row
|
|
|
|
if realm_id is None:
|
|
|
|
plan_data = plan_data_by_remote_server.get(server_id)
|
|
|
|
audit_log_list = audit_logs_by_remote_server.get(server_id)
|
2024-01-19 17:06:10 +01:00
|
|
|
if audit_log_list is None:
|
|
|
|
user_counts = None # nocoverage
|
|
|
|
else:
|
|
|
|
user_counts = get_remote_customer_user_count(audit_log_list)
|
2024-01-17 22:27:14 +01:00
|
|
|
else:
|
2024-01-22 18:00:04 +01:00
|
|
|
server_remote_realms_data = plan_data_by_remote_server_and_realm.get(server_id)
|
2024-01-17 22:27:14 +01:00
|
|
|
if server_remote_realms_data is not None:
|
2024-01-22 18:00:04 +01:00
|
|
|
plan_data = server_remote_realms_data.get(realm_id)
|
2024-01-17 22:27:14 +01:00
|
|
|
else:
|
2024-01-19 17:06:10 +01:00
|
|
|
plan_data = None # nocoverage
|
2024-01-22 18:00:04 +01:00
|
|
|
user_counts = remote_realm_user_counts.get(realm_id)
|
2024-01-17 22:27:14 +01:00
|
|
|
# Format organization type for realm
|
|
|
|
org_type = row[ORG_TYPE]
|
|
|
|
row[ORG_TYPE] = get_org_type_display_name(org_type)
|
2024-01-22 18:00:04 +01:00
|
|
|
|
2024-01-17 22:27:14 +01:00
|
|
|
# Add estimated annual revenue and plan data
|
|
|
|
if plan_data is None:
|
2023-12-12 19:48:15 +01:00
|
|
|
row.append("---")
|
|
|
|
row.append("---")
|
|
|
|
row.append("---")
|
2024-01-31 21:06:53 +01:00
|
|
|
row.append("---")
|
2024-01-19 17:06:10 +01:00
|
|
|
else:
|
2024-01-17 22:27:14 +01:00
|
|
|
total_revenue += plan_data.annual_revenue
|
|
|
|
revenue = cents_to_dollar_string(plan_data.annual_revenue)
|
|
|
|
row.append(plan_data.current_plan_name)
|
|
|
|
row.append(plan_data.current_status)
|
2023-12-19 12:55:08 +01:00
|
|
|
row.append(f"${revenue}")
|
2024-01-31 21:06:53 +01:00
|
|
|
row.append(plan_data.rate)
|
2024-01-22 18:00:04 +01:00
|
|
|
|
2023-12-12 19:48:15 +01:00
|
|
|
# Add user counts
|
2024-01-19 17:06:10 +01:00
|
|
|
if user_counts is None:
|
|
|
|
row.append(0)
|
|
|
|
row.append(0)
|
|
|
|
else:
|
|
|
|
total_users = user_counts.non_guest_user_count + user_counts.guest_user_count
|
|
|
|
row.append(total_users)
|
|
|
|
row.append(user_counts.guest_user_count)
|
2024-01-22 18:00:04 +01:00
|
|
|
|
2024-01-18 11:40:25 +01:00
|
|
|
# Format column data and add total row
|
2024-07-14 21:47:33 +02:00
|
|
|
for i in range(len(cols)):
|
2024-01-18 11:40:25 +01:00
|
|
|
if i == LAST_AUDIT_LOG_DATE:
|
2024-02-05 19:07:13 +01:00
|
|
|
fix_rows(rows, i, format_optional_datetime)
|
2024-01-18 11:40:25 +01:00
|
|
|
if i in [MOBILE_USER_COUNT, MOBILE_PUSH_COUNT]:
|
2024-01-03 19:28:34 +01:00
|
|
|
fix_rows(rows, i, format_none_as_zero)
|
2024-02-13 21:55:37 +01:00
|
|
|
if i == DATE_CREATED:
|
|
|
|
fix_rows(rows, i, format_datetime_as_date)
|
2024-01-22 18:00:04 +01:00
|
|
|
if i == SERVER_AND_REALM_IDS:
|
2023-11-28 13:55:09 +01:00
|
|
|
total_row.append("Total")
|
2024-01-17 22:27:14 +01:00
|
|
|
elif i == MOBILE_USER_COUNT:
|
|
|
|
total_row.append(str(total_mobile_users))
|
|
|
|
elif i == MOBILE_PUSH_COUNT:
|
|
|
|
total_row.append(str(total_pushes))
|
|
|
|
elif i == ARR:
|
|
|
|
total_revenue_string = f"${cents_to_dollar_string(total_revenue)}"
|
|
|
|
total_row.append(total_revenue_string)
|
|
|
|
elif i in [TOTAL_USER_COUNT, GUEST_COUNT]:
|
2023-11-28 13:55:09 +01:00
|
|
|
total_row.append(str(sum(row[i] for row in rows if row[i] is not None)))
|
|
|
|
else:
|
|
|
|
total_row.append("")
|
2023-10-05 13:25:45 +02:00
|
|
|
|
2024-01-25 15:42:50 +01:00
|
|
|
content = make_table(title, cols, rows, totals=total_row)
|
2023-10-05 13:25:45 +02:00
|
|
|
return render(
|
|
|
|
request,
|
2024-01-29 14:53:53 +01:00
|
|
|
"corporate/activity/activity.html",
|
2023-11-28 13:55:09 +01:00
|
|
|
context=dict(data=content, title=title, is_home=False),
|
2023-10-05 13:25:45 +02:00
|
|
|
)
|