|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Iterator, Mapping, Optional, Sequence, Union |
| 3 | + |
| 4 | +from dagster._core.definitions.asset_key import AssetKey |
| 5 | +from dagster._core.definitions.assets import AssetsDefinition |
| 6 | +from dagster._core.definitions.auto_materialize_policy import AutoMaterializePolicy |
| 7 | +from dagster._core.definitions.definitions_class import Definitions |
| 8 | +from dagster._core.definitions.events import AssetMaterialization |
| 9 | +from dagster._core.definitions.result import MaterializeResult |
| 10 | +from dagster_embedded_elt.sling import DagsterSlingTranslator, SlingResource, sling_assets |
| 11 | +from dagster_embedded_elt.sling.resources import AssetExecutionContext |
| 12 | +from pydantic import BaseModel |
| 13 | +from typing_extensions import Self |
| 14 | + |
| 15 | +from dagster_components import Component, ComponentLoadContext |
| 16 | +from dagster_components.core.component import ( |
| 17 | + ComponentGenerateRequest, |
| 18 | + TemplatedValueRenderer, |
| 19 | + component_type, |
| 20 | +) |
| 21 | +from dagster_components.core.dsl_schema import ( |
| 22 | + AssetAttributes, |
| 23 | + AssetAttributesModel, |
| 24 | + AssetSpecProcessor, |
| 25 | + OpSpecBaseModel, |
| 26 | +) |
| 27 | +from dagster_components.generate import generate_component_yaml |
| 28 | + |
| 29 | + |
| 30 | +class SlingReplicationParams(BaseModel): |
| 31 | + path: str |
| 32 | + op: Optional[OpSpecBaseModel] = None |
| 33 | + translator: Optional[AssetAttributesModel] = None |
| 34 | + |
| 35 | + |
| 36 | +class SlingReplicationCollectionParams(BaseModel): |
| 37 | + sling: Optional[SlingResource] = None |
| 38 | + replications: Sequence[SlingReplicationParams] |
| 39 | + asset_attributes: Optional[AssetAttributes] = None |
| 40 | + |
| 41 | + |
| 42 | +class SlingReplicationTranslator(DagsterSlingTranslator): |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + *, |
| 46 | + params: Optional[AssetAttributesModel], |
| 47 | + value_renderer: TemplatedValueRenderer, |
| 48 | + ): |
| 49 | + self.params = params or AssetAttributesModel() |
| 50 | + self.value_renderer = value_renderer |
| 51 | + |
| 52 | + def _get_rendered_attribute( |
| 53 | + self, attribute: str, stream_definition: Mapping[str, Any], default_method |
| 54 | + ) -> Any: |
| 55 | + renderer = self.value_renderer.with_context(stream_definition=stream_definition) |
| 56 | + rendered_attribute = self.params.render_properties(renderer).get(attribute) |
| 57 | + return ( |
| 58 | + rendered_attribute |
| 59 | + if rendered_attribute is not None |
| 60 | + else default_method(stream_definition) |
| 61 | + ) |
| 62 | + |
| 63 | + def get_asset_key(self, stream_definition: Mapping[str, Any]) -> AssetKey: |
| 64 | + return self._get_rendered_attribute("key", stream_definition, super().get_asset_key) |
| 65 | + |
| 66 | + def get_group_name(self, stream_definition: Mapping[str, Any]) -> Optional[str]: |
| 67 | + return self._get_rendered_attribute("group_name", stream_definition, super().get_group_name) |
| 68 | + |
| 69 | + def get_tags(self, stream_definition: Mapping[str, Any]) -> Mapping[str, str]: |
| 70 | + return self._get_rendered_attribute("tags", stream_definition, super().get_tags) |
| 71 | + |
| 72 | + def get_metadata(self, stream_definition: Mapping[str, Any]) -> Mapping[str, Any]: |
| 73 | + return self._get_rendered_attribute("metadata", stream_definition, super().get_metadata) |
| 74 | + |
| 75 | + def get_auto_materialize_policy( |
| 76 | + self, stream_definition: Mapping[str, Any] |
| 77 | + ) -> Optional[AutoMaterializePolicy]: |
| 78 | + return self._get_rendered_attribute( |
| 79 | + "auto_materialize_policy", stream_definition, super().get_auto_materialize_policy |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +@component_type(name="sling_replication_collection") |
| 84 | +class SlingReplicationCollectionComponent(Component): |
| 85 | + params_schema = SlingReplicationCollectionParams |
| 86 | + |
| 87 | + def __init__( |
| 88 | + self, |
| 89 | + dirpath: Path, |
| 90 | + resource: SlingResource, |
| 91 | + sling_replications: Sequence[SlingReplicationParams], |
| 92 | + asset_attributes: Sequence[AssetSpecProcessor], |
| 93 | + ): |
| 94 | + self.dirpath = dirpath |
| 95 | + self.resource = resource |
| 96 | + self.sling_replications = sling_replications |
| 97 | + self.asset_attributes = asset_attributes |
| 98 | + |
| 99 | + @classmethod |
| 100 | + def load(cls, context: ComponentLoadContext) -> Self: |
| 101 | + loaded_params = context.load_params(cls.params_schema) |
| 102 | + return cls( |
| 103 | + dirpath=context.path, |
| 104 | + resource=loaded_params.sling or SlingResource(), |
| 105 | + sling_replications=loaded_params.replications, |
| 106 | + asset_attributes=loaded_params.asset_attributes or [], |
| 107 | + ) |
| 108 | + |
| 109 | + def build_replication_asset( |
| 110 | + self, context: ComponentLoadContext, replication: SlingReplicationParams |
| 111 | + ) -> AssetsDefinition: |
| 112 | + @sling_assets( |
| 113 | + name=replication.op.name if replication.op else Path(replication.path).stem, |
| 114 | + op_tags=replication.op.tags if replication.op else {}, |
| 115 | + replication_config=self.dirpath / replication.path, |
| 116 | + dagster_sling_translator=SlingReplicationTranslator( |
| 117 | + params=replication.translator, |
| 118 | + value_renderer=context.templated_value_renderer, |
| 119 | + ), |
| 120 | + ) |
| 121 | + def _asset(context: AssetExecutionContext): |
| 122 | + yield from self.execute(context=context, sling=self.resource) |
| 123 | + |
| 124 | + return _asset |
| 125 | + |
| 126 | + def execute( |
| 127 | + self, context: AssetExecutionContext, sling: SlingResource |
| 128 | + ) -> Iterator[Union[AssetMaterialization, MaterializeResult]]: |
| 129 | + yield from sling.replicate(context=context) |
| 130 | + |
| 131 | + def build_defs(self, context: ComponentLoadContext) -> Definitions: |
| 132 | + defs = Definitions( |
| 133 | + assets=[ |
| 134 | + self.build_replication_asset(context, replication) |
| 135 | + for replication in self.sling_replications |
| 136 | + ], |
| 137 | + ) |
| 138 | + for transform in self.asset_attributes: |
| 139 | + defs = transform.apply(defs, context.templated_value_renderer) |
| 140 | + return defs |
| 141 | + |
| 142 | + @classmethod |
| 143 | + def generate_files(cls, request: ComponentGenerateRequest, params: Any) -> None: |
| 144 | + generate_component_yaml(request, params) |
0 commit comments