Skip to content

Factory

DagsterSlingFactory

Factory to generate dagster definitions from Sling yaml config files.

Source code in data_platform\defs\sling\factory.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
class DagsterSlingFactory:
    """Factory to generate dagster definitions from Sling yaml config files."""

    @cache
    @staticmethod
    def build_definitions(config_dir: Path) -> dg.Definitions:
        """Returns a Definitions object for a path that contains Sling yaml configs."""
        connections = []
        assets = []
        freshness_checks = []
        kind_map = {}

        for config_path in os.listdir(config_dir):
            if config_path.endswith(".yaml") or config_path.endswith(".yml"):
                config_path = config_dir.joinpath(config_path).resolve()
                with open(config_path) as file:
                    config = yaml.load(file, Loader=yaml.FullLoader)
                if not config:
                    continue

                if connection_configs := config.get("connections"):
                    connections, kind_map = DagsterSlingFactory._get_connections(
                        connection_configs, connections, kind_map
                    )

                if replication_configs := config.get("replications"):
                    assets, freshness_checks = DagsterSlingFactory._get_replications(
                        replication_configs, freshness_checks, kind_map, assets
                    )

        return dg.Definitions(
            resources={"sling": SlingResource(connections=connections)},
            assets=assets,
            asset_checks=freshness_checks,
            sensors=[
                dg.build_sensor_for_freshness_checks(
                    freshness_checks=freshness_checks,
                    name="sling_freshness_checks_sensor",
                )
            ],
        )

    @staticmethod
    def _get_connections(
        connection_configs, connections, kind_map
    ) -> tuple[list[SlingConnectionResource], dict[str, str]]:
        """Returns a list of SlingConnectionResource for connections in the Sling
        yaml file.
        """
        for connection_config in connection_configs:
            if connection := DagsterSlingFactory._get_connection(connection_config):
                source = connection_config.get("name")
                kind = connection_config.get("type")
                kind_map[source] = kind
                connections.append(connection)

        return connections, kind_map

    @staticmethod
    def _get_connection(connection_config: dict) -> SlingConnectionResource | None:
        """Returns a SlingConnectionResource for a connection in the Sling yaml file."""
        for k, v in connection_config.items():
            if isinstance(v, dict):
                secret_name = list(v.keys())[0]
                display_type = list(v.values())[0]

                if display_type == "show":
                    connection_config[k] = get_secret(secret_name).get_value()
                else:
                    connection_config[k] = get_secret(secret_name)

        connection = SlingConnectionResource(**connection_config)
        return connection

    @staticmethod
    def _get_replications(
        replication_configs, freshness_checks, kind_map, assets
    ) -> tuple[list[dg.AssetsDefinition], list[dg.AssetChecksDefinition]]:
        """Returns a list of AssetsDefinitions for
        replications in a Sling yaml file
        """
        for replication_config in replication_configs:
            if bool(os.getenv("DAGSTER_IS_DEV_CLI")):
                replication_config = DagsterSlingFactory._set_dev_schema(
                    replication_config
                )
            assets_definition = DagsterSlingFactory._get_replication(replication_config)

            kind = kind_map.get(replication_config.get("source", None), None)
            dep_asset_specs = DagsterSlingFactory._get_sling_deps(
                replication_config, kind
            )
            asset_freshness_checks = DagsterSlingFactory._get_freshness_checks(
                replication_config
            )

            if asset_freshness_checks:
                freshness_checks.extend(asset_freshness_checks)
            if assets_definition:
                assets.append(assets_definition)
            if dep_asset_specs:
                assets.extend(dep_asset_specs)

        return assets, freshness_checks

    @staticmethod
    def _get_replication(config: dict) -> dg.AssetsDefinition:
        """Returns a AssetsDefinition for replication
        in a Sling yaml file
        """

        @sling_assets(
            name=config["source"] + "_assets",
            replication_config=config,
            backfill_policy=dg.BackfillPolicy.single_run(),
            dagster_sling_translator=CustomDagsterSlingTranslator(),
            pool="sling",
        )
        def assets(
            context: dg.AssetExecutionContext, sling: SlingResource
        ) -> Generator[SlingEventType, Any, None]:
            if "defaults" not in config:
                config["defaults"] = {}

            try:  # to inject start and end dates for partitioned runs
                time_window = context.partition_time_window
                if time_window:
                    if "source_options" not in config["defaults"]:
                        config["defaults"]["source_options"] = {}

                    format = "%Y-%m-%d %H:%M:%S"
                    start = time_window.start.strftime(format)
                    end = time_window.end.strftime(format)
                    config["defaults"]["source_options"]["range"] = f"{start},{end}"
            except Exception:  # run normal run if time window not provided
                pass

            yield from sling.replicate(
                context=context,
                replication_config=config,
                dagster_sling_translator=CustomDagsterSlingTranslator(),
            )
            for row in sling.stream_raw_logs():
                context.log.info(row)

        return assets

    @staticmethod
    def _set_dev_schema(replication_config: dict) -> dict:
        """Override the desination schema set in the yaml file when the environment
        is set to dev to point to a unique schema based on the developer.
        """
        user = os.environ["DESTINATION__SNOWFLAKE__CREDENTIALS__USERNAME"].upper()
        if default_object := replication_config["defaults"]["object"]:
            schema, table = default_object.split(".")
            replication_config["defaults"]["object"] = f"{schema}__{user}.{table}"

        for stream, stream_config in list(
            replication_config.get("streams", {}).items()
        ):
            stream_config = stream_config or {}
            if stream_object := stream_config.get("object"):
                schema, table = stream_object.split(".")
                replication_config["streams"][stream]["object"] = (
                    f"{schema}__{user}.{table}"
                )

        return replication_config

    @staticmethod
    def _get_sling_deps(
        replication_config: dict, kind: str | None
    ) -> list[dg.AssetSpec] | None:
        """Create an external asset that is placed in the same prefix
        as the asset, and assigned the correct resource kind.
        """
        kinds = {kind} if kind else None

        deps = []
        for k in replication_config["streams"]:
            schema, table = k.split(".")
            dep = dg.AssetSpec(
                key=[schema, "src", table], group_name=schema, kinds=kinds
            )
            deps.append(dep)
        return deps

    @staticmethod
    def _get_freshness_checks(
        replication_config: dict,
    ) -> list[dg.AssetChecksDefinition]:
        """Returns a list of AssetChecksDefinition for replication configs.
        Configs supplied on the stream will take priority, otherwise the
        default will be used.
        """
        freshness_checks = []

        default_freshness_check_config = (
            get_nested(
                replication_config, ["defaults", "meta", "dagster", "freshness_check"]
            )
            or {}
        )
        default_partition = get_nested(
            replication_config, ["defaults", "meta", "dagster", "partition"]
        )

        streams = replication_config.get("streams", {})
        for stream_name, steam_config in streams.items():
            freshness_check_config = (
                get_nested(steam_config, ["meta", "dagster", "freshness_check"]) or {}
            )
            partition = get_nested(steam_config, ["meta", "dagster", "partition"])

            freshness_check_config = (
                freshness_check_config | default_freshness_check_config
            )
            partition = partition or default_partition

            if freshness_check_config:
                if lower_bound_delta_seconds := freshness_check_config.pop(
                    "lower_bound_delta_seconds", None
                ):
                    lower_bound_delta = timedelta(
                        seconds=float(lower_bound_delta_seconds)
                    )
                    freshness_check_config["lower_bound_delta"] = lower_bound_delta

                schema, table_name = stream_name.split(".")
                asset_key = [schema, "raw", table_name]
                freshness_check_config["assets"] = [asset_key]

                try:
                    if partition in ["hourly", "daily", "weekly", "monthly"]:
                        freshness_check_config = sanitize_input_signature(
                            dg.build_time_partition_freshness_checks,
                            freshness_check_config,
                        )

                        time_partition_update_freshness_checks = (
                            dg.build_time_partition_freshness_checks(
                                **freshness_check_config
                            )
                        )
                        freshness_checks.extend(time_partition_update_freshness_checks)

                    else:
                        freshness_check_config = sanitize_input_signature(
                            dg.build_last_update_freshness_checks,
                            freshness_check_config,
                        )

                        last_update_freshness_checks = (
                            dg.build_last_update_freshness_checks(
                                **freshness_check_config
                            )
                        )
                        freshness_checks.extend(last_update_freshness_checks)
                except TypeError as e:
                    raise TypeError(
                        "Error creating freshness check, check your configuration for "
                        f"'{asset_key}'. Supplied arguments: {freshness_check_config}"
                    ) from e

        return freshness_checks

build_definitions(config_dir) cached staticmethod

Returns a Definitions object for a path that contains Sling yaml configs.

Source code in data_platform\defs\sling\factory.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@cache
@staticmethod
def build_definitions(config_dir: Path) -> dg.Definitions:
    """Returns a Definitions object for a path that contains Sling yaml configs."""
    connections = []
    assets = []
    freshness_checks = []
    kind_map = {}

    for config_path in os.listdir(config_dir):
        if config_path.endswith(".yaml") or config_path.endswith(".yml"):
            config_path = config_dir.joinpath(config_path).resolve()
            with open(config_path) as file:
                config = yaml.load(file, Loader=yaml.FullLoader)
            if not config:
                continue

            if connection_configs := config.get("connections"):
                connections, kind_map = DagsterSlingFactory._get_connections(
                    connection_configs, connections, kind_map
                )

            if replication_configs := config.get("replications"):
                assets, freshness_checks = DagsterSlingFactory._get_replications(
                    replication_configs, freshness_checks, kind_map, assets
                )

    return dg.Definitions(
        resources={"sling": SlingResource(connections=connections)},
        assets=assets,
        asset_checks=freshness_checks,
        sensors=[
            dg.build_sensor_for_freshness_checks(
                freshness_checks=freshness_checks,
                name="sling_freshness_checks_sensor",
            )
        ],
    )