File size: 1,978 Bytes
fb89920 44c1905 fb89920 44c1905 fb89920 44c1905 fb89920 44c1905 fb89920 44c1905 fb89920 44c1905 fb89920 44c1905 fb89920 44c1905 fb89920 44c1905 fb89920 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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
|