-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_graft.py
221 lines (191 loc) · 6.83 KB
/
manage_graft.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sqlite3
import requests
import yaml
import time
from datetime import datetime, timedelta
import config
def init_db():
conn = sqlite3.connect('subgraph_database.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS manage_graft (
version TEXT,
ipfs TEXT,
graft_ipfs TEXT,
graft_block TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS last_fetch (
id INTEGER PRIMARY KEY,
timestamp DATETIME
)
''')
# Insert initial timestamp if table is empty
cursor.execute('INSERT OR IGNORE INTO last_fetch (id, timestamp) VALUES (1, ?)', (datetime.min.isoformat(),))
conn.commit()
conn.close()
def fetch_subgraphs():
url = config.indexer_agent_network_subgraph_endpoint
all_subgraphs = []
skip = 0
while True:
query_subgraph = f'''
{{
subgraphs(first:1000, skip:{skip}, where:{{currentSignalledTokens_gt:990000000000000000}}) {{
currentSignalledTokens
currentVersion {{
subgraphDeployment {{
ipfsHash
versions {{
id
}}
}}
}}
}}
}}
'''
response = requests.post(url, json={'query': query_subgraph})
data = response.json()
subgraphs = data['data']['subgraphs']
all_subgraphs.extend(subgraphs)
if len(subgraphs) < 1000:
break
skip += 1000
return all_subgraphs
def fetch_100_subgraphs():
url = config.indexer_agent_network_subgraph_endpoint
all_subgraphs = []
skip = 0
while True:
query_subgraph = f'''
{{
subgraphs(first:100,where:{{currentSignalledTokens_gt:990000000000000000}},orderBy:updatedAt,orderDirection:desc){{
currentSignalledTokens
currentVersion {{
subgraphDeployment {{
ipfsHash
versions {{
id
}}
}}
}}
}}
}}
'''
response = requests.post(url, json={'query': query_subgraph})
data = response.json()
subgraphs = data['data']['subgraphs']
all_subgraphs.extend(subgraphs)
if len(subgraphs) < 1000:
break
skip += 1000
return all_subgraphs
def fetch_with_timeout(url, timeout):
try:
response = requests.get(url, timeout=timeout)
response.raise_for_status()
return response.text
except requests.RequestException:
return None
def process_subgraphs(subgraphs):
graft_data = []
i = 0
for subgraph in subgraphs:
print("number : " + str(i))
i += 1
ipfsHash = subgraph['currentVersion']['subgraphDeployment']['ipfsHash']
version = subgraph['currentVersion']['subgraphDeployment']['versions'][0]["id"]
fetch_graft_data(version, ipfsHash, graft_data)
return graft_data
def fetch_graft_data(version, ipfsHash, graft_data):
stillLoadGraft = True
print("Loading graft for subgraph : " + str(ipfsHash) + " - version : " + version)
while stillLoadGraft:
try:
ipfsUrl = f"https://ipfs.network.thegraph.com/api/v0/cat?arg={ipfsHash}"
textResponse = fetch_with_timeout(ipfsUrl, timeout=15)
if textResponse:
responseObj = yaml.safe_load(textResponse)
if "graft" in responseObj:
base = responseObj['graft']['base']
block = responseObj['graft']['block']
graft_data.append({
'version': version,
'ipfs': ipfsHash,
'graft_ipfs': base,
'graft_block': block
})
ipfsHash = base
else:
print("NO GRAFT !")
stillLoadGraft = False
else:
stillLoadGraft = False
except Exception as e:
print(e)
break
def save_graft_data(graft_data):
conn = sqlite3.connect('subgraph_database.db')
cursor = conn.cursor()
for data in graft_data:
try:
cursor.execute('''
SELECT COUNT(*) FROM manage_graft WHERE graft_ipfs = ?
''', (data['graft_ipfs'],))
count = cursor.fetchone()[0]
if count == 0:
cursor.execute('''
INSERT INTO manage_graft (version, ipfs, graft_ipfs, graft_block)
VALUES (?, ?, ?, ?)
''', (data['version'], data['ipfs'], data['graft_ipfs'], data['graft_block']))
except Exception as e:
print("save_graft_data : " + str(e))
conn.commit()
conn.close()
def is_db_has_data():
conn = sqlite3.connect('subgraph_database.db')
cursor = conn.cursor()
try:
cursor.execute('''SELECT COUNT(*) FROM manage_graft ''')
count = cursor.fetchone()[0]
return count > 0
except Exception as e:
print("save_graft_data : " + str(e))
cursor.close()
conn.close()
def get_last_fetch_time():
conn = sqlite3.connect('subgraph_database.db')
cursor = conn.cursor()
cursor.execute('SELECT timestamp FROM last_fetch ORDER BY timestamp DESC LIMIT 1')
result = cursor.fetchone()
conn.close()
return datetime.fromisoformat(result[0]) if result else None
def update_last_fetch_time():
conn = sqlite3.connect('subgraph_database.db')
cursor = conn.cursor()
cursor.execute('UPDATE last_fetch SET timestamp = ? WHERE id = 1', (datetime.now().isoformat(),))
conn.commit()
conn.close()
def start_manage_graft():
init_db()
while True:
if not is_db_has_data():
subgraphs = fetch_subgraphs()
print("Database empty. Fetching all subgraphs.")
else:
last_fetch_time = get_last_fetch_time()
current_time = datetime.now()
if last_fetch_time is None or (current_time - last_fetch_time) > timedelta(hours=1):
subgraphs = fetch_100_subgraphs()
print("Fetching 100 most recent subgraphs.")
update_last_fetch_time()
else:
print("Less than 1 hour since last fetch. Skipping...")
time.sleep(60 * 60) # Sleep for 1 hour before checking again
continue
print("Total subgraphs : " + str(len(subgraphs)))
graft_data = process_subgraphs(subgraphs)
save_graft_data(graft_data)
# Sleep for 6 hours before checking again
time.sleep(6 * 60 * 60)