import os import json import zipfile import datetime def get_revenue_stats(): """ Returns a list of revenue and usage metrics for the past 7 days. Each entry includes date, revenue_usd, active_users, and subscriptions. """ today = datetime.date.today() stats = [] for i in range(7): dt = today - datetime.timedelta(days=i) stats.append({ "date": dt.isoformat(), "revenue_usd": round(100 + i * 10, 2), "active_users": 50 + i * 5, "subscriptions": 10 + i }) return stats def package_artifacts(assets): """ Packages generated app assets (blueprint and code files) into a zip archive stored under the persistent /data directory so artifacts appear in the Space UI. Returns the path to the zip file. """ # Timestamped directory under persistent storage (/data) timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S") base_dir = f"/data/artifacts_{timestamp}" os.makedirs(base_dir, exist_ok=True) # Save blueprint as JSON blueprint = assets.get("blueprint", {}) bp_path = os.path.join(base_dir, "blueprint.json") with open(bp_path, "w") as f: json.dump(blueprint, f, indent=2) # Save each generated code file code_files = assets.get("code", {}) for fname, content in code_files.items(): file_path = os.path.join(base_dir, fname) os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, "w") as f: f.write(content) # Create zip archive alongside the directory zip_path = f"{base_dir}.zip" with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: for root, _, files in os.walk(base_dir): for file in files: full_path = os.path.join(root, file) rel_path = os.path.relpath(full_path, base_dir) zipf.write(full_path, arcname=rel_path) return zip_path