Programming

1. Export marts

"""
Pack the marts into one small archive to ship to the server.

This is the deployment unit. It is a few hundred kilobytes, so the production
box never needs the 8M-row tape, pandas memory headroom, or a long write lock.
"""
import json
import zipfile
from pathlib import Path

from django.core.management.base import BaseCommand

from lending_analytics import config as C
from portfolio import bridge


class Command(BaseCommand):
    help = "Bundle parquet marts into a single zip for deployment."

    def add_arguments(self, parser):
        parser.add_argument("--out", default="marts_bundle.zip")

    def handle(self, *args, **o):
        out = Path(o["out"])
        files = sorted(Path(C.MART_DIR).glob("*.parquet"))
        if not files:
            self.stderr.write("no marts to export — run build_marts first")
            return

        with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as z:
            for f in files:
                z.write(f, f.name)
            z.writestr("manifest.json", json.dumps({
                "version": bridge.mart_version(),
                "latest_period": bridge.latest_period(),
                "files": [f.name for f in files],
            }, indent=2))

        self.stdout.write(self.style.SUCCESS(
            f"{out} — {len(files)} marts, {out.stat().st_size / 1024:.0f} KB"))
        self.stdout.write("on the server: python manage.py import_marts marts_bundle.zip")
Helpful? Dislike 0 Log in to react