Skip to content

Commit 794d861

Browse files
perf2: skip post processing if not needed (#12862)
* test cases: fix caching of system settings * fix tests * fix caching for github * fix caching for github * simplify cache loading * fix test cases * product grade: skip in finding save if disabled * post process only when needed * cleanup * set tags on (re)import * rebase set tags * reduce save with options * update counts, reduce saves with options
1 parent 2507fc9 commit 794d861

4 files changed

Lines changed: 20 additions & 27 deletions

File tree

dojo/importers/default_importer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ def process_findings(
198198

199199
# Force parsers to use unsaved_tags (stored in below after saving)
200200
unsaved_finding.tags = None
201-
unsaved_finding.save(dedupe_option=False)
201+
# postprocessing will be done on next save.
202+
unsaved_finding.save_no_options()
202203
finding = unsaved_finding
203204
# Determine how the finding should be grouped
204205
self.process_finding_groups(

dojo/importers/default_reimporter.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def process_matched_mitigated_finding(
480480
)
481481
note.save()
482482
existing_finding.notes.add(note)
483-
existing_finding.save(dedupe_option=False)
483+
existing_finding.save_no_options()
484484
# Return True here to force the loop to continue
485485
return existing_finding, True
486486
logger.debug(
@@ -498,9 +498,8 @@ def process_matched_mitigated_finding(
498498
component_version = getattr(unsaved_finding, "component_version", None)
499499
existing_finding.component_name = existing_finding.component_name or component_name
500500
existing_finding.component_version = existing_finding.component_version or component_version
501-
existing_finding.save(dedupe_option=False)
502-
# don't dedupe before endpoints are added
503-
existing_finding.save(dedupe_option=False)
501+
# don't dedupe before endpoints are added, postprocessing will be done on next save (in calling method)
502+
existing_finding.save_no_options()
504503
note = Notes(entry=f"Re-activated by {self.scan_type} re-upload.", author=self.user)
505504
note.save()
506505
endpoint_statuses = existing_finding.status_finding.exclude(
@@ -572,7 +571,7 @@ def process_matched_active_finding(
572571
):
573572
existing_finding.component_name = existing_finding.component_name or component_name
574573
existing_finding.component_version = existing_finding.component_version or component_version
575-
existing_finding.save(dedupe_option=False)
574+
existing_finding.save_no_options()
576575
# Return False here to make sure further processing happens
577576
return existing_finding, False
578577

@@ -595,7 +594,7 @@ def process_finding_that_was_not_matched(
595594
if self.scan_date_override:
596595
unsaved_finding.date = self.scan_date.date()
597596
# Save it. Don't dedupe before endpoints are added.
598-
unsaved_finding.save(dedupe_option=False)
597+
unsaved_finding.save_no_options()
599598
finding = unsaved_finding
600599
# Force parsers to use unsaved_tags (stored in finding_post_processing function below)
601600
finding.tags = None

dojo/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2773,7 +2773,8 @@ def save(self, dedupe_option=True, rules_option=True, product_grading_option=Tru
27732773
self.found_by.add(self.test.test_type)
27742774

27752775
# only perform post processing (in celery task) if needed. this check avoids submitting 1000s of tasks to celery that will do nothing
2776-
if dedupe_option or issue_updater_option or product_grading_option or push_to_jira:
2776+
system_settings = System_Settings.objects.get()
2777+
if dedupe_option or issue_updater_option or (product_grading_option and system_settings.enable_product_grade) or push_to_jira:
27772778
finding_helper.post_process_finding_save(self, dedupe_option=dedupe_option, rules_option=rules_option, product_grading_option=product_grading_option,
27782779
issue_updater_option=issue_updater_option, push_to_jira=push_to_jira, user=user, *args, **kwargs)
27792780
else:

unittests/test_importers_performance.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ def setUp(self):
4242
self.system_settings(enable_webhooks_notifications=False)
4343
self.system_settings(enable_product_grade=False)
4444
self.system_settings(enable_github=False)
45-
# from dojo.models import System_Settings
46-
47-
# # Configure system settings directly
48-
# from dojo.middleware import DojoSytemSettingsMiddleware
49-
# from dojo.models import System_Settings
50-
# system_settings = System_Settings.objects.get()
51-
# system_settings.enable_product_tag_inheritance = True
52-
# system_settings.save()
53-
54-
# Initialize middleware with modified settings
55-
# DojoSytemSettingsMiddleware.initialize_for_testing(System_Settings.objects.get())
5645

5746
# Warm up ContentType cache for relevant models. This is needed if we want to be able to run the test in isolation
5847
# As part of the test suite the ContentTYpe ids will already be cached and won't affect the query count.
@@ -172,9 +161,9 @@ def import_reimport_performance(self, expected_num_queries1, expected_num_async_
172161
def test_import_reimport_reimport_performance(self):
173162
self.import_reimport_performance(
174163
expected_num_queries1=712,
175-
expected_num_async_tasks1=15,
164+
expected_num_async_tasks1=10,
176165
expected_num_queries2=656,
177-
expected_num_async_tasks2=23,
166+
expected_num_async_tasks2=22,
178167
expected_num_queries3=332,
179168
expected_num_async_tasks3=20,
180169
)
@@ -190,9 +179,9 @@ def test_import_reimport_reimport_performance_no_async(self, mock):
190179
"""
191180
self.import_reimport_performance(
192181
expected_num_queries1=712,
193-
expected_num_async_tasks1=15,
182+
expected_num_async_tasks1=10,
194183
expected_num_queries2=656,
195-
expected_num_async_tasks2=23,
184+
expected_num_async_tasks2=22,
196185
expected_num_queries3=332,
197186
expected_num_async_tasks3=20,
198187
)
@@ -207,12 +196,15 @@ def test_import_reimport_reimport_performance_no_async_with_product_grading(self
207196
so we patch the we_want_async decorator to always return False.
208197
"""
209198
self.system_settings(enable_product_grade=True)
199+
# Refresh the cache with the new settings
200+
from dojo.middleware import DojoSytemSettingsMiddleware
201+
DojoSytemSettingsMiddleware.load()
210202

211203
self.import_reimport_performance(
212-
expected_num_queries1=752,
213-
expected_num_async_tasks1=25,
214-
expected_num_queries2=690,
215-
expected_num_async_tasks2=30,
204+
expected_num_queries1=732,
205+
expected_num_async_tasks1=15,
206+
expected_num_queries2=686,
207+
expected_num_async_tasks2=28,
216208
expected_num_queries3=357,
217209
expected_num_async_tasks3=25,
218210
)

0 commit comments

Comments
 (0)