Skip to content

Commit

Permalink
Merge pull request #10756 from Johnetordoff/summary-reporter-for-inst…
Browse files Browse the repository at this point in the history
…i-dashboard

[ENG-6124] Create Monthly Reporter for Institution Summary
  • Loading branch information
Johnetordoff authored Oct 7, 2024
2 parents 44dd395 + edeb0ac commit 58c60a5
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 1 deletion.
2 changes: 2 additions & 0 deletions osf/metrics/reporters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .download_count import DownloadCountReporter
from .institution_summary import InstitutionSummaryReporter
from .institutional_users import InstitutionalUsersReporter
from .institution_summary_monthly import InstitutionalSummaryMonthlyReporter
from .new_user_domain import NewUserDomainReporter
from .node_count import NodeCountReporter
from .osfstorage_file_count import OsfstorageFileCountReporter
Expand All @@ -28,3 +29,4 @@ class AllDailyReporters(enum.Enum):
class AllMonthlyReporters(enum.Enum):
SPAM_COUNT = SpamCountReporter
INSTITUTIONAL_USERS = InstitutionalUsersReporter
INSTITUTIONAL_SUMMARY = InstitutionalSummaryMonthlyReporter
103 changes: 103 additions & 0 deletions osf/metrics/reporters/institution_summary_monthly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q, F, Sum

from osf.models import Institution, Preprint, AbstractNode, FileVersion
from osf.models.spam import SpamStatus
from addons.osfstorage.models import OsfStorageFile
from osf.metrics.reports import InstitutionMonthlySummaryReport
from osf.metrics.utils import YearMonth
from ._base import MonthlyReporter


class InstitutionalSummaryMonthlyReporter(MonthlyReporter):
"""Generate an InstitutionMonthlySummaryReport for each institution."""

def report(self, yearmonth: YearMonth):
for institution in Institution.objects.all():
yield self.generate_report(institution, yearmonth)

def generate_report(self, institution, yearmonth):
node_queryset = institution.nodes.filter(
deleted__isnull=True,
created__lt=yearmonth.next_month()
).exclude(
spam_status=SpamStatus.SPAM,
)

preprint_queryset = self.get_published_preprints(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),
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),
published_preprint_count=preprint_queryset.count(),
storage_byte_count=self.get_storage_size(node_queryset, preprint_queryset),
public_file_count=self.get_files(node_queryset, preprint_queryset, is_public=True).count(),
monthly_logged_in_user_count=self.get_monthly_logged_in_user_count(institution, yearmonth),
monthly_active_user_count=self.get_monthly_active_user_count(institution, yearmonth),
)

def _get_count(self, node_queryset, node_type, is_public):
return node_queryset.filter(type=node_type, is_public=is_public, root_id=F('pk')).count()

def get_published_preprints(self, institution, yearmonth):
queryset = Preprint.objects.can_view().filter(
affiliated_institutions=institution,
created__lte=yearmonth.next_month()
).exclude(
spam_status=SpamStatus.SPAM
)

return queryset

def get_files(self, node_queryset, preprint_queryset, is_public=None):
public_kwargs = {}
if is_public:
public_kwargs = {'is_public': is_public}

target_node_q = Q(
target_object_id__in=node_queryset.filter(**public_kwargs).values('pk'),
target_content_type=ContentType.objects.get_for_model(AbstractNode),
)
target_preprint_q = Q(
target_object_id__in=preprint_queryset.values('pk'),
target_content_type=ContentType.objects.get_for_model(Preprint),
)
return OsfStorageFile.objects.filter(
deleted__isnull=True, purged__isnull=True
).filter(target_node_q | target_preprint_q)

def get_storage_size(self, node_queryset, preprint_queryset):
files = self.get_files(node_queryset, preprint_queryset)
return FileVersion.objects.filter(
size__gt=0,
purged__isnull=True,
basefilenode__in=files
).aggregate(storage_bytes=Sum('size', default=0))['storage_bytes']

def get_monthly_logged_in_user_count(self, institution, yearmonth):
return institution.get_institution_users().filter(
date_last_login__gte=yearmonth.target_month(),
date_last_login__lt=yearmonth.next_month()
).count()

def get_monthly_active_user_count(self, institution, yearmonth):
institution_users = institution.get_institution_users().filter(
date_disabled__isnull=True
)

active_users = institution_users.filter(
Q(
logs__created__gte=yearmonth.target_month(),
logs__created__lt=yearmonth.next_month()
) |
Q(
preprint_logs__created__gte=yearmonth.target_month(),
preprint_logs__created__lt=yearmonth.next_month()
)
).distinct()

return active_users.count()
2 changes: 1 addition & 1 deletion osf_tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class BaseNodeFactory(DjangoModelFactory):
title = factory.Faker('catch_phrase')
description = factory.Faker('sentence')
created = factory.LazyFunction(timezone.now)
creator = factory.SubFactory(AuthUserFactory)
creator = factory.LazyAttribute(lambda o: AuthUserFactory())

class Meta:
model = models.Node
Expand Down
Loading

0 comments on commit 58c60a5

Please sign in to comment.