Skip to content

magj2006/k-line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

K-line Data Service

A high-performance real-time data service for meme token trading platform built in Rust, providing K-line (candlestick) chart data and real-time transaction streaming with a modern web interface.

πŸš€ Features

Core Functionality

  • Real-time K-line Data: Provides candlestick chart data for meme tokens (DOGE, SHIB, PEPE)
  • Multiple Time Intervals: Supports 1s, 1m, 5m, 15m, 1h intervals with proper time alignment
  • Real-time Transaction Streaming: WebSocket-based live transaction feed
  • Interactive Web Interface: Modern HTML5 interface with real-time data visualization
  • Mock Data Generation: Built-in configurable data generator for testing and demonstration
  • Configuration Management: TOML-based configuration with environment support

🌐 Web Interface

Access the interactive web interface at http://localhost:8080/:

  • Real-time Transaction Stream: Filter by token (ALL, DOGE, SHIB, PEPE)
  • Multi-timeframe K-line Display: Switch between different intervals
  • System Status Monitoring: Connection status and subscription management
  • Responsive Design: Works on desktop and mobile devices

πŸ“‘ API Endpoints

REST API

  • GET /api/v1/klines - Get historical K-line data with filtering
  • GET /api/v1/klines/latest - Get the latest completed K-line
  • GET /api/v1/klines/current - Get current open K-line
  • GET /api/v1/tokens - Get list of available tokens
  • GET /api/v1/stats - Get service statistics
  • GET /api/v1/health - Health check endpoint

WebSocket API

  • WS /ws - Real-time data streaming endpoint

WebSocket Subscriptions

The WebSocket API supports three types of subscriptions:

  1. All Transactions: Receive all transaction updates

    {"action":"subscribe","subscription":{"type":"all_transactions"}}
  2. Token-specific Transactions: Receive transactions for specific tokens

    {"action":"subscribe","subscription":{"type":"transactions","tokens":["DOGE","SHIB"]}}
  3. K-line Updates: Receive real-time K-line updates for specific token/interval

    {"action":"subscribe","subscription":{"type":"klines","token":"DOGE","interval":"1m"}}

πŸ—οΈ Project Structure

src/
β”œβ”€β”€ main.rs                 # Application entry point with dependency injection
β”œβ”€β”€ lib.rs                  # Library exports
β”œβ”€β”€ config.rs               # Configuration management
β”œβ”€β”€ models/                 # Data models
β”‚   β”œβ”€β”€ mod.rs             # Module exports
β”‚   β”œβ”€β”€ kline.rs           # K-line data structure with time alignment
β”‚   β”œβ”€β”€ transaction.rs     # Transaction data structure
β”‚   └── time_interval.rs   # Time interval enum with proper parsing
β”œβ”€β”€ services/              # Business logic
β”‚   β”œβ”€β”€ mod.rs             # Module exports
β”‚   β”œβ”€β”€ kline.rs           # K-line data management with DashMap
β”‚   └── mock_data.rs       # Configurable mock data generation
└── api/                   # API layer
    β”œβ”€β”€ mod.rs             # Module exports
    β”œβ”€β”€ rest.rs            # REST API endpoints with proper error handling
    └── websocket.rs       # WebSocket implementation with session management

config/                     # Configuration files
β”œβ”€β”€ default.toml           # Default configuration
β”œβ”€β”€ development.toml       # Development environment
└── production.toml        # Production environment

tests/                      # Test suites
β”œβ”€β”€ api_tests.rs           # API endpoint tests
β”œβ”€β”€ kline_tests.rs         # K-line service tests
└── time_interval_tests.rs # Time alignment tests

benches/                    # Performance benchmarks
└── performance.rs         # Benchmark suite

websocket_test.html         # Interactive web interface
docker-compose.yml          # Docker deployment configuration
Dockerfile                  # Multi-stage Docker build

πŸš€ Setup and Running

Prerequisites

  • Rust 1.82+
  • Cargo

Installation

git clone <repository-url>
cd k-line
cargo build --release

Configuration

The service uses a hierarchical TOML configuration system for easy management across different environments.

Configuration Files

The service loads configuration in the following order:

  1. Base Configuration: config/default.toml - Default settings
  2. Environment Configuration: config/{environment}.toml - Environment-specific overrides
  3. Environment Variables: Optional runtime overrides (see below)

Example Configuration

Base configuration (config/default.toml):

[server]
host = "0.0.0.0"
port = 8080

[tokens]
[[tokens.supported_tokens]]
symbol = "DOGE"
base_price = 0.15
volatility = 5.0

[[tokens.supported_tokens]]
symbol = "SHIB"
base_price = 0.00005
volatility = 8.0

[data_generation]
enabled = true
interval_ms = 100
volatility = 0.02
volume_range = [100.0, 1000.0]

[performance]
worker_threads = 4
websocket_heartbeat_interval = 5
client_timeout = 10
kline_retention_hours = 24
max_websocket_connections = 1000

Development configuration (config/development.toml):

[server]
host = "127.0.0.1"  # Override for local development
workers = 2

[logging]
level = "debug"
file_output = false

[performance]
worker_threads = 2
max_websocket_connections = 100

Environment Selection

# Use development configuration (default)
cargo run

# Use production configuration
RUST_ENV=production cargo run

# Use custom environment
RUST_ENV=staging cargo run

Running the Service

Option 1: Direct Cargo Run

# Development mode (default)
cargo run

# Production mode
RUST_ENV=production cargo run

Option 2: Docker (Recommended)

# Build and run with Docker Compose
docker-compose up --build

# Run in background
docker-compose up -d --build

# View logs
docker-compose logs -f k-line

# Stop the service
docker-compose down

The service will start on http://localhost:8080

🌐 Accessing the Web Interface

  1. Open your browser and navigate to http://localhost:8080/websocket_test.html
  2. Click "Connect" to establish WebSocket connection
  3. Monitor real-time data:
    • Transaction stream with token filtering
    • K-line data with interval switching
    • System status and connection info

πŸ“Š API Usage Examples

REST API

Get Available Tokens

curl http://localhost:8080/api/v1/tokens
# Response: {"tokens":["DOGE","SHIB","PEPE"],"count":3}

Get K-line Data

curl "http://localhost:8080/api/v1/klines?token=DOGE&interval=1m&limit=10"
# Response: {"token":"DOGE","interval":"1m","data":[...]}

Get Current Open K-line

curl "http://localhost:8080/api/v1/klines/current?token=DOGE&interval=1m"
# Response: {"token":"DOGE","interval":"1m","data":{...},"is_open":true}

Health Check

curl http://localhost:8080/api/v1/health
# Response: {"status":"healthy","service":"k-line-data-service","timestamp":"..."}

WebSocket API

Connect to ws://localhost:8080/ws and send subscription messages:

const ws = new WebSocket('ws://localhost:8080/ws');

ws.onopen = function() {
    // Subscribe to all transactions
    ws.send(JSON.stringify({
        action: 'subscribe',
        subscription: { type: 'all_transactions' }
    }));

    // Subscribe to DOGE 1-minute K-lines
    ws.send(JSON.stringify({
        action: 'subscribe',
        subscription: { type: 'klines', token: 'DOGE', interval: '1m' }
    }));
};

ws.onmessage = function(event) {
    const data = JSON.parse(event.data);
    console.log('Received:', data);
};

πŸ“‹ Data Models

K-line Structure

{
    "token": "DOGE",
    "timestamp": "2025-05-28T04:00:00Z",
    "interval": "1m",
    "open": 0.15,
    "high": 0.16,
    "low": 0.14,
    "close": 0.155,
    "volume": 1000.0,
    "is_closed": false
}

Transaction Structure

{
    "token": "DOGE",
    "price": 0.15,
    "volume": 100.0,
    "timestamp": "2025-05-28T04:00:00Z",
    "is_buy": true
}

πŸ›οΈ Architecture

Real-time Data Flow

  1. Mock Data Generator creates random transactions every 100ms (configurable)
  2. K-line Service processes transactions and updates K-lines for all intervals simultaneously
  3. Time Alignment ensures K-lines align to natural time boundaries
  4. WebSocket Manager broadcasts updates to subscribed clients with session management
  5. REST API provides historical data access with proper error handling

Technical Implementation

  • Storage: Direct DashMap usage for high-performance concurrent access
  • Memory Management: In-memory storage with configurable retention policies
  • Concurrency: Lock-free data structures for optimal performance
  • Time Handling: Precise interval alignment using UTC timestamps
  • Error Handling: Comprehensive error propagation and logging

Performance Characteristics

Based on benchmark results:

  • Single Transaction Processing: ~11.8 Β΅s
  • Concurrent Transaction Processing: ~167 Β΅s
  • K-line Query: ~4.2 Β΅s
  • High-frequency Updates: ~1.17 ms
  • WebSocket Broadcasting: Sub-millisecond latency

πŸ§ͺ Testing

Run the comprehensive test suite:

# Run all tests
cargo test

# Run specific test suite
cargo test --test api_tests
cargo test --test kline_tests
cargo test --test time_interval_tests

# Run with output
cargo test -- --nocapture

# Run benchmarks
cargo bench

Test Coverage:

  • βœ… 7 Unit tests (models, config)
  • βœ… 5 API tests (REST endpoints)
  • βœ… 9 K-line service tests
  • βœ… 6 Time alignment tests
  • βœ… Performance benchmarks

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“ž Support

For questions and support:

  • Create an issue in the repository
  • Check the documentation in the docs/ directory
  • Review the test cases for usage examples

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors