Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions osf/metrics/reporters/osfstorage_file_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
),
Expand Down