#pragma once
#include "types.hpp"
#include <atomic>
#include <mutex>
#include <thread>
#include <string>
#include <cstdio>
#include <cstring>
#include <chrono>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
namespace quant {
struct RuntimeMetrics {
std::atomic<double> capital{0.0};
std::atomic<int> position_count{0};
std::atomic<long long> total_orders{0};
std::atomic<long long> total_fills{0};
std::atomic<long long> rejected_orders{0};
std::atomic<double> realized_pnl{0.0};
std::atomic<long long> last_kline_latency_us{0};
std::atomic<long long> klines_seen{0};
std::atomic<bool> kill_switch{false};
std::string exchange = "OKX";
std::string strategy = "custom";
};
class MonitorServer {
public:
explicit MonitorServer(uint16_t port = 8080) : port_(port) {}
void set_metrics(RuntimeMetrics* m) { metrics_ = m; }
void start() {
if (running_) return;
running_ = true;
thread_ = std::thread([this] { serve(); });
}
void stop() {
running_ = false;
if (thread_.joinable()) thread_.join();
}
~MonitorServer() { stop(); }
private:
uint16_t port_;
std::atomic<bool> running_{false};
std::thread thread_;
RuntimeMetrics* metrics_ = nullptr;
void serve() {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return;
int one = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port_);
if (bind(fd, (sockaddr*)&addr, sizeof(addr)) != 0) { ::close(fd); return; }
if (listen(fd, 8) != 0) { ::close(fd); return; }
while (running_) {
sockaddr_in client{};
socklen_t clen = sizeof(client);
int cfd = accept(fd, (sockaddr*)&client, &clen);
if (cfd < 0) { if (!running_) break; continue; }
handle_client(cfd);
}
::close(fd);
}
void handle_client(int cfd) {
char buf[2048];
ssize_t n = recv(cfd, buf, sizeof(buf) - 1, 0);
if (n > 0) {
buf[n] = '\0';
std::string req(buf);
std::string body = render_metrics();
std::string resp = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain; version=0.0.4\r\n"
"Content-Length: " + std::to_string(body.size()) + "\r\n"
"Connection: close\r\n\r\n" + body;
send(cfd, resp.data(), resp.size(), 0);
}
::close(cfd);
}
std::string render_metrics() {
if (!metrics_) return "quant_engine_info{} 1\n";
RuntimeMetrics& m = *metrics_;
char buf[1024];
std::string out;
out += "# quant-cpp engine metrics\n";
snprintf(buf, sizeof(buf), "quant_capital{exchange=\"%s\"} %.2f\n", m.exchange.c_str(), m.capital.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_positions %d\n", m.position_count.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_orders_total %lld\n", (long long)m.total_orders.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_fills_total %lld\n", (long long)m.total_fills.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_rejected_total %lld\n", (long long)m.rejected_orders.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_realized_pnl %.2f\n", m.realized_pnl.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_kline_latency_us %lld\n", (long long)m.last_kline_latency_us.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_klines_seen %lld\n", (long long)m.klines_seen.load());
out += buf;
snprintf(buf, sizeof(buf), "quant_kill_switch %d\n", m.kill_switch.load() ? 1 : 0);
out += buf;
return out;
}
};
}