-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_data.py
207 lines (179 loc) · 8.36 KB
/
day_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import requests
import pandas as pd
import json
import os
import time
from datetime import datetime, timezone, date, timedelta
from application_logging.logger import logger
import gspread
from gspread_dataframe import set_with_dataframe
from utils.helpers import read_params
# Params
params_path = 'params.yaml'
config = read_params(params_path)
daydelta = config['delta']['day_data']
# V1
try:
logger.info("Day Data Started")
# Params Data
subgraph = config["query"]["subgraph"]
day_data_query = config["query"]["day_data_query"]
daily_data_csv = config["files"]["daily_data"]
# Date Stuff
todayDate = datetime.utcnow()
twodayago = todayDate - timedelta(daydelta)
my_time = datetime.min.time()
my_datetime = datetime.combine(twodayago, my_time)
timestamp = int(my_datetime.replace(tzinfo=timezone.utc).timestamp())
# Request
day_data_query["variables"]["startTime"] = timestamp
response = requests.post(url=subgraph, json=day_data_query)
data = response.json()["data"]["dayDatas"]
day_data_df = pd.DataFrame(data)
day_data_df = day_data_df[['id', 'date', 'totalVolumeUSD', 'dailyVolumeUSD', 'dailyVolumeETH', 'totalLiquidityUSD', 'totalLiquidityETH', '__typename']]
day_data_df["date"] = day_data_df["date"].apply(lambda timestamp: datetime.utcfromtimestamp(timestamp).date())
day_data_df["date"] = day_data_df["date"].apply(lambda date: datetime.strftime(date, "%Y-%m-%d"))
day_data_old = pd.read_csv(daily_data_csv)
drop_index = day_data_old[day_data_old['date']>datetime.fromtimestamp(timestamp).strftime(format='%Y-%m-%d')].index
index_list = drop_index.to_list()
index_list = list(map(lambda x: x + 2, index_list))
day_data_df['__typename'] = 'V1'
df_values = day_data_df.values.tolist()
# Write to GSheets
credentials = os.environ["GKEY"]
credentials = json.loads(credentials)
gc = gspread.service_account_from_dict(credentials)
# Open a google sheet
sheetkey = config["gsheets"]["daily_data_sheet_key"]
gs = gc.open_by_key(sheetkey)
# Select a work sheet from its name
worksheet1 = gs.worksheet("Master")
if index_list != []:
worksheet1.delete_rows(index_list[0], index_list[-1])
retries, delay = 3, 30
for attempt in range(retries):
try:
gs = gc.open_by_key(sheetkey)
# Append to Worksheet
gs.values_append("Master", {"valueInputOption": "USER_ENTERED"}, {"values": df_values})
logger.error("Data successfully appended to Google Sheets.")
break # Break the loop if successful
except Exception as e:
logger.error(f"Error occurred: {e}")
if attempt < retries - 1:
logger.error(f"Retrying in {delay} seconds... (Attempt {attempt + 2}/{retries})")
time.sleep(delay) # Wait before retrying
else:
logger.error("All retries failed.")
raise # Re-raise the exception if retries are exhausted
logger.info("Day Data Ended")
except Exception as e:
logger.error("Error occurred during Day Data process. Error: %s" % e)
# Fusion
try:
logger.info("Day Data Fusion Started")
# Params Data
subgraph = config["query"]["fusion_subgraph"]
GRAPH_KEY = os.environ["GRAPH_KEY"]
day_data_fusion_query = config["query"]["day_data_fusion_query"]
daily_data_fusion_csv = config["files"]["daily_data_fusion"]
# Date Stuff
todayDate = datetime.utcnow()
twodayago = todayDate - timedelta(daydelta)
my_time = datetime.min.time()
my_datetime = datetime.combine(twodayago, my_time)
timestamp = int(my_datetime.replace(tzinfo=timezone.utc).timestamp())
# Request
day_data_fusion_query["variables"]["startTime"] = timestamp
if "[api-key]" in subgraph:
subgraph = subgraph.replace("[api-key]", GRAPH_KEY)
response = requests.post(url=subgraph, json=day_data_fusion_query)
try:
data = response.json()["data"]["fusionDayData"]
except:
logger.info(response.json())
day_data_fusion_df = pd.DataFrame(data)
# day_data_fusion_df['feesUSD'] = 0
day_data_fusion_df = day_data_fusion_df[['id', 'date', 'volumeUSD', 'feesUSD', 'tvlUSD', '__typename']]
day_data_fusion_df["date"] = day_data_fusion_df["date"].apply(lambda timestamp: datetime.utcfromtimestamp(timestamp).date())
day_data_fusion_df["date"] = day_data_fusion_df["date"].apply(lambda date: datetime.strftime(date, "%Y-%m-%d"))
day_data_fusion_old = pd.read_csv(daily_data_fusion_csv)
drop_index = day_data_fusion_old[day_data_fusion_old['date']>datetime.fromtimestamp(timestamp).strftime(format='%Y-%m-%d')].index
index_list = drop_index.to_list()
index_list = list(map(lambda x: x + 2, index_list))
day_data_fusion_df['__typename'] = 'Fusion'
df_values = day_data_fusion_df.values.tolist()
# Write to GSheets
credentials = os.environ["GKEY"]
credentials = json.loads(credentials)
gc = gspread.service_account_from_dict(credentials)
# Open a google sheet
sheetkey = config["gsheets"]["daily_data_fusion_sheet_key"]
gs = gc.open_by_key(sheetkey)
# Select a work sheet from its name
worksheet1 = gs.worksheet("Master")
if index_list != []:
worksheet1.delete_rows(index_list[0], index_list[-1])
retries, delay = 3, 30
for attempt in range(retries):
try:
gs = gc.open_by_key(sheetkey)
# Append to Worksheet
gs.values_append("Master", {"valueInputOption": "USER_ENTERED"}, {"values": df_values})
logger.error("Data successfully appended to Google Sheets.")
break # Break the loop if successful
except Exception as e:
logger.error(f"Error occurred: {e}")
if attempt < retries - 1:
logger.error(f"Retrying in {delay} seconds... (Attempt {attempt + 2}/{retries})")
time.sleep(delay) # Wait before retrying
else:
logger.error("All retries failed.")
raise # Re-raise the exception if retries are exhausted
logger.info("Day Data Fusion Ended")
except Exception as e:
logger.error("Error occurred during Day Data Fusion process. Error: %s" % e)
# Combined
try:
logger.info("Day Data Combined Started")
# Data Manipulation
day_data_old = pd.read_csv(daily_data_csv)
day_data_fusion_old = pd.read_csv(daily_data_fusion_csv)
df1 = day_data_old[['id', 'date', 'dailyVolumeUSD', 'totalLiquidityUSD', '__typename']]
df2 = day_data_fusion_old[['id', 'date', 'volumeUSD', 'tvlUSD', '__typename']]
df2.columns = ['id', 'date', 'dailyVolumeUSD', 'totalLiquidityUSD', '__typename']
day_data_combined_df = pd.concat([df1, df2], ignore_index=True, axis=0)
# Write to GSheets
credentials = os.environ["GKEY"]
credentials = json.loads(credentials)
gc = gspread.service_account_from_dict(credentials)
# Open a google sheet
sheetkey = config["gsheets"]["daily_data_combined_sheet_key"]
retries, delay = 3, 30
for attempt in range(retries):
try:
gs = gc.open_by_key(sheetkey)
# Select a work sheet from its name
worksheet1 = gs.worksheet("Master")
worksheet1.clear()
# Add to Worksheet
set_with_dataframe(
worksheet=worksheet1,
dataframe=day_data_combined_df,
include_index=False,
include_column_header=True,
resize=True,
)
logger.error("Data successfully added to Google Sheets.")
break # Break the loop if successful
except Exception as e:
logger.error(f"Error occurred: {e}")
if attempt < retries - 1:
logger.error(f"Retrying in {delay} seconds... (Attempt {attempt + 2}/{retries})")
time.sleep(delay) # Wait before retrying
else:
logger.error("All retries failed.")
raise # Re-raise the exception if retries are exhausted
logger.info("Day Data Combined Ended")
except Exception as e:
logger.error("Error occurred during Day Data Combined process. Error: %s" % e)