# harlanljones/urban-signal architecture

> Architecture diagram of the GitHub repository harlanljones/urban-signal, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/harlanljones/urban-signal
- Repository: https://github.com/harlanljones/urban-signal

## Overview

#### Purpose and Entry Points
Urban Signal is a multi-city municipal-data streaming and spatial forecasting platform. It converts permits, 311 complaints, licenses, and deeds into H3-grid features, catalyst alerts, and 6/12/18-month commercial appreciation forecasts.

Operational entry points:
- Ingestion scheduler: `src/producers/scheduler.py`
- Kafka workers: `src/consumers/spatial_enrichment_worker.py`, `src/consumers/feature_aggregation_worker.py`, `src/consumers/postgis_worker.py`
- Retraining/export: `src/models/retraining_job.py`
- FastAPI service: `src/serving/app.py`
- Cloudflare edge handler: `workers/src/index.ts`

#### Municipal Ingestion and City Boundary
Producers retrieve city-specific open-data feeds through Socrata and ArcGIS, normalize/deduplicate events, and publish typed raw events. City/feed availability is intentionally partial and centrally resolved through the city registry rather than assumed uniformly across metros.

Core paths:
- `src/producers/base_producer.py`
- `src/producers/socrata_client.py`
- `src/producers/arcgis_client.py`
- `src/spatial/city_registry.py`
- `src/spatial/cities/`

Avro contracts form the boundary between producers and consumers: `src/schemas/avro/`.

#### Event Streaming and Consumer Stages
Kafka is the asynchronous backbone. Raw municipal topics flow into three separable consumer responsibilities:
1. spatial normalization onto H3 resolutions 7/8/9;
2. temporal feature aggregation and catalyst-alert emission;
3. durable raw/feature synchronization to PostGIS.

Core paths:
- `src/consumers/base_consumer.py`
- `src/consumers/spatial_enrichment_worker.py`
- `src/consumers/feature_aggregation_worker.py`
- `src/consumers/postgis_worker.py`

Kafka topic and cluster infrastructure is defined in `deploy/k8s/kafka/kafka-topics.yaml` and `deploy/k8s/kafka/strimzi-cluster.yaml`; KEDA scales consumer capacity in `deploy/k8s/consumers/keda-scaledobject.yaml`.

#### Spatial and Feature Computation
Spatial processing assigns events to hierarchical H3 cells, graphs neighboring cells, and maps cells into city divisions/submarkets. Feature engineering computes time-decayed CapEx, 311 shift dynamics, and the normalized Leading Indicator Momentum Score (LIMS). LIMS threshold crossings become catalyst alerts.

Core paths:
- `src/spatial/h3_indexer.py`
- `src/spatial/graph_builder.py`
- `src/spatial/submarkets.py`
- `src/features/pipeline.py`
- `src/features/lims_calculator.py`

This stage is the boundary between heterogeneous municipal records and model-ready spatial-temporal feature vectors.

#### Storage, Training, and Model Artifacts
PostGIS is the queryable spatial system of record; feature partitions and model artifacts use MinIO/S3. Retraining applies walk-forward temporal validation with H3 spatial holdouts, trains three horizon-specific models, and exports ONNX for serving.

Core paths:
- `src/storage/postgis_sync.py`
- `src/models/retraining_job.py`
- `src/models/trainer.py`
- `src/models/export_onnx.py`
- `models_storage/`

Models are LightGBM quantiles (6m), spatio-temporal GNN (12m), and DCN-v2 (18m): `src/models/quantile_lgbm.py`, `src/models/st_gnn.py`, `src/models/dcn_v2.py`.

#### Real-Time API, Dashboard, and Edge Delivery
FastAPI validates requests, resolves H3/city context, runs ONNX inference, adds SHAP-style driver attribution, and exposes prediction, grid, catalog, and health endpoints. It also serves the MapLibre dashboard and dispatches webhook alerts.

Core paths:
- `src/serving/app.py`
- `src/serving/router.py`
- `src/serving/engine.py`
- `src/serving/dashboard.py`
- `src/serving/dispatcher.py`

`src/export/snapshot_builder.py` produces precomputed dashboard/API snapshots for the Cloudflare Worker, which serves edge/KV-backed responses via `workers/src/index.ts`.

#### Runtime and Deployment Topology
Primary runtime is Python 3.11/3.12 with FastAPI, Kafka clients, H3, PostGIS, DuckDB/Polars, and ONNX Runtime using CUDA when available. External services are Socrata, ArcGIS FeatureServer, Kafka/Schema Registry, PostGIS, MinIO, webhooks, and Cloudflare KV.

Local dependencies are composed in `deploy/docker/docker-compose.dev.yml`. Kubernetes deploys Strimzi Kafka, PostGIS storage, autoscaled consumers, and GPU inference through `deploy/k8s/` and `deploy/k8s/inference/inference-deployment.yaml`.

Main entry points: [`deploy/k8s/kafka/kafka-topics.yaml`](https://github.com/harlanljones/urban-signal/blob/main/deploy/k8s/kafka/kafka-topics.yaml) (Kafka topics), [`src/storage/postgis_sync.py`](https://github.com/harlanljones/urban-signal/blob/main/src/storage/postgis_sync.py) (PostGIS), [`src/consumers/spatial_enrichment_worker.py`](https://github.com/harlanljones/urban-signal/blob/main/src/consumers/spatial_enrichment_worker.py) (Spatial enrichment), [`src/producers/base_producer.py`](https://github.com/harlanljones/urban-signal/blob/main/src/producers/base_producer.py) (Municipal producers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingestion["Municipal Ingestion"]
  node_city_registry["City registry<br/>city boundary<br/>[city_registry.py]"]
  node_municipal_sources(("Socrata &amp; ArcGIS<br/>external data APIs<br/>[socrata_client.py]"))
  node_producers["Municipal producers<br/>ingestion workers<br/>[base_producer.py]"]
  node_avro_contracts["Avro contracts<br/>event schemas"]
end

subgraph group_streaming["Kafka Pipeline"]
  node_kafka["Kafka topics<br/>event backbone<br/>[kafka-topics.yaml]"]
  node_spatial_worker["Spatial enrichment<br/>Kafka worker"]
  node_feature_worker["Feature aggregation<br/>Kafka worker"]
  node_postgis_worker["PostGIS sync worker<br/>Kafka worker<br/>[postgis_worker.py]"]
  node_keda{{"KEDA autoscaling<br/>consumer scaling"}}
end

subgraph group_spatial["Spatial Features"]
  node_h3_context["H3 &amp; submarkets<br/>spatial context<br/>[graph_builder.py]"]
  node_feature_pipeline["Feature pipeline<br/>feature engineering<br/>[pipeline.py]"]
end

subgraph group_ml["Training &amp; Storage"]
  node_postgis[("PostGIS<br/>spatial system of record<br/>[postgis_sync.py]")]
  node_object_store[("MinIO/S3 artifacts<br/>feature and model storage")]
  node_retraining["Retraining job<br/>model orchestration<br/>[retraining_job.py]"]
  node_horizon_models["Horizon models<br/>forecast models<br/>[trainer.py]"]
  node_onnx_export["ONNX export<br/>model packaging<br/>[export_onnx.py]"]
end

subgraph group_delivery["Serving &amp; Edge"]
  node_fastapi["FastAPI service<br/>prediction API<br/>[app.py]"]
  node_snapshots["Snapshot builder<br/>edge export"]
  node_cloudflare_worker{{"Cloudflare Worker<br/>edge API<br/>[index.ts]"}}
end

node_city_registry -->|"supported feeds"| node_producers
node_municipal_sources -->|"city records"| node_producers
node_producers -->|"typed events"| node_avro_contracts
node_avro_contracts -->|"raw topics"| node_kafka
node_kafka -->|"raw events"| node_spatial_worker
node_spatial_worker -->|"H3 assignment"| node_h3_context
node_spatial_worker -->|"enriched events"| node_kafka
node_kafka -->|"enriched stream"| node_feature_worker
node_h3_context -->|"cell context"| node_feature_pipeline
node_feature_worker -->|"event windows"| node_feature_pipeline
node_feature_pipeline -->|"features and alerts"| node_kafka
node_kafka -->|"raw and feature topics"| node_postgis_worker
node_postgis_worker -->|"durable sync"| node_postgis
node_keda -.->|"scales"| node_spatial_worker
node_keda -.->|"scales"| node_feature_worker
node_postgis -->|"historical data"| node_retraining
node_object_store -->|"feature partitions"| node_retraining
node_retraining -->|"validated training"| node_horizon_models
node_horizon_models -->|"trained models"| node_onnx_export
node_onnx_export -->|"ONNX artifacts"| node_object_store
node_object_store -->|"model artifacts"| node_fastapi
node_postgis -->|"grid and catalog queries"| node_fastapi
node_postgis -->|"queryable data"| node_snapshots
node_snapshots -->|"precomputed snapshots"| node_object_store
node_object_store -->|"edge snapshots"| node_cloudflare_worker

click node_city_registry "https://github.com/harlanljones/urban-signal/blob/main/src/spatial/city_registry.py"
click node_municipal_sources "https://github.com/harlanljones/urban-signal/blob/main/src/producers/socrata_client.py"
click node_producers "https://github.com/harlanljones/urban-signal/blob/main/src/producers/base_producer.py"
click node_kafka "https://github.com/harlanljones/urban-signal/blob/main/deploy/k8s/kafka/kafka-topics.yaml"
click node_spatial_worker "https://github.com/harlanljones/urban-signal/blob/main/src/consumers/spatial_enrichment_worker.py"
click node_feature_worker "https://github.com/harlanljones/urban-signal/blob/main/src/consumers/feature_aggregation_worker.py"
click node_postgis_worker "https://github.com/harlanljones/urban-signal/blob/main/src/consumers/postgis_worker.py"
click node_keda "https://github.com/harlanljones/urban-signal/blob/main/deploy/k8s/consumers/keda-scaledobject.yaml"
click node_h3_context "https://github.com/harlanljones/urban-signal/blob/main/src/spatial/graph_builder.py"
click node_feature_pipeline "https://github.com/harlanljones/urban-signal/blob/main/src/features/pipeline.py"
click node_postgis "https://github.com/harlanljones/urban-signal/blob/main/src/storage/postgis_sync.py"
click node_retraining "https://github.com/harlanljones/urban-signal/blob/main/src/models/retraining_job.py"
click node_horizon_models "https://github.com/harlanljones/urban-signal/blob/main/src/models/trainer.py"
click node_onnx_export "https://github.com/harlanljones/urban-signal/blob/main/src/models/export_onnx.py"
click node_fastapi "https://github.com/harlanljones/urban-signal/blob/main/src/serving/app.py"
click node_snapshots "https://github.com/harlanljones/urban-signal/blob/main/src/export/snapshot_builder.py"
click node_cloudflare_worker "https://github.com/harlanljones/urban-signal/blob/main/workers/src/index.ts"

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_city_registry,node_municipal_sources,node_producers,node_avro_contracts toneBlue
class node_kafka,node_spatial_worker,node_feature_worker,node_postgis_worker,node_keda toneAmber
class node_h3_context,node_feature_pipeline toneMint
class node_postgis,node_object_store,node_retraining,node_horizon_models,node_onnx_export toneRose
class node_fastapi,node_snapshots,node_cloudflare_worker toneIndigo
```

## Components

### Municipal Ingestion

- [City registry](https://github.com/harlanljones/urban-signal/blob/main/src/spatial/city_registry.py): city boundary · Resolves supported city and feed availability. · `src/spatial/city_registry.py`
- [Socrata & ArcGIS](https://github.com/harlanljones/urban-signal/blob/main/src/producers/socrata_client.py): external data APIs · `src/producers/socrata_client.py`
- [Municipal producers](https://github.com/harlanljones/urban-signal/blob/main/src/producers/base_producer.py): ingestion workers · Normalize and deduplicate city feed events. · `src/producers/base_producer.py`
- **Avro contracts**: event schemas · Typed boundary between producers and consumers.

### Kafka Pipeline

- [Kafka topics](https://github.com/harlanljones/urban-signal/blob/main/deploy/k8s/kafka/kafka-topics.yaml): event backbone · Raw, enriched, feature, and alert streams. · `deploy/k8s/kafka/kafka-topics.yaml`
- [Spatial enrichment](https://github.com/harlanljones/urban-signal/blob/main/src/consumers/spatial_enrichment_worker.py): Kafka worker · Assigns events to H3 resolutions 7, 8, and 9. · `src/consumers/spatial_enrichment_worker.py`
- [Feature aggregation](https://github.com/harlanljones/urban-signal/blob/main/src/consumers/feature_aggregation_worker.py): Kafka worker · Builds temporal features and emits catalyst alerts. · `src/consumers/feature_aggregation_worker.py`
- [PostGIS sync worker](https://github.com/harlanljones/urban-signal/blob/main/src/consumers/postgis_worker.py): Kafka worker · Persists raw events and features. · `src/consumers/postgis_worker.py`
- [KEDA autoscaling](https://github.com/harlanljones/urban-signal/blob/main/deploy/k8s/consumers/keda-scaledobject.yaml): consumer scaling · `deploy/k8s/consumers/keda-scaledobject.yaml`

### Spatial Features

- [H3 & submarkets](https://github.com/harlanljones/urban-signal/blob/main/src/spatial/graph_builder.py): spatial context · Builds cell neighborhoods and maps cells to city divisions. · `src/spatial/graph_builder.py`
- [Feature pipeline](https://github.com/harlanljones/urban-signal/blob/main/src/features/pipeline.py): feature engineering · Computes CapEx, 311 dynamics, and LIMS vectors. · `src/features/pipeline.py`

### Training & Storage

- [PostGIS](https://github.com/harlanljones/urban-signal/blob/main/src/storage/postgis_sync.py): spatial system of record · `src/storage/postgis_sync.py`
- **MinIO/S3 artifacts**: feature and model storage · Stores feature partitions, ONNX models, and snapshots.
- [Retraining job](https://github.com/harlanljones/urban-signal/blob/main/src/models/retraining_job.py): model orchestration · Uses walk-forward validation and H3 spatial holdouts. · `src/models/retraining_job.py`
- [Horizon models](https://github.com/harlanljones/urban-signal/blob/main/src/models/trainer.py): forecast models · LightGBM 6m, GNN 12m, and DCN-v2 18m models. · `src/models/trainer.py`
- [ONNX export](https://github.com/harlanljones/urban-signal/blob/main/src/models/export_onnx.py): model packaging · `src/models/export_onnx.py`

### Serving & Edge

- [FastAPI service](https://github.com/harlanljones/urban-signal/blob/main/src/serving/app.py): prediction API · Resolves city/H3 context, runs inference, dashboard, and webhooks. · `src/serving/app.py`
- [Snapshot builder](https://github.com/harlanljones/urban-signal/blob/main/src/export/snapshot_builder.py): edge export · Precomputes dashboard and API responses. · `src/export/snapshot_builder.py`
- [Cloudflare Worker](https://github.com/harlanljones/urban-signal/blob/main/workers/src/index.ts): edge API · Serves KV-backed snapshots at the edge. · `workers/src/index.ts`

## Connections

- City registry → Municipal producers: supported feeds
- Socrata & ArcGIS → Municipal producers: city records
- Municipal producers → Avro contracts: typed events
- Avro contracts → Kafka topics: raw topics
- Kafka topics → Spatial enrichment: raw events
- Spatial enrichment → H3 & submarkets: H3 assignment
- Spatial enrichment → Kafka topics: enriched events
- Kafka topics → Feature aggregation: enriched stream
- H3 & submarkets → Feature pipeline: cell context
- Feature aggregation → Feature pipeline: event windows
- Feature pipeline → Kafka topics: features and alerts
- Kafka topics → PostGIS sync worker: raw and feature topics
- PostGIS sync worker → PostGIS: durable sync
- KEDA autoscaling → Spatial enrichment: scales
- KEDA autoscaling → Feature aggregation: scales
- PostGIS → Retraining job: historical data
- MinIO/S3 artifacts → Retraining job: feature partitions
- Retraining job → Horizon models: validated training
- Horizon models → ONNX export: trained models
- ONNX export → MinIO/S3 artifacts: ONNX artifacts
- MinIO/S3 artifacts → FastAPI service: model artifacts
- PostGIS → FastAPI service: grid and catalog queries
- PostGIS → Snapshot builder: queryable data
- Snapshot builder → MinIO/S3 artifacts: precomputed snapshots
- MinIO/S3 artifacts → Cloudflare Worker: edge snapshots
