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

Commit

Permalink
Merge branch '21/02_production_hotfixes' into 'test'
Browse files Browse the repository at this point in the history
21/02 production hotfixes

See merge request fairdata/fairdata-metax!8
  • Loading branch information
Tommi Pulli committed Apr 1, 2021
2 parents d0daf69 + 8c43b8c commit aaba789
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/metax_api/management/commands/fix_file_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

class Command(BaseCommand):
def handle(self, *args, **options):
dirs_with_no_files = Directory.objects.filter(file_count=0, parent_directory=None)
logger.info(f"fix_file_counts command found {dirs_with_no_files.count()} directories with file_count=0")
dirs_with_no_files = Directory.objects_unfiltered.all()
dir_sum = dirs_with_no_files.count()
logger.info(f"fix_file_counts command found {dir_sum} directories")
i=0
for dir in dirs_with_no_files:
dir.calculate_byte_size_and_file_count()
logger.info(f"folder has {dir.file_count} files after recalculation")
i += 1
try:
dir.calculate_byte_size_and_file_count()
except Exception as e:
logger.error(f"can't fix filecount for directory {i}/{dir_sum}")
logger.info(f"folder {i}/{dir_sum} has {dir.file_count} files after recalculation")
logger.info(f"fix_file_counts command executed successfully")
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
1 change: 0 additions & 1 deletion src/metax_api/services/file_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#
# :author: CSC - IT Center for Science Ltd., Espoo Finland <[email protected]>
# :license: MIT

import logging
from collections import defaultdict
from os import getpid
Expand Down

0 comments on commit aaba789

Please sign in to comment.