From 759c8a44d68638c91d3d63bf72b589402a4bdf80 Mon Sep 17 00:00:00 2001 From: John Tordoff <> Date: Fri, 25 Oct 2024 09:45:28 -0400 Subject: [PATCH] add report_date parameter that allows user to specify summary month --- api/institutions/views.py | 15 +++- .../views/test_institution_summary_metrics.py | 68 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/api/institutions/views.py b/api/institutions/views.py index f5602bafcb2..f4ffff6e5c8 100644 --- a/api/institutions/views.py +++ b/api/institutions/views.py @@ -1,3 +1,4 @@ +from datetime import datetime from django.db.models import F from rest_framework import generics from rest_framework import permissions as drf_permissions @@ -13,6 +14,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,7 +614,18 @@ def get_object(self): return object def get_default_search(self): - _yearmonth = InstitutionMonthlySummaryReport.most_recent_yearmonth() + report_date_str = self.request.query_params.get('report_date') + _yearmonth = None + if report_date_str: + try: + _yearmonth = YearMonth.from_str(report_date_str) + except ValueError: + _yearmonth = None + print(_yearmonth) + + if not _yearmonth: + _yearmonth = InstitutionMonthlySummaryReport.most_recent_yearmonth() + if _yearmonth is None: return None return ( 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(