Skip to content

Commit

Permalink
fix up issue with lingering connections and optimize query a tad usin…
Browse files Browse the repository at this point in the history
…g pre-select
  • Loading branch information
John Tordoff committed Oct 7, 2024
1 parent edeb0ac commit a2af94e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions osf/metrics/reporters/institution_summary_monthly.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from django.db import connection
from django.db.models import Q, F, Sum, Count
from django.db import close_old_connections
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():
close_old_connections()
yield self.generate_report(institution, yearmonth)

def generate_report(self, institution, yearmonth):
node_queryset = institution.nodes.filter(
node_queryset = institution.nodes.select_related('type').filter(
deleted__isnull=True,
created__lt=yearmonth.next_month()
).exclude(
Expand All @@ -44,15 +45,13 @@ 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(
return Preprint.objects.can_view().select_related('affiliated_institutions').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:
Expand Down

0 comments on commit a2af94e

Please sign in to comment.