Files
BytbitBTC/monitor/monitor.py
2026-01-13 21:03:27 +02:00

101 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
import socket
import json
import time
import os
from datetime import datetime
# --- CONFIGURATION ---
# Adjust these paths/ports to match your config.json files
INPUT_SOCKET = "/tmp/streamer.sock"
ONRAMP_HOST = "127.0.0.1"
ONRAMP_PORT = 9999
REFRESH_RATE = 1.0 # Seconds
def query_input_go():
"""Queries the Go Input service via Unix Socket."""
if not os.path.exists(INPUT_SOCKET):
return "OFFLINE (Socket not found)"
try:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
s.settimeout(0.5)
s.connect(INPUT_SOCKET)
data = s.recv(1024)
return data.decode('utf-8').strip()
except Exception as e:
return f"OFFLINE ({e})"
def query_onramp_json(command):
"""Queries the Python Onramp service via TCP."""
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.5)
s.connect((ONRAMP_HOST, ONRAMP_PORT))
s.sendall(command.encode('utf-8'))
data = s.recv(8192) # Larger buffer for 'live' data
return json.loads(data.decode('utf-8'))
except:
return None
def format_candle(candle):
"""Formats a single candle dictionary into a readable line."""
return (f"O: {candle['open']:.2f} | H: {candle['high']:.2f} | "
f"L: {candle['low']:.2f} | C: {candle['close']:.2f} | V: {candle['volume']:.2f}")
def main():
try:
while True:
# 1. Collect Data
input_status = query_input_go()
onramp_status = query_onramp_json("status")
onramp_live = query_onramp_json("live")
# 2. Clear Screen
print("\033[2J\033[H", end="")
print("="*70)
print(f" UNIFIED MONITOR - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("="*70)
# 3. Display Input (Go) Section
print(f"\n[1] INPUT SERVICE (Go)")
print(f" Status: {input_status}")
# 4. Display Onramp (Python) Section
print(f"\n[2] ONRAMP SERVICE (Python)")
if onramp_status:
stats = onramp_status
# Calculate lag
last_ts = stats.get('last_ts', 0) / 1000
lag = time.time() - last_ts if last_ts > 0 else 0
print(f" Uptime Start : {stats.get('uptime_start')}")
print(f" Processed : {stats.get('total_trades')} trades")
print(f" Current File : {os.path.basename(str(stats.get('last_file')))}")
print(f" Data Lag : {lag:.2f}s")
else:
print(" Status: OFFLINE")
# 5. Display Live Market Snapshot
if onramp_live and "data" in onramp_live:
print(f"\n[3] LIVE MARKET SNAPSHOT")
candles = onramp_live["data"]
# Show 1m and 1h as examples
for tf in ["1m", "1h"]:
if tf in candles:
# Get the most recent timestamp in that timeframe
latest_ts = max(candles[tf].keys())
c = candles[tf][latest_ts]
ts_str = datetime.fromtimestamp(int(latest_ts)).strftime('%H:%M')
print(f" {tf} ({ts_str}) >> {format_candle(c)}")
print("\n" + "="*70)
print(" (Ctrl+C to exit)")
time.sleep(REFRESH_RATE)
except KeyboardInterrupt:
print("\nClosing monitor...")
if __name__ == "__main__":
main()