fuse-rule 0.1.0

High-performance, Arrow-native Complex Event Processing (CEP) engine with SQL-powered rules
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# FuseRule ⚑

[![Crates.io](https://img.shields.io/crates/v/arrow-rule-agent.svg)](https://crates.io/crates/arrow-rule-agent)
[![Documentation](https://docs.rs/arrow-rule-agent/badge.svg)](https://docs.rs/arrow-rule-agent)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)

**FuseRule** is a high-performance, developer-first rule engine built for the cloud-native ecosystem. It leverages **Apache Arrow** and **DataFusion** to provide a lightning-fast, SQL-expressive core for real-time data auditing and event processing.

Designed as an **Infrastructure Primitive**, FuseRule decouples its deterministic core from pluggable "edges" like persistence, evaluation engines, and notification agents.

## πŸš€ Features

- ⚑ **Arrow-Native**: Zero-copy columnar data processing for maximum performance
- πŸ” **SQL-Powered Rules**: Write complex predicates using standard SQL expressions
- πŸ—οΈ **Infrastructure-First**: Decoupled core with swappable traits for `StateStore`, `RuleEvaluator`, and `Agent`
- πŸ“Š **Real-Time Observability**: Returns machine-readable `EvaluationTrace` logs for every ingestion
- πŸ”„ **Zero-Downtime Reloading**: Hot-swap rules and agents via `SIGHUP` without restarting the daemon
- πŸ“ˆ **Cloud-Native Metrics**: Built-in Prometheus-formatted telemetry
- πŸ”” **Stateful Transitions**: Built-in state management for `Activated` and `Deactivated` transitions
- πŸͺŸ **Time Windows**: Sliding time windows for aggregate functions (AVG, COUNT, SUM)
- πŸ” **API Key Authentication**: Secure endpoints with configurable API keys
- 🚦 **Rate Limiting**: Built-in rate limiting for ingestion endpoints
- πŸ“‘ **Multiple Ingestion Sources**: HTTP, Kafka, and WebSocket support
- 🎨 **Action Templates**: Handlebars templating for custom webhook payloads
- πŸ› **Interactive Debugging**: REPL and step-through debugger for rule development

## πŸ“¦ Installation

### As a Library

Add to your `Cargo.toml`:

```toml
[dependencies]
arrow-rule-agent = "0.1.0"
```

### As a Binary

```bash
cargo install arrow-rule-agent
```

## 🎯 Quickstart

### 1. Create a Configuration File

Create `fuse_rule_config.yaml`:

```yaml
engine:
  persistence_path: "fuserule_state"
  ingest_rate_limit: 1000  # requests per second
  api_keys:
    - "sk_live_abc123..."

schema:
  - name: "price"
    data_type: "float64"
  - name: "symbol"
    data_type: "utf8"
  - name: "volume"
    data_type: "int32"

agents:
  - name: "logger"
    type: "logger"
  - name: "slack-webhook"
    type: "webhook"
    url: "https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
    template: |
      {
        "text": "🚨 {{rule_name}} triggered!",
        "symbol": "{{matched_data.0.symbol}}",
        "price": "{{matched_data.0.price}}"
      }

rules:
  - id: "high_price_alert"
    name: "High Price Alert"
    predicate: "price > 1000"
    action: "slack-webhook"
    version: 1
    enabled: true
    state_ttl_seconds: 3600  # Expire state after 1 hour

  - id: "volume_spike"
    name: "Volume Spike"
    predicate: "AVG(volume) > 10000"
    action: "logger"
    window_seconds: 60  # 60-second sliding window
    version: 1
    enabled: true
```

### 2. Start the Server

```bash
fuserule run --config fuse_rule_config.yaml --port 3030
```

### 3. Ingest Data

```bash
curl -X POST http://localhost:3030/ingest \
     -H "Content-Type: application/json" \
     -H "X-API-Key: sk_live_abc123..." \
     -d '[{"price": 1500, "symbol": "AAPL", "volume": 5000}]'
```

### 4. Check Rule States

```bash
# Get all rule states
curl http://localhost:3030/api/v1/state \
     -H "X-API-Key: sk_live_abc123..."

# Get specific rule state
curl http://localhost:3030/api/v1/state/high_price_alert \
     -H "X-API-Key: sk_live_abc123..."
```

## πŸ“š Usage as a Library

### Basic Example

```rust
use arrow_rule_agent::{RuleEngine, config::FuseRuleConfig};
use arrow::array::{Float64Array, StringArray};
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Load configuration
    let config = FuseRuleConfig::from_file("fuse_rule_config.yaml")?;
    
    // Create engine from config
    let mut engine = RuleEngine::from_config(config).await?;
    
    // Create a test batch
    let schema = Schema::new(vec![
        Field::new("price", DataType::Float64, true),
        Field::new("symbol", DataType::Utf8, true),
    ]);
    
    let price_array = Arc::new(Float64Array::from(vec![1500.0, 500.0]));
    let symbol_array = Arc::new(StringArray::from(vec!["AAPL", "GOOGL"]));
    
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![price_array, symbol_array],
    )?;
    
    // Process batch and get evaluation traces
    let traces = engine.process_batch(&batch).await?;
    
    for trace in traces {
        if trace.action_fired {
            println!("Rule '{}' activated!", trace.rule_name);
        }
    }
    
    Ok(())
}
```

### Programmatic Rule Management

```rust
use arrow_rule_agent::{RuleEngine, rule::Rule};

// Add a rule programmatically
let rule = Rule {
    id: "custom_rule".to_string(),
    name: "Custom Rule".to_string(),
    predicate: "price > 100 AND volume < 50".to_string(),
    action: "logger".to_string(),
    window_seconds: None,
    version: 1,
    enabled: true,
};

engine.add_rule(rule).await?;

// Update a rule
engine.update_rule("custom_rule", updated_rule).await?;

// Toggle rule
engine.toggle_rule("custom_rule", false).await?;
```

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      FuseRule Engine                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚   Ingest     │───▢│   Evaluate   │───▢│   Activate   β”‚ β”‚
β”‚  β”‚   Sources    β”‚    β”‚   Rules      β”‚    β”‚   Agents     β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚         β”‚                   β”‚                    β”‚           β”‚
β”‚         β”‚                   β”‚                    β”‚           β”‚
β”‚         β–Ό                   β–Ό                    β–Ό           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚          Arrow RecordBatch (Zero-Copy)              β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚   State      β”‚    β”‚   Windows     β”‚    β”‚   Metrics    β”‚ β”‚
β”‚  β”‚   Store      β”‚    β”‚   Buffers     β”‚    β”‚   (Prom)     β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Design Philosophy: "Hard Core, Soft Edges"

FuseRule is built on the philosophy that the core logic of a rule engine should be a "boring," deterministic primitive, while the integration points (Ingress, Persistence, Notifications) should be flexible and pluggable.

**Core (Hard):**
- Rule evaluation logic
- State transitions
- Window management
- Metrics collection

**Edges (Soft):**
- `StateStore` trait (Sled, Redis, etc.)
- `RuleEvaluator` trait (DataFusion, custom SQL engines)
- `Agent` trait (Webhooks, Loggers, custom actions)
- Ingestion sources (HTTP, Kafka, WebSocket)

## πŸ”§ CLI Commands

### Run Server

```bash
fuserule run --config fuse_rule_config.yaml --port 3030
```

### Validate Rules

```bash
# Validate all rules in config
fuserule validate --config fuse_rule_config.yaml

# Validate specific predicate
fuserule validate --config fuse_rule_config.yaml --predicate "price > 100"
```

### Interactive REPL

```bash
fuserule repl --config fuse_rule_config.yaml
```

### Rule Debugger

```bash
fuserule debug --config fuse_rule_config.yaml
```

## πŸ“‘ API Endpoints

### Public Endpoints

- `GET /status` - Server status
- `GET /health` - Health check with engine stats
- `GET /metrics` - Prometheus metrics

### Protected Endpoints (Require `X-API-Key` header)

#### Rule Management

- `GET /rules` - List all rules
- `POST /api/v1/rules` - Create new rule
- `PUT /api/v1/rules/:id` - Update rule
- `PATCH /api/v1/rules/:id` - Partial update (e.g., enable/disable)
- `DELETE /api/v1/rules/:id` - Delete rule
- `POST /api/v1/rules/validate` - Validate rule predicate

#### State Management

- `GET /api/v1/state` - Get all rule states
- `GET /api/v1/state/:rule_id` - Get specific rule state

#### Data Ingestion

- `POST /ingest` - Ingest JSON data (rate-limited)

## πŸ“Š Monitoring

FuseRule exposes Prometheus metrics at `/metrics`:

- `fuserule_batches_processed_total` - Total batches ingested
- `fuserule_activations_total` - Total rule activations
- `fuserule_agent_failures_total` - Total agent failures
- `fuserule_evaluation_duration_seconds` - Evaluation latency histogram
- `fuserule_rule_evaluations_total{rule_id}` - Per-rule evaluation count
- `fuserule_rule_activations_total{rule_id}` - Per-rule activation count

## πŸ”Œ Ingestion Sources

### HTTP (Default)

```bash
curl -X POST http://localhost:3030/ingest \
     -H "Content-Type: application/json" \
     -d '[{"price": 150, "symbol": "AAPL"}]'
```

### Kafka

Configure in `fuse_rule_config.yaml`:

```yaml
sources:
  - type: "kafka"
    brokers: ["localhost:9092"]
    topic: "events"
    group_id: "fuserule"
    auto_commit: true
```

### WebSocket

Configure in `fuse_rule_config.yaml`:

```yaml
sources:
  - type: "websocket"
    bind: "0.0.0.0:3031"
    max_connections: 1000
```

Connect and send JSON:

```javascript
const ws = new WebSocket('ws://localhost:3031/ws');
ws.send(JSON.stringify([{"price": 150, "symbol": "AAPL"}]));
```

## 🎨 Action Templates

Use Handlebars templates for custom webhook payloads:

```yaml
agents:
  - name: "custom-webhook"
    type: "webhook"
    url: "https://api.example.com/webhook"
    template: |
      {
        "alert": "{{rule_name}}",
        "timestamp": "{{timestamp}}",
        "data": {{#each matched_data}}
          {
            "price": {{price}},
            "symbol": "{{symbol}}"
          }{{#unless @last}},{{/unless}}
        {{/each}},
        "count": {{count}}
      }
```

## πŸ§ͺ Testing

### Unit Tests

```bash
cargo test --test unit_test
```

### Integration Tests

```bash
cargo test --test integration_test
```

### Property-Based Tests

```bash
cargo test --test property_test
```

## πŸ“– Documentation

- [Architecture Guide]docs/ARCHITECTURE.md - Deep dive into design
- [API Documentation]https://docs.rs/arrow-rule-agent - Full API reference (when published)
- **Local API Documentation**: Generate and view locally with:
  ```bash
  cargo doc --no-deps --open
  ```
  This will build and open the documentation in your browser at `target/doc/arrow_rule_agent/index.html`
- [Examples]examples/ - Code examples

## 🀝 Contributing

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

## πŸ“œ License

Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for details.

## πŸ™ Acknowledgments

- Built on [Apache Arrow]https://arrow.apache.org/ for zero-copy data processing
- Powered by [DataFusion]https://github.com/apache/datafusion for SQL evaluation
- Inspired by the "Hard Core, Soft Edges" philosophy