fix: stats flush ensures schema, query flushes pending, add sqlite round-trip test
This commit is contained in:
+2
-2
@@ -13,7 +13,7 @@ CLI_TEST_BINS := $(patsubst tests/src/cli/%.cpp,build/tests/%,$(CLI_TEST_SRCS))
|
||||
|
||||
$(MOLO_BIN): $(CLI_OBJS) $(CLI_MAIN_OBJ)
|
||||
@mkdir -p build
|
||||
$(CXX) $(CXXFLAGS) $(CLI_OBJS) $(CLI_MAIN_OBJ) $(SUPPLY_LIB) $(OPERABILITY_LIB) -o $(MOLO_BIN)
|
||||
$(CXX) $(CXXFLAGS) $(CLI_OBJS) $(CLI_MAIN_OBJ) $(REALTIME_LIB) $(SUPPLY_LIB) $(OPERABILITY_LIB) -lsqlite3 -l:libpq.so.5 -o $(MOLO_BIN)
|
||||
|
||||
build/obj/%.o: %.cpp
|
||||
@mkdir -p $(dir $@)
|
||||
@@ -31,4 +31,4 @@ cli-test: $(MOLO_BIN) $(CLI_TEST_BINS)
|
||||
# "supplychain/..." include spelling resolves; MOLO_BIN link carries the lib).
|
||||
CLI_INC += -Isrc
|
||||
|
||||
$(MOLO_BIN): $(SUPPLY_LIB) $(OPERABILITY_LIB)
|
||||
$(MOLO_BIN): $(REALTIME_LIB) $(SUPPLY_LIB) $(OPERABILITY_LIB)
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "ai/socket_transport.hpp"
|
||||
#include "cli/arg_parser.hpp"
|
||||
#include "cli/command.hpp"
|
||||
#include "cli/expose_cli.hpp"
|
||||
#include "cli/help_renderer.hpp"
|
||||
#include "cli/json_writer.hpp"
|
||||
#include "cli/output.hpp"
|
||||
@@ -633,6 +634,7 @@ void CliApp::build_tree() {
|
||||
tree_.add(supervisor);
|
||||
tree_.add(task);
|
||||
tree_.add(ai);
|
||||
RegisterExposeCli(tree_);
|
||||
tree_.add(help);
|
||||
tree_.add(verify);
|
||||
tree_.add(supply_check);
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
#include "cli/expose_cli.hpp"
|
||||
|
||||
#include "cli/command.hpp"
|
||||
#include "cli/command_tree.hpp"
|
||||
#include "cli/exit_code.hpp"
|
||||
#include "cli/output.hpp"
|
||||
#include "app/views/view.h"
|
||||
#include "realtime/expose/claim.h"
|
||||
#include "realtime/expose/stats.h"
|
||||
#include "realtime/expose/tunnel.h"
|
||||
#include "realtime/expose/upstream.h"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
using realtime::expose::ClaimRegistry;
|
||||
using realtime::expose::ParseClaim;
|
||||
using realtime::expose::ParseUpstream;
|
||||
|
||||
static const char* FlagVal(const ParsedArgs& a, const std::string& k,
|
||||
const char* d) {
|
||||
auto it = a.flags.find(k);
|
||||
return it == a.flags.end() ? d : it->second.c_str();
|
||||
}
|
||||
static ClaimRegistry& Reg() {
|
||||
static ClaimRegistry r("cli");
|
||||
return r;
|
||||
}
|
||||
|
||||
void RegisterExposeCli(CommandTree& tree) {
|
||||
auto expose = std::make_shared<Command>("expose", "reverse tunnel upstreams");
|
||||
auto serve =
|
||||
std::make_shared<Command>("serve", "start expose server (claim+upstream)");
|
||||
auto agent = std::make_shared<Command>("agent", "run expose agent (tunnel out)");
|
||||
auto key = std::make_shared<Command>("expose-key", "manage claim keys");
|
||||
auto rotate = std::make_shared<Command>("rotate", "rotate claim key");
|
||||
auto list = std::make_shared<Command>("list", "list tunnels");
|
||||
auto stats = std::make_shared<Command>("stats", "view/stream stats (text|json)");
|
||||
serve->handler(RunExposeServe);
|
||||
agent->handler(RunExposeAgent);
|
||||
rotate->handler(RunExposeKeyRotate);
|
||||
list->handler(RunExposeList);
|
||||
stats->handler(RunExposeStats);
|
||||
key->child(rotate);
|
||||
expose->child(serve);
|
||||
expose->child(agent);
|
||||
expose->child(key);
|
||||
expose->child(list);
|
||||
expose->child(stats);
|
||||
tree.add(std::move(expose));
|
||||
}
|
||||
|
||||
ExitCode RunExposeServe(const ParsedArgs& args, Output& out) {
|
||||
std::string reason, slug = FlagVal(args, "slug", "");
|
||||
if (slug.empty()) {
|
||||
out.error("usage: molo expose serve --slug <claim> --upstream <url>");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
auto claim = ParseClaim(slug, reason);
|
||||
if (!claim) {
|
||||
out.error("bad claim: " + reason);
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
std::string us = FlagVal(args, "upstream", "http://127.0.0.1:3000");
|
||||
auto ue = ParseUpstream(us, reason);
|
||||
if (!ue) {
|
||||
out.error("bad upstream: " + reason);
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
realtime::expose::UpstreamTable t;
|
||||
if (!t.Add(*ue, reason)) {
|
||||
out.error("upstream: " + reason);
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
std::shared_ptr<realtime::expose::Tunnel> tun;
|
||||
std::string detail;
|
||||
auto r = Reg().Register(*claim, realtime::expose::Sha256Hex("cli-key"),
|
||||
std::move(t), false, tun, detail);
|
||||
if (r != realtime::expose::RegisterResult::kOk) {
|
||||
out.error("register: " + detail);
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
// reverse.md §9: wire View dispatch -> StatsCollector (zero-cost when idle).
|
||||
auto& sc = realtime::expose::StatsCollector::Global();
|
||||
sc.Configure(realtime::expose::MakeStatsStore(""));
|
||||
std::string serr;
|
||||
(void)sc.Flush(serr); // ensure schema exists; batch flushes follow on timer
|
||||
molo::app::views::View::SetObserver([](const std::string& m, std::uint64_t bi,
|
||||
std::uint64_t bo, std::uint64_t us) {
|
||||
realtime::expose::StatsCollector::Global().Note(m, bi, bo, us);
|
||||
});
|
||||
out.print("serving " + slug + " -> " + us);
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode RunExposeAgent(const ParsedArgs& args, Output& out) {
|
||||
std::string slug = FlagVal(args, "slug", "");
|
||||
std::string key = FlagVal(args, "key", "");
|
||||
std::string server = FlagVal(args, "server", "http://127.0.0.1:8080");
|
||||
if (slug.empty() || key.empty()) {
|
||||
out.error("usage: molo expose agent --slug <claim> --key <key>");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
out.print("tunnel " + slug + " -> " + server + " (H2, concurrent streams)");
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode RunExposeKeyRotate(const ParsedArgs& args, Output& out) {
|
||||
std::string reason, slug = FlagVal(args, "slug", "");
|
||||
if (slug.empty()) {
|
||||
out.error("usage: molo expose-key rotate --slug <claim>");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
auto claim = ParseClaim(slug, reason);
|
||||
if (!claim) {
|
||||
out.error("bad claim: " + reason);
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
Reg().RotateKey(*claim, realtime::expose::Sha256Hex("rotated"));
|
||||
out.print("rotated key for " + slug);
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode RunExposeList(const ParsedArgs&, Output& out) {
|
||||
auto v = Reg().List();
|
||||
out.print("tunnels: " + std::to_string(v.size()));
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode RunExposeStats(const ParsedArgs& args, Output& out) {
|
||||
// reverse.md §9: molo expose stats [--json]; backed by system.db (sqlite
|
||||
// default) or DATABASE_URL (.env.json/env) postgres. Flush pending first.
|
||||
using realtime::expose::StatsCollector;
|
||||
auto& sc = StatsCollector::Global();
|
||||
std::string err;
|
||||
bool want_json = FlagVal(args, "json", "")[0] != '\0' ||
|
||||
FlagVal(args, "format", "") == std::string("json");
|
||||
std::string text;
|
||||
if (sc.Flush(err) && sc.Query(text, want_json, err)) {
|
||||
out.print(text.empty() ? (want_json ? std::string("[]") : std::string("(no stats yet)")) : text);
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
// Fallback: serve process not running in this CLI; query store directly.
|
||||
auto store = realtime::expose::MakeStatsStore(FlagVal(args, "db", ""));
|
||||
if (!store->Query(text, want_json, err)) { out.error("stats: " + err); return ExitCode::Usage; }
|
||||
out.print(text.empty() ? (want_json ? std::string("[]") : std::string("(no stats yet)")) : text);
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "cli/command.hpp"
|
||||
|
||||
namespace molo::cli { class CommandTree; }
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
class Output;
|
||||
enum class ExitCode : int;
|
||||
|
||||
// Registers: molo expose serve|agent|list + molo expose-key rotate.
|
||||
void RegisterExposeCli(CommandTree& tree);
|
||||
|
||||
ExitCode RunExposeServe(const ParsedArgs& args, Output& out);
|
||||
ExitCode RunExposeAgent(const ParsedArgs& args, Output& out);
|
||||
ExitCode RunExposeKeyRotate(const ParsedArgs& args, Output& out);
|
||||
ExitCode RunExposeList(const ParsedArgs& args, Output& out);
|
||||
ExitCode RunExposeStats(const ParsedArgs& args, Output& out);
|
||||
|
||||
} // namespace molo::cli
|
||||
Reference in New Issue
Block a user