Skip to content

Commit

Permalink
chore: further breakdown tracing in BA processor
Browse files Browse the repository at this point in the history
re-organize the code to make tracing more evident.
Sadly the part that usually takes the longest needs to be updated in shared.

I include a small optimization making the CacheConfig query return the data in the format we want it
instead of having to go over the list again.
  • Loading branch information
giovanni-guidini committed Oct 18, 2024
1 parent 48ce427 commit cc1c403
Showing 1 changed file with 41 additions and 31 deletions.
72 changes: 41 additions & 31 deletions services/bundle_analysis/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ def _get_parent_commit(
.first()
)

@sentry_sdk.trace
def _previous_bundle_analysis_report(
self,
bundle_loader: BundleAnalysisReportLoader,
commit: Commit,
head_bundle_report: BundleAnalysisReport,
) -> Optional[BundleAnalysisReport]:
) -> BundleAnalysisReport | None:
"""
Helper function to fetch the parent commit's BAR for the purpose of matching previous bundle's
Assets to the current one being parsed.
Expand Down Expand Up @@ -180,9 +181,45 @@ def _previous_bundle_analysis_report(

return bundle_loader.load(parent_commit_report.external_id)

@sentry_sdk.trace
def _attempt_init_from_previous_report(
self,
commit: Commit,
bundle_loader: BundleAnalysisReportLoader,
bundle_report: BundleAnalysisReport,
) -> BundleAnalysisReport:
"""Attempts to carry over parent bundle analysis report if current commit doesn't have a report.
Fallback to creating a fresh bundle analysis report if there is no previous report to carry over.
"""
# load a new copy of the previous bundle report into temp file
bundle_report = self._previous_bundle_analysis_report(
bundle_loader, commit, head_bundle_report=bundle_report
)
if bundle_report:
# query which bundle names has caching turned on
bundles_to_be_cached = CacheConfig.objects.filter(
is_caching=True,
repo_id=commit.repoid,
).values_list("bundle_name", flat=True)

# For each bundle:
# if caching is on then update bundle.is_cached property to true
# if caching is off then delete that bundle from the report
update_fields = {}
for bundle in bundle_report.bundle_reports():
if bundle.name in bundles_to_be_cached:
update_fields[bundle.name] = True
else:
bundle_report.delete_bundle_by_name(bundle.name)
if update_fields:
bundle_report.update_is_cached(update_fields)
return bundle_report
# fallback to create a fresh bundle analysis report if there is no previous report to carry over
bundle_report = BundleAnalysisReport()

@sentry_sdk.trace
def process_upload(
self, commit: Commit, upload: Upload, compare_sha: Optional[str] = None
self, commit: Commit, upload: Upload, compare_sha: str | None = None
) -> ProcessingResult:
"""
Download and parse the data associated with the given upload and
Expand All @@ -195,37 +232,10 @@ def process_upload(

# fetch existing bundle report from storage
bundle_report = bundle_loader.load(commit_report.external_id)

# attempt to carry over parent bundle analysis report if commit doesn't have a report
if bundle_report is None:
# load a new copy of the previous bundle report into temp file
bundle_report = self._previous_bundle_analysis_report(
bundle_loader, commit, head_bundle_report=bundle_report
bundle_report = self._attempt_init_from_previous_report(
commit, bundle_loader, bundle_report
)
if bundle_report:
# query which bundle names has caching turned on
qs = CacheConfig.objects.filter(
is_caching=True,
repo_id=commit.repoid,
)
bundles_to_be_cached = [item.bundle_name for item in qs]

# For each bundle,
# if caching is on then update bundle.is_cached property to true
# if caching is off then delete that bundle from the report
update_fields = {}
for bundle in bundle_report.bundle_reports():
if bundle.name in bundles_to_be_cached:
update_fields[bundle.name] = True
else:
bundle_report.delete_bundle_by_name(bundle.name)
if update_fields:
bundle_report.update_is_cached(update_fields)

# fallback to create a fresh bundle analysis report if there is no previous
# report to carry over from
if bundle_report is None:
bundle_report = BundleAnalysisReport()

# download raw upload data to local tempfile
_, local_path = tempfile.mkstemp()
Expand Down

0 comments on commit cc1c403

Please sign in to comment.