import { DockerIsol8 } from "isol8";
const isol8 = new DockerIsol8({ mode: "ephemeral", network: "none" });
await isol8.start();
// Step 1: Generate raw data with Bash
const step1 = await isol8.execute({
code: `
for i in $(seq 1 100); do
echo "$i,$((RANDOM % 1000)),$((RANDOM % 5 + 1))"
done
`,
runtime: "bash",
});
// Step 2: Analyze with Python (pass data via stdin)
const step2 = await isol8.execute({
code: `
import csv, json, sys, io
reader = csv.reader(io.StringIO(sys.stdin.read()))
rows = [(int(r[0]), int(r[1]), int(r[2])) for r in reader]
stats = {
"count": len(rows),
"avg_value": sum(r[1] for r in rows) / len(rows),
"max_value": max(r[1] for r in rows),
"rating_distribution": {}
}
for _, _, rating in rows:
stats["rating_distribution"][str(rating)] = \
stats["rating_distribution"].get(str(rating), 0) + 1
json.dump(stats, sys.stdout)
`,
runtime: "python",
stdin: step1.stdout,
});
// Step 3: Format report with Node.js
const step3 = await isol8.execute({
code: `
const stats = JSON.parse(require("fs").readFileSync("/dev/stdin", "utf8"));
console.log("=== Data Analysis Report ===");
console.log(\`Total records: \${stats.count}\`);
console.log(\`Average value: \${stats.avg_value.toFixed(2)}\`);
console.log(\`Maximum value: \${stats.max_value}\`);
console.log("\\nRating distribution:");
Object.entries(stats.rating_distribution)
.sort(([a], [b]) => Number(a) - Number(b))
.forEach(([rating, count]) => {
const bar = "█".repeat(Math.round(count / 2));
console.log(\` \${rating} stars: \${bar} (\${count})\`);
});
`,
runtime: "node",
stdin: step2.stdout,
});
console.log(step3.stdout);