diff --git a/api/institutions/serializers.py b/api/institutions/serializers.py index 366e36d7079..70afb93c157 100644 --- a/api/institutions/serializers.py +++ b/api/institutions/serializers.py @@ -155,19 +155,11 @@ def create(self, validated_data): 'self': inst, } - -class InstitutionRelated(JSONAPIRelationshipSerializer): - id = ser.CharField(source='_id', required=False, allow_null=True) - class Meta: - type_ = 'institutions' - - class RegistrationRelated(JSONAPIRelationshipSerializer): id = ser.CharField(source='_id', required=False, allow_null=True) class Meta: type_ = 'registrations' - class InstitutionRegistrationsRelationshipSerializer(BaseAPISerializer): data = ser.ListField(child=RegistrationRelated()) links = LinksField({ @@ -321,6 +313,7 @@ def get_absolute_url(self, obj): }, ) + class NewInstitutionUserMetricsSerializer(JSONAPISerializer): '''serializer for institution-users metrics @@ -364,3 +357,9 @@ class Meta: def get_absolute_url(self): return None # there is no detail view for institution-users + + +class InstitutionRelated(JSONAPIRelationshipSerializer): + id = ser.CharField(source='_id', required=False, allow_null=True) + class Meta: + type_ = 'institutions' diff --git a/osf/metadata/osf_gathering.py b/osf/metadata/osf_gathering.py index 6e5e25c6d0b..9a93f7214fe 100644 --- a/osf/metadata/osf_gathering.py +++ b/osf/metadata/osf_gathering.py @@ -823,7 +823,7 @@ def gather_agents(focus): def gather_affiliated_institutions(focus): if hasattr(focus.dbmodel, 'get_affiliated_institutions'): # like OSFUser institution_qs = focus.dbmodel.get_affiliated_institutions() - elif hasattr(focus.dbmodel, 'affiliated_institutions'): # like AbstractNode + elif hasattr(focus.dbmodel, 'affiliated_institutions'): # like AbstractNode or Preprint institution_qs = focus.dbmodel.affiliated_institutions.all() else: institution_qs = () diff --git a/osf/metrics/reporters/institution_summary_monthly.py b/osf/metrics/reporters/institution_summary_monthly.py index 34c71c2d1d6..a08ac0fd8a5 100644 --- a/osf/metrics/reporters/institution_summary_monthly.py +++ b/osf/metrics/reporters/institution_summary_monthly.py @@ -22,12 +22,12 @@ def generate_report(self, institution, yearmonth): return InstitutionMonthlySummaryReport( institution_id=institution._id, + user_count=institution.get_institution_users().count(), private_project_count=self._get_count(node_queryset, 'osf.node', is_public=False), public_project_count=self._get_count(node_queryset, 'osf.node', is_public=True), - user_count=institution.get_institution_users().count(), public_registration_count=self._get_count(node_queryset, 'osf.registration', is_public=True), embargoed_registration_count=self._get_count(node_queryset, 'osf.registration', is_public=False), - preprint_count=self.get_published_preprints(institution).count(), + published_preprint_count=self.get_published_preprints(institution).count(), storage_byte_count=self.get_storage_size(node_queryset, institution), public_file_count=self.get_files(node_queryset, institution, is_public=True).count(), monthly_logged_in_user_count=self.get_monthly_logged_in_user_count(institution, yearmonth), @@ -81,7 +81,7 @@ def get_monthly_logged_in_user_count(self, institution, yearmonth): start_date, end_date = self.get_month_start_end(yearmonth) return institution.get_institution_users().filter( date_last_login__gte=start_date, - date_last_login__lte=end_date + date_last_login__lt=end_date ).count() def get_monthly_active_user_count(self, institution, yearmonth): @@ -89,5 +89,5 @@ def get_monthly_active_user_count(self, institution, yearmonth): return institution.get_institution_users().filter( date_disabled__isnull=True, date_last_login__gte=start_date, - date_last_login__lte=end_date + date_last_login__lt=end_date ).count() diff --git a/osf/metrics/reporters/institutional_users.py b/osf/metrics/reporters/institutional_users.py index abe835bcb9c..98e13003387 100644 --- a/osf/metrics/reporters/institutional_users.py +++ b/osf/metrics/reporters/institutional_users.py @@ -103,8 +103,6 @@ def _embargoed_registration_queryset(self): ) def _published_preprint_queryset(self): - if not hasattr(osfdb.Preprint, 'affiliated_institutions'): - return osfdb.Preprint.objects.none() # HACK: preprints affiliation project still in-progress return ( osfdb.Preprint.objects.can_view() # published/publicly-viewable .filter( diff --git a/osf/metrics/reports.py b/osf/metrics/reports.py index 03f9fd341b0..08d14867ae0 100644 --- a/osf/metrics/reports.py +++ b/osf/metrics/reports.py @@ -275,12 +275,13 @@ class InstitutionalUserReport(MonthlyReport): class InstitutionMonthlySummaryReport(MonthlyReport): UNIQUE_TOGETHER_FIELDS = ('report_yearmonth', 'institution_id', ) institution_id = metrics.Keyword() + user_count = metrics.Integer() public_project_count = metrics.Integer() private_project_count = metrics.Integer() public_registration_count = metrics.Integer() embargoed_registration_count = metrics.Integer() published_preprint_count = metrics.Integer() + storage_byte_count = metrics.Long() public_file_count = metrics.Long() - private_file_count = metrics.Long() - public_storage_count = metrics.Long() - private_storage_count = metrics.Long() + monthly_logged_in_user_count = metrics.Long() + monthly_active_user_count = metrics.Long() diff --git a/osf/migrations/0024_preprint_affiliated_institutions.py b/osf/migrations/0023_preprint_affiliated_institutions.py similarity index 82% rename from osf/migrations/0024_preprint_affiliated_institutions.py rename to osf/migrations/0023_preprint_affiliated_institutions.py index 19673224138..cdfecc03858 100644 --- a/osf/migrations/0024_preprint_affiliated_institutions.py +++ b/osf/migrations/0023_preprint_affiliated_institutions.py @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('osf', '0023_institution_link_to_external_reports_archive'), + ('osf', '0022_alter_abstractnode_subjects_alter_abstractnode_tags_and_more'), ] operations = [ diff --git a/osf/migrations/0023_institution_link_to_external_reports_archive.py b/osf/migrations/0024_institution_link_to_external_reports_archive.py similarity index 84% rename from osf/migrations/0023_institution_link_to_external_reports_archive.py rename to osf/migrations/0024_institution_link_to_external_reports_archive.py index 2900f45e12d..8e1a47fcffb 100644 --- a/osf/migrations/0023_institution_link_to_external_reports_archive.py +++ b/osf/migrations/0024_institution_link_to_external_reports_archive.py @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('osf', '0022_alter_abstractnode_subjects_alter_abstractnode_tags_and_more'), + ('osf', '0023_preprint_affiliated_institutions'), ] operations = [ diff --git a/osf_tests/metrics/reporters/test_institutional_summary_reporter.py b/osf_tests/metrics/reporters/test_institutional_summary_reporter.py index 6d1d51d396b..957523b6276 100644 --- a/osf_tests/metrics/reporters/test_institutional_summary_reporter.py +++ b/osf_tests/metrics/reporters/test_institutional_summary_reporter.py @@ -7,7 +7,6 @@ ProjectFactory, RegistrationFactory, PreprintFactory, - UserFactory, AuthUserFactory, ) @@ -25,7 +24,7 @@ def setUpTestData(cls): cls._public_registration = cls._create_affiliated_registration(is_public=True) cls._embargoed_registration = cls._create_affiliated_registration(is_public=False) - cls._published_preprint = PreprintFactory(creator=UserFactory(), is_public=True) + cls._published_preprint = PreprintFactory(is_public=True) cls._published_preprint.affiliated_institutions.add(cls._institution) cls._logged_in_user = cls._create_logged_in_user() @@ -33,13 +32,13 @@ def setUpTestData(cls): @classmethod def _create_affiliated_project(cls, is_public): - project = ProjectFactory(creator=UserFactory(), is_public=is_public) + project = ProjectFactory(is_public=is_public) project.affiliated_institutions.add(cls._institution) return project @classmethod def _create_affiliated_registration(cls, is_public): - registration = RegistrationFactory(creator=UserFactory(), is_public=is_public) + registration = RegistrationFactory(is_public=is_public) registration.affiliated_institutions.add(cls._institution) return registration @@ -66,10 +65,13 @@ def test_report_generation(self): report = reports[0] self.assertEqual(report.institution_id, self._institution._id) + self.assertEqual(report.user_count, 2) # _logged_in_user and _active_user self.assertEqual(report.public_project_count, 1) self.assertEqual(report.private_project_count, 1) self.assertEqual(report.public_registration_count, 1) self.assertEqual(report.embargoed_registration_count, 1) self.assertEqual(report.published_preprint_count, 1) + self.assertEqual(report.storage_byte_count, 1337) # test value for one file + self.assertEqual(report.public_file_count, 1) self.assertEqual(report.monthly_logged_in_user_count, 1) self.assertEqual(report.monthly_active_user_count, 1) diff --git a/osf_tests/metrics/reporters/test_institutional_users_reporter.py b/osf_tests/metrics/reporters/test_institutional_users_reporter.py index 6af191cc669..8fbb873083f 100644 --- a/osf_tests/metrics/reporters/test_institutional_users_reporter.py +++ b/osf_tests/metrics/reporters/test_institutional_users_reporter.py @@ -20,11 +20,6 @@ ) -def _can_affiliate_preprints() -> bool: - # HACK: preprints affiliation project still in-progress - return hasattr(osfdb.Preprint, 'affiliated_institutions') - - def _patch_now(fakenow: datetime.datetime): return unittest.mock.patch('django.utils.timezone.now', return_value=fakenow) @@ -64,10 +59,7 @@ def _assert_report_matches_setup(self, report: InstitutionalUserReport, setup: _ self.assertEqual(report.private_project_count, setup.private_project_count) self.assertEqual(report.public_registration_count, setup.public_registration_count) self.assertEqual(report.embargoed_registration_count, setup.embargoed_registration_count) - if _can_affiliate_preprints(): - self.assertEqual(report.published_preprint_count, setup.published_preprint_count) - else: - self.assertEqual(report.published_preprint_count, 0) + self.assertEqual(report.published_preprint_count, setup.published_preprint_count) def test_no_users(self): _actual_reports = list(InstitutionalUsersReporter().report(self._yearmonth)) @@ -90,8 +82,8 @@ def test_one_user_with_stuff_and_no_files(self): _reports = list(InstitutionalUsersReporter().report(self._yearmonth)) self.assertEqual(len(_reports), 1) self._assert_report_matches_setup(_reports[0], self._user_setup_with_stuff) - self.assertEqual(_reports[0].public_file_count, 0) - self.assertEqual(_reports[0].storage_byte_count, 0) + self.assertEqual(_reports[0].public_file_count, 2) # preprint 2 files + self.assertEqual(_reports[0].storage_byte_count, 2674) # preprint bytes def test_one_user_with_stuff_and_a_file(self): self._user_setup_with_stuff.affiliate_user() @@ -101,8 +93,8 @@ def test_one_user_with_stuff_and_a_file(self): create_test_file(target=_project, user=_user, size=37) (_report,) = InstitutionalUsersReporter().report(self._yearmonth) self._assert_report_matches_setup(_report, self._user_setup_with_stuff) - self.assertEqual(_report.public_file_count, 1) - self.assertEqual(_report.storage_byte_count, 37) + self.assertEqual(_report.public_file_count, 3) # 2 preprint files + self.assertEqual(_report.storage_byte_count, 2711) # 2 preprint files def test_one_user_with_stuff_and_multiple_files(self): self._user_setup_with_stuff.affiliate_user() @@ -118,8 +110,8 @@ def test_one_user_with_stuff_and_multiple_files(self): create_test_file(target=_component, user=_user, size=47, filename='blarg') (_report,) = InstitutionalUsersReporter().report(self._yearmonth) self._assert_report_matches_setup(_report, self._user_setup_with_stuff) - self.assertEqual(_report.public_file_count, 5) - self.assertEqual(_report.storage_byte_count, 37 + 73 + 53 + 51 + 47) + self.assertEqual(_report.public_file_count, 7) # 2 preprint files + self.assertEqual(_report.storage_byte_count, 2935) # 2 preprint files + 37 + 73 + 53 + 51 + 47 def test_several_users(self): _setups = [ @@ -249,9 +241,7 @@ def _add_embargoed_registration(self) -> osfdb.Registration: ) def _add_published_preprint(self) -> osfdb.Preprint | None: - if _can_affiliate_preprints(): # HACK: preprints affiliation project still in-progress - return PreprintFactory( - creator=self.user, - is_public=True, - ) - return None + return PreprintFactory( + creator=self.user, + is_public=True, + )