Skip to content

Commit

Permalink
Optimize get_monthly_active_user_count by splitting up queries to avo…
Browse files Browse the repository at this point in the history
…id usage of .distinct
  • Loading branch information
John Tordoff committed Oct 8, 2024
1 parent 58c60a5 commit 9004737
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions osf/metrics/reporters/institution_summary_monthly.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,22 @@ def get_monthly_logged_in_user_count(self, institution, yearmonth):
).count()

def get_monthly_active_user_count(self, institution, yearmonth):
institution_users = institution.get_institution_users().filter(
institution_user_ids = institution.get_institution_users().filter(
date_disabled__isnull=True
)
).values_list('id', flat=True)

logs_active_users = institution.logs.filter(
created__gte=yearmonth.target_month(),
created__lt=yearmonth.next_month(),
user_id__in=institution_user_ids
).values_list('user_id', flat=True)

preprint_logs_active_users = institution.preprint_logs.filter(
created__gte=yearmonth.target_month(),
created__lt=yearmonth.next_month(),
user_id__in=institution_user_ids
).values_list('user_id', flat=True)

active_user_ids = set(logs_active_users).union(set(preprint_logs_active_users))

active_users = institution_users.filter(
Q(
logs__created__gte=yearmonth.target_month(),
logs__created__lt=yearmonth.next_month()
) |
Q(
preprint_logs__created__gte=yearmonth.target_month(),
preprint_logs__created__lt=yearmonth.next_month()
)
).distinct()

return active_users.count()
return len(active_user_ids)

0 comments on commit 9004737

Please sign in to comment.