|
1 | 1 | //! Session writer for ClickHouse storage. |
2 | 2 |
|
| 3 | +use crate::client::ClickHouseClient; |
3 | 4 | use scrybe_core::{types::Session, ScrybeError}; |
| 5 | +use serde::Serialize; |
| 6 | + |
| 7 | +/// Row format for ClickHouse sessions table. |
| 8 | +#[derive(Debug, Serialize, clickhouse::Row)] |
| 9 | +struct SessionRow { |
| 10 | + session_id: String, |
| 11 | + timestamp: i64, |
| 12 | + fingerprint_hash: String, |
| 13 | + ip: String, |
| 14 | + user_agent: String, |
| 15 | + network_signals: String, |
| 16 | + browser_signals: String, |
| 17 | + behavioral_signals: String, |
| 18 | + bot_probability: f32, |
| 19 | + confidence_score: f32, |
| 20 | +} |
| 21 | + |
| 22 | +impl SessionRow { |
| 23 | + /// Convert a Session to ClickHouse row format. |
| 24 | + fn from_session(session: &Session) -> Result<Self, ScrybeError> { |
| 25 | + Ok(Self { |
| 26 | + session_id: session.id.to_string(), |
| 27 | + timestamp: session.timestamp.timestamp_millis(), |
| 28 | + fingerprint_hash: session.fingerprint.hash.clone(), |
| 29 | + ip: session.network.ip.to_string(), |
| 30 | + user_agent: session.browser.user_agent.clone(), |
| 31 | + network_signals: serde_json::to_string(&session.network).map_err(|e| { |
| 32 | + ScrybeError::storage_error( |
| 33 | + "clickhouse", |
| 34 | + format!("JSON serialization failed: {}", e), |
| 35 | + ) |
| 36 | + })?, |
| 37 | + browser_signals: serde_json::to_string(&session.browser).map_err(|e| { |
| 38 | + ScrybeError::storage_error( |
| 39 | + "clickhouse", |
| 40 | + format!("JSON serialization failed: {}", e), |
| 41 | + ) |
| 42 | + })?, |
| 43 | + behavioral_signals: serde_json::to_string(&session.behavioral).map_err(|e| { |
| 44 | + ScrybeError::storage_error( |
| 45 | + "clickhouse", |
| 46 | + format!("JSON serialization failed: {}", e), |
| 47 | + ) |
| 48 | + })?, |
| 49 | + bot_probability: 0.0, // Will be filled by enrichment pipeline |
| 50 | + confidence_score: 0.0, // Will be filled by enrichment pipeline |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
4 | 54 |
|
5 | 55 | /// Writes session data to ClickHouse. |
6 | | -pub struct SessionWriter; |
| 56 | +pub struct SessionWriter { |
| 57 | + client: ClickHouseClient, |
| 58 | +} |
7 | 59 |
|
8 | 60 | impl SessionWriter { |
| 61 | + /// Create a new session writer. |
| 62 | + pub fn new(client: ClickHouseClient) -> Self { |
| 63 | + Self { client } |
| 64 | + } |
| 65 | + |
9 | 66 | /// Write a session to ClickHouse. |
10 | 67 | /// |
11 | 68 | /// # Errors |
12 | 69 | /// |
13 | 70 | /// Returns `ScrybeError::StorageError` if the write fails. |
14 | | - pub async fn write(_session: &Session) -> Result<(), ScrybeError> { |
15 | | - // TODO: Implement actual ClickHouse write |
| 71 | + pub async fn write(&self, session: &Session) -> Result<(), ScrybeError> { |
| 72 | + let row = SessionRow::from_session(session)?; |
| 73 | + |
| 74 | + let mut insert = self.client.client().insert("sessions").map_err(|e| { |
| 75 | + ScrybeError::storage_error("clickhouse", format!("Insert preparation failed: {}", e)) |
| 76 | + })?; |
| 77 | + |
| 78 | + insert.write(&row).await.map_err(|e| { |
| 79 | + ScrybeError::storage_error("clickhouse", format!("Write failed: {}", e)) |
| 80 | + })?; |
| 81 | + |
| 82 | + insert.end().await.map_err(|e| { |
| 83 | + ScrybeError::storage_error("clickhouse", format!("Write completion failed: {}", e)) |
| 84 | + })?; |
| 85 | + |
16 | 86 | Ok(()) |
17 | 87 | } |
18 | 88 |
|
19 | 89 | /// Batch write multiple sessions to ClickHouse. |
20 | 90 | /// |
| 91 | + /// More efficient for high-throughput scenarios. |
| 92 | + /// |
21 | 93 | /// # Errors |
22 | 94 | /// |
23 | 95 | /// Returns `ScrybeError::StorageError` if the write fails. |
24 | | - pub async fn write_batch(_sessions: &[Session]) -> Result<(), ScrybeError> { |
25 | | - // TODO: Implement batch write |
| 96 | + pub async fn write_batch(&self, sessions: &[Session]) -> Result<(), ScrybeError> { |
| 97 | + if sessions.is_empty() { |
| 98 | + return Ok(()); |
| 99 | + } |
| 100 | + |
| 101 | + let mut insert = self.client.client().insert("sessions").map_err(|e| { |
| 102 | + ScrybeError::storage_error("clickhouse", format!("Insert preparation failed: {}", e)) |
| 103 | + })?; |
| 104 | + |
| 105 | + for session in sessions { |
| 106 | + let row = SessionRow::from_session(session)?; |
| 107 | + insert.write(&row).await.map_err(|e| { |
| 108 | + ScrybeError::storage_error("clickhouse", format!("Write failed: {}", e)) |
| 109 | + })?; |
| 110 | + } |
| 111 | + |
| 112 | + insert.end().await.map_err(|e| { |
| 113 | + ScrybeError::storage_error( |
| 114 | + "clickhouse", |
| 115 | + format!("Batch write completion failed: {}", e), |
| 116 | + ) |
| 117 | + })?; |
| 118 | + |
26 | 119 | Ok(()) |
27 | 120 | } |
28 | 121 | } |
|
0 commit comments