feat: add durable WorldQuant backtests with UI and AI confirmation
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
"""Synthetic simulation HTTP used by isolated API and browser acceptance."""
|
||||
|
||||
import json
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
class Platform:
|
||||
def __init__(self):
|
||||
self.posts = []
|
||||
self.existing_alpha_ids = None
|
||||
self.simulations = {}
|
||||
self.alphas = {}
|
||||
self.reject = None
|
||||
self.pending = False
|
||||
self.detail_fail = False
|
||||
self.fail_child = None
|
||||
self.missing = False
|
||||
self.secret = "synthetic-platform-secret"
|
||||
|
||||
def __call__(self, request):
|
||||
path = request.url.path
|
||||
if path == "/authentication":
|
||||
return httpx.Response(201, json={})
|
||||
if path == "/simulations" and request.method == "POST":
|
||||
data = json.loads(request.content)
|
||||
data = data if isinstance(data, list) else [data]
|
||||
self.posts.append(data)
|
||||
if self.reject == "unknown":
|
||||
raise httpx.ReadTimeout("synthetic timeout", request=request)
|
||||
if self.reject == "session":
|
||||
self.reject = None
|
||||
return httpx.Response(401)
|
||||
if self.reject == "rate":
|
||||
return httpx.Response(429, headers={"Retry-After": "0.01"})
|
||||
if self.reject == "bad":
|
||||
return httpx.Response(400, json={"error": self.secret})
|
||||
parent = f"p{len(self.posts)}"
|
||||
ids = []
|
||||
for i, item in enumerate(data):
|
||||
child = parent if len(data) == 1 else f"{parent}c{i}"
|
||||
aid = self.existing_alpha_ids[i] if self.existing_alpha_ids else f"alpha{parent}{i}"
|
||||
progress = {
|
||||
"status": "COMPLETE",
|
||||
"alpha": aid,
|
||||
"regular": item["regular"],
|
||||
"settings": item["settings"],
|
||||
}
|
||||
if i == self.fail_child:
|
||||
progress = {
|
||||
"status": "FAILED",
|
||||
"regular": item["regular"],
|
||||
"settings": item["settings"],
|
||||
"message": "invalid expression",
|
||||
}
|
||||
self.simulations[child] = progress
|
||||
self.alphas[aid] = {
|
||||
"id": aid,
|
||||
"regular": {"code": item["regular"]},
|
||||
"type": "REGULAR",
|
||||
"settings": item["settings"],
|
||||
"is": {"sharpe": None, "fitness": 0.8},
|
||||
"status": "UNSUBMITTED",
|
||||
}
|
||||
ids.append(child)
|
||||
if len(data) > 1:
|
||||
self.simulations[parent] = {
|
||||
"status": "COMPLETE",
|
||||
"children": list(reversed(ids[1:] if self.missing else ids)),
|
||||
}
|
||||
if self.reject == "missing_location":
|
||||
return httpx.Response(201)
|
||||
return httpx.Response(
|
||||
201, headers={"Location": f"https://api.worldquantbrain.com/simulations/{parent}"}
|
||||
)
|
||||
if path.startswith("/simulations/"):
|
||||
return httpx.Response(
|
||||
200, json={"status": "PENDING"} if self.pending else self.simulations[path.rsplit("/", 1)[-1]]
|
||||
)
|
||||
if path.startswith("/alphas/"):
|
||||
if self.detail_fail:
|
||||
return httpx.Response(404)
|
||||
return httpx.Response(200, json=self.alphas[path.rsplit("/", 1)[-1]])
|
||||
if path == "/users/self":
|
||||
return httpx.Response(200, json={"id": "TEST_USER"})
|
||||
if path.startswith("/users/self/"):
|
||||
return httpx.Response(200, json={"results": [], "count": 0})
|
||||
raise AssertionError(f"Unexpected HTTP {request.method} {path}")
|
||||
Reference in New Issue
Block a user