# allegro/turnilo architecture

> Architecture diagram of the GitHub repository allegro/turnilo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/allegro/turnilo
- Repository: https://github.com/allegro/turnilo

## Overview

##### Project type and purpose
- Turnilo is a full-stack, stateless web application for interactive exploration and visualization of Apache Druid data.
- It provides a React/TypeScript single-page application (SPA) client, a Node.js/Express-style server and a CLI wrapper to start in various modes (run examples, connect to Druid, run with config).
- Shared “common” modules house domain models, query/serialization logic and visualization manifests, used by both client and server.

##### Top-level directories and architectural patterns
- bin/turnilo – CLI entrypoint that parses arguments, loads config and orchestrates server startup.
- src/client – React/TS SPA, built with Webpack (dev/prod). Implements UI components, views, visualizations, styles and deserializers that consume server JSON APIs.
- src/server – Node.js/TypeScript HTTP server (Express under the hood). Defines CLI commands, loads configuration, exposes REST routes for liveness/readiness, data-cube discovery, query (Timeseries/TopN/GroupBy), URL shortening, static file serving, plugins and request decorators.
- src/common – Shared models (Essence, DataCube, FilterClause, etc.), utils, visualization manifests and view-definition converters. Centralizes business logic, serialization and compatibility layers.
- config, config-examples.yaml – Configuration files for clusters, data cubes, plugins, OAuth, etc.
- assets, lib, docs – Static JSON data sources, fonts, polyfills and Jekyll-based documentation.
- cypress – End-to-end tests against a live Turnilo server with example datasets.
- test – Unit tests for common, client and server modules.

##### Key architectural patterns and design principles
- Monorepo with three vertically layered modules: common (domain + shared logic), client (presentation + consumption), server (API + orchestration).
- Stateless server: no session/state stored server-side. All state lives in the client (Essence model, URL‐hash, local storage).
- Plugin and request-decorator system: server dynamically loads plugins and decorators per config.
- Code-as-config for data-cubes: YAML/JSON config files instead of UI editor.
- CI/CD automated via GitHub Actions: lint/test/build/e2e/deploy workflows.
- Containerization: Dockerfile to package the server + built SPA assets into a container.

##### Main components and their interactions
[Browser (User)]
↓ Browses to SPA URL
[Node.js Server]
- Serves static SPA assets
- Exposes REST endpoints:
– /liveness, /readiness
– /sources (list Druid datasources)
– /query (Timeseries, TopN, GroupBy, raw data, pinboard)
– /mkurl & /shorten (URL‐shortener service)
– /plyql & /plywood (ad hoc query endpoints)
- Loads config, plugins, request decorators
- Uses common/models & utils to validate/serialize requests
↓ Forwards Druid queries
[Apache Druid Broker]
← Returns query results JSON
[Node.js Server]
↓ Returns JSON to client
[Browser (React App)]
- Deserializers (app-settings, cluster, data-cube, etc.) build client‐side models
- Renders charts (bar, line, grid, heat-map, table, scatterplot, totals) using visualization settings
- Updates URL hash / local storage with view definition

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% Frontend Layer
  subgraph "Frontend (React/TypeScript SPA)"
    direction TB
    Browser["User's Web Browser"]:::external
    EntryFE["SPA Entry Point\n(main.tsx)"]:::frontend
    StylesFE["Main Styles\n(main.scss)"]:::frontend
    ComponentsFE["UI Components & Views"]:::frontend
    DeserializersFE["Client-side Deserializers"]:::frontend
    ConfigFE["Client Config & Webpack"]:::frontend
  end

  %% Common Layer
  subgraph "Shared Common Module"
    direction TB
    DomainModels["Domain Models & Serialization"]:::common
    VizManifests["Visualization Manifests"]:::common
    ViewDefs["View-Definition Converters"]:::common
  end

  %% Backend Layer
  subgraph "Backend (Node.js/Express)"
    direction TB
    CLIEntry["CLI Entrypoint\n(bin/turnilo)"]:::backend
    Bootstrap["Server Bootstrap & Settings Loader"]:::backend
    ExpressApp["HTTP Server (Express App)"]:::backend
    CLICommands["Server CLI Commands"]:::backend
    Routes["REST Route Handlers"]:::backend
    Utils["Server Utilities & Plugin Loader"]:::backend
  end

  %% External Services
  subgraph "External Services"
    direction TB
    Druid["Apache Druid Broker"]:::data
    ConfigStore["Configuration Store\n(YAML, env)"]:::config
    Assets["Static Assets\n(assets/, lib/)"]:::infra
    Docs["Documentation\n(docs/)"]:::infra
    CI["CI/CD Pipeline\n(GitHub Actions)"]:::infra
    Docker["Docker Registry"]:::infra
  end

  %% Tests
  subgraph "Testing"
    direction TB
    E2E["End-to-End Tests (Cypress)"]:::test
    UnitTests["Unit Tests (Mocha)"]:::test
  end

  %% Interactions
  Browser -->|"Loads SPA"| EntryFE
  EntryFE --> StylesFE
  EntryFE --> ComponentsFE
  EntryFE --> DeserializersFE
  EntryFE --> ConfigFE
  EntryFE -->|HTTP GET static| ExpressApp

  ExpressApp -->|Serves assets| Assets
  ExpressApp -->|Uses common logic| DomainModels
  ExpressApp -->|Uses common logic| VizManifests
  ExpressApp -->|Uses common logic| ViewDefs
  ExpressApp -->|Loads config| ConfigStore
  Bootstrap --> ExpressApp
  CLIEntry --> Bootstrap
  CLIEntry --> CLICommands
  CLICommands --> ExpressApp
  ExpressApp --> Utils
  Utils --> Routes
  Routes -->|Query Druid via HTTP/JSON| Druid
  Druid -->|JSON results| ExpressApp
  ExpressApp -->|JSON API response| Browser

  E2E -->|HTTP calls| ExpressApp
  UnitTests -->|Test modules| DomainModels
  UnitTests -->|Test modules| ComponentsFE
  UnitTests -->|Test modules| ExpressApp

  CI -->|Build & Test| EntryFE
  CI -->|Build & Test| ExpressApp
  CI -->|Publish Docker image| Docker
  Docker -->|Deploy| ExpressApp

  %% Click events
  click CLIEntry "https://github.com/allegro/turnilo/tree/master/bin/turnilo"
  click Bootstrap "https://github.com/allegro/turnilo/blob/master/src/server/cli.ts"
  click ExpressApp "https://github.com/allegro/turnilo/blob/master/src/server/app.ts"
  click CLICommands "https://github.com/allegro/turnilo/tree/master/src/server/cli/*"
  click Routes "https://github.com/allegro/turnilo/tree/master/src/server/routes/*"
  click Utils "https://github.com/allegro/turnilo/tree/master/src/server/utils/*"
  click DomainModels "https://github.com/allegro/turnilo/tree/master/src/common/*"
  click VizManifests "https://github.com/allegro/turnilo/tree/master/src/common/visualization-manifests/*"
  click ViewDefs "https://github.com/allegro/turnilo/tree/master/src/common/view-definitions/*"
  click EntryFE "https://github.com/allegro/turnilo/blob/master/src/client/main.tsx"
  click StylesFE "https://github.com/allegro/turnilo/blob/master/src/client/main.scss"
  click ComponentsFE "https://github.com/allegro/turnilo/tree/master/src/client/components/*"
  click DeserializersFE "https://github.com/allegro/turnilo/tree/master/src/client/deserializers/*"
  click ConfigFE "https://github.com/allegro/turnilo/blob/master/src/client/config/constants.ts"
  click ConfigFE "https://github.com/allegro/turnilo/blob/master/config/webpack.dev.js"
  click ConfigFE "https://github.com/allegro/turnilo/blob/master/config/webpack.prod.js"
  click Assets "https://github.com/allegro/turnilo/tree/master/assets/"
  click Assets "https://github.com/allegro/turnilo/tree/master/lib/polyfill/*"
  click E2E "https://github.com/allegro/turnilo/tree/master/cypress/"
  click E2E "https://github.com/allegro/turnilo/blob/master/cypress.config.ts"
  click UnitTests "https://github.com/allegro/turnilo/tree/master/test/"
  click Docs "https://github.com/allegro/turnilo/tree/master/docs/"
  click Docker "https://github.com/allegro/turnilo/tree/master/Dockerfile"
  click Docker "https://github.com/allegro/turnilo/blob/master/.dockerignore"
  click CI "https://github.com/allegro/turnilo/blob/master/.github/workflows/build.yml"
  click CI "https://github.com/allegro/turnilo/blob/master/.github/workflows/test.yml"
  click CI "https://github.com/allegro/turnilo/blob/master/.github/workflows/deploy.yml"

  %% Styles
  classDef frontend fill:#ffe4e1,stroke:#333,stroke-width:1px
  classDef backend fill:#e0ffff,stroke:#333,stroke-width:1px
  classDef common fill:#fafad2,stroke:#333,stroke-width:1px
  classDef data fill:#d3ffce,stroke:#333,stroke-width:1px
  classDef config fill:#f0e68c,stroke:#333,stroke-width:1px
  classDef infra fill:#d3d3d3,stroke:#333,stroke-width:1px
  classDef test fill:#e6e6fa,stroke:#333,stroke-width:1px
  classDef external fill:#fff0f5,stroke:#333,stroke-width:1px
```
