-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
87 lines (62 loc) · 2.23 KB
/
Copy pathseed.py
File metadata and controls
87 lines (62 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
"""Seed historical timeseries data from S3 SQL dump into PostgreSQL.
This script handles the one-time backfill of historical data.
For ongoing incremental updates, see src/process.py.
"""
import os
import tempfile
import psycopg2
import requests
from src.config import load_config
SQL_DUMP_URL = "https://fsenergy.s3.amazonaws.com/datasets/template_timeseries.sql"
def download_sql_dump(url: str) -> str:
"""Download SQL dump from S3 to temporary file.
Args:
url: S3 URL of the SQL dump file
Returns:
Path to downloaded temporary file
"""
print(f"=� Downloading SQL dump from {url}...")
response = requests.get(url, timeout=30)
response.raise_for_status()
# Write to temporary file
with tempfile.NamedTemporaryFile(mode="w", suffix=".sql", delete=False) as f:
f.write(response.text)
temp_path = f.name
print(f" Downloaded {len(response.text):,} bytes to {temp_path}")
return temp_path
def execute_sql_dump(sql_path: str, timeseries_url: str) -> None:
"""Execute SQL dump against PostgreSQL database.
Args:
sql_path: Path to SQL dump file
timeseries_url: PostgreSQL connection string
"""
print("=� Connecting to timeseries database...")
conn = psycopg2.connect(timeseries_url)
cursor = conn.cursor()
print(f"� Executing SQL dump from {sql_path}...")
with open(sql_path) as f:
sql = f.read()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
print(" SQL dump executed successfully")
def seed() -> None:
"""Download and load historical timeseries data into PostgreSQL."""
# Load configuration
load_config()
timeseries_url = os.environ.get("TIMESERIES_URL", "")
if not timeseries_url:
raise ValueError("TIMESERIES_URL environment variable is required")
# Download SQL dump
sql_path = download_sql_dump(SQL_DUMP_URL)
try:
# Execute SQL dump
execute_sql_dump(sql_path=sql_path, timeseries_url=timeseries_url)
finally:
# Cleanup temporary file
if os.path.exists(sql_path):
os.unlink(sql_path)
print(f">� Cleaned up temporary file {sql_path}")
if __name__ == "__main__":
seed()