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 73ebebe
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions osf/metrics/reporters/institution_summary_monthly.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,10 @@ def get_monthly_logged_in_user_count(self, institution, yearmonth):
def get_monthly_active_user_count(self, institution, yearmonth):
institution_users = institution.get_institution_users().filter(
date_disabled__isnull=True
)
).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())
).values_list('id', flat=True) # Retrieve only user IDs to minimize data loaded

return len(set(institution_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()

0 comments on commit 73ebebe

Please sign in to comment.