Repository navigation
feat: delete pathway data on learner retirement (ENT-12476) #28
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
Open
sjasti-sonata-svg
wants to merge
1
commit into
master
Choose a base branch
from
sjasti/ENT-12476-retire-pathway-progress
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,4 +2,4 @@ | |
| A plugin to track learners progress in pathways.. | ||
| """ | ||
|
|
||
| __version__ = '2.1.0' | ||
| __version__ = '2.2.0' | ||
This file contains hidden or 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
108 changes: 108 additions & 0 deletions
108
learner_pathway_progress/management/commands/retire_pathway_data_backlog.py
This file contains hidden or 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,108 @@ | ||
| """ | ||
| Management command that deletes the pathway data of learners retired before the retirement receiver was turned on. | ||
| """ | ||
| import logging | ||
| import math | ||
| import time | ||
|
|
||
| from django.core.management import BaseCommand, CommandError | ||
|
|
||
| from learner_pathway_progress.models import LearnerEnterprisePathwayMembership, LearnerPathwayProgress | ||
| from learner_pathway_progress.signals import retire_learner_pathway_data | ||
| from learner_pathway_progress.utilities import completed_retirement_user_ids, cooling_off_user_ids, retired_user_ids | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| LOG_PREFIX = '[Retire Pathway Data Backlog]' | ||
| SKIPPED = ( | ||
| '; skipped %d learners with pathway data in the retirement cooling-off period (handled by the receiver) and %d ' | ||
| 'with another unfinished retirement (if above 0, run again once those retirements finish)' | ||
| ) | ||
| DRY_RUN_MESSAGE = '%s Dry run: would remove the pathway data of %d retired learners: %s' + SKIPPED | ||
| DONE_MESSAGE = '%s Removed the pathway data of %d retired learners: %s' + SKIPPED | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """ | ||
| Delete the pathway progress, its history and the pathway memberships of learners who were already retired. | ||
|
|
||
| The ``USER_RETIRE_LMS_CRITICAL`` receiver only handles retirements that happen after it is turned on. This command | ||
| handles the earlier ones (tech spec EE-9, Privacy Q2): it finds the users whose retirement has finished (see | ||
| ``completed_retirement_user_ids``) who still have pathway data, and removes it with the same | ||
| ``retire_learner_pathway_data`` as the receiver, one learner per transaction. Learners still in the cooling-off | ||
| period, who can cancel, are left alone; the receiver handles them when their retirement runs. A retirement that had | ||
| already passed the LMS step when the receiver was turned on, but has not finished yet (in progress, errored or | ||
| aborted), is skipped too, and the receiver will not see it again. Both groups of skipped learners with pathway data | ||
| are counted separately: only the second one is a reason to run the command again once those retirements finish. | ||
| The usernames of the learners removed here were already replaced, so admin log entries of memberships deleted | ||
| before are not matched by username; those of the rows deleted here are blanked as usual. | ||
|
|
||
| Dry run by default: it only reports how many learners and rows would be removed. Pass ``--commit`` to remove them. | ||
| Logs only counts. | ||
|
|
||
| Examples: | ||
|
|
||
| ./manage.py lms retire_pathway_data_backlog | ||
| ./manage.py lms retire_pathway_data_backlog --commit --batch-size 100 --sleep 1 | ||
| """ | ||
| help = 'Deletes the pathway data of learners who were already retired.' | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument('--commit', action='store_true', help='Delete the data. Without it, dry run.') | ||
| parser.add_argument('--batch-size', type=int, default=100, help='Learners handled between sleeps.') | ||
| parser.add_argument('--sleep', type=float, default=1.0, help='Seconds to wait between batches.') | ||
|
|
||
| def handle(self, *args, **options): | ||
| batch_size = options['batch_size'] | ||
| if batch_size < 1: | ||
| raise CommandError('--batch-size must be at least 1.') | ||
| sleep = options['sleep'] | ||
| if not math.isfinite(sleep) or sleep < 0: | ||
| raise CommandError('--sleep must be a finite number of seconds, 0 or more.') | ||
| retired = completed_retirement_user_ids() | ||
| if retired is None: # pragma: no cover | ||
| raise CommandError('User retirement requests are not available; run this command in the LMS.') | ||
|
|
||
| tables = _pathway_tables(retired) | ||
| user_ids = _user_ids(tables) | ||
| unfinished = set(_user_ids(_pathway_tables(retired_user_ids().exclude(user_id__in=retired)))) | ||
| cooling_off = len(unfinished & set(_user_ids(_pathway_tables(cooling_off_user_ids())))) | ||
| skipped = (cooling_off, len(unfinished) - cooling_off) | ||
|
|
||
| if not options['commit']: | ||
| counts = {name: rows.count() for name, rows in tables.items()} | ||
| log.info(DRY_RUN_MESSAGE, LOG_PREFIX, len(user_ids), counts, *skipped) | ||
| return | ||
|
|
||
| totals = {} | ||
| for start in range(0, len(user_ids), batch_size): | ||
| if start: | ||
| time.sleep(sleep) | ||
| for user_id in user_ids[start:start + batch_size]: | ||
| for name, count in retire_learner_pathway_data(user_id).items(): | ||
| totals[name] = totals.get(name, 0) + count | ||
| log.info(DONE_MESSAGE, LOG_PREFIX, len(user_ids), totals, *skipped) | ||
|
|
||
|
|
||
| def _pathway_tables(user_ids): | ||
| """ | ||
| Return the querysets, by table name, of the pathway rows of the users in ``user_ids``. | ||
| """ | ||
| history_model = LearnerPathwayProgress.history.model # pylint: disable=no-member | ||
| return { | ||
| 'pathway_progress': LearnerPathwayProgress.objects.filter(user_id__in=user_ids), | ||
| 'pathway_progress_history': history_model.objects.filter(user_id__in=user_ids), | ||
| 'pathway_memberships': LearnerEnterprisePathwayMembership.objects.filter(user_id__in=user_ids), | ||
| } | ||
|
|
||
|
|
||
| def _user_ids(tables): | ||
| """ | ||
| Return the sorted ids of the users who have rows in any of ``tables``. | ||
|
|
||
| Each table returns its distinct user ids from the database, so a learner's many history rows count once. The | ||
| default ordering is cleared, or the history table's ordering columns would be part of ``DISTINCT``. | ||
| """ | ||
| return sorted(set().union(*( | ||
| rows.order_by().values_list('user_id', flat=True).distinct() for rows in tables.values() | ||
| ))) |
This file contains hidden or 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 hidden or 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.
Fixed in 6a29185.