nice-bill commited on
Commit
91f2ea1
·
1 Parent(s): 1228690

changed api url

Browse files
Files changed (1) hide show
  1. app.py +56 -11
app.py CHANGED
@@ -10,6 +10,7 @@ from src.llm import PersonaExplainer
10
  import os
11
  import requests
12
  import uuid
 
13
  from dotenv import load_dotenv
14
  from diskcache import Cache
15
  from typing import Optional, Dict, Any
@@ -89,25 +90,69 @@ def process_wallet_analysis(job_id: str, wallet_address: str):
89
  # Update status to processing
90
  cache.set(job_id, {"status": "processing", "wallet": wallet_address}, expire=CACHE_TTL)
91
 
92
- # 1. Fetch Data from Dune
93
  if not DUNE_API_KEY:
94
  raise Exception("Dune API Key missing configuration.")
95
 
96
- dune_url = f"https://api.dune.com/api/v1/query/6252521/results?wallet={wallet_address}"
 
97
  headers = {"X-Dune-API-Key": DUNE_API_KEY}
 
98
 
99
- print(f"Fetching Dune data for {wallet_address}...")
100
- response = requests.get(dune_url, headers=headers, timeout=15)
101
 
102
- if response.status_code != 200:
103
- raise Exception(f"Dune API Error: {response.status_code} - {response.text}")
104
-
105
- data_json = response.json()
106
- if not data_json.get("result", {}).get("rows"):
107
- raise Exception("Wallet not found in Dune dataset (no activity).")
108
 
109
- row_data = data_json["result"]["rows"][0]
 
 
 
 
 
 
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  # 2. Prepare Features & Predict
112
  if predictor is None:
113
  raise Exception("Inference Model not loaded.")
 
10
  import os
11
  import requests
12
  import uuid
13
+ import time
14
  from dotenv import load_dotenv
15
  from diskcache import Cache
16
  from typing import Optional, Dict, Any
 
90
  # Update status to processing
91
  cache.set(job_id, {"status": "processing", "wallet": wallet_address}, expire=CACHE_TTL)
92
 
93
+ # 1. Execute Dune Query (Start New Run)
94
  if not DUNE_API_KEY:
95
  raise Exception("Dune API Key missing configuration.")
96
 
97
+ # Step A: Submit Execution
98
+ execute_url = "https://api.dune.com/api/v1/query/6252521/execute"
99
  headers = {"X-Dune-API-Key": DUNE_API_KEY}
100
+ payload = {"query_parameters": {"wallet": wallet_address}}
101
 
102
+ print(f"Submitting Dune query for {wallet_address}...")
103
+ exec_res = requests.post(execute_url, headers=headers, json=payload, timeout=10)
104
 
105
+ if exec_res.status_code != 200:
106
+ raise Exception(f"Dune Execution Failed: {exec_res.status_code} - {exec_res.text}")
 
 
 
 
107
 
108
+ execution_id = exec_res.json().get("execution_id")
109
+ if not execution_id:
110
+ raise Exception("No execution_id returned from Dune.")
111
+
112
+ # Step B: Poll for Completion
113
+ print(f"Polling Dune execution {execution_id}...")
114
+ status_url = f"https://api.dune.com/api/v1/execution/{execution_id}/status"
115
 
116
+ max_retries = 30 # 30 * 2s = 60s max wait
117
+ for i in range(max_retries):
118
+ status_res = requests.get(status_url, headers=headers, timeout=10)
119
+ if status_res.status_code != 200:
120
+ # Temporary network glitch? Wait and retry.
121
+ time.sleep(2)
122
+ continue
123
+
124
+ state = status_res.json().get("state")
125
+ if state == "QUERY_STATE_COMPLETED":
126
+ break
127
+ elif state == "QUERY_STATE_FAILED":
128
+ raise Exception("Dune Query Execution FAILED internally.")
129
+ elif state == "QUERY_STATE_CANCELLED":
130
+ raise Exception("Dune Query was CANCELLED.")
131
+
132
+ time.sleep(2)
133
+ else:
134
+ raise Exception("Dune Query Timed Out (60s).")
135
+
136
+ # Step C: Fetch Results
137
+ results_url = f"https://api.dune.com/api/v1/execution/{execution_id}/results"
138
+ results_res = requests.get(results_url, headers=headers, timeout=15)
139
+
140
+ if results_res.status_code != 200:
141
+ raise Exception(f"Failed to fetch results: {results_res.status_code}")
142
+
143
+ data_json = results_res.json()
144
+ rows = data_json.get("result", {}).get("rows", [])
145
+
146
+ if not rows:
147
+ # If the query ran but returned no rows (maybe empty wallet?)
148
+ # We can either fail or proceed with zero-filled data.
149
+ # Given the SQL logic, it usually returns 1 row with 0s if empty,
150
+ # but let's be safe.
151
+ raise Exception("Dune returned no data rows for this wallet.")
152
+
153
+ # We trust the execution result because we just ran it with the param.
154
+ row_data = rows[0]
155
+
156
  # 2. Prepare Features & Predict
157
  if predictor is None:
158
  raise Exception("Inference Model not loaded.")