Spaces:
Runtime error
Runtime error
huggingface112
commited on
Commit
•
65eba49
1
Parent(s):
ec82168
implement method to retrieve oldest record
Browse files- db_operation.py +20 -3
db_operation.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
'''
|
2 |
Abstraction for saving data to db
|
3 |
'''
|
4 |
import pandas as pd
|
@@ -49,6 +49,25 @@ def get_most_recent_profile(type):
|
|
49 |
df['date'] = pd.to_datetime(df['date'])
|
50 |
return df
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
def _get_most_recent(table_name, ts_column='date'):
|
53 |
'''return the most recent entry in the table'''
|
54 |
query = f"SELECT * FROM {table_name} WHERE {ts_column} = (SELECT MAX({ts_column}) FROM {table_name})"
|
@@ -65,12 +84,10 @@ def get_all_portfolio_profile():
|
|
65 |
|
66 |
def get_most_recent_portfolio_profile():
|
67 |
df = _get_most_recent(ts.PORTFOLIO_TABLE)
|
68 |
-
df['date'] = pd.to_datetime(df['date'])
|
69 |
return df
|
70 |
|
71 |
def get_most_recent_stocks_price():
|
72 |
df = _get_most_recent(ts.STOCKS_PRICE_TABLE, ts_column='time')
|
73 |
-
df['time'] = pd.to_datetime(df['time'])
|
74 |
return df
|
75 |
|
76 |
def _append_df_to_db(df, table_name, schema):
|
|
|
1 |
+
'''
|
2 |
Abstraction for saving data to db
|
3 |
'''
|
4 |
import pandas as pd
|
|
|
49 |
df['date'] = pd.to_datetime(df['date'])
|
50 |
return df
|
51 |
|
52 |
+
def _get_oldest(table_name, ts_column='date'):
|
53 |
+
query = f"SELECT * FROM {table_name} WHERE {ts_column} = (SELECT MIN({ts_column}) FROM {table_name})"
|
54 |
+
with create_engine(db_url).connect() as conn:
|
55 |
+
df = pd.read_sql(query, con=conn)
|
56 |
+
df[ts_column] = pd.to_datetime(df[ts_column])
|
57 |
+
return df
|
58 |
+
|
59 |
+
def get_oldest_portfolio_profile():
|
60 |
+
df = _get_oldest(ts.PORTFOLIO_TABLE)
|
61 |
+
return df
|
62 |
+
|
63 |
+
def get_oldest_stocks_proce():
|
64 |
+
df = _get_oldest(ts.STOCKS_PRICE_TABLE, ts_column='time')
|
65 |
+
return df
|
66 |
+
|
67 |
+
def get_oldest_benchmark_profile():
|
68 |
+
df = _get_oldest(ts.BENCHMARK_TABLE)
|
69 |
+
return df
|
70 |
+
|
71 |
def _get_most_recent(table_name, ts_column='date'):
|
72 |
'''return the most recent entry in the table'''
|
73 |
query = f"SELECT * FROM {table_name} WHERE {ts_column} = (SELECT MAX({ts_column}) FROM {table_name})"
|
|
|
84 |
|
85 |
def get_most_recent_portfolio_profile():
|
86 |
df = _get_most_recent(ts.PORTFOLIO_TABLE)
|
|
|
87 |
return df
|
88 |
|
89 |
def get_most_recent_stocks_price():
|
90 |
df = _get_most_recent(ts.STOCKS_PRICE_TABLE, ts_column='time')
|
|
|
91 |
return df
|
92 |
|
93 |
def _append_df_to_db(df, table_name, schema):
|