Skip to content

Commit dd73c59

Browse files
authored
Merge pull request #599 from waterscape03/feat/add_fetch_data
added queries for fetch vesu positions, health_factor for users
2 parents 3873acb + b33d5ad commit dd73c59

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

apps/dashboard_app/data_conector.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,41 @@ class DataConnector:
4949
WHERE protocol_id = 'zkLend' AND block = (SELECT max_block FROM max_block);
5050
"""
5151

52+
VESU_POSITIONS_SQL_QUERY = """
53+
SELECT
54+
vp.user,
55+
vp.pool_id,
56+
vp.collateral_asset,
57+
vp.debt_asset,
58+
vp.block_number
59+
FROM
60+
vesu_positions AS vp
61+
WHERE
62+
vp.block_number = (
63+
SELECT MAX(block_number)
64+
FROM vesu_positions
65+
WHERE user = vp.user
66+
AND pool_id = vp.pool_id
67+
);
68+
"""
69+
70+
VESU_HEALTH_FACTORS_SQL_QUERY = """
71+
SELECT
72+
hrl.user_id as user,
73+
hrl.protocol_id as pool_id,
74+
hrl.value as health_factor,
75+
hrl.timestamp
76+
FROM
77+
health_ratio_level AS hrl
78+
WHERE
79+
hrl.timestamp = (
80+
SELECT MAX(timestamp)
81+
FROM health_ratio_level
82+
WHERE user_id = hrl.user_id
83+
AND protocol_id = hrl.protocol_id
84+
);
85+
"""
86+
5287
def __init__(self):
5388
"""
5489
Initialize the DataConnector with database connection details.
@@ -209,12 +244,40 @@ def fetch_data_from_csv(self, file_path: str) -> pd.DataFrame:
209244
logger.error(f"Error reading CSV file: {e}")
210245
return None
211246

247+
def fetch_vesu_positions(self) -> pd.DataFrame:
248+
"""
249+
Fetch Vesu positions data using the predefined query.
250+
251+
:return: DataFrame containing Vesu positions data
252+
"""
253+
try:
254+
with self.engine.connect() as connection:
255+
return pd.read_sql(self.VESU_POSITIONS_SQL_QUERY, connection)
256+
except sqlalchemy.exc.SQLAlchemyError as e:
257+
logger.error(f"Database error: {e}")
258+
raise DatabaseConnectionError(f"Failed to fetch Vesu positions: {str(e)}")
259+
260+
def fetch_vesu_health_factors(self) -> pd.DataFrame:
261+
"""
262+
Fetch Vesu health factors data using the predefined query.
263+
264+
:return: DataFrame containing Vesu health factors data
265+
"""
266+
try:
267+
with self.engine.connect() as connection:
268+
return pd.read_sql(self.VESU_HEALTH_FACTORS_SQL_QUERY, connection)
269+
except sqlalchemy.exc.SQLAlchemyError as e:
270+
logger.error(f"Database error: {e}")
271+
raise DatabaseConnectionError(
272+
f"Failed to fetch Vesu health factors: {str(e)}"
273+
)
274+
212275

213276
class DataConnectorAsync(DataConnector):
214277
"""
215278
Handles database connection and fetches data asynchronously.
216279
"""
217-
280+
218281
async def fetch_data(
219282
self,
220283
query: str,
@@ -298,7 +361,7 @@ async def fetch_data(
298361
return pd.DataFrame()
299362

300363
return pd.concat(all_data, ignore_index=True)
301-
364+
302365
async def fetch_protocol_first_block_number(self, protocol: str) -> int:
303366
"""
304367
Asynchronously fetch the first block number for a specific protocol.

0 commit comments

Comments
 (0)