rustcmdpev_core/structure/
raw.rs1use serde::Deserialize;
2
3use crate::structure::data::actuals::PlanActuals;
4use crate::structure::data::analysis_flags::PlanAnalysisFlags;
5use crate::structure::data::buffers::PlanBuffers;
6use crate::structure::data::estimates::PlanEstimates;
7use crate::structure::data::explain::Explain;
8use crate::structure::data::identity::PlanIdentity;
9use crate::structure::data::io_timing::PlanIoTiming;
10use crate::structure::data::plan::Plan;
11use crate::structure::data::predicates::PlanPredicates;
12use crate::VisualizeError;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum PostgresSchemaProfile {
16 Unknown,
17 LegacyIoTiming,
18 ModernIoTiming,
19}
20
21#[derive(Debug, Clone, Deserialize, Default)]
22pub struct RawExplain {
23 #[serde(flatten)]
24 pub metadata: RawExplainMetadata,
25 #[serde(default, rename = "Plan")]
26 pub plan: Option<RawPlan>,
27}
28
29impl RawExplain {
30 pub fn schema_profile(&self) -> PostgresSchemaProfile {
31 match self
32 .metadata
33 .postgres_version
34 .as_deref()
35 .and_then(extract_major_version)
36 {
37 Some(major) if major < 13 => PostgresSchemaProfile::LegacyIoTiming,
38 Some(_) => PostgresSchemaProfile::ModernIoTiming,
39 None => PostgresSchemaProfile::Unknown,
40 }
41 }
42
43 pub fn into_domain(self) -> Result<Explain, VisualizeError> {
44 let schema_profile = self.schema_profile();
45 let plan = self
46 .plan
47 .ok_or(VisualizeError::MissingPlan)?
48 .into_domain(schema_profile)?;
49
50 Ok(Explain {
51 plan,
52 postgres_version: self.metadata.postgres_version,
53 planning_time: self.metadata.planning_time,
54 execution_time: self.metadata.execution_time,
55 ..Default::default()
56 })
57 }
58}
59
60#[derive(Debug, Clone, Deserialize, Default)]
61pub struct RawExplainMetadata {
62 #[serde(default, rename = "Planning Time")]
63 pub planning_time: f64,
64 #[serde(default, rename = "Execution Time")]
65 pub execution_time: f64,
66 #[serde(default, rename = "PostgreSQL Version", alias = "Postgres Version")]
67 pub postgres_version: Option<String>,
68}
69
70#[derive(Debug, Clone, Deserialize, Default)]
71pub struct RawPlan {
72 #[serde(flatten)]
73 pub actuals: PlanActuals,
74 #[serde(flatten)]
75 pub identity: PlanIdentity,
76 #[serde(flatten)]
77 pub predicates: PlanPredicates,
78 #[serde(flatten)]
79 pub buffers: PlanBuffers,
80 #[serde(flatten)]
81 pub estimates: PlanEstimates,
82 #[serde(flatten)]
83 pub io_timing: RawPlanIoTiming,
84 #[serde(default, rename = "Plans")]
85 pub plans: Vec<RawPlan>,
86}
87
88#[derive(Debug, Clone, Deserialize, Default)]
89pub struct RawPlanIoTiming {
90 #[serde(default, flatten)]
91 pub canonical: PlanIoTiming,
92 #[serde(default, rename = "IO Read Time")]
93 pub legacy_io_read_time: Option<f64>,
94 #[serde(default, rename = "IO Write Time")]
95 pub legacy_io_write_time: Option<f64>,
96}
97
98impl RawPlan {
99 pub fn into_domain(
100 self,
101 schema_profile: PostgresSchemaProfile,
102 ) -> Result<Plan, VisualizeError> {
103 let plans = self
104 .plans
105 .into_iter()
106 .map(|plan| plan.into_domain(schema_profile))
107 .collect::<Result<Vec<_>, _>>()?;
108
109 let resolved_io = self.io_timing.resolve(schema_profile);
110
111 Ok(Plan {
112 actuals: self.actuals,
113 analysis_flags: PlanAnalysisFlags::default(),
114 identity: self.identity,
115 predicates: self.predicates,
116 buffers: self.buffers,
117 estimates: self.estimates,
118 io_timing: resolved_io,
119 plans,
120 })
121 }
122}
123
124impl RawPlanIoTiming {
125 pub fn resolve(self, schema_profile: PostgresSchemaProfile) -> PlanIoTiming {
126 match schema_profile {
127 PostgresSchemaProfile::LegacyIoTiming => PlanIoTiming {
128 io_read_time: self
129 .legacy_io_read_time
130 .or(Some(self.canonical.io_read_time))
131 .unwrap_or_default(),
132 io_write_time: self
133 .legacy_io_write_time
134 .or(Some(self.canonical.io_write_time))
135 .unwrap_or_default(),
136 },
137 PostgresSchemaProfile::ModernIoTiming | PostgresSchemaProfile::Unknown => {
138 PlanIoTiming {
139 io_read_time: if self.canonical.io_read_time == 0.0 {
140 self.legacy_io_read_time.unwrap_or_default()
141 } else {
142 self.canonical.io_read_time
143 },
144 io_write_time: if self.canonical.io_write_time == 0.0 {
145 self.legacy_io_write_time.unwrap_or_default()
146 } else {
147 self.canonical.io_write_time
148 },
149 }
150 }
151 }
152 }
153}
154
155pub fn extract_major_version(version: &str) -> Option<u32> {
156 version
157 .split(|c: char| !c.is_ascii_digit())
158 .find(|part| !part.is_empty())
159 .and_then(|part| part.parse::<u32>().ok())
160}
161
162pub fn schema_profile_from_hint(hint: &str) -> Option<PostgresSchemaProfile> {
164 extract_major_version(hint).map(|major| {
165 if major < 13 {
166 PostgresSchemaProfile::LegacyIoTiming
167 } else {
168 PostgresSchemaProfile::ModernIoTiming
169 }
170 })
171}