-
Notifications
You must be signed in to change notification settings - Fork 332
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-6704] [ENG-6705] Reference PR #10857
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
292dca2
[ENG-6364] Migrate Preprint Affilations (#10787)
Johnetordoff f832e5e
[ENG-4438] Add OOPSpam and Akismet metrics to spam report (#10783)
uditijmehta f67b86f
Add PrivateSpamMetricsReport (#10791)
mfraezz eadb41f
[ENG-6435] Fix: duplicate reports when run for past years (#10800)
aaxelb 913889d
[ENG-6506] Fix: counted-usage clobbers (#10799)
aaxelb 6cef157
Merge branch 'hotfix/24.09.3'
mfraezz 0ec9101
Avoid Sequence Scans on BFN
mfraezz 0a510f5
Use low queue for metric reporters
mfraezz ecd96ec
Merge branch 'hotfix/24.09.4'
mfraezz 869c146
Merge branch 'hotfix/24.09.4' into develop
mfraezz 663db9b
Merge remote-tracking branch 'upstream/develop' into feature/b-and-i-…
cslzchen d34cac0
Fix failures caused by base class MonthlyReporter update
cslzchen 8997814
Follow-up fix for target/next (start/end) month
cslzchen ed9bac7
Merge pull request #10822 from cslzchen/feature/b-and-i-with-dashboar…
cslzchen cadb79b
Merge branch 'feature/b-and-i-24-22-release' into release/24.10.0
cslzchen 40e7f26
Update changelog and bump versions
cslzchen 279245a
Merge branch 'release/24.10.0'
cslzchen c025346
Merge tag '24.10.0' into develop
cslzchen d9b4598
Fix backfill, report
mfraezz 86dae50
Merge branch 'hotfix/24.10.1'
mfraezz 68c84ce
Merge branch 'hotfix/24.10.1' into develop
mfraezz c966fac
[Feature] Dashboard B&I (#10843)
mfraezz 4d1708f
Update CHANGELOG, bump version
mfraezz d68a07b
Merge branch 'release/24.11.0'
mfraezz c72f3c6
Merge branch 'release/24.11.0' into develop
mfraezz 6dce520
Assume default for global_ notifications
mfraezz 8643e3f
Merge branch 'hotfix/24.11.1' into develop
mfraezz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
osf/management/commands/migrate_preprint_affiliation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import datetime | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
from django.db.models import F, Exists, OuterRef | ||
|
||
from osf.models import PreprintContributor, InstitutionAffiliation | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
AFFILIATION_TARGET_DATE = datetime.datetime(2024, 9, 19, 14, 37, 48, tzinfo=datetime.timezone.utc) | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Assign affiliations from users to preprints where they have write or admin permissions, with optional exclusion by user GUIDs.""" | ||
|
||
help = 'Assign affiliations from users to preprints where they have write or admin permissions.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--exclude-guids', | ||
nargs='+', | ||
dest='exclude_guids', | ||
help='List of user GUIDs to exclude from affiliation assignment' | ||
) | ||
parser.add_argument( | ||
'--dry-run', | ||
action='store_true', | ||
dest='dry_run', | ||
help='If true, performs a dry run without making changes' | ||
) | ||
parser.add_argument( | ||
'--batch-size', | ||
type=int, | ||
default=1000, | ||
dest='batch_size', | ||
help='Number of contributors to process in each batch' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
start_time = datetime.datetime.now() | ||
logger.info(f'Script started at: {start_time}') | ||
|
||
exclude_guids = set(options.get('exclude_guids') or []) | ||
dry_run = options.get('dry_run', False) | ||
batch_size = options.get('batch_size', 1000) | ||
|
||
if dry_run: | ||
logger.info('Dry run mode activated.') | ||
|
||
processed_count, updated_count = assign_affiliations_to_preprints( | ||
exclude_guids=exclude_guids, | ||
dry_run=dry_run, | ||
batch_size=batch_size | ||
) | ||
|
||
finish_time = datetime.datetime.now() | ||
logger.info(f'Script finished at: {finish_time}') | ||
logger.info(f'Total processed: {processed_count}, Updated: {updated_count}') | ||
logger.info(f'Total run time: {finish_time - start_time}') | ||
|
||
|
||
def assign_affiliations_to_preprints(exclude_guids=None, dry_run=True, batch_size=1000): | ||
exclude_guids = exclude_guids or set() | ||
processed_count = updated_count = 0 | ||
|
||
# Subquery to check if the user has any affiliated institutions | ||
user_has_affiliations = Exists( | ||
InstitutionAffiliation.objects.filter( | ||
user=OuterRef('user') | ||
) | ||
) | ||
|
||
contributors_qs = PreprintContributor.objects.filter( | ||
preprint__preprintgroupobjectpermission__permission__codename__in=['write_preprint'], | ||
preprint__preprintgroupobjectpermission__group__user=F('user'), | ||
).filter( | ||
user_has_affiliations | ||
).select_related( | ||
'user', | ||
'preprint' | ||
).exclude( | ||
user__guids___id__in=exclude_guids | ||
).order_by('pk') # Ensure consistent ordering for batching | ||
|
||
total_contributors = contributors_qs.count() | ||
logger.info(f'Total contributors to process: {total_contributors}') | ||
|
||
# Process contributors in batches | ||
with transaction.atomic(): | ||
for offset in range(0, total_contributors, batch_size): | ||
# Use select_for_update() to ensure query hits the primary database | ||
batch_contributors = contributors_qs[offset:offset + batch_size].select_for_update() | ||
|
||
logger.info(f'Processing contributors {offset + 1} to {min(offset + batch_size, total_contributors)}') | ||
|
||
for contributor in batch_contributors: | ||
user = contributor.user | ||
preprint = contributor.preprint | ||
|
||
if preprint.created > AFFILIATION_TARGET_DATE: | ||
continue | ||
|
||
user_institutions = user.get_affiliated_institutions() | ||
processed_count += 1 | ||
if not dry_run: | ||
preprint.affiliated_institutions.add(*user_institutions) | ||
updated_count += 1 | ||
logger.info( | ||
f'Assigned {len(user_institutions)} affiliations from user <{user._id}> to preprint <{preprint._id}>.' | ||
) | ||
else: | ||
logger.info( | ||
f'Dry run: Would assign {len(user_institutions)} affiliations from user <{user._id}> to preprint <{preprint._id}>.' | ||
) | ||
|
||
return processed_count, updated_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TODO: we may have not covered PreprintLog fixes/updates in DOI, need to take a look.