···11+# Export Blog Post Metrics Script
22+33+This script exports monitor metrics data from OpenStatus for use in blog posts and documentation.
44+55+## Overview
66+77+The script fetches monitor data directly from the database and Tinybird analytics, then exports it to a JSON file that can be used for visualizations in blog posts.
88+99+**Features:**
1010+- Fetches metrics from both regular regions and private locations
1111+- Automatically combines public regions with private location data
1212+- Supports both HTTP and TCP monitors
1313+1414+## Configuration
1515+1616+Edit the constants at the top of `export-blog-post-metrics.ts`:
1717+1818+```typescript
1919+const MONITOR_ID = "1"; // The ID of the monitor to export
2020+const PERIOD = "7d"; // Time period: "1d", "7d", or "14d"
2121+const INTERVAL = 60; // Interval in minutes for data points
2222+const TYPE = "http"; // Fallback monitor type: "http" or "tcp" (auto-detected from monitor)
2323+const OUTPUT_FILE = "blog-post-metrics.json"; // Output filename
2424+```
2525+2626+**Note:** The script automatically detects the monitor type from the database, but you can set a fallback with the `TYPE` constant.
2727+2828+## Prerequisites
2929+3030+1. Make sure you have the `TINY_BIRD_API_KEY` environment variable set in your `.env` file
3131+2. The database should be accessible (local or remote)
3232+3. Install dependencies: `pnpm install`
3333+3434+## Usage
3535+3636+> [!IMPORTANT]
3737+> Go to the `/tinybird/src/client.ts` file and make sure tb is **not using the NoopClient**.
3838+3939+From the `apps/dashboard` directory:
4040+4141+```bash
4242+# Using the npm script
4343+pnpm export-metrics
4444+4545+# Or directly with bun
4646+bun src/scripts/export-blog-post-metrics.ts
4747+```
4848+4949+## Output Format
5050+5151+The script generates a JSON file with the following structure:
5252+5353+```json
5454+{
5555+ "regions": ["ams", "fra", "lhr", ...],
5656+ "data": {
5757+ "regions": ["ams", "fra", "lhr", ...],
5858+ "data": [
5959+ {
6060+ "timestamp": "2025-08-18T16:00:00.000Z",
6161+ "ams": 207,
6262+ "fra": 142,
6363+ "lhr": 327,
6464+ ...
6565+ }
6666+ ]
6767+ },
6868+ "metricsByRegions": [
6969+ {
7070+ "region": "ams",
7171+ "count": 1000,
7272+ "ok": 995,
7373+ "p50Latency": 150,
7474+ "p75Latency": 200,
7575+ "p90Latency": 250,
7676+ "p95Latency": 300,
7777+ "p99Latency": 400
7878+ }
7979+ ]
8080+}
8181+```
8282+8383+## Data Fields
8484+8585+- **regions**: Array of region codes and private location names for the monitor
8686+- **data.data**: Timeline data with latency values per region/location at each timestamp
8787+- **metricsByRegions**: Summary statistics per region/location including:
8888+ - `count`: Total number of checks
8989+ - `ok`: Number of successful checks
9090+ - `p50Latency`, `p75Latency`, `p90Latency`, `p95Latency`, `p99Latency`: Latency percentiles in milliseconds
9191+9292+**Note:** The script automatically includes both public Fly.io regions and any private locations connected to the monitor.
9393+9494+## Example: Moving to Web Assets
9595+9696+To use the exported data in the web app (like the existing `hono-cold.json`):
9797+9898+```bash
9999+# After running the script
100100+cp blog-post-metrics.json ../web/public/assets/posts/your-blog-post/data.json
101101+```
102102+103103+## Troubleshooting
104104+105105+**Error: "TINY_BIRD_API_KEY environment variable is required"**
106106+- Make sure you have the `TINY_BIRD_API_KEY` set in your `.env` file
107107+108108+**Error: "Monitor with ID X not found"**
109109+- Verify the monitor ID exists in your database
110110+- Check that you're connected to the correct database
111111+112112+**No data returned**
113113+- Ensure the monitor has been running and collecting data for the specified period
114114+- Try a different time period (e.g., "7d" instead of "1d")
115115+