tftsr-devops_investigation/src-tauri/src/commands/system.rs

142 lines
4.2 KiB
Rust
Raw Normal View History

feat: initial implementation of TFTSR IT Triage & RCA application Implements Phases 1-8 of the TFTSR implementation plan. Rust backend (Tauri 2.x, src-tauri/): - Multi-provider AI: OpenAI-compatible, Anthropic, Gemini, Mistral, Ollama - PII detection engine: 11 regex patterns with overlap resolution - SQLCipher AES-256 encrypted database with 10 versioned migrations - 28 Tauri IPC commands for triage, analysis, document, and system ops - Ollama: hardware probe, model recommendations, pull/delete with events - RCA and blameless post-mortem Markdown document generators - PDF export via printpdf - Audit log: SHA-256 hash of every external data send - Integration stubs for Confluence, ServiceNow, Azure DevOps (v0.2) Frontend (React 18 + TypeScript + Vite, src/): - 9 pages: full triage workflow NewIssue→LogUpload→Triage→Resolution→RCA→Postmortem→History+Settings - 7 components: ChatWindow, TriageProgress, PiiDiffViewer, DocEditor, HardwareReport, ModelSelector, UI primitives - 3 Zustand stores: session, settings (persisted), history - Type-safe tauriCommands.ts matching Rust backend types exactly - 8 IT domain system prompts (Linux, Windows, Network, K8s, DB, Virt, HW, Obs) DevOps: - .woodpecker/test.yml: rustfmt, clippy, cargo test, tsc, vitest on every push - .woodpecker/release.yml: linux/amd64 + linux/arm64 builds, Gogs release upload Verified: - cargo check: zero errors - tsc --noEmit: zero errors - vitest run: 13/13 unit tests passing Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
2026-03-15 03:36:25 +00:00
use crate::db::models::{AuditEntry, AuditFilter};
use crate::ollama::{
hardware, installer, manager, recommender, InstallGuide, ModelRecommendation, OllamaModel,
OllamaStatus,
};
use crate::state::{AppSettings, AppState};
// --- Ollama commands ---
#[tauri::command]
pub async fn check_ollama_installed() -> Result<OllamaStatus, String> {
installer::check_ollama()
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn get_ollama_install_guide(platform: String) -> Result<InstallGuide, String> {
Ok(installer::get_install_instructions(&platform))
}
#[tauri::command]
pub async fn list_ollama_models() -> Result<Vec<OllamaModel>, String> {
manager::list_models().await.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn pull_ollama_model(
app_handle: tauri::AppHandle,
model_name: String,
) -> Result<(), String> {
manager::pull_model(app_handle, &model_name)
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn delete_ollama_model(model_name: String) -> Result<(), String> {
manager::delete_model(&model_name)
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn detect_hardware() -> Result<hardware::HardwareInfo, String> {
hardware::probe_hardware().map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn recommend_models() -> Result<Vec<ModelRecommendation>, String> {
let hw = hardware::probe_hardware().map_err(|e| e.to_string())?;
Ok(recommender::recommend_models(&hw))
}
// --- Settings commands ---
#[tauri::command]
pub async fn get_settings(
state: tauri::State<'_, AppState>,
) -> Result<AppSettings, String> {
state
.settings
.lock()
.map(|s| s.clone())
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn update_settings(
partial_settings: serde_json::Value,
state: tauri::State<'_, AppState>,
) -> Result<AppSettings, String> {
let mut settings = state.settings.lock().map_err(|e| e.to_string())?;
if let Some(theme) = partial_settings.get("theme").and_then(|v| v.as_str()) {
settings.theme = theme.to_string();
}
if let Some(active_provider) = partial_settings
.get("active_provider")
.and_then(|v| v.as_str())
{
settings.active_provider = Some(active_provider.to_string());
}
Ok(settings.clone())
}
// --- Audit log command ---
#[tauri::command]
pub async fn get_audit_log(
filter: AuditFilter,
state: tauri::State<'_, AppState>,
) -> Result<Vec<AuditEntry>, String> {
let db = state.db.lock().map_err(|e| e.to_string())?;
let limit = filter.limit.unwrap_or(100);
let mut sql = String::from(
"SELECT id, timestamp, action, entity_type, entity_id, user_id, details \
FROM audit_log WHERE 1=1",
);
let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = vec![];
if let Some(ref action) = filter.action {
sql.push_str(&format!(" AND action = ?{}", params.len() + 1));
params.push(Box::new(action.clone()));
}
if let Some(ref entity_type) = filter.entity_type {
sql.push_str(&format!(" AND entity_type = ?{}", params.len() + 1));
params.push(Box::new(entity_type.clone()));
}
if let Some(ref entity_id) = filter.entity_id {
sql.push_str(&format!(" AND entity_id = ?{}", params.len() + 1));
params.push(Box::new(entity_id.clone()));
}
sql.push_str(" ORDER BY timestamp DESC");
sql.push_str(&format!(" LIMIT ?{}", params.len() + 1));
params.push(Box::new(limit));
let param_refs: Vec<&dyn rusqlite::types::ToSql> = params.iter().map(|p| p.as_ref()).collect();
let mut stmt = db.prepare(&sql).map_err(|e| e.to_string())?;
let rows = stmt
.query_map(param_refs.as_slice(), |row| {
Ok(AuditEntry {
id: row.get(0)?,
timestamp: row.get(1)?,
action: row.get(2)?,
entity_type: row.get(3)?,
entity_id: row.get(4)?,
user_id: row.get(5)?,
details: row.get(6)?,
})
})
.map_err(|e| e.to_string())?
.filter_map(|r| r.ok())
.collect::<Vec<_>>();
Ok(rows)
}