|
46 | 46 | } |
47 | 47 |
|
48 | 48 |
|
| 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 | + |
49 | 96 | def hex_to_decimal_tag(hex_tag): |
50 | 97 | """ |
51 | 98 | Convert hexadecimal PIT tag format to ISO decimal format. |
@@ -481,7 +528,7 @@ def replicate( |
481 | 528 | if tags and check_table_has_data(duckdb_path, "tags", dataset_name): |
482 | 529 | stream_configs["tags"] = { |
483 | 530 | "table_name": f"{dataset_name}.tags", |
484 | | - "primary_key": ["detected_at"], |
| 531 | + "primary_key": ["detected_at", "tag"], |
485 | 532 | "update_key": "detected_at", |
486 | 533 | "time_column": "detected_at", |
487 | 534 | "location": "antenna__reader__site__slug", |
@@ -532,6 +579,15 @@ def replicate( |
532 | 579 | debug=True, |
533 | 580 | ).run() |
534 | 581 |
|
| 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 | + |
535 | 591 |
|
536 | 592 | if __name__ == "__main__": |
537 | 593 | app() |
0 commit comments