Skip to content

Commit 4d5b0cc

Browse files
committed
add possibility to replicate data from different tables in duckdb-file
1 parent 14c3c13 commit 4d5b0cc

1 file changed

Lines changed: 51 additions & 65 deletions

File tree

src/datasync/pit_registering_salmon.py

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,13 @@ def run(
252252
locations = [place]
253253
log.info("Processing single location", location=place)
254254

255-
# Set up DLT pipeline
256255
pipeline = dlt.pipeline(
257-
pipeline_name="biomark_pit_registering_salmon_v1_test",
256+
pipeline_name="biomark_pit_registering_salmon_v1",
258257
destination="duckdb",
259258
dataset_name="main",
260259
progress="log",
261260
)
262261

263-
# Run the pipeline
264262
log.info(
265263
pipeline.run(
266264
biomark_pit_salmon(
@@ -276,6 +274,28 @@ def run(
276274
)
277275

278276

277+
def create_stream_config(config: dict, location: str) -> dict:
278+
"""Create a stream configuration for a specific data type and location."""
279+
return {
280+
"primary_key": config["primary_key"],
281+
"update_key": config["update_key"],
282+
"object": (
283+
f"tables/{config['table_name']}/location={location}/"
284+
f"{{part_year}}/{{part_month}}/{{part_day}}"
285+
),
286+
"mode": "incremental",
287+
"target_options": {"format": "parquet"},
288+
"sql": (
289+
f"select * replace (" # noqa: S608
290+
f"{config['time_column']} at time zone 'UTC' as "
291+
f"{config['time_column']}) "
292+
f"from {config['table_name']} where "
293+
f"{config['location']} = '{location}' "
294+
f"and {{incremental_where_cond}}"
295+
),
296+
}
297+
298+
279299
@app.command()
280300
def replicate(
281301
bucket: str = BIOMARK_BUCKET,
@@ -297,85 +317,51 @@ def replicate(
297317
"{type: duckdb, instance: biomark_pit_registering_salmon_v1.duckdb}"
298318
)
299319

300-
# Validate that at least one data type is selected
301320
if not any([readers, tags, environment]):
302321
typer.echo(
303322
"Error: At least one data type must be selected "
304323
"(--readers, --tags, or --environment)"
305324
)
306325
raise typer.Exit(1)
307326

308-
# Build streams dictionary by merging all selected data types
309-
streams = {}
327+
# Only include configs for enabled data types
328+
stream_configs = {}
310329

311330
if readers:
312-
reader_streams = {
313-
f"readers_voltage__{location}": {
314-
"primary_key": ["read_at"],
315-
"update_key": "read_at",
316-
"object": (
317-
f"tables/readers_voltage/location={location}/"
318-
f"{{part_year}}/{{part_month}}/{{part_day}}"
319-
),
320-
"mode": "incremental",
321-
"target_options": {"format": "parquet"},
322-
"sql": (
323-
"select * replace (read_at at time zone 'UTC' as read_at) "
324-
"from readers_voltage where reader__site__slug = ? "
325-
"and {incremental_where_cond}"
326-
),
327-
"sql_parameters": [location],
328-
}
329-
for location in SITES.values()
331+
stream_configs["readers"] = {
332+
"table_name": "readers_voltage",
333+
"primary_key": ["read_at"],
334+
"update_key": "read_at",
335+
"time_column": "read_at",
336+
"location": "reader__site__slug",
330337
}
331-
streams.update(reader_streams)
332-
typer.echo(f"Added {len(reader_streams)} reader voltage streams")
333338

334339
if tags:
335-
tag_streams = {
336-
f"tags__{location}": {
337-
"primary_key": ["detected_at"],
338-
"update_key": "detected_at",
339-
"object": (
340-
f"tables/tags/location={location}/"
341-
f"{{part_year}}/{{part_month}}/{{part_day}}"
342-
),
343-
"mode": "incremental",
344-
"target_options": {"format": "parquet"},
345-
"sql": (
346-
"select * replace (detected_at at time zone 'UTC' as detected_at) "
347-
"from tags where reader__site__slug = ? "
348-
"and {incremental_where_cond}"
349-
),
350-
"sql_parameters": [location],
351-
}
352-
for location in SITES.values()
340+
stream_configs["tags"] = {
341+
"table_name": "tags",
342+
"primary_key": ["detected_at"],
343+
"update_key": "detected_at",
344+
"time_column": "detected_at",
345+
"location": "antenna__reader__site__slug",
353346
}
354-
streams.update(tag_streams)
355-
typer.echo(f"Added {len(tag_streams)} tag streams")
356347

357348
if environment:
358-
env_streams = {
359-
f"environment__{location}": {
360-
"primary_key": ["read_at"],
361-
"update_key": "read_at",
362-
"object": (
363-
f"tables/environment/location={location}/"
364-
f"{{part_year}}/{{part_month}}/{{part_day}}"
365-
),
366-
"mode": "incremental",
367-
"target_options": {"format": "parquet"},
368-
"sql": (
369-
"select * replace (read_at at time zone 'UTC' as read_at) "
370-
"from environment where reader__site__slug = ? "
371-
"and {incremental_where_cond}"
372-
),
373-
"sql_parameters": [location],
374-
}
349+
stream_configs["environment"] = {
350+
"table_name": "environment_data",
351+
"primary_key": ["read_at"],
352+
"update_key": "read_at",
353+
"time_column": "read_at",
354+
"location": "reader__site__slug",
355+
}
356+
357+
streams = {}
358+
for data_type, config in stream_configs.items():
359+
type_streams = {
360+
f"{data_type}__{location}": create_stream_config(config, location)
375361
for location in SITES.values()
376362
}
377-
streams.update(env_streams)
378-
typer.echo(f"Added {len(env_streams)} environment streams")
363+
streams.update(type_streams)
364+
typer.echo(f"Added {len(type_streams)} {data_type} streams")
379365

380366
typer.echo(f"Total streams to replicate: {len(streams)}")
381367

0 commit comments

Comments
 (0)