uns-state — State Duration Tracker

Tracks machine state transitions and logs completed state durations to PostgreSQL.

LanguageGo
TypeHTTP function (polled)
Scaffolded withfnkit go
Depends onValkey, PostgreSQL

What It Does

Reads /status topics, maintains in-memory state tracker per machine. On state change, logs completed state with exact duration. Foundation for all time-based KPIs.

cnc-01: ACTIVE -> IDLE   -> logs: state=ACTIVE, duration=120s, next=IDLE
cnc-01: IDLE -> SETUP     -> logs: state=IDLE, duration=45s, next=SETUP

Machine States

StateCategory
ACTIVERunning
IDLEStopped
WAITINGStopped
ALARMStopped
SETUPStopped
OFFLINEStopped

Useful Queries

SELECT machine,
  ROUND(SUM(CASE WHEN state='ACTIVE' THEN duration_s ELSE 0 END) /
  SUM(duration_s) * 100, 1) AS utilisation_pct
FROM uns_state WHERE started_at >= NOW() - INTERVAL '24 hours' GROUP BY machine;

Quick Start

cd uns-state && cp .env.example .env && docker compose up -d
while true; do curl -s http://localhost:8080/uns-state | jq; sleep 5; done