Skip to content

Commit

Permalink
add reporter stat for monthly_active_users
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Sep 23, 2024
1 parent 884bdfc commit 2347759
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/institutions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ class Meta:
department = ser.CharField(read_only=True, source='department_name')
orcid_id = ser.CharField(read_only=True)
month_last_login = YearmonthField(read_only=True)
month_last_active = YearmonthField(read_only=True)
account_creation_date = YearmonthField(read_only=True)

public_projects = ser.IntegerField(read_only=True, source='public_project_count')
Expand Down
1 change: 1 addition & 0 deletions api/institutions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ class _NewInstitutionUserMetricsList(InstitutionMixin, ElasticsearchListView):
'user_name',
'department',
'month_last_login',
'month_last_active',
'account_creation_date',
'public_projects',
'private_projects',
Expand Down
18 changes: 18 additions & 0 deletions osf/metrics/reporters/institutional_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __post_init__(self):
if self.user.date_last_login is not None
else None
),
month_last_active=self.get_month_last_active(),
account_creation_date=YearMonth.from_date(self.user.created),
orcid_id=self.user.get_verified_external_id('ORCID', verified_only=True),
public_project_count=self._public_project_queryset().count(),
Expand Down Expand Up @@ -80,6 +81,23 @@ def _public_project_queryset(self):
root_id=F('pk'), # only root nodes
)

def get_month_last_active(self):
"""
Calculate the last month the user was active, based on their node and preprint logs,
get the most recent
"""
last_node_log_date = self.user.logs.filter(
created__lt=self.before_datetime
).order_by('-created').values_list('created', flat=True).first()

last_preprint_log_date = self.user.preprint_logs.filter(
created__lt=self.before_datetime
).order_by('-created').values_list('created', flat=True).first()

if last_node_log_date and last_preprint_log_date:
most_recent_log = max(last_node_log_date, last_preprint_log_date)
return YearMonth.from_date(most_recent_log)

def _private_project_queryset(self):
return self._node_queryset().filter(
type='osf.node', # `type` field from TypedModel
Expand Down
1 change: 1 addition & 0 deletions osf/metrics/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class InstitutionalUserReport(MonthlyReport):
user_name = metrics.Keyword()
department_name = metrics.Keyword()
month_last_login = YearmonthField()
month_last_active = YearmonthField()
account_creation_date = YearmonthField()
orcid_id = metrics.Keyword()
# counts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ def test_one_user_with_nothing(self):
_reports = list(InstitutionalUsersReporter().report(self._yearmonth))
self.assertEqual(len(_reports), 1)
self._assert_report_matches_setup(_reports[0], self._user_setup_with_nothing)
self.assertEqual(_reports[0].month_last_active, None)

def test_one_user_with_ones(self):
self._user_setup_with_ones.affiliate_user()
_reports = list(InstitutionalUsersReporter().report(self._yearmonth))
self.assertEqual(len(_reports), 1)
self._assert_report_matches_setup(_reports[0], self._user_setup_with_ones)
self.assertEqual(_reports[0].month_last_active, self._yearmonth)

def test_one_user_with_stuff_and_no_files(self):
self._user_setup_with_stuff.affiliate_user()
Expand Down

0 comments on commit 2347759

Please sign in to comment.