Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
fix issues in fix_file_counts_cr command
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni Nurmi authored and tompulli committed Apr 1, 2021
1 parent a179ca2 commit 8c43b8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/metax_api/management/commands/fix_file_counts_cr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging

from django.core.management.base import BaseCommand

from metax_api.models import CatalogRecord

logger = logging.getLogger(__name__)

class Command(BaseCommand):
def handle(self, *args, **options):
CRS = CatalogRecord.objects.all()
crs_sum = CRS.count()
logger.info(f"fix_file_counts command found {crs_sum} catalog records with file_count=0 and byte_size=0")
i = 1
for catalog_record in CRS:
logger.info(f"Calculating {i}/{crs_sum} {catalog_record.identifier} ")
catalog_record.calculate_directory_byte_sizes_and_file_counts()
i += 1
logger.info(f"fix_file_counts command executed successfully")
11 changes: 9 additions & 2 deletions src/metax_api/models/catalog_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -2097,9 +2097,13 @@ def calculate_directory_byte_sizes_and_file_counts(self):
if self.research_dataset.get('directories', None):
dir_identifiers = [d['identifier'] for d in self.research_dataset['directories']]

file_dir_identifiers = []
if self.research_dataset.get('files', None):
file_dir_identifiers = [File.objects.get(identifier=f['identifier']).parent_directory.identifier
for f in self.research_dataset['files']]
try:
file_dir_identifiers = [File.objects.get(identifier=f['identifier']).parent_directory.identifier
for f in self.research_dataset['files']]
except Exception as e:
_logger.error(e)

if not dir_identifiers and not file_dir_identifiers:
return
Expand All @@ -2108,6 +2112,9 @@ def calculate_directory_byte_sizes_and_file_counts(self):

highest_level_dirs_by_project = self._get_top_level_parent_dirs_by_project(dir_identifiers)

if len(highest_level_dirs_by_project) == 0:
return

directory_data = {}

for project_identifier, dir_paths in highest_level_dirs_by_project.items():
Expand Down

0 comments on commit 8c43b8c

Please sign in to comment.