Commit 19f7044
authored
perf(importers): batch endpoint creation and status updates during import/reimport (#14489)
* perf(importers): batch endpoint creation and status updates during import/reimport
Replace per-finding endpoint_get_or_create() calls with a stateful EndpointManager
that accumulates endpoints and statuses across findings and flushes them in bulk at
batch boundaries. Reduces ~6200 DB queries to ~3-5 for a 1000-finding scan with 5
endpoints per finding (200 unique).
- EndpointManager now takes a `product` param and holds internal accumulators
- `record_endpoint()` deduplicates by normalized key within a batch
- `record_status_for_create()` / `record_statuses_to_mitigate()` / `record_statuses_to_reactivate()` accumulate operations
- `persist()` flushes all pending creates and bulk_updates in one shot
- `update_endpoint_status()` accumulates mitigate/reactivate lists instead of dispatching per-finding Celery tasks
- Removed old `chunk_endpoints_and_*`, `add_endpoints_to_unsaved_finding`, `mitigate_endpoint_status`, `reactivate_endpoint_status` methods
- Added unit tests for `_make_endpoint_unique_tuple` normalization
- Updated performance test fixture to match new stateful manager interface
* fix endpoint manager initialization
* fix(importers): restore tag inheritance and endpoint_manager init for direct callers
- bulk_create bypasses Django post_save signals, so manually call inherit_instance_tags()
for each newly created Endpoint to preserve product tag inheritance behavior
- Initialize endpoint_manager in create_test() so callers that invoke create_test() +
process_findings() directly (without going through process_scan()) don't hit a NoneType error
* refactor(importers): explicit test param for endpoint manager, rename and improve get_or_create_endpoints
- Add `test` parameter to `_create_endpoint_manager()` so callers pass
the test explicitly instead of relying on `self.test` being set
- Raise `ValueError` with a clear message when `test is None`
- Initialize endpoint_manager in `DefaultImporter.process_scan()` after
`parse_findings()` (which calls `create_test()` for fresh imports),
covering both the sync path and the async Celery task path
- Rename `_fetch_and_create_endpoints()` → `get_or_create_endpoints()`
- Change return type to `tuple[dict, list]`: returns `(endpoints_by_key, created)`
so callers know exactly which endpoints were newly inserted
- Rename local `key_to_endpoint` → `endpoints_by_key` for clarity
* ruff
* fix create endpoint manager
* fix counts
* refactor(importers): initialize EndpointManager eagerly in __init__
Instead of lazily initializing endpoint_manager in process_scan() or
create_test(), initialize it immediately in __init__ using the product
from the required engagement/test parameter. This eliminates the NoneType
AttributeError when callers invoke create_test() + process_findings()
directly without going through process_scan().
- DefaultImporter: uses self.engagement.product (engagement is required)
- DefaultReImporter: uses self.test.engagement.product (test is required)
- Remove _create_endpoint_manager() factory methods and lazy init sites
- Remove self.endpoint_manager = None from BaseImporter.__init__
* add test case
* ruff
* remove mock
* fix: replace status_finding_non_special prefetch with Python filtering
Drop the to_attr Prefetch for status_finding_non_special and instead
prefetch all endpoint statuses, filtering non-special ones in Python
via EndpointManager.get_non_special_endpoint_statuses().
This avoids AttributeError when a finding created during the same
reimport batch (via add_new_finding_to_candidates) is matched by a
subsequent finding — such findings were never loaded through the
prefetch queryset and lacked the to_attr attribute.
See: #145691 parent 0a72930 commit 19f7044
File tree
9 files changed
+716
-180
lines changed- dojo
- finding
- importers
- unittests
- scans/stackhawk
9 files changed
+716
-180
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
339 | | - | |
340 | | - | |
341 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
342 | 346 | | |
343 | 347 | | |
344 | 348 | | |
345 | 349 | | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
| 350 | + | |
350 | 351 | | |
351 | 352 | | |
352 | 353 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
| |||
83 | 82 | | |
84 | 83 | | |
85 | 84 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | 85 | | |
90 | 86 | | |
91 | 87 | | |
| |||
825 | 821 | | |
826 | 822 | | |
827 | 823 | | |
828 | | - | |
829 | | - | |
830 | | - | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
831 | 830 | | |
832 | 831 | | |
833 | | - | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
834 | 835 | | |
835 | 836 | | |
836 | 837 | | |
| |||
934 | 935 | | |
935 | 936 | | |
936 | 937 | | |
937 | | - | |
938 | | - | |
939 | | - | |
940 | | - | |
941 | | - | |
942 | | - | |
943 | | - | |
944 | | - | |
| 938 | + | |
| 939 | + | |
945 | 940 | | |
946 | 941 | | |
947 | 942 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| 60 | + | |
| 61 | + | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
| |||
109 | 112 | | |
110 | 113 | | |
111 | 114 | | |
| 115 | + | |
| 116 | + | |
112 | 117 | | |
113 | 118 | | |
114 | 119 | | |
| |||
259 | 264 | | |
260 | 265 | | |
261 | 266 | | |
262 | | - | |
| 267 | + | |
263 | 268 | | |
| 269 | + | |
| 270 | + | |
264 | 271 | | |
265 | 272 | | |
266 | 273 | | |
| |||
378 | 385 | | |
379 | 386 | | |
380 | 387 | | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
381 | 391 | | |
382 | 392 | | |
383 | 393 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| 80 | + | |
| 81 | + | |
79 | 82 | | |
80 | 83 | | |
81 | 84 | | |
| |||
430 | 433 | | |
431 | 434 | | |
432 | 435 | | |
| 436 | + | |
| 437 | + | |
433 | 438 | | |
434 | 439 | | |
435 | 440 | | |
| |||
497 | 502 | | |
498 | 503 | | |
499 | 504 | | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
500 | 508 | | |
501 | 509 | | |
502 | 510 | | |
| |||
763 | 771 | | |
764 | 772 | | |
765 | 773 | | |
766 | | - | |
767 | | - | |
768 | | - | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
769 | 778 | | |
770 | 779 | | |
771 | 780 | | |
| |||
932 | 941 | | |
933 | 942 | | |
934 | 943 | | |
935 | | - | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
936 | 947 | | |
937 | | - | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
938 | 951 | | |
939 | 952 | | |
940 | 953 | | |
| |||
0 commit comments