|
| 1 | +//! Live progress rows, completion rows and runtime diagnostics of `sqb build`. |
| 2 | +
|
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +use pyo3::prelude::{Bound, Py, PyAny, PyAnyMethods, PyResult}; |
| 6 | +use sqlbuild_core::build_progress::models::{SchedulerStateText, StatementText, TestResultText}; |
| 7 | + |
| 8 | +use crate::build_progress::_helpers::result_text::optional; |
| 9 | +use crate::build_progress::_helpers::result_text::text; |
| 10 | +use sqlbuild_python_base::records::main::fields::field::field as native_field; |
| 11 | + |
| 12 | +pub(crate) const RUNTIME_LOGGER: &str = "sqlbuild.executor.runtime"; |
| 13 | +pub(crate) const PASS_OUTCOME: &str = "pass"; |
| 14 | +pub(crate) const SCHEDULER_DIAGNOSTIC_LIMIT: usize = 50; |
| 15 | +pub(crate) const QUERY_DIAGNOSTIC_LIMIT: usize = 25; |
| 16 | + |
| 17 | +/// Which executor result type a completion reports. |
| 18 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 19 | +pub(crate) enum ResultKind { |
| 20 | + Model, |
| 21 | + Seed, |
| 22 | + Function, |
| 23 | + Load, |
| 24 | + PythonNode, |
| 25 | + SqlTest, |
| 26 | + Other, |
| 27 | +} |
| 28 | + |
| 29 | +pub(crate) fn result_kind(result: &Bound<'_, PyAny>) -> PyResult<ResultKind> { |
| 30 | + for (field, kind) in [ |
| 31 | + ("model_name", ResultKind::Model), |
| 32 | + ("seed_name", ResultKind::Seed), |
| 33 | + ("function_name", ResultKind::Function), |
| 34 | + ("loader_name", ResultKind::Load), |
| 35 | + ("node_name", ResultKind::PythonNode), |
| 36 | + ("test_name", ResultKind::SqlTest), |
| 37 | + ] { |
| 38 | + if result.hasattr(field)? { |
| 39 | + return Ok(kind); |
| 40 | + } |
| 41 | + } |
| 42 | + Ok(ResultKind::Other) |
| 43 | +} |
| 44 | + |
| 45 | +fn sql_test_resource_id(result: &Bound<'_, PyAny>) -> PyResult<String> { |
| 46 | + let name: String = text(result, "test_name")?; |
| 47 | + let path: Bound<'_, PyAny> = native_field(result, "source_path")?; |
| 48 | + let block: Bound<'_, PyAny> = native_field(result, "block_index")?; |
| 49 | + let case: Bound<'_, PyAny> = native_field(result, "case_name")?; |
| 50 | + if path.is_none() || block.is_none() || case.is_none() { |
| 51 | + return Ok(format!("sql_test:{name}")); |
| 52 | + } |
| 53 | + Ok(format!( |
| 54 | + "sql_test:{}:{}:{}", |
| 55 | + path.call_method0("as_posix")?.str()?, |
| 56 | + block.str()?, |
| 57 | + case.str()? |
| 58 | + )) |
| 59 | +} |
| 60 | + |
| 61 | +/// The resource name and projector identity of a completion. |
| 62 | +pub(crate) fn resource_identity(result: &Bound<'_, PyAny>) -> PyResult<Option<(String, String)>> { |
| 63 | + Ok(match result_kind(result)? { |
| 64 | + ResultKind::Model => { |
| 65 | + let name: String = text(result, "model_name")?; |
| 66 | + Some((name.clone(), format!("model:{name}"))) |
| 67 | + } |
| 68 | + ResultKind::Seed => { |
| 69 | + let name: String = text(result, "seed_name")?; |
| 70 | + Some((name.clone(), format!("seed:{name}"))) |
| 71 | + } |
| 72 | + ResultKind::Function => { |
| 73 | + let name: String = text(result, "function_name")?; |
| 74 | + let kind: String = text(result, "function_kind")?; |
| 75 | + Some((name.clone(), format!("{kind}:{name}"))) |
| 76 | + } |
| 77 | + ResultKind::Load => { |
| 78 | + let name: String = text(result, "source_name")?; |
| 79 | + Some((name.clone(), format!("source:{name}"))) |
| 80 | + } |
| 81 | + ResultKind::PythonNode => { |
| 82 | + let name: String = text(result, "node_name")?; |
| 83 | + let kind: String = text(result, "kind")?; |
| 84 | + Some((name.clone(), format!("{kind}:{name}"))) |
| 85 | + } |
| 86 | + ResultKind::SqlTest => Some((text(result, "test_name")?, sql_test_resource_id(result)?)), |
| 87 | + ResultKind::Other => None, |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +/// Mutable callback state; only held briefly and never across Python calls. |
| 92 | +#[derive(Debug, Default)] |
| 93 | +pub(crate) struct CallbackState { |
| 94 | + pub(crate) counter: usize, |
| 95 | + pub(crate) node_name: String, |
| 96 | + pub(crate) node_kind: String, |
| 97 | + pub(crate) sub_message: String, |
| 98 | + pub(crate) frame: usize, |
| 99 | + pub(crate) line_active: bool, |
| 100 | + pub(crate) cursor_hidden: bool, |
| 101 | + pub(crate) stop_event: Option<Py<PyAny>>, |
| 102 | + pub(crate) thread: Option<Py<PyAny>>, |
| 103 | + pub(crate) tests_by_model: HashMap<String, Vec<TestResultText>>, |
| 104 | + pub(crate) scheduler_count: usize, |
| 105 | + pub(crate) scheduler_omitted: usize, |
| 106 | + pub(crate) last_scheduler: Option<SchedulerStateText>, |
| 107 | + pub(crate) query_counts: HashMap<String, usize>, |
| 108 | + pub(crate) query_omitted: usize, |
| 109 | + pub(crate) last_query: Option<StatementText>, |
| 110 | +} |
| 111 | + |
| 112 | +pub(crate) fn scheduler_state(state: &Bound<'_, PyAny>) -> PyResult<SchedulerStateText> { |
| 113 | + Ok(SchedulerStateText { |
| 114 | + running: native_field(state, "running")?.extract()?, |
| 115 | + ready: native_field(state, "ready")?.extract()?, |
| 116 | + waiting: native_field(state, "waiting")?.extract()?, |
| 117 | + limit: native_field(state, "limit")?.extract()?, |
| 118 | + aborted: native_field(state, "aborted")?.extract()?, |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +pub(crate) fn statement(telemetry: &Bound<'_, PyAny>) -> PyResult<StatementText> { |
| 123 | + Ok(StatementText { |
| 124 | + query_id: optional(telemetry, "query_id")?, |
| 125 | + status: text(telemetry, "status")?, |
| 126 | + elapsed_seconds: native_field(telemetry, "elapsed_seconds")?.extract()?, |
| 127 | + resource_type: text(telemetry, "resource_type")?, |
| 128 | + resource_name: text(telemetry, "resource_name")?, |
| 129 | + phase: text(telemetry, "phase")?, |
| 130 | + }) |
| 131 | +} |
0 commit comments