Dashboard Compiler Architecture¶
This document describes the architecture of the dashboard compiler, which converts a simplified YAML representation of Kibana dashboards into the complex Kibana dashboard JSON format.
Goal¶
The primary goal is to provide a human-readable and maintainable way to define Kibana dashboards using YAML, abstracting away the complexities of the native JSON structure.
Design¶
The compiler is designed using a layered approach with distinct components responsible for different stages of the conversion process.
-
YAML Loading and Parsing:
- The process begins by loading the YAML configuration file.
- The
PyYAMLlibrary is used to parse the YAML content into a Python dictionary.
-
Pydantic Model Representation:
- The codebase uses a three-layer pattern with separate models for input configuration and output views:
- Config Models (
**/config.py): Define the YAML schema structure using Pydantic, handling validation and ensuring the parsed YAML conforms to the defined schema. These models use aBaseCfgModelbase class. - View Models (
**/view.py): Define the Kibana JSON output structure using Pydantic. These models use aBaseVwModelbase class and include custom serialization logic. - Compile Functions (
**/compile.py): Transform config models into view models, handling the specific formatting and mapping required for each component. - Each major component of a dashboard (Dashboard, Panel, Grid, etc.) and its variations (different panel types, Lens visualizations, dimensions, metrics, etc.) follows this pattern.
- A custom validator in the base
Panelclass is used to dynamically instantiate the correct panel subclass based on thetypefield in the YAML data.
-
Compilation Process:
- Compile functions in
compile.pyfiles take config model instances and transform them into view model instances. - These functions handle the specific formatting and nesting required for each element (panels, visualizations, layers, etc.).
- The top-level dashboard compilation orchestrates the compilation of all components (panels, controls, filters, queries) and assembles the final Kibana JSON structure.
- View models use Pydantic's
model_dump_json()method to serialize to JSON.
- Compile functions in
Components¶
The codebase is organized into packages:
Core Package (packages/kb-dashboard-core/src/kb_dashboard_core/):
dashboard_compiler.py: Main entry point containing the core compilation orchestration functions (load,render,dump).dashboard/: Top-level dashboard compilation withconfig.py,view.py, andcompile.py.panels/: Panel compilation with subdirectories for each panel type (markdown, links, images, search, charts).panels/charts/: Chart-specific compilation with subdirectories for different chart types (metric, pie, xy, gauge, heatmap, datatable, waffle, mosaic, treemap) and components (lens/esql metrics, dimensions, columns).controls/: Control group compilation for dashboard interactivity.filters/: Filter compilation supporting various filter types.queries/: Query compilation for KQL, Lucene, and ES|QL.
CLI Package (packages/kb-dashboard-cli/src/dashboard_compiler/):
cli.py: Command-line interface for compiling dashboards and uploading to Kibana.lsp/: Language Server Protocol implementation for VS Code extension.sample_data/: Sample data loading utilities.tools/: CLI tooling utilities.
Data Flow¶
- YAML file is read.
- YAML content is parsed into a Python dictionary.
- The dictionary is validated and converted into a hierarchy of Pydantic config model objects.
- Compile functions transform the config model hierarchy into view model objects.
- The compile functions are called recursively on nested objects to build the view model structure.
- View models are serialized to JSON using Pydantic's
model_dump_json()method. - A Kibana-compatible NDJSON file is generated.
graph TD
A[YAML File] --> B{Load & Parse YAML}
B --> C[Python Dictionary]
C --> D{Pydantic Validation}
D --> E[Config Model Hierarchy]
E --> F{Compile Functions}
F --> G[View Model Hierarchy]
G --> H{model_dump_json}
H --> I[Kibana JSON/NDJSON]