forked from taoshidev/serve_outputs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserve.py
242 lines (185 loc) · 7.01 KB
/
serve.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import sys
from flask import Flask, jsonify, request, Response
import os
import time
import json
from waitress import serve
app = Flask(__name__)
accessible_api_keys = [
'xxxx'
]
def get_api_key():
# Get the API key from the query parameters or request headers
if "api_key" in request.json:
api_key = request.json["api_key"]
else:
api_key = request.headers.get('Authorization')
if api_key:
api_key = api_key.split(' ')[1] # Remove 'Bearer ' prefix
return api_key
def get_file(f, attempts=3, binary=False):
file_path = os.path.abspath(os.path.join(path, f))
if not os.path.exists(file_path):
return None
for attempt_number in range(attempts):
try:
if binary:
with open(file_path, 'rb') as f:
data = f.read()
else:
with open(file_path, "r") as file:
data = json.load(file)
return data
except json.JSONDecodeError as e:
if attempt_number == attempts - 1:
print(f"serve.py Failed to decode JSON after multiple attempts: {e}")
raise
else:
print(f"serve.py Attempt {attempt_number + 1} failed with JSONDecodeError, retrying...")
time.sleep(1) # Wait before retrying
except Exception as e:
print(f"serve.py Unexpected error reading file: {e}")
raise
# Endpoint to read and serve JSON data from outputs.json
@app.route("/miner-positions", methods=["GET"])
def get_miner_positions():
api_key = get_api_key()
# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
# Get the 'tier' query parameter from the request
tier = request.args.get('tier')
is_gz_data = tier is not None
if is_gz_data:
# Validate the 'tier' parameter
if tier not in ['0', '30', '50', '100']:
return jsonify({'error': 'Invalid tier value. Allowed values are 0, 30, 50, or 100'}), 400
# Construct the relative path based on the specified tier
f = f"outputs/tiered_positions/{tier}/output.json.gz"
else:
# If 'tier' parameter is not provided, return the default output.json
f = "outputs/output.json"
# Attempt to retrieve the file
data = get_file(f, binary=is_gz_data)
if data is None:
return f"{f} not found", 404
if is_gz_data:
return Response(data, content_type='application/json', headers={
'Content-Encoding': 'gzip'
})
return jsonify(data)
@app.route("/miner-positions/<minerid>", methods=["GET"])
def get_miner_positions_unique(minerid):
api_key = get_api_key()
# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
f = "outputs/output.json"
data = get_file(f)
if data is None:
return f"{f} not found", 404
# Filter the data for the specified miner ID
filtered_data = data.get(minerid, None)
if filtered_data is None:
return jsonify({'error': 'Miner ID not found'}), 404
return jsonify(filtered_data)
# Endpoint to read and serve JSON data from outputs.json
@app.route("/miner-hotkeys", methods=["GET"])
def get_miner_hotkeys():
api_key = get_api_key()
# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
f = "outputs/output.json"
data = get_file(f)
if data is None:
return f"{f} not found", 404
miner_hotkeys = list(data.keys())
if len(miner_hotkeys) == 0:
return f"{f} not found", 404
else:
return jsonify(miner_hotkeys)
# serve miner positions v2 now named validator checkpoint
@app.route("/validator-checkpoint", methods=["GET"])
def get_validator_checkpoint():
api_key = get_api_key()
# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
f = "../runnable/validator_checkpoint.json"
data = get_file(f)
if data is None:
return f"{f} not found", 404
else:
return jsonify(data)
# serve miner positions v2 now named validator checkpoint
@app.route("/statistics", methods=["GET"])
def get_validator_checkpoint_statistics():
api_key = get_api_key()
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
f = "../runnable/minerstatistics.json"
data = get_file(f)
if data is None:
return f"{f} not found", 404
# Grab the optional "checkpoints" query param; default it to "true"
show_checkpoints = request.args.get("checkpoints", "true").lower()
# If checkpoints=false, remove the "checkpoints" key from each element in data
if show_checkpoints == "false":
for element in data.get("data", []):
element.pop("checkpoints", None)
return jsonify(data)
@app.route("/statistics/<minerid>/", methods=["GET"])
def get_validator_checkpoint_statistics_unique(minerid):
api_key = get_api_key()
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
f = "../runnable/minerstatistics.json"
data = get_file(f)
if data is None:
return f"{f} not found", 404
data_summary = data.get("data", [])
if not data_summary:
return jsonify({'error': 'No data found'}), 404
# Grab the optional "checkpoints" query param; default it to "true"
show_checkpoints = request.args.get("checkpoints", "true").lower()
for element in data_summary:
if element.get("hotkey", None) == minerid:
# If the user set checkpoints=false, remove them from this element
if show_checkpoints == "false":
element.pop("checkpoints", None)
return jsonify(element)
return jsonify({'error': 'Miner ID not found'}), 404
@app.route("/eliminations", methods=["GET"])
def get_eliminations():
api_key = get_api_key()
# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
f = "eliminations.json"
data = get_file(f)
if data is None:
return f"{f} not found", 404
else:
return jsonify(data)
@app.route("/miner-copying", methods=["GET"])
def get_miner_copying():
api_key = get_api_key()
# Check if the API key is valid
if api_key not in accessible_api_keys:
return jsonify({'error': 'Unauthorized access'}), 401
f = "miner_copying.json"
data = get_file(f)
if data is None:
return f"{f} not found", 404
else:
return jsonify(data)
if __name__ == "__main__":
# sys.argv[0] is the script name itself
# Arguments start from sys.argv[1]
if len(sys.argv) > 1:
path = sys.argv[1]
else:
path = "../proprietary-trading-network/validation/"
print(path)
serve(app, host="127.0.0.1", port=48888)