From ef8da4d8076c76d92ba71b650031515c31b2fc88 Mon Sep 17 00:00:00 2001 From: djeck1432 Date: Mon, 18 Nov 2024 22:31:27 +0100 Subject: [PATCH] rework data connector --- apps/dashboard_app/data_conector.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/apps/dashboard_app/data_conector.py b/apps/dashboard_app/data_conector.py index 9c01496a..673fd6b3 100644 --- a/apps/dashboard_app/data_conector.py +++ b/apps/dashboard_app/data_conector.py @@ -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): """ @@ -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