2023-10-05 13:25:45 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.shortcuts import render
|
|
|
|
from psycopg2.sql import SQL
|
|
|
|
|
2023-11-28 13:55:09 +01:00
|
|
|
from analytics.views.activity_common import (
|
|
|
|
fix_rows,
|
|
|
|
format_date_for_activity_reports,
|
|
|
|
get_query_data,
|
|
|
|
make_table,
|
|
|
|
remote_installation_stats_link,
|
|
|
|
remote_installation_support_link,
|
|
|
|
)
|
2023-12-12 19:48:15 +01:00
|
|
|
from corporate.lib.analytics import get_plan_data_by_remote_server
|
2023-10-05 13:25:45 +02:00
|
|
|
from zerver.decorator import require_server_admin
|
2023-12-05 09:09:29 +01:00
|
|
|
from zilencer.models import get_remote_server_guest_and_non_guest_count
|
2023-10-05 13:25:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@require_server_admin
|
|
|
|
def get_remote_server_activity(request: HttpRequest) -> HttpResponse:
|
|
|
|
title = "Remote servers"
|
|
|
|
|
|
|
|
query = SQL(
|
|
|
|
"""
|
2023-12-12 20:40:05 +01:00
|
|
|
with icount_id as (
|
2023-10-05 13:25:45 +02:00
|
|
|
select
|
|
|
|
server_id,
|
2023-12-12 20:40:05 +01:00
|
|
|
max(id) as max_count_id
|
2023-10-05 13:25:45 +02:00
|
|
|
from zilencer_remoteinstallationcount
|
|
|
|
where
|
|
|
|
property='active_users:is_bot:day'
|
|
|
|
and subgroup='false'
|
|
|
|
group by server_id
|
2023-12-12 20:40:05 +01:00
|
|
|
),
|
|
|
|
icount as (
|
|
|
|
select
|
|
|
|
icount_id.server_id,
|
|
|
|
value as latest_value,
|
|
|
|
end_time as latest_end_time
|
|
|
|
from icount_id
|
|
|
|
join zilencer_remoteinstallationcount
|
|
|
|
on max_count_id = zilencer_remoteinstallationcount.id
|
|
|
|
),
|
2023-11-29 15:46:41 +01:00
|
|
|
mobile_push_forwarded_count as (
|
|
|
|
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
|
|
|
|
)
|
|
|
|
select
|
|
|
|
rserver.id,
|
|
|
|
rserver.hostname,
|
|
|
|
rserver.contact_email,
|
2023-11-28 13:58:30 +01:00
|
|
|
rserver.last_version,
|
2023-12-12 20:40:05 +01:00
|
|
|
latest_value,
|
2023-10-05 13:25:45 +02:00
|
|
|
push_user_count,
|
2023-12-12 20:40:05 +01:00
|
|
|
latest_end_time,
|
2023-11-29 15:46:41 +01:00
|
|
|
push_forwarded_count
|
2023-10-05 13:25:45 +02:00
|
|
|
from zilencer_remotezulipserver rserver
|
|
|
|
left join icount on icount.server_id = rserver.id
|
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
|
2023-12-13 05:10:58 +01:00
|
|
|
where not deactivated
|
2023-12-12 20:40:05 +01:00
|
|
|
order by latest_value DESC NULLS LAST, push_user_count DESC NULLS LAST
|
2023-10-05 13:25:45 +02:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
cols = [
|
|
|
|
"ID",
|
|
|
|
"Hostname",
|
|
|
|
"Contact email",
|
2023-11-28 13:58:30 +01:00
|
|
|
"Zulip version",
|
2023-10-05 13:25:45 +02:00
|
|
|
"Analytics users",
|
|
|
|
"Mobile users",
|
|
|
|
"Last update time",
|
2023-11-29 15:52:19 +01:00
|
|
|
"Mobile pushes forwarded",
|
2023-12-12 19:48:15 +01:00
|
|
|
"Plan name",
|
|
|
|
"Plan status",
|
|
|
|
"ARR",
|
2023-12-05 09:09:29 +01:00
|
|
|
"Non guest users",
|
|
|
|
"Guest users",
|
2023-11-29 15:52:19 +01:00
|
|
|
"Links",
|
2023-10-05 13:25:45 +02:00
|
|
|
]
|
|
|
|
|
2023-11-28 13:55:09 +01:00
|
|
|
rows = get_query_data(query)
|
|
|
|
total_row = []
|
2023-11-28 13:58:30 +01:00
|
|
|
totals_columns = [4, 5]
|
2023-12-12 19:48:15 +01:00
|
|
|
plan_data_by_remote_server = get_plan_data_by_remote_server()
|
|
|
|
|
2023-11-28 13:55:09 +01:00
|
|
|
for row in rows:
|
2023-12-12 19:48:15 +01:00
|
|
|
# Add estimated revenue for server
|
|
|
|
server_plan_data = plan_data_by_remote_server.get(row[0])
|
|
|
|
if server_plan_data is None:
|
|
|
|
row.append("---")
|
|
|
|
row.append("---")
|
|
|
|
row.append("---")
|
|
|
|
else:
|
|
|
|
row.append(server_plan_data.current_plan_name)
|
|
|
|
row.append(server_plan_data.current_status)
|
|
|
|
row.append(server_plan_data.annual_revenue)
|
|
|
|
# Add user counts
|
2023-12-05 09:09:29 +01:00
|
|
|
remote_server_counts = get_remote_server_guest_and_non_guest_count(row[0])
|
|
|
|
row.append(remote_server_counts.non_guest_user_count)
|
|
|
|
row.append(remote_server_counts.guest_user_count)
|
2023-12-12 19:48:15 +01:00
|
|
|
# Add links
|
|
|
|
stats = remote_installation_stats_link(row[0])
|
|
|
|
support = remote_installation_support_link(row[1])
|
|
|
|
links = stats + " " + support
|
2023-11-29 15:52:19 +01:00
|
|
|
row.append(links)
|
2023-11-28 13:55:09 +01:00
|
|
|
for i, col in enumerate(cols):
|
|
|
|
if col == "Last update time":
|
|
|
|
fix_rows(rows, i, format_date_for_activity_reports)
|
|
|
|
if i == 0:
|
|
|
|
total_row.append("Total")
|
|
|
|
elif i in totals_columns:
|
|
|
|
total_row.append(str(sum(row[i] for row in rows if row[i] is not None)))
|
|
|
|
else:
|
|
|
|
total_row.append("")
|
|
|
|
rows.insert(0, total_row)
|
2023-10-05 13:25:45 +02:00
|
|
|
|
2023-11-28 13:55:09 +01:00
|
|
|
content = make_table(title, cols, rows)
|
2023-10-05 13:25:45 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"analytics/activity_details_template.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
|
|
|
)
|