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

Bundle Analysis: Add Module Report and use dynamic DB sessions some more #132

Merged
merged 3 commits into from
Feb 24, 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
1 change: 1 addition & 0 deletions shared/bundle_analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -13,5 +13,6 @@
AssetReport,
BundleAnalysisReport,
BundleReport,
ModuleReport,
)
from shared.bundle_analysis.storage import BundleAnalysisReportLoader, StoragePaths
44 changes: 33 additions & 11 deletions shared/bundle_analysis/report.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions tests/unit/bundle_analysis/test_bundle_analysis.py
Original file line number Diff line number Diff line change
@@ -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: