# p2p-energy-trading-platform/iot-ingestion-dispatch architecture

> Architecture diagram of the GitHub repository p2p-energy-trading-platform/iot-ingestion-dispatch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/p2p-energy-trading-platform/iot-ingestion-dispatch
- Repository: https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch

## Overview

GridX IoT Ingestion & Dispatch consumes Kafka telemetry, validates it against provisioned grids, writes durable TimescaleDB records, and maintains Redis latest-state/liveness projections before acknowledging records. Operators can replay quarantined failures or rebuild Redis; gRPC registration and live-stream components are also present. The README says the query API is not implemented, but query code and a handler are in the supplied tree and excerpts, so the graph shows those components without asserting listener exposure. Unsampled wiring is included only where the README documents it.

Main entry points: [`internal/app/app.go`](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/app/app.go) (App wiring), [`internal/store/postgres/heartbeat_writer.go`](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/store/postgres/heartbeat_writer.go) (TimescaleDB adapters), [`internal/ingestion/meter_handler.go`](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/meter_handler.go) (Meter workflow), [`internal/ingestion/heartbeat_handler.go`](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/heartbeat_handler.go) (Heartbeat workflow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Ingestion"]
  node_iot_main["Service entry<br/>[main.go]"]
  node_app["App wiring<br/>[app.go]"]
  node_consumer["Kafka consumer<br/>[consumer.go]"]
  node_worker["Partition workers<br/>[worker.go]"]
  node_router["Topic router<br/>[router.go]"]
  node_decoder["Record decoder<br/>[decoder.go]"]
  node_admission["Grid admission<br/>[registry.go]"]
  node_meter_handler["Meter workflow<br/>[meter_handler.go]"]
  node_heartbeat_handler["Heartbeat workflow"]
  node_actuation_handler["Actuation responses"]
end

subgraph group_state["State &amp; storage"]
  node_postgres[("TimescaleDB adapters")]
  node_redis[("Redis projections<br/>[latest.go]")]
  node_rebuild["Projection rebuild<br/>[rebuild.go]"]
end

subgraph group_api["APIs &amp; registration"]
  node_registration["Meter registration<br/>[service.go]"]
  node_grpc_registration["Registration RPCs"]
  node_query["Query service<br/>[service.go]"]
  node_iot_handler["IoT RPC handler<br/>[iot_handler.go]"]
  node_grpc_server["gRPC server<br/>[server.go]"]
  node_live["Live event hub<br/>[hub.go]"]
end

subgraph group_ops["Operations &amp; signals"]
  node_failure_replay["Failure replay<br/>[main.go]"]
  node_redis_rebuild["Rebuild command<br/>[main.go]"]
  node_health["Health endpoints<br/>[server.go]"]
  node_metrics["Metrics<br/>[metrics.go]"]
end

node_operator(("Operator"))
node_kafka["Kafka topics"]
node_external_db[("PostgreSQL")]
node_external_redis[("Redis service")]
node_grpc_client(("Internal gRPC clients"))

node_iot_main -->|"starts"| node_app
node_app -->|"configures"| node_consumer
node_consumer -.->|"dispatches"| node_worker
node_worker -.->|"routes records"| node_router
node_router -->|"selects topic"| node_meter_handler
node_router -->|"selects topic"| node_heartbeat_handler
node_router -->|"selects topic"| node_actuation_handler
node_meter_handler -.->|"decodes records"| node_decoder
node_heartbeat_handler -.->|"decodes records"| node_decoder
node_meter_handler -->|"checks grid"| node_admission
node_heartbeat_handler -->|"checks grid"| node_admission
node_meter_handler -.->|"persists readings"| node_postgres
node_heartbeat_handler -->|"persists heartbeat"| node_postgres
node_meter_handler -.->|"updates latest"| node_redis
node_heartbeat_handler -->|"updates liveness"| node_redis
node_app -->|"refreshes grids"| node_admission
node_admission -->|"loads grids"| node_postgres
node_meter_handler -->|"publishes readings"| node_live
node_heartbeat_handler -->|"publishes online"| node_live
node_postgres -.->|"reads and writes"| node_external_db
node_redis -.->|"reads and writes"| node_external_redis
node_operator -->|"requests replay"| node_failure_replay
node_failure_replay -->|"replays payloads"| node_router
node_failure_replay -->|"lists failures"| node_postgres
node_operator -->|"requests rebuild"| node_redis_rebuild
node_redis_rebuild -->|"runs"| node_rebuild
node_rebuild -->|"reads history"| node_postgres
node_rebuild -->|"rebuilds projection"| node_redis
node_app -->|"wires service"| node_registration
node_grpc_registration -->|"invokes"| node_registration
node_app -->|"wires handler"| node_iot_handler
node_iot_handler -->|"calls queries"| node_query
node_query -.->|"reads energy and houses"| node_postgres
node_query -.->|"reads latest state"| node_redis
node_app -.->|"starts server"| node_grpc_server
node_app -.->|"starts endpoints"| node_health
node_grpc_client -.->|"calls"| node_grpc_server
node_app -->|"registers metrics"| node_metrics

click node_iot_main "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/cmd/iot-ingestion/main.go"
click node_app "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/app/app.go"
click node_consumer "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/consumer.go"
click node_worker "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/worker.go"
click node_router "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/router.go"
click node_decoder "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/decoder.go"
click node_admission "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/admission/registry.go"
click node_meter_handler "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/meter_handler.go"
click node_heartbeat_handler "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/heartbeat_handler.go"
click node_actuation_handler "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/actuation_handler.go"
click node_postgres "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/store/postgres/heartbeat_writer.go"
click node_redis "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/store/redis/latest.go"
click node_rebuild "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/rebuild/rebuild.go"
click node_failure_replay "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/cmd/failure-replay/main.go"
click node_redis_rebuild "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/cmd/redis-rebuild/main.go"
click node_registration "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/registration/service.go"
click node_grpc_registration "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/grpc/services/registration_handler.go"
click node_query "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/query/service.go"
click node_iot_handler "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/grpc/services/iot_handler.go"
click node_grpc_server "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/grpc/server.go"
click node_live "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/live/hub.go"
click node_health "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/httphealth/server.go"
click node_metrics "https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/observability/metrics.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_iot_main,node_app,node_consumer,node_worker,node_router,node_decoder,node_admission,node_meter_handler,node_heartbeat_handler,node_actuation_handler,node_grpc_client toneBlue
class node_postgres,node_redis,node_rebuild,node_external_db,node_external_redis toneAmber
class node_registration,node_grpc_registration,node_query,node_iot_handler,node_grpc_server,node_live toneMint
class node_failure_replay,node_redis_rebuild,node_health,node_metrics toneRose
class node_operator,node_kafka toneIndigo
```

## Components

### Ingestion

- [Service entry](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/cmd/iot-ingestion/main.go): `cmd/iot-ingestion/main.go`
- [App wiring](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/app/app.go): `internal/app/app.go`
- [Kafka consumer](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/consumer.go): `internal/ingestion/consumer.go`
- [Partition workers](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/worker.go): `internal/ingestion/worker.go`
- [Topic router](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/router.go): `internal/ingestion/router.go`
- [Record decoder](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/decoder.go): `internal/ingestion/decoder.go`
- [Grid admission](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/admission/registry.go): `internal/admission/registry.go`
- [Meter workflow](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/meter_handler.go): `internal/ingestion/meter_handler.go`
- [Heartbeat workflow](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/heartbeat_handler.go): `internal/ingestion/heartbeat_handler.go`
- [Actuation responses](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/ingestion/actuation_handler.go): `internal/ingestion/actuation_handler.go`

### State & storage

- [TimescaleDB adapters](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/store/postgres/heartbeat_writer.go): `internal/store/postgres/heartbeat_writer.go`
- [Redis projections](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/store/redis/latest.go): `internal/store/redis/latest.go`
- [Projection rebuild](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/rebuild/rebuild.go): `internal/rebuild/rebuild.go`

### APIs & registration

- [Meter registration](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/registration/service.go): `internal/registration/service.go`
- [Registration RPCs](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/grpc/services/registration_handler.go): `internal/transport/grpc/services/registration_handler.go`
- [Query service](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/query/service.go): `internal/query/service.go`
- [IoT RPC handler](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/grpc/services/iot_handler.go): `internal/transport/grpc/services/iot_handler.go`
- [gRPC server](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/grpc/server.go): `internal/transport/grpc/server.go`
- [Live event hub](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/live/hub.go): `internal/live/hub.go`

### Operations & signals

- [Failure replay](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/cmd/failure-replay/main.go): `cmd/failure-replay/main.go`
- [Rebuild command](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/cmd/redis-rebuild/main.go): `cmd/redis-rebuild/main.go`
- [Health endpoints](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/transport/httphealth/server.go): `internal/transport/httphealth/server.go`
- [Metrics](https://github.com/p2p-energy-trading-platform/iot-ingestion-dispatch/blob/main/internal/observability/metrics.go): `internal/observability/metrics.go`

### Other components

- **Operator**
- **Kafka topics**
- **PostgreSQL**
- **Redis service**
- **Internal gRPC clients**

## Connections

- Service entry → App wiring: starts (evidence: `cmd/iot-ingestion/main.go`)
- App wiring → Kafka consumer: configures (evidence: `internal/app/app.go`)
- Kafka consumer → Partition workers: dispatches
- Partition workers → Topic router: routes records
- Topic router → Meter workflow: selects topic
- Topic router → Heartbeat workflow: selects topic
- Topic router → Actuation responses: selects topic
- Meter workflow → Record decoder: decodes records
- Heartbeat workflow → Record decoder: decodes records
- Meter workflow → Grid admission: checks grid (evidence: `internal/ingestion/meter_handler.go`)
- Heartbeat workflow → Grid admission: checks grid (evidence: `internal/ingestion/heartbeat_handler.go`)
- Meter workflow → TimescaleDB adapters: persists readings
- Heartbeat workflow → TimescaleDB adapters: persists heartbeat (evidence: `internal/ingestion/heartbeat_handler.go`)
- Meter workflow → Redis projections: updates latest
- Heartbeat workflow → Redis projections: updates liveness (evidence: `internal/ingestion/heartbeat_handler.go`)
- App wiring → Grid admission: refreshes grids (evidence: `internal/app/app.go`)
- Grid admission → TimescaleDB adapters: loads grids
- Meter workflow → Live event hub: publishes readings (evidence: `internal/ingestion/meter_handler.go`)
- Heartbeat workflow → Live event hub: publishes online (evidence: `internal/ingestion/heartbeat_handler.go`)
- TimescaleDB adapters → PostgreSQL: reads and writes
- Redis projections → Redis service: reads and writes
- Operator → Failure replay: requests replay (evidence: `cmd/failure-replay/main.go`)
- Failure replay → Topic router: replays payloads (evidence: `cmd/failure-replay/main.go`)
- Failure replay → TimescaleDB adapters: lists failures (evidence: `cmd/failure-replay/main.go`)
- Operator → Rebuild command: requests rebuild (evidence: `cmd/redis-rebuild/main.go`)
- Rebuild command → Projection rebuild: runs (evidence: `cmd/redis-rebuild/main.go`)
- Projection rebuild → TimescaleDB adapters: reads history (evidence: `internal/rebuild/rebuild.go`)
- Projection rebuild → Redis projections: rebuilds projection (evidence: `internal/rebuild/rebuild.go`)
- App wiring → Meter registration: wires service (evidence: `internal/app/app.go`)
- Registration RPCs → Meter registration: invokes (evidence: `internal/transport/grpc/services/registration_handler.go`)
- App wiring → IoT RPC handler: wires handler (evidence: `internal/app/app.go`)
- IoT RPC handler → Query service: calls queries (evidence: `internal/transport/grpc/services/iot_handler.go`)
- Query service → TimescaleDB adapters: reads energy and houses
- Query service → Redis projections: reads latest state
- App wiring → gRPC server: starts server
- App wiring → Health endpoints: starts endpoints
- Internal gRPC clients → gRPC server: calls
- App wiring → Metrics: registers metrics (evidence: `internal/app/app.go`)
