diff --git a/shared/bundle_analysis/__init__.py b/shared/bundle_analysis/__init__.py index 80e2330ee..f9c620b46 100644 --- a/shared/bundle_analysis/__init__.py +++ b/shared/bundle_analysis/__init__.py @@ -13,5 +13,6 @@ AssetReport, BundleAnalysisReport, BundleReport, + ModuleReport, ) from shared.bundle_analysis.storage import BundleAnalysisReportLoader, StoragePaths diff --git a/shared/bundle_analysis/report.py b/shared/bundle_analysis/report.py index ec6b85ac6..7791dfca9 100644 --- a/shared/bundle_analysis/report.py +++ b/shared/bundle_analysis/report.py @@ -11,6 +11,24 @@ from shared.bundle_analysis.parser import Parser +class ModuleReport: + """ + Report wrapper around a single module (many of which can exist in a single Asset via Chunks) + """ + + def __init__(self, db_path: str, module: models.Module): + self.db_path = db_path + self.module = module + + @property + def name(self): + return self.module.name + + @property + def size(self): + return self.module.size + + class AssetReport: """ Report wrapper around a single asset (many of which can exist in a single bundle). @@ -34,13 +52,14 @@ def size(self): def modules(self): with models.get_db_session(self.db_path) as session: - return ( + modules = ( session.query(models.Module) .join(models.Module.chunks) .join(models.Chunk.assets) .filter(models.Asset.id == self.asset.id) .all() ) + return [ModuleReport(self.db_path, module) for module in modules] class BundleReport: @@ -144,20 +163,23 @@ def ingest(self, path: str) -> int: return session_id def metadata(self) -> Dict[models.MetadataKey, Any]: - metadata = self.db_session.query(models.Metadata).all() - return {models.MetadataKey(item.key): item.value for item in metadata} + with models.get_db_session(self.db_path) as session: + metadata = session.query(models.Metadata).all() + return {models.MetadataKey(item.key): item.value for item in metadata} def bundle_reports(self) -> Iterator[BundleReport]: bundles = self.db_session.query(models.Bundle).all() - return (BundleReport(self.db_path, bundle) for bundle in bundles) + with models.get_db_session(self.db_path) as session: + bundles = session.query(models.Bundle).all() + return (BundleReport(self.db_path, bundle) for bundle in bundles) def bundle_report(self, bundle_name: str) -> Optional[BundleReport]: - bundle = ( - self.db_session.query(models.Bundle).filter_by(name=bundle_name).first() - ) - if bundle is None: - return None - return BundleReport(self.db_path, bundle) + with models.get_db_session(self.db_path) as session: + bundle = session.query(models.Bundle).filter_by(name=bundle_name).first() + if bundle is None: + return None + return BundleReport(self.db_path, bundle) def session_count(self) -> int: - return self.db_session.query(models.Session).count() + with models.get_db_session(self.db_path) as session: + return session.query(models.Session).count() diff --git a/tests/unit/bundle_analysis/test_bundle_analysis.py b/tests/unit/bundle_analysis/test_bundle_analysis.py index 52268ce54..e46e41755 100644 --- a/tests/unit/bundle_analysis/test_bundle_analysis.py +++ b/tests/unit/bundle_analysis/test_bundle_analysis.py @@ -45,6 +45,11 @@ def test_create_bundle_report(): ("assets/index-*.js", "assets/index-666d2e09.js", 144577, 28), ] + for ar in asset_reports: + for module in ar.modules(): + assert type(module.name) == str + assert type(module.size) == int + assert bundle_report.total_size() == 150572 assert report.session_count() == 1 finally: