zerver: Add a partial audit log index for counting active users.

This index is used by `active_users_audit:is_bot:day`, and provides
roughly a 2x speedup.  The existing
`zerver_realmauditlog_realm__event_type__event_time` is used if there
is a realm limit, but the standard statistics fill runs for all realms
at once, and thus cannot use it.
This commit is contained in:
Alex Vandiver 2024-06-02 15:16:31 +00:00 committed by Tim Abbott
parent 48d3601649
commit 3ea0d73182
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,26 @@
from django.contrib.postgres.operations import AddIndexConcurrently
from django.db import migrations, models
class Migration(migrations.Migration):
atomic = False
dependencies = [
("zerver", "0527_presencesequence"),
]
operations = [
AddIndexConcurrently(
model_name="realmauditlog",
index=models.Index(
# event_type__in:
# AbstractRealmAuditLog.USER_CREATED,
# AbstractRealmAuditLog.USER_ACTIVATED,
# AbstractRealmAuditLog.USER_DEACTIVATED,
# AbstractRealmAuditLog.USER_REACTIVATED,
condition=models.Q(("event_type__in", [101, 102, 103, 104])),
fields=["modified_user", "event_time"],
name="zerver_realmauditlog_user_activations_idx",
),
),
]

View File

@ -242,6 +242,19 @@ class RealmAuditLog(AbstractRealmAuditLog):
] ]
), ),
), ),
models.Index(
# Used in analytics/lib/counts.py for computing active users for realm_active_humans
name="zerver_realmauditlog_user_activations_idx",
fields=["modified_user", "event_time"],
condition=Q(
event_type__in=[
AbstractRealmAuditLog.USER_CREATED,
AbstractRealmAuditLog.USER_ACTIVATED,
AbstractRealmAuditLog.USER_DEACTIVATED,
AbstractRealmAuditLog.USER_REACTIVATED,
]
),
),
] ]
@override @override