# sensortower/heatmap architecture

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

- Interactive diagram: https://gitdiagram.com/sensortower/heatmap
- Repository: https://github.com/sensortower/heatmap

## Overview

##### Project Type and Purpose
- The project is a standalone, in-memory time series database implemented in Go.
- It accepts metrics using a statsd protocol (see statsd.go) and renders data through an HTTP API (using modules like http.go and httprender.go).
- It targets visualization in Grafana’s heatmap panel, and it also includes functionality for generating dummy data (dummy.go) for testing or demonstration.

##### Analyzing the File Structure
- Top-Level Files (e.g., main.go, config.go, datastore.go, logging.go, etc.) indicate that much of the core logic is spread out over individual components.
- The directory “cmd/heatmap” contains the CLI entry point (main.go) for running the application.
- Files such as ramdatastore.go (and its test file) suggest a specific, in-memory implementation for data storage.
- Several files are dedicated to monitoring and profiling (memwatch*.go, pprof.go) which provide insight into performance and memory usage.
- The presence of heatmap_suite_test.go points to an integrated testing suite, ensuring the system components are working as expected.

##### Recommended System Design Components
a. Main Application / CLI:
- Represented by main.go and the cmd/heatmap/main.go. This is the entry point of the application, responsible for reading configuration (via config.go) and command-line flags (like -create-dummy-data, -http-addr, and -statsd-addr).

b. Statsd Input Interface:

c. In-Memory Datastore:
- Abstracted in datastore.go with a concrete implementation in ramdatastore.go. This module stores and organizes time series data in memory.

d. HTTP API & Rendering:

e. Dummy Data Generator:
- Represented by dummy.go, this optional component can be indicated as an auxiliary module for testing and demo purposes when the -create-dummy-data flag is used.

f. Monitoring & Profiling:
- With dedicated modules like memwatch (including OS-specific implementations) and pprof.go, include these components to show how operational metrics and profiling data are collected and analyzed.

g. Logging and Diagnostics:
- The logging.go file suggests a centralized logging system. Include this as an underpinning service that all other modules use to log their activities.

##### Relationships and Interactions
- Incoming metrics (using statsd) flow from external clients → Statsd Interface.
- The Statsd Interface forwards data to the In-Memory Datastore.
- Simultaneously, if dummy data is enabled, the Dummy Data Generator injects sample metrics into the Datastore.
- The HTTP API receives requests from visualization tools (like Grafana) → fetches and renders data via the In-Memory Datastore.
- Monitoring and Profiling modules (memwatch and pprof) continuously observe system performance across these flows.
- That the Main Application/CLI is the orchestrator that initializes these components based on configuration.

##### Architectural Patterns and Design Principles
- Separation of concerns: Input (Statsd), Storage (Datastore), and Output (HTTP API for rendering).
- Modularity: Each module (or file) encapsulates a distinct responsibility.
- Pattern for extensibility and testing, such as dependency injection for the datastore and using a suite of tests (e.g., heatmap_suite_test.go).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Initialization Layer
    subgraph "Initialization Layer"
        CLI["Main Application / CLI"]:::component
    end

    %% Data Ingestion Layer
    subgraph "Data Ingestion Layer"
        Statsd["Statsd Input Interface"]:::component
        Dummy["Dummy Data Generator"]:::component
    end

    %% Data Storage Layer
    subgraph "Data Storage Layer"
        Datastore["In-Memory Datastore"]:::datastore
    end

    %% API Layer
    subgraph "API Layer"
        API["HTTP API & Rendering"]:::api
    end

    %% Cross-Cutting Services
    Monitor["Monitoring & Profiling"]:::monitor
    Log["Logging and Diagnostics"]:::logging

    %% Primary Flow Connections
    CLI -->|"init"| Statsd
    CLI -->|"init"| Dummy
    CLI -->|"init"| Datastore
    CLI -->|"init"| API

    Statsd -->|"input_metrics"| Datastore
    Dummy -->|"inject_dummy"| Datastore
    API -->|"query_data"| Datastore

    %% Monitoring Connections
    Statsd -->|"monitored"| Monitor
    Dummy -->|"monitored"| Monitor
    Datastore -->|"monitored"| Monitor
    API -->|"monitored"| Monitor
    CLI -->|"monitored"| Monitor

    %% Logging Connections (dotted)
    CLI ---|"logs"| Log
    Statsd ---|"logs"| Log
    Dummy ---|"logs"| Log
    Datastore ---|"logs"| Log
    API ---|"logs"| Log
    Monitor ---|"logs"| Log

    %% Click Events for Main Application / CLI
    click CLI "https://github.com/sensortower/heatmap/blob/master/cmd/heatmap/main.go"
    click CLI "https://github.com/sensortower/heatmap/blob/master/main.go"
    click CLI "https://github.com/sensortower/heatmap/blob/master/config.go"

    %% Click Event for Statsd Input Interface
    click Statsd "https://github.com/sensortower/heatmap/blob/master/statsd.go"

    %% Click Events for In-Memory Datastore
    click Datastore "https://github.com/sensortower/heatmap/blob/master/datastore.go"
    click Datastore "https://github.com/sensortower/heatmap/blob/master/ramdatastore.go"
    click Datastore "https://github.com/sensortower/heatmap/blob/master/ramdatastore_test.go"

    %% Click Events for HTTP API & Rendering
    click API "https://github.com/sensortower/heatmap/blob/master/http.go"
    click API "https://github.com/sensortower/heatmap/blob/master/httprender.go"

    %% Click Event for Dummy Data Generator
    click Dummy "https://github.com/sensortower/heatmap/blob/master/dummy.go"

    %% Click Events for Monitoring & Profiling
    click Monitor "https://github.com/sensortower/heatmap/blob/master/memwatch.go"
    click Monitor "https://github.com/sensortower/heatmap/blob/master/memwatch_darwin.go"
    click Monitor "https://github.com/sensortower/heatmap/blob/master/memwatch_linux.go"
    click Monitor "https://github.com/sensortower/heatmap/blob/master/memwatch_other.go"
    click Monitor "https://github.com/sensortower/heatmap/blob/master/pprof.go"

    %% Click Event for Logging and Diagnostics
    click Log "https://github.com/sensortower/heatmap/blob/master/logging.go"

    %% Styles
    classDef component fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef datastore fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef api fill:#A9DFBF,stroke:#1E8449,stroke-width:2px;
    classDef monitor fill:#F5CBA7,stroke:#E67E22,stroke-width:2px;
    classDef logging fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
```
