analytics: Add summary statistic for guest users in realm.

Adds the count of users with the role of guest to the stats view
`page_params` via a database query. This information is then added
to the summary statistics section of the analytics page after being
formatted by `stats.js`.

Creates Bassanio as a guest user in the database for the analytics
realm.

Fixes #20162.
This commit is contained in:
Lauryn Menard 2022-10-10 21:22:46 +02:00 committed by Tim Abbott
parent f37ac80384
commit a3f6220fe4
5 changed files with 28 additions and 0 deletions

View File

@ -92,6 +92,16 @@ class Command(BaseCommand):
) )
do_change_user_role(shylock, UserProfile.ROLE_REALM_OWNER, acting_user=None) do_change_user_role(shylock, UserProfile.ROLE_REALM_OWNER, acting_user=None)
# Create guest user for set_guest_users_statistic.
create_user(
"bassanio@analytics.ds",
"Bassanio",
realm,
full_name="Bassanio",
role=UserProfile.ROLE_GUEST,
force_date_joined=installation_time,
)
administrators_user_group = UserGroup.objects.get( administrators_user_group = UserGroup.objects.get(
name=UserGroup.ADMINISTRATORS_GROUP_NAME, realm=realm, is_system_group=True name=UserGroup.ADMINISTRATORS_GROUP_NAME, realm=realm, is_system_group=True
) )

View File

@ -55,11 +55,18 @@ def render_stats(
analytics_ready: bool = True, analytics_ready: bool = True,
) -> HttpResponse: ) -> HttpResponse:
assert request.user.is_authenticated assert request.user.is_authenticated
# Same query to get guest user count as in get_seat_count in corporate/lib/stripe.py.
guest_users = UserProfile.objects.filter(
realm=request.user.realm, is_active=True, is_bot=False, role=UserProfile.ROLE_GUEST
).count()
page_params = dict( page_params = dict(
data_url_suffix=data_url_suffix, data_url_suffix=data_url_suffix,
for_installation=for_installation, for_installation=for_installation,
remote=remote, remote=remote,
upload_space_used=request.user.realm.currently_used_upload_space_bytes(), upload_space_used=request.user.realm.currently_used_upload_space_bytes(),
guest_users=guest_users,
) )
request_language = get_and_set_request_language( request_language = get_and_set_request_language(

View File

@ -69,6 +69,8 @@ def get_seat_count(
.exclude(role=UserProfile.ROLE_GUEST) .exclude(role=UserProfile.ROLE_GUEST)
.count() .count()
) + extra_non_guests_count ) + extra_non_guests_count
# This guest count calculation should match the similar query in render_stats().
guests = ( guests = (
UserProfile.objects.filter( UserProfile.objects.filter(
realm=realm, is_active=True, is_bot=False, role=UserProfile.ROLE_GUEST realm=realm, is_active=True, is_bot=False, role=UserProfile.ROLE_GUEST

View File

@ -194,6 +194,13 @@ function set_storage_space_used_statistic(upload_space_used) {
$("#id_storage_space_used").closest("summary-stats").show(); $("#id_storage_space_used").closest("summary-stats").show();
} }
function set_guest_users_statistic(guest_users) {
const guest_users_string = guest_users.toLocaleString();
$("#id_guest_users_count").text(guest_users_string);
$("#id_guest_users_count").closest("summary-stats").show();
}
// PLOTLY CHARTS // PLOTLY CHARTS
function populate_messages_sent_over_time(data) { function populate_messages_sent_over_time(data) {
if (data.end_times.length === 0) { if (data.end_times.length === 0) {
@ -1116,3 +1123,4 @@ get_chart_data(
); );
set_storage_space_used_statistic(page_params.upload_space_used); set_storage_space_used_statistic(page_params.upload_space_used);
set_guest_users_statistic(page_params.guest_users);

View File

@ -26,6 +26,7 @@
<ul> <ul>
<li>{{ _("Number of users") }}: <span id="id_total_users"></span></li> <li>{{ _("Number of users") }}: <span id="id_total_users"></span></li>
<li>{{ _("Users active during the last 15 days") }}: <span id="id_active_fifteen_day_users"></span></li> <li>{{ _("Users active during the last 15 days") }}: <span id="id_active_fifteen_day_users"></span></li>
<li>{{ _("Number of guests") }}: <span id="id_guest_users_count"></span></li>
<li>{{ _("Total number of messages") }}: <span id="id_total_messages_sent"></span></li> <li>{{ _("Total number of messages") }}: <span id="id_total_messages_sent"></span></li>
<li>{{ _("Number of messages in the last 30 days") }}: <span id="id_thirty_days_messages_sent"></span></li> <li>{{ _("Number of messages in the last 30 days") }}: <span id="id_thirty_days_messages_sent"></span></li>
<li>{{ _("File storage in use") }}: <span id="id_storage_space_used"></span></li> <li>{{ _("File storage in use") }}: <span id="id_storage_space_used"></span></li>