-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3244 from GSA-TTS/main
- Loading branch information
Showing
20 changed files
with
435 additions
and
157 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
name: Failed data migration reprocessor | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
environment: | ||
required: true | ||
type: choice | ||
description: The environment the workflow should run on. | ||
options: | ||
- dev | ||
- staging | ||
- preview | ||
year: | ||
required: true | ||
type: string | ||
description: Provide audit year. | ||
page_size: | ||
required: true | ||
type: string | ||
description: Number of audit reports by page. | ||
pages: | ||
required: true | ||
type: string | ||
description: Comma-separated list of pages. | ||
error_tag: | ||
required: true | ||
type: string | ||
description: Error tag associated with failed migrations. | ||
|
||
jobs: | ||
historic-data-migrator: | ||
name: Generate and disseminate historic data in ${{ inputs.environment }} database | ||
runs-on: ubuntu-latest | ||
environment: ${{ inputs.environment }} | ||
env: | ||
space: ${{ inputs.environment }} | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run Django command to generate and disseminate historic data in ${{ inputs.environment }} | ||
uses: cloud-gov/cg-cli-tools@main | ||
with: | ||
cf_username: ${{ secrets.CF_USERNAME }} | ||
cf_password: ${{ secrets.CF_PASSWORD }} | ||
cf_org: gsa-tts-oros-fac | ||
cf_space: ${{ env.space }} | ||
command: cf run-task gsa-fac -k 1G -m 1G --name failed_data_migration_reprocessor --command "python manage.py reprocess_failed_migration --year ${{ inputs.year }} --page_size ${{ inputs.page_size }} --pages ${{ inputs.pages }} --error_tag ${{ inputs.error_tag }}" |
Binary file not shown.
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
47 changes: 47 additions & 0 deletions
47
backend/census_historical_migration/management/commands/reprocess_failed_migration.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,47 @@ | ||
from census_historical_migration.process_failed_migration import ( | ||
reprocess_failed_reports, | ||
) | ||
from census_historical_migration.sac_general_lib.utils import ( | ||
normalize_year_string_or_exit, | ||
) | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
import logging | ||
import sys | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.WARNING) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = """ | ||
Reprocess failed migration reports for a given year and error tag using pagination | ||
Usage: | ||
manage.py run_migration | ||
--year <audit year> | ||
--page_size <page size> | ||
--pages <comma separated pages> | ||
--error_tag <error tag> | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("--year", help="Audit Year") | ||
parser.add_argument("--page_size", help="Number of records by page", type=int) | ||
parser.add_argument("--pages", help="comma separated pages", type=str) | ||
parser.add_argument("--error_tag", help="error tag", type=str) | ||
|
||
def handle(self, *args, **options): | ||
year = normalize_year_string_or_exit(options.get("year")) | ||
|
||
try: | ||
pages_str = options["pages"] | ||
pages = list(map(lambda d: int(d), pages_str.split(","))) | ||
except ValueError: | ||
logger.error(f"Found a non-integer in pages '{pages_str}'") | ||
sys.exit(-1) | ||
|
||
reprocess_failed_reports( | ||
year, options["page_size"], pages, options["error_tag"] | ||
) |
56 changes: 56 additions & 0 deletions
56
backend/census_historical_migration/process_failed_migration.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,56 @@ | ||
import logging | ||
|
||
from .historic_data_loader import ( | ||
create_or_get_user, | ||
log_results, | ||
perform_migration, | ||
) | ||
from .models import ELECAUDITHEADER as AuditHeader, ReportMigrationStatus | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.paginator import Paginator | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
User = get_user_model() | ||
|
||
|
||
def reprocess_failed_reports(audit_year, page_size, pages, error_tag): | ||
"""Iterates over and processes submissions for the given audit year""" | ||
total_count = error_count = 0 | ||
user = create_or_get_user() | ||
failed_migrations = ( | ||
ReportMigrationStatus.objects.filter( | ||
audit_year=audit_year, | ||
migration_status="FAILURE", | ||
migrationerrordetail__tag=error_tag, | ||
) | ||
.order_by("id") | ||
.distinct() | ||
) | ||
|
||
paginator = Paginator(failed_migrations, page_size) | ||
|
||
logger.info( | ||
f"{failed_migrations.count()} reports have failed migration with error tag {error_tag}" | ||
) | ||
|
||
for page_number in pages: | ||
if page_number < paginator.num_pages: | ||
page = paginator.page(page_number) | ||
if page.object_list.count() > 0: | ||
dbkey_list = [status.dbkey for status in page.object_list] | ||
|
||
submissions = AuditHeader.objects.filter( | ||
DBKEY__in=dbkey_list, AUDITYEAR=audit_year | ||
) | ||
logger.info( | ||
f"Processing page {page_number} with {submissions.count() if submissions else 0} submissions." | ||
) | ||
total_count, error_count = perform_migration( | ||
user, submissions, total_count, error_count | ||
) | ||
else: | ||
logger.info(f"Skipping page {page_number} as it is out of range") | ||
|
||
log_results(error_count, total_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.