Skip to content

Commit

Permalink
rework data connector
Browse files Browse the repository at this point in the history
  • Loading branch information
djeck1432 committed Nov 18, 2024
1 parent eb6d2b6 commit ef8da4d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions apps/dashboard_app/data_conector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@

class DataConnector:
REQUIRED_VARS = ("DB_USER", "DB_PASSWORD", "DB_HOST", "DB_PORT", "DB_NAME")
SQL_QUERY = "SELECT * FROM %s WHERE protocol_id = 'zkLend'"
ZKLEND_SQL_QUERY = """
SELECT
ls.block,
ls.user,
ls.collateral,
ls.debt,
zcd.collateral_enabled
FROM
loan_state AS ls
JOIN
zklend_collateral_debt AS zcd
ON
ls.user = zcd.user_id
WHERE
ls.protocol_id = 'zkLend';
"""

def __init__(self):
"""
Expand All @@ -23,15 +38,13 @@ def __init__(self):
)
self.engine = sqlalchemy.create_engine(self.db_url)

def fetch_data(self, table_name: str, protocol_id: str) -> pd.DataFrame:
def fetch_data(self, query: str) -> pd.DataFrame:
"""
Fetch data from the database using a SQL query.
:param table_name: Name of the table to fetch data from.
:param protocol_id: ID of the protocol to fetch data for.
:param query: SQL query to execute.
:return: DataFrame containing the query results
"""
query = self.SQL_QUERY % (table_name,)
with self.engine.connect() as connection:
df = pd.read_sql(query, connection)
return df
Expand Down

0 comments on commit ef8da4d

Please sign in to comment.