Sparkline Visualization
Relevant source files
The following files were used as context for generating this wiki page:
The Sparkline Visualization in LogUtil provides a real-time, high-level graphical representation of request patterns over time. It is implemented as a specialized tab within the terminal user interface (TUI), allowing users to monitor traffic spikes and trends at a glance without navigating through granular log entries. This feature specifically visualizes the volume of requests processed by the system within defined time intervals, typically showing the last hour of activity.
The visualization utilizes the ratatui crate's Sparkline widget to render compact bar charts. It integrates directly with the project's global in-memory database to fetch aggregated time-series data, ensuring that the visual representation remains synchronized with the live log stream being monitored. Sources: src/tabs/sparkline.rs:1-15, README.md:144-148
Architecture and Data Flow
The Sparkline feature follows a structured data flow from the raw log ingestion to the final TUI rendering. Data is stored in a global singleton database, queried by the Sparkline component, and then transformed into a format suitable for the ratatui rendering engine.
Data Acquisition
The SparklineTab queries GLOBAL_DB for time-series data. The database calculates request counts based on a specified interval (defaulting to 3600 seconds or 1 hour intervals in the current implementation). Sources: src/tabs/sparkline.rs:46-47, src/memory_db.rs:373-392
The flow shows how raw database records are aggregated into intervals and eventually truncated to fit the available terminal width before being rendered. Sources: src/tabs/sparkline.rs:46-52
Component Relationship
The SparklineTab implements the Tab trait, allowing it to be managed by the central App and TabManager systems.
This diagram illustrates the relationship between the base trait, the concrete sparkline implementation, and the main application container. Sources: src/tabs/sparkline.rs:75-88, src/app.rs:56-105
Implementation Details
The SparklineTab Struct
The SparklineTab is a stateless struct in terms of persistent data, as it pulls its state from the global database during each draw cycle.
| Function | Description |
|---|---|
new() | Creates a new instance of the tab. |
draw_sparkline | Configures the visual style, including colors (Light Green/Bright Blue) and bar levels (9 levels). |
draw_requests_sparkline | Fetches data from GLOBAL_DB, calculates statistics (Total, Avg, Max), and renders the widget. |
handle_input | Currently returns false as the Sparkline tab is purely informational and does not handle keyboard events. |
Sources: src/tabs/sparkline.rs:17-44, src/tabs/sparkline.rs:81-84
Visual Configuration
The visualization is styled using specific RGB colors to provide a modern TUI aesthetic:
- Border Color: Light Green (RGB 144, 238, 144) using a
Roundedborder type. - Line/Bar Color: Bright Blue (RGB 0, 191, 255).
- Background: Dark Gray (RGB 28, 28, 28).
- Direction:
RightToLeft, meaning newer data enters from the right side of the graph.
Sources: src/tabs/sparkline.rs:22-38
Statistics Calculation
Before rendering the bars, the component calculates several key metrics displayed in the widget title:
- Total Requests: The sum of all values in the current data set.
- Average Requests: The total divided by the number of intervals.
- Maximum Requests: The highest peak found in the data set, which is also used to scale the sparkline height via the
.max()method.
Sources: src/tabs/sparkline.rs:42-44, src/tabs/sparkline.rs:64-71
Integration and Configuration
The Sparkline Visualization is an optional component that must be enabled via command-line arguments or the interactive settings menu.
Enabling the Feature
In main.rs, the application checks the enable_sparkline flag. If set, it adds the SparklineTab to the application's active tab list. Sources: src/main.rs:105-106, src/app.rs:68-71
// From src/app.rs:68-71
if config.enable_sparkline {
tab_names.push("Sparkline".to_string());
tabs.push(Box::new(SparklineTab::new()));
}
Configuration Options
Users can control the sparkline through the following CLI options:
| Option | Type | Default | Description |
|---|---|---|---|
--enable-sparkline | Flag | false | Enables the Sparkline tab in the TUI. |
count | Integer | 0 | Influences the amount of historical data initially read into the database for visualization. |
Sources: src/main.rs:105-106, README.md:239-241
Conclusion
Sparkline Visualization serves as the primary time-series monitoring tool within LogUtil. By aggregating data from the MemoryDB and utilizing the ratatui rendering engine, it provides a performant and visually distinct way to track Nginx request volumes. While it currently lacks interactive filtering, its integration into the broader Tab-based architecture allows it to function as a critical component of the real-time analysis suite. Sources: src/tabs/sparkline.rs:46-73, src/app.rs:205-208