From a2af94ea693e437564d53043a5dbc30d409dc4ac Mon Sep 17 00:00:00 2001 From: John Tordoff <> Date: Mon, 7 Oct 2024 12:51:12 -0400 Subject: [PATCH] fix up issue with lingering connections and optimize query a tad using pre-select --- .../reporters/institution_summary_monthly.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/osf/metrics/reporters/institution_summary_monthly.py b/osf/metrics/reporters/institution_summary_monthly.py index a46a7af8a98..95baffd0f7b 100644 --- a/osf/metrics/reporters/institution_summary_monthly.py +++ b/osf/metrics/reporters/institution_summary_monthly.py @@ -1,6 +1,7 @@ +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 @@ -8,16 +9,16 @@ 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( @@ -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: