mono/packages/kbot/cpp/src/cmd_gridsearch.h

89 lines
2.9 KiB
C++

#pragma once
#include <CLI/CLI.hpp>
#include <functional>
#include <string>
#include <memory>
#include <atomic>
#include "search/search.h"
#include "grid/grid.h"
#include <vector>
namespace polymech {
std::string json_escape(const std::string &s);
struct AreaDef {
std::string gid;
std::string name;
int level;
};
struct AccumulatedResult {
search::MapResult result;
std::string grid_area;
std::string grid_gid;
};
struct PipelineOptions {
std::vector<AreaDef> areas;
grid::GridOptions grid_opts;
std::string search_query;
std::string search_domain = "google.com";
std::string search_language = "en";
std::string search_country;
int search_limit = 20;
int search_zoom = 13;
bool dry_run = false;
bool enrich = false;
std::string config_path = "config/postgres.toml";
std::string cache_dir = "cache/gadm";
bool persistence_postgres = false;
bool daemon_mode = false;
std::string job_id;
std::string default_user_id = "3bb4cfbf-318b-44d3-a9d3-35680e738421";
search::SystemTuningOptions tuning;
std::shared_ptr<std::atomic<bool>> cancel_token;
std::vector<std::string> filter_types; // if non-empty, only locations matching ≥1 type pass
std::vector<std::string> exclude_types; // if non-empty, drop locations matching any
bool no_cache = false; // skip pre-enrich dedup — force re-enrichment
std::string parent_id; // if set, this run is an "expand" child of another run
};
std::string json_escape(const std::string &s);
/// Optional callbacks for streaming progress events (used in IPC mode).
/// When nullptr / empty, the pipeline runs silently (CLI mode).
struct GridsearchCallbacks {
/// Emit a progress event. `type` is one of:
/// grid-ready, waypoint-start, area, location,
/// enrich-start, node, node-error, nodePage
/// `json` is the raw JSON payload string.
std::function<void(const std::string& type, const std::string& json)> onEvent;
};
CLI::App* setup_cmd_gridsearch(CLI::App& app);
/// CLI entry point (standalone mode — reads static vars set by CLI11).
int run_cmd_gridsearch();
/// IPC entry point — parse `payload` JSON, run the pipeline, emit events via `cb`.
/// Returns 0 on success.
int run_cmd_gridsearch_ipc(const std::string& payload,
const std::string& jobId,
const GridsearchCallbacks& cb,
bool daemon_mode = false,
const std::string& daemon_uid = "");
/// Core Pipeline
int run_pipeline(const PipelineOptions &opts, std::ostream *file_out,
const GridsearchCallbacks &cb);
/// UDS entry point — starts a persistent AF_UNIX / Named Pipe server that processes
/// concurrent jobs using Moodycamel ConcurrentQueue and Taskflow executor.
int run_cmd_gridsearch_uds(const std::string& pipe_path,
bool daemon_mode,
const std::string& daemon_uid);
} // namespace polymech