Skip to content

Latest commit

 

History

History
257 lines (184 loc) · 10.8 KB

04-setup-runbook.md

File metadata and controls

257 lines (184 loc) · 10.8 KB

Setup Runbook — reproducing the full environment

The code in this repo is reproducible by design: docker compose up rebuilds the warehouse and the sensor stream exactly. Metabase, however, is configured by hand through its web UI, and that configuration lives inside Metabase's own database — not in Git. This runbook captures those manual steps so anyone (or future-you) can rebuild the dashboard from scratch.

Why this file exists: the GUI clicks are the one un-reproducible seam in an otherwise reproducible project. Section 7 explains how that seam could be closed entirely by configuring Metabase as code.


1. Bring up the stack (CLI)

docker compose up --build

This builds the sensor image and starts five containers: postgres, metabase, and three machines (machine-cnc01, machine-cnc02, machine-press01). Each machine backfills ~2000 historical cycles immediately, then streams live, so there is data to query within seconds.

Leave this terminal running. To stop later: Ctrl+C, then docker compose down (add -v to also wipe the stored data for a clean slate).

The logs show each machine backfilling, then streaming live cycles (note the wear= counter climbing and resetting, and state= switching between running / idle / down):

docker compose logs showing live sensor cycles


2. Verify the warehouse (CLI)

In a second terminal, confirm rows are landing in Postgres:

docker compose exec postgres psql -U sensors -d plant \
  -c "SELECT machine_id, count(*) FROM sensor_readings GROUP BY 1;"

Expected: three machines, each with a few thousand rows and climbing. This confirms the OT → warehouse half of the pipeline before touching the BI layer.

You can also connect with a single connection string instead of flags:

docker compose exec postgres \
  psql "postgresql://sensors:sensors_pw@localhost:5432/plant" \
  -c "SELECT count(*) FROM sensor_readings;"

3. Connect Metabase to Postgres (GUI)

  1. Open http://localhost:3000 (first load takes ~30s while Metabase initialises) and create the admin account.

  2. Add database → PostgreSQL. Fill the connection. Either:

    • paste the connection string jdbc:postgresql://postgres:5432/plant into the Connection string field to pre-fill host/port/database, then add the credentials below; or
    • type the fields directly.

    Postgres connection form in Metabase

    Field Value Note
    Host postgres the Docker service name, NOT localhost — Metabase reaches the DB over the internal Docker network
    Port 5432
    Database name plant
    Username sensors
    Password sensors_pw
  3. Save. Metabase syncs the schema and you'll see the Sensor Readings table.

    Browsing that table confirms the warehouse is connected and populated:

    Sensor Readings table in Metabase

Gotcha: using localhost as the host is the most common mistake — inside the Metabase container, localhost means Metabase itself, not Postgres.


4. Build the five KPI questions (GUI)

For each query file in ../sql/kpi/:

  1. + New → SQL query → plant. The + New menu (top right) is where every question and the dashboard are created:

    Metabase + New menu

  2. Open the .sql file, copy all, paste into the editor, click Run (▶). The results appear as a table below the editor:

    SQL query with results in Metabase

  3. Confirm it returns rows, then click the Visualization button (bottom-left) to open the chart picker. It opens on the current type (here, Table):

    Visualization picker, Table selected

    Pick the chart type for this question — e.g. Line for hourly downtime — and the preview updates immediately:

    Visualization picker, Line selected with chart preview

    Chart type per question:

    Question (paste from) Visualization Axis / settings
    01_oee_by_machine.sql Bar X = machine_id, Y = oee
    02_failure_breakdown.sql Bar X = failure_type, Y = events
    03_hourly_downtime.sql Line X = hour, Y = pct_downtime
    04_tool_wear_trend.sql Line X = ts, Y = tool_wear_min
    05_data_quality_checks.sql Table leave as table; all values should be 0
  4. Save each into the "Our analytics" collection with a clear name (e.g. OEE by Machine). A one-line Description is optional but shows up on hover and in the collection list.

    Save new question dialog

Verify a saved question by opening it and checking its column headers match the table above — that confirms each question holds its own query and not, say, the OEE query pasted five times.


5. Assemble the dashboard (GUI)

  1. + New → Dashboard. Name it Plant Operations, save to "Our analytics". You start in edit mode with an empty grid:

    Dashboard in edit mode, empty grid

  2. Click + (Add a card); a panel lists your saved questions — click each of the five to drop it onto the canvas:

    Adding a saved question to the dashboard

  3. Drag / resize cards. Recommended layout, reading descriptive → diagnostic:

    • Row 1: OEE by Machine (wide) — the headline.
    • Row 2: Failure Breakdown by Type + Downtime % by Hour.
    • Row 3: Tool Wear Trend + Data Quality Checks.
    • Row 4 (optional, live): Live Sensor Monitor (all-machines tool wear) — pairs with the single-machine Tool Wear Trend for a focus-vs-compare view.
  4. (Optional) Add a machine filter: click the filter icon, then choose a filter type. For filtering by machine, use Text or Category:

    Add a filter or parameter menu

    After adding it, connect the filter to each card by mapping it to that card's machine_id column.

  5. Save.

    The finished result — five cards reading descriptive → diagnostic:

    Plant Operations dashboard

Filter caveat: the Tool Wear question is hardcoded to a single machine (CNC-01) and shows the live tail (ORDER BY ts DESC LIMIT 1000), so it has no machine_id variable and can't connect to the dashboard filter. Either leave it pinned as a fixed example, or change its WHERE clause to a Field Filter (WHERE {{machine_id}} mapped to Sensor Readings → Machine Id) so it follows the filter like the other cards. A plain {{machine_id}} text variable will NOT connect to a category filter — it must be a Field Filter.


6. Make it live (auto-refresh)

The sensors stream continuously, so Postgres is always gaining new rows — but a Metabase chart runs its SQL once when it loads and then shows that snapshot. It doesn't re-query on its own until you tell it to. To turn the dashboard into a continuously-updating view, enable auto-refresh:

  1. Open the dashboard and click the clock icon (top-right).
  2. Pick an interval — 1 minute is the shortest.

Dashboard auto-refresh interval menu

Now the whole dashboard re-queries on that cadence and reflects the newest data without a manual reload.

Prove the stream is actually live by counting rows twice, a minute apart — the number climbs:

docker compose exec postgres psql -U sensors -d plant \
  -c "SELECT count(*) FROM sensor_readings;"

Row counts climbing across repeated runs

Honest wording: Metabase's fastest auto-refresh is 1 minute, so this is near-real-time, not millisecond-live. For an operations/BI dashboard that's exactly what "real-time" means in practice — nobody watches OEE tick by the second. True sub-second streaming would need a different tool (e.g. Grafana with a streaming source). The data stream itself is genuinely live; only the dashboard's refresh is on a timer.

"My live chart is stuck on old data." Two things to know. (1) Hourly / aggregate charts dilute new rows into a large current-hour bucket, so they barely move minute to minute — a new point only appears when the hour rolls over. To see movement, plot a raw per-row value against ts (e.g. tool wear or torque). (2) Metabase returns at most 2000 rows per query; an unbounded ORDER BY ts hands back the first 2000 — the OLDEST rows (the backfill) — so the chart freezes on ancient history. Filter to a recent window (WHERE ts > now() - interval '5 minutes') so the result is small and current; now() re-evaluates on each refresh, sliding the window forward. The ready-made example is sql/kpi/06_live_sensor_monitor.sql.

The all-machines live monitor (06) — three independent sawtooths, each machine wearing and resetting on its own cycle, extending right on every refresh:

Live sensor monitor — tool wear, all machines


7. The configure-as-code alternative (how to remove the manual steps)

Sections 3–5 are manual because Metabase stores its configuration in its own application database, not in this repo. For a portfolio that trade-off is fine — this runbook documents the clicks. In a production setting you would not click; you would manage Metabase as code, so the dashboard is version-controlled, reviewable, and rebuildable without anyone touching a mouse. Two standard approaches:

  • Metabase serialization. Metabase can export all questions, dashboards, and database connections to YAML and re-import them (docker compose exec metabase ... export/import, or the equivalent API calls). You commit that YAML to the repo; restoring the dashboard becomes one command instead of this whole runbook.
  • Metabase REST API. Every action in Sections 3–5 (add database, create card, create dashboard, add filter) has an API endpoint. A small setup script could perform them in order, making the entire BI layer reproducible from code.

A further step is to move the transformations themselves out of Metabase into a tool like dbt: the KPI SQL would live in version-controlled models that dbt runs against Postgres, and Metabase would query the resulting tables. That makes the SQL the single source of truth (rather than the copies pasted into Metabase), with tests and lineage on top.

None of this is necessary for the demo — but knowing where the manual seam is, and how you'd close it, is the point. The honest summary:

The pipeline (Docker, Postgres, sensors) is reproducible from code. The BI layer is currently reproducible from this runbook. To make the BI layer reproducible from code too, manage Metabase via serialization/API and move the transformations into dbt.