113 lines
No EOL
3.3 KiB
Rust
113 lines
No EOL
3.3 KiB
Rust
use crate::*;
|
|
use std::error::Error as StdError;
|
|
use uuid::Uuid;
|
|
|
|
pub mod writer;
|
|
pub mod mock;
|
|
pub mod storage;
|
|
pub mod sqlite_storage;
|
|
pub mod query_engine;
|
|
|
|
#[derive(Debug)]
|
|
pub enum BuildEventLogError {
|
|
DatabaseError(String),
|
|
SerializationError(String),
|
|
ConnectionError(String),
|
|
QueryError(String),
|
|
}
|
|
|
|
impl std::fmt::Display for BuildEventLogError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
BuildEventLogError::DatabaseError(msg) => write!(f, "Database error: {}", msg),
|
|
BuildEventLogError::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
|
|
BuildEventLogError::ConnectionError(msg) => write!(f, "Connection error: {}", msg),
|
|
BuildEventLogError::QueryError(msg) => write!(f, "Query error: {}", msg),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl StdError for BuildEventLogError {}
|
|
|
|
pub type Result<T> = std::result::Result<T, BuildEventLogError>;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct QueryResult {
|
|
pub columns: Vec<String>,
|
|
pub rows: Vec<Vec<String>>,
|
|
}
|
|
|
|
// Summary types for list endpoints
|
|
#[derive(Debug, Clone)]
|
|
pub struct BuildRequestSummary {
|
|
pub build_request_id: String,
|
|
pub status: BuildRequestStatus,
|
|
pub requested_partitions: Vec<String>,
|
|
pub created_at: i64,
|
|
pub updated_at: i64,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct PartitionSummary {
|
|
pub partition_ref: String,
|
|
pub status: PartitionStatus,
|
|
pub updated_at: i64,
|
|
pub build_request_id: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct ActivitySummary {
|
|
pub active_builds_count: u32,
|
|
pub recent_builds: Vec<BuildRequestSummary>,
|
|
pub recent_partitions: Vec<PartitionSummary>,
|
|
pub total_partitions_count: u32,
|
|
}
|
|
|
|
|
|
// Helper function to generate event ID
|
|
pub fn generate_event_id() -> String {
|
|
Uuid::new_v4().to_string()
|
|
}
|
|
|
|
// Helper function to get current timestamp in nanoseconds
|
|
pub fn current_timestamp_nanos() -> i64 {
|
|
std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_nanos() as i64
|
|
}
|
|
|
|
// Helper function to create build event with metadata
|
|
pub fn create_build_event(
|
|
build_request_id: String,
|
|
event_type: crate::build_event::EventType,
|
|
) -> BuildEvent {
|
|
BuildEvent {
|
|
event_id: generate_event_id(),
|
|
timestamp: current_timestamp_nanos(),
|
|
build_request_id: Some(build_request_id.clone()),
|
|
event_type: Some(event_type),
|
|
}
|
|
}
|
|
|
|
|
|
// Parse build event log URI and create BEL query engine with appropriate storage backend
|
|
pub async fn create_bel_query_engine(uri: &str) -> Result<std::sync::Arc<query_engine::BELQueryEngine>> {
|
|
use std::sync::Arc;
|
|
use storage::BELStorage;
|
|
|
|
if uri == "stdout" {
|
|
let storage: Arc<dyn BELStorage> = Arc::new(storage::StdoutBELStorage::new());
|
|
storage.initialize().await?;
|
|
Ok(Arc::new(query_engine::BELQueryEngine::new(storage)))
|
|
} else if uri.starts_with("sqlite://") {
|
|
let path = &uri[9..]; // Remove "sqlite://" prefix
|
|
let storage: Arc<dyn BELStorage> = Arc::new(sqlite_storage::SqliteBELStorage::new(path)?);
|
|
storage.initialize().await?;
|
|
Ok(Arc::new(query_engine::BELQueryEngine::new(storage)))
|
|
} else {
|
|
Err(BuildEventLogError::ConnectionError(
|
|
format!("Unsupported build event log URI for BEL query engine: {}", uri)
|
|
))
|
|
}
|
|
} |