Skip to content

Commit

Permalink
add report_date parameter that allows user to specify summary month
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Oct 25, 2024
1 parent 2dd6052 commit fb1d90d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
22 changes: 16 additions & 6 deletions api/institutions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)


Expand Down
68 changes: 68 additions & 0 deletions api_tests/institutions/views/test_institution_summary_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit fb1d90d

Please sign in to comment.