-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
4541 Introduced clean_up_docket_judges command to clean up assigned_to and referred_to #4555
Open
albertisfu
wants to merge
3
commits into
main
Choose a base branch
from
4541-clean-up-docket-judges-command
base: main
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.
+169
−27
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# !/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
import time | ||
|
||
from asgiref.sync import async_to_sync | ||
from django.db.models import Q | ||
|
||
from cl.lib.command_utils import VerboseCommand, logger | ||
from cl.people_db.lookup_utils import lookup_judge_by_full_name | ||
from cl.search.models import Docket | ||
|
||
|
||
def find_and_fix_docket_judges( | ||
iteration_wait: float, testing_mode: bool | ||
) -> None: | ||
"""Find and fix Dockets referred_to and assigned_to judges. | ||
|
||
:param iteration_wait: A float that indicates the time to wait between iterations. | ||
:param testing_mode: True if testing mode is enabled to avoid wait between | ||
iterations. | ||
:return: None | ||
""" | ||
|
||
logger.info("Finding dockets with judges.") | ||
dockets_with_judges = Docket.objects.filter( | ||
Q(referred_to__isnull=False) | Q(assigned_to__isnull=False) | ||
) | ||
total_dockets = dockets_with_judges.count() | ||
logger.info(f"Total dockets to process: {total_dockets}") | ||
|
||
fixed_dockets = 0 | ||
for iteration, d in enumerate(dockets_with_judges.iterator(), start=1): | ||
new_referred = ( | ||
async_to_sync(lookup_judge_by_full_name)( | ||
d.referred_to_str, d.court_id, d.date_filed | ||
) | ||
if d.referred_to_str | ||
else None | ||
) | ||
new_assigned = ( | ||
async_to_sync(lookup_judge_by_full_name)( | ||
d.assigned_to_str, d.court_id, d.date_filed | ||
) | ||
if d.assigned_to_str | ||
else None | ||
) | ||
|
||
if d.referred_to != new_referred or d.assigned_to != new_assigned: | ||
logger.info("Fixing Docket with ID %s", d.pk) | ||
d.referred_to = new_referred | ||
d.assigned_to = new_assigned | ||
d.save() | ||
fixed_dockets += 1 | ||
if not testing_mode: | ||
# This will hit ES. So better to do it slowly. | ||
time.sleep(iteration_wait) | ||
mlissner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Log progress every 100 items. | ||
if iteration % 100 == 0 or iteration == total_dockets: | ||
progress_percentage = (iteration / total_dockets) * 100 | ||
logger.info( | ||
f"Progress: {iteration}/{total_dockets} ({progress_percentage:.2f}%) dockets processed. Fixed: {fixed_dockets}" | ||
) | ||
|
||
logger.info( | ||
f"Completed. Total dockets processed: {total_dockets}, Fixed: {fixed_dockets} Dockets" | ||
) | ||
|
||
|
||
class Command(VerboseCommand): | ||
help = "Find and fix Dockets referred and assigned judges." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--iteration-wait", | ||
type=float, | ||
default="0.1", | ||
help="The time to wait between each iteration.", | ||
) | ||
parser.add_argument( | ||
"--testing-mode", | ||
action="store_true", | ||
help="Use this flag only when running the command in tests based on TestCase", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
testing_mode = options.get("testing_mode", False) | ||
iteration_wait = options.get("iteration_wait", 0.1) | ||
find_and_fix_docket_judges(iteration_wait, testing_mode) |
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.
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.
Could you use lru_cache here to avoid zillions of DB lookups? Maybe set a maxsize of, say 30k so that we catch most judge names?