Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENG-6366] Add Monthly Activity Stat to Dashboard Reporter #10780

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -561,6 +561,7 @@ class _NewInstitutionUserMetricsList(InstitutionMixin, ElasticsearchListView):
'user_name',
'department',
'month_last_login',
'month_last_active',
'account_creation_date',
'public_projects',
'private_projects',
Expand Down
19 changes: 19 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_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 @@ -140,3 +141,21 @@ def _storage_byte_count(self):
purged__isnull=True,
basefilenode__in=self._public_osfstorage_file_queryset(),
).aggregate(storage_bytes=Sum('size', default=0))['storage_bytes']

def _get_last_active(self):
end_date = self.yearmonth.next_month()

node_logs = self.user.logs.filter(created__lt=end_date).order_by('-created')
preprint_logs = self.user.preprint_logs.filter(created__lt=end_date).order_by('-created')

dates = filter(bool, [
node_logs.values_list('created', flat=True).first(),
preprint_logs.values_list('created', flat=True).first(),
])

latest_activity_date = max(dates, default=None)

if latest_activity_date:
return YearMonth.from_date(latest_activity_date)
else:
return None
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
15 changes: 15 additions & 0 deletions osf_tests/metrics/reporters/test_institutional_users_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def _assert_report_matches_setup(self, report: InstitutionalUserReport, setup: _
self.assertEqual(report.user_name, setup.user.fullname)
self.assertEqual(report.department_name, setup.department_name)
self.assertEqual(report.month_last_login, YearMonth.from_date(setup.user.date_last_login))
if setup.month_last_active:
self.assertEqual(report.month_last_active, YearMonth.from_date(setup.month_last_active))
else:
self.assertEqual(report.month_last_active, setup.month_last_active)

self.assertEqual(report.account_creation_date, YearMonth.from_date(setup.user.created))
self.assertEqual(report.orcid_id, setup.orcid_id)
# counts (NOTE: report.public_file_count and report.storage_byte_count tested separately)
Expand Down Expand Up @@ -148,6 +153,7 @@ class _InstiUserSetup:
department_name: str | None = None
orcid_id: str | None = None
user: osfdb.OSFUser = dataclasses.field(init=False)
month_last_active: datetime.datetime | None = dataclasses.field(init=False)

def __post_init__(self):
self.user = UserFactory(
Expand All @@ -159,6 +165,15 @@ def __post_init__(self):
),
)
self._add_affiliations(self._generate_counted_objects())
node_logs = self.user.logs.order_by('-created')
preprint_logs = self.user.preprint_logs.order_by('-created')

dates = filter(bool, [
node_logs.values_list('created', flat=True).first(),
preprint_logs.values_list('created', flat=True).first(),
])

self.month_last_active = max(dates, default=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, small point of "names shouldn't lie" (not to mention that type annotations shouldn't lie):

if leaving names (and annotations) as they are, should cast this to YearMonth here instead of in the test (which would save you the if/else)

or if leaving logic as it is, should rename the field something like date_last_active and change its type annotation to datetime.date | None

either way, as long as words and logic agree


def affiliate_user(self):
self.user.add_or_update_affiliated_institution(
Expand Down
Loading