Get registry statistics
(&self)
| 331 | |
| 332 | /// Get registry statistics |
| 333 | pub fn get_stats(&self) -> PyResult<String> { |
| 334 | let metadata = self.metadata.read().map_err(|e| { |
| 335 | PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!( |
| 336 | "Failed to acquire metadata lock: {}", |
| 337 | e |
| 338 | )) |
| 339 | })?; |
| 340 | |
| 341 | let history = self.execution_history.read().map_err(|e| { |
| 342 | PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!( |
| 343 | "Failed to acquire history lock: {}", |
| 344 | e |
| 345 | )) |
| 346 | })?; |
| 347 | |
| 348 | let total_tools = metadata.len(); |
| 349 | let total_executions = history.len(); |
| 350 | let successful_executions = history.iter().filter(|r| r.success).count(); |
| 351 | let success_rate = if total_executions > 0 { |
| 352 | (successful_executions as f64 / total_executions as f64) * 100.0 |
| 353 | } else { |
| 354 | 0.0 |
| 355 | }; |
| 356 | |
| 357 | let stats = serde_json::json!({ |
| 358 | "total_tools": total_tools, |
| 359 | "total_executions": total_executions, |
| 360 | "successful_executions": successful_executions, |
| 361 | "success_rate": success_rate, |
| 362 | "tools": metadata.values().collect::<Vec<_>>() |
| 363 | }); |
| 364 | |
| 365 | serde_json::to_string_pretty(&stats).map_err(|e| { |
| 366 | PyErr::new::<pyo3::exceptions::PyValueError, _>(format!( |
| 367 | "Failed to serialize stats: {}", |
| 368 | e |
| 369 | )) |
| 370 | }) |
| 371 | } |
| 372 | |
| 373 | /// String representation |
| 374 | pub fn __repr__(&self) -> PyResult<String> { |