Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: further breakdown tracing in BA processor #798

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 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]:
head_bundle_report: BundleAnalysisReport | None,
) -> 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,44 @@ 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,
) -> 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=None
)
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
return 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 +231,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
)
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
Loading