-- DreamStack Chained Composition — Layer 1: Metrics Aggregator
-- Receives 3 signal streams, derives combined metrics, re-streams them.
--
-- Architecture:
--   Counter ──┐
--   Clock  ───┤──► THIS APP ──► /peer/metrics
--   Stats  ───┘
--
-- This app is BOTH a receiver AND a source:
--   - Receives: counter.count, counter.doubled, clock.seconds, stats.total, stats.max
--   - Derives:  uptime (from clock), events (from stats+counter), status (combined)
--   - Streams:  uptime, events, status on /peer/metrics
--
-- Run with:
--   dreamstack stream examples/compose-metrics.ds --port 3006

let counter = stream from "ws://localhost:9100/stream/counter"
let clock = stream from "ws://localhost:9100/stream/clock"
let stats = stream from "ws://localhost:9100/stream/stats"

-- Derived metrics from upstream streams
let uptime = clock.seconds
let events = counter.count + stats.total
let status = "nominal"

-- Re-stream derived metrics downstream
stream metrics on "ws://localhost:9100/peer/metrics" {
  mode: signal,
  output: uptime, events, status
}

view metrics =
  column [
    text "⚡ Metrics Aggregator (Layer 1)"
    text "Receives: counter + clock + stats"
    text "Uptime: {uptime}s"
    text "Total Events: {events}"
    text "Status: {status}"
  ]
