|
11 | 11 |
|
12 | 12 | logger = logging.getLogger(__name__) |
13 | 13 |
|
| 14 | +# Chunk size for DB cursor and progress report |
| 15 | +CHUNK_SIZE = 1000 |
14 | 16 |
|
15 | 17 | class Command(BaseCommand): |
16 | 18 |
|
@@ -88,11 +90,31 @@ def _associate_location_with_findings(self, endpoint: Endpoint, location: Locati |
88 | 90 | location.associate_with_product(product) |
89 | 91 |
|
90 | 92 | def handle(self, *args, **options): |
| 93 | + # Allow endpoints to work with V3/Locations enabled |
91 | 94 | with Endpoint.allow_endpoint_init(): |
| 95 | + # Progress counter |
| 96 | + i = 0 |
92 | 97 | # Start off with the endpoint objects - it should contain everything we need |
93 | | - for endpoint in Endpoint.objects.all().iterator(): |
| 98 | + queryset = Endpoint.objects.all() |
| 99 | + # Grab the total count so we can communicate progress |
| 100 | + endpoint_count = queryset.count() |
| 101 | + |
| 102 | + # Process each endpoint |
| 103 | + for i, endpoint in enumerate(queryset.iterator(chunk_size=CHUNK_SIZE), 1): |
| 104 | + # Progress report every chunk |
| 105 | + if not i % CHUNK_SIZE: |
| 106 | + self.stdout.write( |
| 107 | + self.style.SUCCESS( |
| 108 | + f"Migrated {i}/{endpoint_count} endpoints..." |
| 109 | + ) |
| 110 | + ) |
94 | 111 | # Get the URL object first |
95 | 112 | location = self._endpoint_to_url(endpoint) |
96 | 113 | # Associate the URL with the findings associated with the Findings |
97 | 114 | # the association to a finding will also apply to a product automatically |
98 | 115 | self._associate_location_with_findings(endpoint, location) |
| 116 | + self.stdout.write( |
| 117 | + self.style.SUCCESS( |
| 118 | + f"Migrated {i} total endpoints." |
| 119 | + ) |
| 120 | + ) |
0 commit comments