pub mod kyma_extractor;
pub mod persistence;
pub mod similarity_engine;
pub mod tauri_examples;
pub use similarity_engine::{
FilteredWidgetDescription, Preset, Suggestion, ValueStats, Widget, WidgetFeatures,
WidgetRecord, WidgetSuggestionEngine, WidgetValue,
};
pub use persistence::{
ExportData, PersistentWidgetSuggestionEngine, SledPersistenceError, SledPersistenceManager,
};
pub use kyma_extractor::{KymaWidgetExtractor, WidgetMetadata};
pub use tauri_examples::{
IntelligenceStats, PresetData, StandaloneIntelligenceService, SuggestionResponse,
WidgetInsightResponse,
};
impl Default for Widget {
fn default() -> Self {
Self {
label: None,
minimum: None,
maximum: None,
current_value: None,
is_generated: None,
display_type: None,
event_id: None,
values: Vec::new(),
}
}
}
pub fn init_intelligence_system<P: AsRef<std::path::Path>>(
db_path: P,
) -> Result<PersistentWidgetSuggestionEngine, persistence::SledPersistenceError> {
PersistentWidgetSuggestionEngine::new(db_path)
}
pub fn init_standalone_service(db_path: &str) -> Result<StandaloneIntelligenceService, String> {
StandaloneIntelligenceService::new(db_path)
}
pub fn validate_widget(widget: &Widget) -> Result<(), String> {
if let (Some(min), Some(max)) = (widget.minimum, widget.maximum) {
if min >= max {
return Err("Minimum value must be less than maximum value".to_string());
}
if let Some(current) = widget.current_value {
if current < min || current > max {
return Err("Current value must be within minimum and maximum bounds".to_string());
}
}
}
Ok(())
}
pub fn create_test_widget(label: &str, min: f64, max: f64, current: f64) -> Widget {
Widget {
label: Some(label.to_string()),
minimum: Some(min),
maximum: Some(max),
current_value: Some(current),
is_generated: Some(false),
display_type: Some("slider".to_string()),
event_id: None,
values: vec![current],
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{thread, time::Duration};
use tempfile::tempdir;
#[test]
fn test_persistent_system() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = tempdir()?;
let db_path = temp_dir.path().join("test_persistent_lib");
thread::sleep(Duration::from_millis(100));
let mut system = init_intelligence_system(&db_path)?;
let widget = create_test_widget("Test Widget", 0.0, 100.0, 50.0);
system.store_widget(widget)?;
let stats = system.get_stats();
assert_eq!(stats.get("total_widgets"), Some(&1));
system.flush()?;
drop(system);
thread::sleep(Duration::from_millis(100));
let system2 = init_intelligence_system(&db_path)?;
let stats2 = system2.get_stats();
assert_eq!(stats2.get("total_widgets"), Some(&1));
drop(system2);
temp_dir.close()?;
Ok(())
}
}