From f5b3480f442f6538057f20cbc853a7009bc55bd7 Mon Sep 17 00:00:00 2001 From: abram axel booth Date: Fri, 31 Jul 2026 17:22:09 -0400 Subject: [PATCH] wip: fix ENG-11883 --- .../reporters/osfstorage_file_count.py | 54 +++++++++++++++---- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/osf/metrics/reporters/osfstorage_file_count.py b/osf/metrics/reporters/osfstorage_file_count.py index 5db9ad1cff6..20b598b45fd 100644 --- a/osf/metrics/reporters/osfstorage_file_count.py +++ b/osf/metrics/reporters/osfstorage_file_count.py @@ -2,7 +2,11 @@ from django.db.models import Q import logging -from osf.models import AbstractNode, Preprint +from osf.models import ( + AbstractNode, + Preprint, + SpamStatus, +) from osf.metrics.daily_reports import DailyOsfstorageFileCountReport from osf.metrics.utils import cycle_coverage_date from ._base import DailyReporter @@ -13,32 +17,60 @@ class OsfstorageFileCountReporter(DailyReporter): - def report(self, date): + def report(self, report_date): from addons.osfstorage.models import OsfStorageFile - file_qs = OsfStorageFile.objects + file_qs = ( + OsfStorageFile.objects + .filter(created__date__lte=report_date) + .exclude(deleted__date__lte=report_date) + ) abstract_node_content_type = ContentType.objects.get_for_model(AbstractNode) preprint_content_type = ContentType.objects.get_for_model(Preprint) + node_ids = ( + AbstractNode.objects + .exclude(spam_status=SpamStatus.SPAM) + .values('id') + ) + preprint_ids = ( + Preprint.objects + .exclude(spam_status=SpamStatus.SPAM) + .values('id') + ) + + total_query = Q( + target_object_id__in=node_ids, + target_content_type=abstract_node_content_type, + ) | Q( + target_object_id__in=preprint_ids, + target_content_type=preprint_content_type, + ) public_query = Q( - target_object_id__in=AbstractNode.objects.filter(is_public=True).values('id'), - target_content_type__in=[abstract_node_content_type, preprint_content_type], + target_object_id__in=node_ids.filter(is_public=True), + target_content_type=abstract_node_content_type, + ) | Q( + target_object_id__in=preprint_ids.filter(is_public=True), + target_content_type=preprint_content_type, ) private_query = Q( - target_object_id__in=AbstractNode.objects.filter(is_public=False).values('id'), - target_content_type__in=[abstract_node_content_type, preprint_content_type], + target_object_id__in=node_ids.filter(is_public=False), + target_content_type=abstract_node_content_type, + ) | Q( + target_object_id__in=preprint_ids.filter(is_public=False), + target_content_type=preprint_content_type, ) - daily_query = Q(created__date=date) + daily_query = Q(created__date=report_date) yield DailyOsfstorageFileCountReport( - cycle_coverage=cycle_coverage_date(date), + cycle_coverage=cycle_coverage_date(report_date), files=dict( - total=file_qs.count(), + total=file_qs.filter(total_query).count(), public=file_qs.filter(public_query).count(), private=file_qs.filter(private_query).count(), - total_daily=file_qs.filter(daily_query).count(), + total_daily=file_qs.filter(total_query & daily_query).count(), public_daily=file_qs.filter(public_query & daily_query).count(), private_daily=file_qs.filter(private_query & daily_query).count(), ),