diff --git a/api/institutions/views.py b/api/institutions/views.py index f5602bafcb2..ebc7cb64a60 100644 --- a/api/institutions/views.py +++ b/api/institutions/views.py @@ -13,6 +13,7 @@ from osf.models import OSFUser, Node, Institution, Registration from osf.metrics import UserInstitutionProjectCounts from osf.metrics.reports import InstitutionalUserReport, InstitutionMonthlySummaryReport +from osf.metrics.utils import YearMonth from osf.utils import permissions as osf_permissions from api.base import permissions as base_permissions @@ -612,13 +613,22 @@ def get_object(self): return object def get_default_search(self): - _yearmonth = InstitutionMonthlySummaryReport.most_recent_yearmonth() - if _yearmonth is None: + yearmonth = InstitutionMonthlySummaryReport.most_recent_yearmonth() + if report_date_str := self.request.query_params.get('report_date'): + try: + yearmonth = YearMonth.from_str(report_date_str) + except ValueError: + pass + + if yearmonth is None: return None - return ( - InstitutionMonthlySummaryReport.search() - .filter('term', report_yearmonth=str(_yearmonth)) - .filter('term', institution_id=self.get_institution()._id) + + return InstitutionMonthlySummaryReport.search().filter( + 'term', + report_yearmonth=str(yearmonth), + ).filter( + 'term', + institution_id=self.get_institution()._id, ) diff --git a/api_tests/institutions/views/test_institution_summary_metrics.py b/api_tests/institutions/views/test_institution_summary_metrics.py index ff1d1d34694..06cfaf8a2fa 100644 --- a/api_tests/institutions/views/test_institution_summary_metrics.py +++ b/api_tests/institutions/views/test_institution_summary_metrics.py @@ -265,6 +265,74 @@ def test_get_report_with_multiple_months_and_institutions( assert attributes['monthly_logged_in_user_count'] == 220 assert attributes['monthly_active_user_count'] == 200 + def test_get_with_valid_report_dates(self, app, url, institution, institutional_admin): + _summary_report_factory( + '2024-08', + institution, + user_count=0, + ) + _summary_report_factory( + '2024-09', + institution, + user_count=999, + + ) + _summary_report_factory( + '2018-02', + institution, + user_count=4133, + ) + + resp = app.get(f'{url}?report_date=2024-08', auth=institutional_admin.auth) + assert resp.status_code == 200 + + attributes = resp.json['data']['attributes'] + assert attributes['user_count'] == 0 + + resp = app.get(f'{url}?report_date=2018-02', auth=institutional_admin.auth) + assert resp.status_code == 200 + + attributes = resp.json['data']['attributes'] + assert attributes['user_count'] == 4133 + + def test_get_with_invalid_report_date(self, app, url, institution, institutional_admin): + _summary_report_factory( + '2024-08', + institution, + user_count=0, + ) + _summary_report_factory( + '2024-09', + institution, + user_count=999, + ) + + # Request with an invalid report_date format + resp = app.get(f'{url}?report_date=invalid-date', auth=institutional_admin.auth) + assert resp.status_code == 200 + + # Verify it defaults to the most recent report data + attributes = resp.json['data']['attributes'] + assert attributes['user_count'] == 999 + + def test_get_without_report_date_uses_most_recent(self, app, url, institution, institutional_admin): + _summary_report_factory( + '2024-08', + institution, + user_count=0, + ) + _summary_report_factory( + '2024-09', + institution, + user_count=999, + ) + + resp = app.get(url, auth=institutional_admin.auth) + assert resp.status_code == 200 + + attributes = resp.json['data']['attributes'] + assert attributes['user_count'] == 999 + def _summary_report_factory(yearmonth, institution, **kwargs): report = InstitutionMonthlySummaryReport(