|
| 1 | +use dal::{ |
| 2 | + DalContext, |
| 3 | + SchemaVariantId, |
| 4 | + cached_module::CachedModule, |
| 5 | + schema::variant::DEFAULT_SCHEMA_VARIANT_COLOR, |
| 6 | +}; |
| 7 | +use si_frontend_mv_types::cached_schema_variant::CachedSchemaVariant as CachedSchemaVariantMv; |
| 8 | +use si_id::FuncId; |
| 9 | +use si_pkg::{ |
| 10 | + SiPkgSchema, |
| 11 | + SiPkgSchemaVariant, |
| 12 | +}; |
| 13 | +use telemetry::prelude::*; |
| 14 | + |
| 15 | +use crate::cached::collect_function_ids; |
| 16 | + |
| 17 | +/// Data structure for assembled variant information |
| 18 | +pub(crate) struct AssembledVariantData { |
| 19 | + pub variant_id: SchemaVariantId, |
| 20 | + pub display_name: String, |
| 21 | + pub category: String, |
| 22 | + pub color: String, |
| 23 | + pub description: Option<String>, |
| 24 | + pub link: Option<String>, |
| 25 | + pub asset_func_id: FuncId, |
| 26 | + pub variant_func_ids: Vec<FuncId>, |
| 27 | +} |
| 28 | + |
| 29 | +/// Assembles variant data from schema and variant components |
| 30 | +pub(crate) async fn assemble_variant_data( |
| 31 | + schema: &SiPkgSchema<'_>, |
| 32 | + variant: &SiPkgSchemaVariant<'_>, |
| 33 | + variant_id: SchemaVariantId, |
| 34 | +) -> crate::Result<AssembledVariantData> { |
| 35 | + // Get variant data |
| 36 | + let variant_data = variant.data().ok_or_else(|| { |
| 37 | + crate::Error::SchemaVariant(dal::SchemaVariantError::NotFound(variant_id)) |
| 38 | + })?; |
| 39 | + |
| 40 | + // Get the asset func unique_id and convert to FuncId |
| 41 | + let asset_func_id = variant_data |
| 42 | + .func_unique_id() |
| 43 | + .parse::<FuncId>() |
| 44 | + .map_err(|_| crate::Error::SchemaVariant(dal::SchemaVariantError::NotFound(variant_id)))?; |
| 45 | + |
| 46 | + // Get category from schema data |
| 47 | + let category = schema |
| 48 | + .data |
| 49 | + .as_ref() |
| 50 | + .map(|d| d.category()) |
| 51 | + .unwrap_or("Component") // default category |
| 52 | + .to_string(); |
| 53 | + |
| 54 | + // Get display name from variant spec |
| 55 | + let variant_spec = variant.to_spec().await?; |
| 56 | + let display_name = variant_spec |
| 57 | + .data |
| 58 | + .as_ref() |
| 59 | + .and_then(|d| d.display_name.as_ref()) |
| 60 | + .map(|d| d.to_string()) |
| 61 | + .unwrap_or_else(|| schema.name().to_string()); // fallback to schema name |
| 62 | + |
| 63 | + // Collect all function IDs attached to this variant |
| 64 | + let mut variant_func_ids = Vec::with_capacity(100); // Pre-allocate based on typical size |
| 65 | + let schema_id_str = schema.unique_id().unwrap_or("unknown"); |
| 66 | + let variant_id_str = variant_id.to_string(); |
| 67 | + |
| 68 | + // All function types use the same helper with closure syntax |
| 69 | + collect_function_ids( |
| 70 | + || variant.leaf_functions(), |
| 71 | + &mut variant_func_ids, |
| 72 | + schema_id_str, |
| 73 | + &variant_id_str, |
| 74 | + "leaf function", |
| 75 | + )?; |
| 76 | + collect_function_ids( |
| 77 | + || variant.action_funcs(), |
| 78 | + &mut variant_func_ids, |
| 79 | + schema_id_str, |
| 80 | + &variant_id_str, |
| 81 | + "action function", |
| 82 | + )?; |
| 83 | + collect_function_ids( |
| 84 | + || variant.auth_funcs(), |
| 85 | + &mut variant_func_ids, |
| 86 | + schema_id_str, |
| 87 | + &variant_id_str, |
| 88 | + "auth function", |
| 89 | + )?; |
| 90 | + collect_function_ids( |
| 91 | + || variant.management_funcs(), |
| 92 | + &mut variant_func_ids, |
| 93 | + schema_id_str, |
| 94 | + &variant_id_str, |
| 95 | + "management function", |
| 96 | + )?; |
| 97 | + collect_function_ids( |
| 98 | + || variant.si_prop_funcs(), |
| 99 | + &mut variant_func_ids, |
| 100 | + schema_id_str, |
| 101 | + &variant_id_str, |
| 102 | + "si prop function", |
| 103 | + )?; |
| 104 | + collect_function_ids( |
| 105 | + || variant.root_prop_funcs(), |
| 106 | + &mut variant_func_ids, |
| 107 | + schema_id_str, |
| 108 | + &variant_id_str, |
| 109 | + "root prop function", |
| 110 | + )?; |
| 111 | + |
| 112 | + // Remove duplicates efficiently, and ensure we have stable output for the MV. |
| 113 | + variant_func_ids.sort_unstable(); |
| 114 | + variant_func_ids.dedup(); |
| 115 | + |
| 116 | + Ok(AssembledVariantData { |
| 117 | + variant_id, |
| 118 | + display_name, |
| 119 | + category, |
| 120 | + color: variant_data |
| 121 | + .color() |
| 122 | + .unwrap_or(DEFAULT_SCHEMA_VARIANT_COLOR) |
| 123 | + .to_string(), |
| 124 | + description: variant_data.description().map(|d| d.to_string()), |
| 125 | + link: variant_data.link().map(|l| l.to_string()), |
| 126 | + asset_func_id, |
| 127 | + variant_func_ids, |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +#[instrument( |
| 132 | + name = "dal_materialized_views.cached_schema_variant", |
| 133 | + level = "debug", |
| 134 | + skip_all |
| 135 | +)] |
| 136 | +pub async fn assemble( |
| 137 | + ctx: DalContext, |
| 138 | + id: SchemaVariantId, |
| 139 | +) -> crate::Result<CachedSchemaVariantMv> { |
| 140 | + // Find the cached module containing this variant by storing the module info |
| 141 | + for mut module in CachedModule::latest_modules(&ctx).await? { |
| 142 | + let si_pkg = module.si_pkg(&ctx).await?; |
| 143 | + let schemas = si_pkg.schemas()?; |
| 144 | + for schema in schemas { |
| 145 | + let variants = schema.variants()?; |
| 146 | + for variant in variants { |
| 147 | + if let Some(unique_id) = variant.unique_id() { |
| 148 | + if let Ok(variant_id) = unique_id.parse::<SchemaVariantId>() { |
| 149 | + if variant_id == id { |
| 150 | + // Found the variant, assemble its data |
| 151 | + let assembled_data = |
| 152 | + assemble_variant_data(&schema, &variant, id).await?; |
| 153 | + |
| 154 | + // Determine if this variant is the default variant for the schema |
| 155 | + let is_default_variant = if let Some(schema_data) = schema.data.as_ref() |
| 156 | + { |
| 157 | + if let Some(default_variant_unique_id) = |
| 158 | + schema_data.default_schema_variant() |
| 159 | + { |
| 160 | + // Check if this variant's unique_id matches the default |
| 161 | + variant.unique_id() == Some(default_variant_unique_id) |
| 162 | + } else { |
| 163 | + // No default specified in schema, check if this is the first variant |
| 164 | + let all_variants = schema.variants()?; |
| 165 | + all_variants.first().map(|first| first.unique_id()) |
| 166 | + == variant.unique_id().map(Some) |
| 167 | + } |
| 168 | + } else { |
| 169 | + // No schema data, assume first variant is default |
| 170 | + let all_variants = schema.variants()?; |
| 171 | + all_variants.first().map(|first| first.unique_id()) |
| 172 | + == variant.unique_id().map(Some) |
| 173 | + }; |
| 174 | + |
| 175 | + return Ok(CachedSchemaVariantMv::new( |
| 176 | + assembled_data.variant_id, |
| 177 | + assembled_data.display_name, |
| 178 | + assembled_data.category, |
| 179 | + assembled_data.color, |
| 180 | + true, // is_locked - cached modules are locked until installed |
| 181 | + assembled_data.description, |
| 182 | + assembled_data.link, |
| 183 | + assembled_data.asset_func_id, |
| 184 | + assembled_data.variant_func_ids, |
| 185 | + is_default_variant, |
| 186 | + )); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // Variant not found |
| 195 | + Err(crate::Error::SchemaVariant( |
| 196 | + dal::SchemaVariantError::NotFound(id), |
| 197 | + )) |
| 198 | +} |
| 199 | + |
| 200 | +pub mod default; |
0 commit comments