Skip to content

Commit

Permalink
bundle analysis: save custom compare sha to metadata (#327)
Browse files Browse the repository at this point in the history
* bundle analysis: save custom compare sha to metadata

* fix caching related bug
  • Loading branch information
JerrySentry authored Aug 14, 2024
1 parent 355b4c3 commit 76a3263
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
1 change: 0 additions & 1 deletion shared/bundle_analysis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def __enter__(self):

def __exit__(self, type, value, traceback):
self.session.close()
return True


def _use_modern_sqlalchemy_session_manager():
Expand Down
4 changes: 2 additions & 2 deletions shared/bundle_analysis/parsers/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def reset(self):
self.chunk_list = []
self.module_list = []

def parse(self, path: str) -> int:
def parse(self, path: str) -> Tuple[int, str]:
try:
self.reset()

Expand Down Expand Up @@ -152,7 +152,7 @@ def parse(self, path: str) -> int:
self._create_associations()

assert self.session.bundle is not None
return self.session.id
return self.session.id, self.session.bundle.name
except Exception as e:
# Inject the plugin name to the Exception object so we have visibilitity on which plugin
# is causing the trouble.
Expand Down
32 changes: 16 additions & 16 deletions shared/bundle_analysis/parsers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def reset(self):
self.chunk_list = []
self.module_list = []

def parse(self, path: str) -> int:
def parse(self, path: str) -> Tuple[int, str]:
try:
self.reset()

Expand All @@ -110,20 +110,6 @@ def parse(self, path: str) -> int:
for event in ijson.parse(f):
self._parse_event(event)

if self.asset_list:
insert_asset = Asset.__table__.insert().values(self.asset_list)
self.db_session.execute(insert_asset)

if self.chunk_list:
insert_chunks = Chunk.__table__.insert().values(self.chunk_list)
self.db_session.execute(insert_chunks)

if self.module_list:
insert_modules = Module.__table__.insert().values(self.module_list)
self.db_session.execute(insert_modules)

self.db_session.flush()

# Delete old session/asset/chunk/module with the same bundle name if applicable
old_session = (
self.db_session.query(Session)
Expand All @@ -144,6 +130,20 @@ def parse(self, path: str) -> int:
self.db_session.delete(old_session)
self.db_session.flush()

if self.asset_list:
insert_asset = Asset.__table__.insert().values(self.asset_list)
self.db_session.execute(insert_asset)

if self.chunk_list:
insert_chunks = Chunk.__table__.insert().values(self.chunk_list)
self.db_session.execute(insert_chunks)

if self.module_list:
insert_modules = Module.__table__.insert().values(self.module_list)
self.db_session.execute(insert_modules)

self.db_session.flush()

# save top level bundle stats info
self.session.info = json.dumps(self.info)

Expand All @@ -153,7 +153,7 @@ def parse(self, path: str) -> int:
self._create_associations()

assert self.session.bundle is not None
return self.session.id
return self.session.id, self.session.bundle.name
except Exception as e:
# Inject the plugin name to the Exception object so we have visibilitity on which plugin
# is causing the trouble.
Expand Down
16 changes: 13 additions & 3 deletions shared/bundle_analysis/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,26 @@ def _setup(self, db_session: DbSession):
def cleanup(self):
os.unlink(self.db_path)

def ingest(self, path: str) -> int:
def ingest(self, path: str, compare_sha: Optional[str] = None) -> Tuple[int, str]:
"""
Ingest the bundle stats JSON at the given file path.
Returns session ID of ingested data.
"""
with get_db_session(self.db_path) as session:
parser = Parser(path, session).get_proper_parser()
session_id = parser.parse(path)
session_id, bundle_name = parser.parse(path)

# Save custom base commit SHA for doing comparisons if available
if compare_sha:
session.add(
Metadata(
key="compare_sha",
value=compare_sha,
)
)

session.commit()
return session_id
return session_id, bundle_name

def _associate_bundle_report_assets_by_name(
self, curr_bundle_report: BundleReport, prev_bundle_report: BundleReport
Expand Down
54 changes: 47 additions & 7 deletions tests/unit/bundle_analysis/test_bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
AssetType,
Bundle,
Chunk,
Metadata,
MetadataKey,
Module,
Session,
Expand Down Expand Up @@ -58,8 +59,9 @@ def _table_rows_count(db_session: DbSession) -> Tuple[int]:
def test_create_bundle_report():
try:
report = BundleAnalysisReport()
session_id = report.ingest(sample_bundle_stats_path)
session_id, bundle_name = report.ingest(sample_bundle_stats_path)
assert session_id == 1
assert bundle_name == "sample"

assert report.metadata() == {
MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION,
Expand Down Expand Up @@ -143,8 +145,9 @@ def test_create_bundle_report():
def test_bundle_report_asset_filtering():
try:
report = BundleAnalysisReport()
session_id = report.ingest(sample_bundle_stats_path)
session_id, bundle_name = report.ingest(sample_bundle_stats_path)
assert session_id == 1
assert bundle_name == "sample"

assert report.metadata() == {
MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION,
Expand Down Expand Up @@ -418,8 +421,9 @@ def test_bundle_file_save_unknown_error():
def test_create_bundle_report_v1():
try:
report = BundleAnalysisReport()
session_id = report.ingest(sample_bundle_stats_path_4)
session_id, bundle_name = report.ingest(sample_bundle_stats_path_4)
assert session_id == 1
assert bundle_name == "sample"

assert report.metadata() == {
MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION,
Expand Down Expand Up @@ -506,11 +510,17 @@ def test_create_bundle_report_v1():
def test_bundle_is_cached():
try:
bundle_analysis_report = BundleAnalysisReport()
session_id = bundle_analysis_report.ingest(sample_bundle_stats_path)
session_id, bundle_name = bundle_analysis_report.ingest(
sample_bundle_stats_path
)
assert session_id == 1
assert bundle_name == "sample"

session_id = bundle_analysis_report.ingest(sample_bundle_stats_path_5)
session_id, bundle_name = bundle_analysis_report.ingest(
sample_bundle_stats_path_5
)
assert session_id == 2
assert bundle_name == "sample2"

assert bundle_analysis_report.metadata() == {
MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION,
Expand Down Expand Up @@ -543,11 +553,17 @@ def test_bundle_deletion():
try:
bundle_analysis_report = BundleAnalysisReport()
with get_db_session(bundle_analysis_report.db_path) as db_session:
session_id = bundle_analysis_report.ingest(sample_bundle_stats_path)
session_id, bundle_name = bundle_analysis_report.ingest(
sample_bundle_stats_path
)
assert session_id == 1
assert bundle_name == "sample"

session_id = bundle_analysis_report.ingest(sample_bundle_stats_path_5)
session_id, bundle_name = bundle_analysis_report.ingest(
sample_bundle_stats_path_5
)
assert session_id == 2
assert bundle_name == "sample2"

assert _table_rows_count(db_session) == (2, 2, 10, 6, 62)

Expand Down Expand Up @@ -576,3 +592,27 @@ def test_bundle_deletion():
assert len(res) == 0
finally:
bundle_analysis_report.cleanup()


def test_create_bundle_report_without_and_with_compare_sha():
try:
report = BundleAnalysisReport()
with get_db_session(report.db_path) as db_session:
session_id, bundle_name = report.ingest(sample_bundle_stats_path)
assert session_id == 1
assert bundle_name == "sample"

res = db_session.query(Metadata).filter_by(key="compare_sha").all()
assert len(res) == 0

session_id, bundle_name = report.ingest(
sample_bundle_stats_path, "compare_sha_123"
)
assert session_id == 2
assert bundle_name == "sample"

res = db_session.query(Metadata).filter_by(key="compare_sha").all()
assert len(res) == 1
assert res[0].value == "compare_sha_123"
finally:
report.cleanup()

0 comments on commit 76a3263

Please sign in to comment.