-
Notifications
You must be signed in to change notification settings - Fork 351
[ENG-9122] Fix/eng 9122 #11435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ENG-9122] Fix/eng 9122 #11435
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import logging | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from osf.models import Preprint | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def reindex_versioned_preprints(dry_run=False, batch_size=100, provider_id=None, guids=None): | ||
| if guids: | ||
| preprints = Preprint.objects.filter(guids___id__in=guids) | ||
| else: | ||
| preprints = Preprint.objects.filter(versioned_guids__isnull=False).distinct() | ||
|
|
||
| if provider_id: | ||
| preprints = preprints.filter(provider___id=provider_id) | ||
|
|
||
| preprints = preprints.filter(is_published=True) | ||
|
|
||
| total_count = preprints.count() | ||
| logger.info(f'{"[DRY RUN] " if dry_run else ""}Found {total_count} versioned preprints to re-index') | ||
|
|
||
| if total_count == 0: | ||
| logger.info('No preprints to re-index') | ||
| return | ||
|
|
||
| processed = 0 | ||
| for preprint in preprints.iterator(chunk_size=batch_size): | ||
| processed += 1 | ||
|
|
||
| if dry_run: | ||
| logger.info( | ||
| f'[DRY RUN] Would re-index preprint {preprint._id} ' | ||
| f'(version {preprint.versioned_guids.first().version if preprint.versioned_guids.exists() else "N/A"}, ' | ||
| f'date_created_first_version={preprint.date_created_first_version}) ' | ||
| f'[{processed}/{total_count}]' | ||
| ) | ||
| else: | ||
| try: | ||
| preprint.update_search() | ||
| if processed % 10 == 0: | ||
| logger.info( | ||
| f'Re-indexed preprint {preprint._id} ' | ||
| f'(version {preprint.versioned_guids.first().version if preprint.versioned_guids.exists() else "N/A"}) ' | ||
| f'[{processed}/{total_count}]' | ||
| ) | ||
| except Exception as e: | ||
| logger.error(f'Failed to re-index preprint {preprint._id}: {e}') | ||
|
|
||
| logger.info( | ||
| f'{"[DRY RUN] " if dry_run else ""}Completed. ' | ||
| f'{"Would have re-indexed" if dry_run else "Re-indexed"} {processed} preprints' | ||
| ) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = ( | ||
| 'Re-index all versioned preprints to Elasticsearch to ensure computed properties ' | ||
| 'like date_created_first_version are up to date.' | ||
| ) | ||
|
|
||
| def add_arguments(self, parser): | ||
| super().add_arguments(parser) | ||
| parser.add_argument( | ||
| '--dry-run', | ||
| action='store_true', | ||
| dest='dry_run', | ||
| help='Preview what would be re-indexed without actually making changes', | ||
| ) | ||
| parser.add_argument( | ||
| '--batch-size', | ||
| type=int, | ||
| default=100, | ||
| help='Number of preprints to process in each batch (default: 100)', | ||
| ) | ||
| parser.add_argument( | ||
| '--provider', | ||
| type=str, | ||
| help='Optional provider ID to filter preprints', | ||
| ) | ||
| parser.add_argument( | ||
| '--guids', | ||
| type=str, | ||
| nargs='+', | ||
| help='Optional list of specific preprint GUIDs to re-index', | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| dry_run = options.get('dry_run', False) | ||
| batch_size = options.get('batch_size', 100) | ||
| provider_id = options.get('provider') | ||
| guids = options.get('guids') | ||
|
|
||
| if dry_run: | ||
| logger.info('=' * 60) | ||
| logger.info('DRY RUN MODE - No changes will be made') | ||
| logger.info('=' * 60) | ||
|
|
||
| reindex_versioned_preprints( | ||
| dry_run=dry_run, | ||
| batch_size=batch_size, | ||
| provider_id=provider_id, | ||
| guids=guids | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -383,14 +383,18 @@ def create(cls, provider, title, creator, description, manual_guid=None, manual_ | |
|
|
||
| def get_last_not_rejected_version(self): | ||
| """Get the last version that is not rejected. | ||
| Returns None if all versions are rejected. | ||
| """ | ||
| return self.get_guid().versions.filter(is_rejected=False).order_by('-version').first().referent | ||
| last_not_rejected = self.get_guid().versions.filter(is_rejected=False).order_by('-version').first() | ||
| return last_not_rejected.referent if last_not_rejected else None | ||
cslzchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def has_unpublished_pending_version(self): | ||
| """Check if preprint has pending unpublished version. | ||
| Note: use `.check_unfinished_or_unpublished_version()` if checking both types | ||
| """ | ||
| last_not_rejected_version = self.get_last_not_rejected_version() | ||
| if not last_not_rejected_version: | ||
| return False | ||
| return not last_not_rejected_version.date_published and last_not_rejected_version.machine_state == 'pending' | ||
|
|
||
| def has_initiated_but_unfinished_version(self): | ||
|
|
@@ -801,7 +805,7 @@ def date_created_first_version(self): | |
| if not base_guid: | ||
| return self.created | ||
|
|
||
| first_version = base_guid.versions.filter(is_rejected=False).order_by('version').first() | ||
| first_version = base_guid.versions.order_by('version').first() | ||
|
||
|
|
||
| if first_version and first_version.referent: | ||
| return first_version.referent.created | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is a "backfill" command that fix existing preprints' indexing. Do we have to run this daily (or more frequently) to re-index? If understand this correctly, we should/must also fix the indexing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, new preprints and versions are indexed appropriately.