Skip to content

Commit 0ca1d7b

Browse files
committed
chg(pit_registering_salmon): add last succesfull run + fixing some linter issues
1 parent 727c48b commit 0ca1d7b

1 file changed

Lines changed: 57 additions & 1 deletion

File tree

src/datasync/pit_registering_salmon.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,53 @@
4646
}
4747

4848

49+
def write_timestamp(
50+
con: duckdb.DuckDBPyConnection,
51+
bucket: str,
52+
prefix: str,
53+
) -> str:
54+
"""
55+
TODO: This is also used in NVA script, move to a common utils
56+
Write last successful run timestamp to S3.
57+
"""
58+
timestamp = datetime.now().isoformat()
59+
con.execute(f"""
60+
COPY (SELECT '{timestamp}' as last_successful_run)
61+
TO 's3://{bucket}/{prefix}/last_successful_run.parquet'
62+
(FORMAT PARQUET, COMPRESSION ZSTD)
63+
""")
64+
log.info("Last successful run timestamp written", time=timestamp)
65+
return timestamp
66+
67+
68+
def setup_duckdb_s3_connection(
69+
endpoint_url: str,
70+
access_key: str,
71+
secret_key: str,
72+
region: str,
73+
) -> duckdb.DuckDBPyConnection:
74+
"""
75+
TODO: This is also used in NVA script, move to a common utils
76+
Set up DuckDB connection with S3 support
77+
"""
78+
con = duckdb.connect()
79+
con.execute("INSTALL httpfs;")
80+
con.execute("LOAD httpfs;")
81+
82+
con.execute(f"""
83+
CREATE OR REPLACE SECRET (
84+
TYPE S3,
85+
KEY_ID '{access_key}',
86+
SECRET '{secret_key}',
87+
ENDPOINT '{endpoint_url}',
88+
REGION '{region}',
89+
URL_STYLE 'path'
90+
);
91+
""")
92+
93+
return con
94+
95+
4996
def hex_to_decimal_tag(hex_tag):
5097
"""
5198
Convert hexadecimal PIT tag format to ISO decimal format.
@@ -481,7 +528,7 @@ def replicate(
481528
if tags and check_table_has_data(duckdb_path, "tags", dataset_name):
482529
stream_configs["tags"] = {
483530
"table_name": f"{dataset_name}.tags",
484-
"primary_key": ["detected_at"],
531+
"primary_key": ["detected_at", "tag"],
485532
"update_key": "detected_at",
486533
"time_column": "detected_at",
487534
"location": "antenna__reader__site__slug",
@@ -532,6 +579,15 @@ def replicate(
532579
debug=True,
533580
).run()
534581

582+
con = setup_duckdb_s3_connection(
583+
endpoint_url=endpoint_url,
584+
access_key=access_key,
585+
secret_key=secret_key,
586+
region=region,
587+
)
588+
589+
write_timestamp(con, bucket, "tables")
590+
535591

536592
if __name__ == "__main__":
537593
app()

0 commit comments

Comments
 (0)