|
8 | 8 | from src.insert import insert |
9 | 9 | from src.last_id import get_last_id |
10 | 10 |
|
11 | | -BATCH_SIZE = 1000 # Adjust batch size as needed |
| 11 | +async def get_data(last_id, mariadb_url, db_url): |
| 12 | + BATCH_SIZE = 1000 # Adjust batch size as needed |
12 | 13 |
|
13 | | -raw_url = os.getenv("MARIADB_URL") |
| 14 | + raw_url = mariadb_url |
14 | 15 |
|
15 | | -# Ensure the URL uses asyncmy driver |
16 | | -if raw_url.startswith("mysql://"): |
17 | | - raw_url = raw_url.replace("mysql://", "mysql+asyncmy://", 1) |
18 | | -else: |
19 | | - raise ValueError( |
20 | | - "Invalid DATABASE_URL: must start with mysql:// or mysql+asyncmy://" |
21 | | - ) |
| 16 | + # Ensure the URL uses asyncmy driver |
| 17 | + if raw_url.startswith("mysql://"): |
| 18 | + raw_url = raw_url.replace("mysql://", "mysql+asyncmy://", 1) |
| 19 | + else: |
| 20 | + raise ValueError( |
| 21 | + "Invalid DATABASE_URL: must start with mysql:// or mysql+asyncmy://" |
| 22 | + ) |
22 | 23 |
|
23 | | -DATABASE_URL = raw_url |
| 24 | + DATABASE_URL = raw_url |
24 | 25 |
|
25 | | -# Create async SQLAlchemy engine |
26 | | -engine = create_async_engine(DATABASE_URL, echo=True) |
| 26 | + # Create async SQLAlchemy engine |
| 27 | + engine = create_async_engine(DATABASE_URL, echo=True) |
27 | 28 |
|
28 | | -# Create async session factory |
29 | | -AsyncSessionLocal = sessionmaker( |
30 | | - bind=engine, |
31 | | - expire_on_commit=False, |
32 | | - class_=AsyncSession, |
33 | | -) |
| 29 | + # Create async session factory |
| 30 | + AsyncSessionLocal = sessionmaker( |
| 31 | + bind=engine, |
| 32 | + expire_on_commit=False, |
| 33 | + class_=AsyncSession, |
| 34 | + ) |
34 | 35 |
|
35 | 36 |
|
36 | | -async def get_data(last_id): |
37 | 37 | async with AsyncSessionLocal() as session: |
38 | 38 | query = text( |
39 | 39 | """ |
@@ -91,18 +91,18 @@ async def get_data(last_id): |
91 | 91 | } |
92 | 92 | ) |
93 | 93 |
|
94 | | - await insert(payload, "rockd") |
| 94 | + await insert(payload, "rockd", db_url) |
95 | 95 |
|
96 | 96 |
|
97 | | -async def fetch_last_id(): |
98 | | - return await get_last_id("rockd") |
| 97 | +async def fetch_last_id(db_url): |
| 98 | + return await get_last_id("rockd", db_url) |
99 | 99 |
|
100 | 100 |
|
101 | | -async def fetch_matomo_data(last_id): |
102 | | - await get_data(last_id) |
| 101 | +async def fetch_matomo_data(last_id, mariadb_url, db_url): |
| 102 | + await get_data(last_id, mariadb_url, db_url) |
103 | 103 |
|
104 | 104 |
|
105 | | -async def get_rockd_data(): |
106 | | - last_id = await fetch_last_id() |
107 | | - await fetch_matomo_data(last_id) |
| 105 | +async def get_rockd_data(mariadb_url, db_url): |
| 106 | + last_id = await fetch_last_id(db_url) |
| 107 | + await fetch_matomo_data(last_id, mariadb_url, db_url) |
108 | 108 | print("Data fetching completed.") |
0 commit comments