Next commit is monitor.py that reads analyst.py also! This version to safety.
This commit is contained in:
@@ -18,7 +18,7 @@ from rich.text import Text
|
|||||||
INPUT_SOCKET = "/tmp/streamer.sock"
|
INPUT_SOCKET = "/tmp/streamer.sock"
|
||||||
ONRAMP_HOST = "127.0.0.1"
|
ONRAMP_HOST = "127.0.0.1"
|
||||||
ONRAMP_PORT = 9999
|
ONRAMP_PORT = 9999
|
||||||
REFRESH_RATE = 1.0
|
REFRESH_RATE = 1.0
|
||||||
|
|
||||||
# Global state to track lag history (last 300 seconds)
|
# Global state to track lag history (last 300 seconds)
|
||||||
LAG_HISTORY = deque(maxlen=300)
|
LAG_HISTORY = deque(maxlen=300)
|
||||||
@@ -39,17 +39,17 @@ def query_input_go():
|
|||||||
def query_onramp(command):
|
def query_onramp(command):
|
||||||
try:
|
try:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
s.settimeout(1.0)
|
s.settimeout(1.0)
|
||||||
s.connect((ONRAMP_HOST, ONRAMP_PORT))
|
s.connect((ONRAMP_HOST, ONRAMP_PORT))
|
||||||
s.sendall(command.encode('utf-8'))
|
s.sendall(command.encode('utf-8'))
|
||||||
|
|
||||||
chunks = []
|
chunks = []
|
||||||
while True:
|
while True:
|
||||||
chunk = s.recv(4096)
|
chunk = s.recv(4096)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
chunks.append(chunk)
|
chunks.append(chunk)
|
||||||
|
|
||||||
full_data = b"".join(chunks).decode('utf-8')
|
full_data = b"".join(chunks).decode('utf-8')
|
||||||
return json.loads(full_data)
|
return json.loads(full_data)
|
||||||
except:
|
except:
|
||||||
@@ -81,15 +81,15 @@ def get_onramp_panel():
|
|||||||
data = query_onramp("status")
|
data = query_onramp("status")
|
||||||
if not data:
|
if not data:
|
||||||
LAG_HISTORY.clear() # Clear history if service goes down
|
LAG_HISTORY.clear() # Clear history if service goes down
|
||||||
return Panel(Text("OFFLINE", style="bold red"), title="[2] Onramp Service (Python)", border_style="red")
|
return Panel(Text("OFFLINE", style="bold red"), title="[2] Onramp Service (Go)", border_style="red")
|
||||||
|
|
||||||
# 1. Calculate Instant Lag
|
# 1. Calculate Instant Lag
|
||||||
last_ts = data.get('last_ts', 0) / 1000
|
last_ts = data.get('last_ts', 0) / 1000
|
||||||
current_lag = time.time() - last_ts if last_ts > 0 else 0
|
current_lag = time.time() - last_ts if last_ts > 0 else 0
|
||||||
|
|
||||||
# 2. Update History
|
# 2. Update History
|
||||||
LAG_HISTORY.append(current_lag)
|
LAG_HISTORY.append(current_lag)
|
||||||
|
|
||||||
# 3. Calculate Averages (Load Average style)
|
# 3. Calculate Averages (Load Average style)
|
||||||
avg_1m = sum(list(LAG_HISTORY)[-60:]) / min(len(LAG_HISTORY), 60)
|
avg_1m = sum(list(LAG_HISTORY)[-60:]) / min(len(LAG_HISTORY), 60)
|
||||||
avg_5m = sum(LAG_HISTORY) / len(LAG_HISTORY)
|
avg_5m = sum(LAG_HISTORY) / len(LAG_HISTORY)
|
||||||
@@ -101,14 +101,14 @@ def get_onramp_panel():
|
|||||||
content.append(f"Uptime Start : {data.get('uptime_start')}\n")
|
content.append(f"Uptime Start : {data.get('uptime_start')}\n")
|
||||||
content.append(f"Total Trades : {data.get('total_trades')}\n")
|
content.append(f"Total Trades : {data.get('total_trades')}\n")
|
||||||
content.append(f"Current File : {os.path.basename(str(data.get('last_file')))}\n")
|
content.append(f"Current File : {os.path.basename(str(data.get('last_file')))}\n")
|
||||||
|
|
||||||
# The "Load Average" line
|
# The "Load Average" line
|
||||||
content.append("Lag (Avg) : ", style="white")
|
content.append("Lag (Avg) : ", style="white")
|
||||||
content.append(f"{current_lag:.2f}s", style=lag_style)
|
content.append(f"{current_lag:.2f}s", style=lag_style)
|
||||||
content.append(f", {avg_1m:.2f}s/1m", style="dim" if avg_1m < 2 else "yellow")
|
content.append(f", {avg_1m:.2f}s/1m", style="dim" if avg_1m < 2 else "yellow")
|
||||||
content.append(f", {avg_5m:.2f}s/5m", style="dim" if avg_5m < 2 else "yellow")
|
content.append(f", {avg_5m:.2f}s/5m", style="dim" if avg_5m < 2 else "yellow")
|
||||||
|
|
||||||
return Panel(content, title="[2] Onramp Service (Python)", border_style="blue")
|
return Panel(content, title="[2] Onramp Service (Go)", border_style="blue")
|
||||||
|
|
||||||
def get_market_table():
|
def get_market_table():
|
||||||
res = query_onramp("live")
|
res = query_onramp("live")
|
||||||
@@ -129,14 +129,14 @@ def get_market_table():
|
|||||||
all_ts = [int(ts) for ts in candles_data[tf].keys()]
|
all_ts = [int(ts) for ts in candles_data[tf].keys()]
|
||||||
latest_ts = str(max(all_ts))
|
latest_ts = str(max(all_ts))
|
||||||
c = candles_data[tf][latest_ts]
|
c = candles_data[tf][latest_ts]
|
||||||
|
|
||||||
ts_str = datetime.fromtimestamp(int(latest_ts)).strftime('%H:%M:%S')
|
ts_str = datetime.fromtimestamp(int(latest_ts)).strftime('%H:%M:%S')
|
||||||
color = "green" if c['close'] >= c['open'] else "red"
|
color = "green" if c['close'] >= c['open'] else "red"
|
||||||
buy_pct = (c['buy_volume'] / c['volume'] * 100) if c['volume'] > 0 else 0
|
buy_pct = (c['buy_volume'] / c['volume'] * 100) if c['volume'] > 0 else 0
|
||||||
buy_color = "green" if buy_pct > 50 else "red"
|
buy_color = "green" if buy_pct > 50 else "red"
|
||||||
|
|
||||||
table.add_row(
|
table.add_row(
|
||||||
tf, ts_str, f"{c['open']:.2f}", f"{c['high']:.2f}", f"{c['low']:.2f}",
|
tf, ts_str, f"{c['open']:.2f}", f"{c['high']:.2f}", f"{c['low']:.2f}",
|
||||||
Text(f"{c['close']:.2f}", style=f"bold {color}"),
|
Text(f"{c['close']:.2f}", style=f"bold {color}"),
|
||||||
f"{c['volume']:.2f}", Text(f"{buy_pct:.1f}%", style=buy_color)
|
f"{c['volume']:.2f}", Text(f"{buy_pct:.1f}%", style=buy_color)
|
||||||
)
|
)
|
||||||
@@ -148,7 +148,7 @@ def main():
|
|||||||
layout = make_layout()
|
layout = make_layout()
|
||||||
with Live(layout, refresh_per_second=2, screen=True):
|
with Live(layout, refresh_per_second=2, screen=True):
|
||||||
while True:
|
while True:
|
||||||
layout["header"].update(Panel(Text(f"BYBIT BTC UNIFIED MONITOR | {datetime.now().strftime('%H:%M:%S')}", justify="center", style="bold white on blue")))
|
layout["header"].update(Panel(Text(f"BYBIT BTCUSDT UNIFIED MONITOR | {datetime.now().strftime('%H:%M:%S')}", justify="center", style="bold white on blue")))
|
||||||
layout["input_svc"].update(get_input_panel())
|
layout["input_svc"].update(get_input_panel())
|
||||||
layout["onramp_svc"].update(get_onramp_panel())
|
layout["onramp_svc"].update(get_onramp_panel())
|
||||||
layout["market"].update(get_market_table())
|
layout["market"].update(get_market_table())
|
||||||
@@ -156,4 +156,4 @@ def main():
|
|||||||
time.sleep(REFRESH_RATE)
|
time.sleep(REFRESH_RATE)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user