8357774b86
- CMakeLists.txt - app.md - main.cpp - panels.cpp - appicon.ico - autoextract_panel.cpp - picker_state.cpp - picker_state.h - py_subprocess.cpp - py_subprocess.h - ... Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
314 lines
10 KiB
C++
314 lines
10 KiB
C++
// recipes_panel — Listado de recetas (YAML) en
|
|
// projects/navegator/profiles/default/recipes/*.yaml.
|
|
//
|
|
// Acciones por fila:
|
|
// Run -> subprocess Python con cdp_extract_recipe (record_run=True).
|
|
// Edit -> abre InputTextMultiline con el YAML; "Save" reescribe.
|
|
// Delete -> rm + refresh list.
|
|
// Open in data_factory -> noop (placeholder; mostraria link/cmd).
|
|
|
|
#include "imgui.h"
|
|
#include "core/icons_tabler.h"
|
|
#include "core/tokens.h"
|
|
|
|
#include "py_subprocess.h"
|
|
#include "session_state.h"
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <mutex>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#ifdef _WIN32
|
|
# define WIN32_LEAN_AND_MEAN
|
|
# include <windows.h>
|
|
#else
|
|
# include <dirent.h>
|
|
# include <sys/stat.h>
|
|
#endif
|
|
|
|
namespace navegator {
|
|
|
|
namespace {
|
|
|
|
struct RecipeRow {
|
|
std::string name;
|
|
std::string url_pattern;
|
|
std::string yaml_path;
|
|
std::string last_run_status;
|
|
std::string last_run_at;
|
|
int rows_last_run = 0;
|
|
};
|
|
|
|
struct RecipesState {
|
|
std::mutex mu;
|
|
std::vector<RecipeRow> rows;
|
|
std::string status;
|
|
std::string last_error;
|
|
std::atomic<bool> busy{false};
|
|
int editing_idx = -1;
|
|
std::string edit_buf;
|
|
char edit_textarea[16384] = {0};
|
|
};
|
|
RecipesState g_rs;
|
|
|
|
std::string recipes_dir() {
|
|
std::string root = py_resolve_registry_root();
|
|
if (root.empty()) return "";
|
|
#ifdef _WIN32
|
|
return root + "\\projects\\navegator\\profiles\\default\\recipes";
|
|
#else
|
|
return root + "/projects/navegator/profiles/default/recipes";
|
|
#endif
|
|
}
|
|
|
|
std::string slurp(const std::string& path) {
|
|
std::ifstream f(path, std::ios::binary);
|
|
if (!f) return "";
|
|
std::ostringstream ss; ss << f.rdbuf();
|
|
return ss.str();
|
|
}
|
|
|
|
// Mini-parser YAML especifico: solo extrae name + url_pattern.
|
|
void parse_recipe_min(const std::string& body, RecipeRow& r) {
|
|
std::stringstream ss(body);
|
|
std::string line;
|
|
while (std::getline(ss, line)) {
|
|
auto strip = [](std::string s){
|
|
size_t a = s.find_first_not_of(" \t");
|
|
size_t b = s.find_last_not_of(" \t\r");
|
|
return (a == std::string::npos) ? std::string() : s.substr(a, b - a + 1);
|
|
};
|
|
if (line.rfind("name:", 0) == 0) {
|
|
r.name = strip(line.substr(5));
|
|
if (!r.name.empty() && (r.name.front()=='"' || r.name.front()=='\'')) {
|
|
r.name = r.name.substr(1, r.name.size() - 2);
|
|
}
|
|
} else if (line.rfind("url_pattern:", 0) == 0) {
|
|
r.url_pattern = strip(line.substr(12));
|
|
if (!r.url_pattern.empty() && (r.url_pattern.front()=='"' || r.url_pattern.front()=='\'')) {
|
|
r.url_pattern = r.url_pattern.substr(1, r.url_pattern.size() - 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> list_yaml_files(const std::string& dir) {
|
|
std::vector<std::string> out;
|
|
#ifdef _WIN32
|
|
std::string pattern = dir + "\\*.yaml";
|
|
WIN32_FIND_DATAA fd;
|
|
HANDLE h = FindFirstFileA(pattern.c_str(), &fd);
|
|
if (h == INVALID_HANDLE_VALUE) return out;
|
|
do {
|
|
out.push_back(dir + "\\" + fd.cFileName);
|
|
} while (FindNextFileA(h, &fd));
|
|
FindClose(h);
|
|
#else
|
|
DIR* d = opendir(dir.c_str());
|
|
if (!d) return out;
|
|
while (auto e = readdir(d)) {
|
|
std::string n = e->d_name;
|
|
if (n.size() > 5 && n.substr(n.size() - 5) == ".yaml") {
|
|
out.push_back(dir + "/" + n);
|
|
}
|
|
}
|
|
closedir(d);
|
|
#endif
|
|
std::sort(out.begin(), out.end());
|
|
return out;
|
|
}
|
|
|
|
void refresh_list() {
|
|
std::string dir = recipes_dir();
|
|
if (dir.empty()) {
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
g_rs.last_error = "FN_REGISTRY_ROOT not set";
|
|
return;
|
|
}
|
|
auto files = list_yaml_files(dir);
|
|
std::vector<RecipeRow> rows;
|
|
for (const auto& f : files) {
|
|
RecipeRow r; r.yaml_path = f;
|
|
std::string body = slurp(f);
|
|
parse_recipe_min(body, r);
|
|
if (r.name.empty()) {
|
|
// fallback al basename sin ext
|
|
size_t p1 = f.find_last_of("/\\");
|
|
std::string base = (p1 == std::string::npos) ? f : f.substr(p1 + 1);
|
|
if (base.size() > 5) base = base.substr(0, base.size() - 5);
|
|
r.name = base;
|
|
}
|
|
rows.push_back(std::move(r));
|
|
}
|
|
// Anota last_run_* desde data_factory.runs (subprocess sqlite3 best-effort).
|
|
// Lo dejamos como TODO — la primera version queda con campos vacios.
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
g_rs.rows = std::move(rows);
|
|
g_rs.last_error.clear();
|
|
g_rs.status = "Listed " + std::to_string(g_rs.rows.size()) + " recipes";
|
|
}
|
|
|
|
void run_recipe_async(const std::string& yaml_path) {
|
|
if (g_rs.busy.exchange(true)) return;
|
|
{
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
g_rs.status = "Running " + yaml_path;
|
|
}
|
|
std::thread([yaml_path]() {
|
|
const char* code = R"PY(
|
|
import sys, os, json, traceback
|
|
root = os.environ.get('FN_REGISTRY_ROOT', '')
|
|
if not root:
|
|
print(json.dumps({"error":"FN_REGISTRY_ROOT not set"})); sys.exit(2)
|
|
for sub in ('pipelines','core','infra'):
|
|
sys.path.insert(0, os.path.join(root, 'python', 'functions', sub))
|
|
try:
|
|
from cdp_extract_recipe import cdp_extract_recipe
|
|
path = sys.argv[1]
|
|
res = cdp_extract_recipe(path, debug_port=9222, record_run=True)
|
|
print(json.dumps(res if isinstance(res, dict) else {"result": res}))
|
|
except Exception as e:
|
|
print(json.dumps({"error": str(e), "trace": traceback.format_exc()})); sys.exit(1)
|
|
)PY";
|
|
std::vector<std::string> argv;
|
|
argv.push_back(py_resolve_interpreter());
|
|
argv.push_back("-c");
|
|
argv.push_back(code);
|
|
argv.push_back(yaml_path);
|
|
PyResult r = py_run(argv, 120000);
|
|
{
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
if (r.exit_code != 0) {
|
|
g_rs.last_error = r.error.empty() ? "python exited non-zero" : r.error;
|
|
g_rs.status = "Run failed";
|
|
} else {
|
|
g_rs.status = "Run OK: " + r.stdout_data.substr(0, 200);
|
|
}
|
|
}
|
|
g_rs.busy.store(false);
|
|
refresh_list();
|
|
}).detach();
|
|
}
|
|
|
|
void delete_recipe(const std::string& path) {
|
|
std::remove(path.c_str());
|
|
refresh_list();
|
|
}
|
|
|
|
} // anon
|
|
|
|
void render_recipes_panel(bool* p_open) {
|
|
if (!ImGui::Begin(TI_LIST_DETAILS " Recipes", p_open)) {
|
|
ImGui::End();
|
|
return;
|
|
}
|
|
|
|
if (ImGui::Button(TI_REFRESH " Refresh")) refresh_list();
|
|
ImGui::SameLine();
|
|
{
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
if (!g_rs.status.empty()) ImGui::Text("%s", g_rs.status.c_str());
|
|
if (!g_rs.last_error.empty()) {
|
|
ImGui::PushStyleColor(ImGuiCol_Text, fn_tokens::colors::error);
|
|
ImGui::TextWrapped("Error: %s", g_rs.last_error.c_str());
|
|
ImGui::PopStyleColor();
|
|
}
|
|
}
|
|
ImGui::Separator();
|
|
|
|
std::vector<RecipeRow> rows_copy;
|
|
int editing_idx = -1;
|
|
{
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
rows_copy = g_rs.rows;
|
|
editing_idx = g_rs.editing_idx;
|
|
}
|
|
|
|
if (rows_copy.empty()) {
|
|
ImGui::TextDisabled("No recipes in projects/navegator/profiles/default/recipes/.");
|
|
ImGui::TextDisabled("Use AutoExtract panel to create one.");
|
|
} else if (ImGui::BeginTable("##recipes_tbl", 6,
|
|
ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) {
|
|
ImGui::TableSetupColumn("name");
|
|
ImGui::TableSetupColumn("url_pattern");
|
|
ImGui::TableSetupColumn("last_status");
|
|
ImGui::TableSetupColumn("last_at");
|
|
ImGui::TableSetupColumn("rows");
|
|
ImGui::TableSetupColumn("actions");
|
|
ImGui::TableHeadersRow();
|
|
|
|
for (size_t i = 0; i < rows_copy.size(); ++i) {
|
|
const RecipeRow& r = rows_copy[i];
|
|
ImGui::TableNextRow();
|
|
ImGui::PushID((int)i);
|
|
ImGui::TableNextColumn();
|
|
ImGui::TextUnformatted(r.name.c_str());
|
|
ImGui::TableNextColumn();
|
|
ImGui::TextWrapped("%s", r.url_pattern.c_str());
|
|
ImGui::TableNextColumn();
|
|
ImGui::TextUnformatted(r.last_run_status.empty() ? "-" : r.last_run_status.c_str());
|
|
ImGui::TableNextColumn();
|
|
ImGui::TextUnformatted(r.last_run_at.empty() ? "-" : r.last_run_at.c_str());
|
|
ImGui::TableNextColumn();
|
|
ImGui::Text("%d", r.rows_last_run);
|
|
ImGui::TableNextColumn();
|
|
if (ImGui::SmallButton("Run")) run_recipe_async(r.yaml_path);
|
|
ImGui::SameLine();
|
|
if (ImGui::SmallButton("Edit")) {
|
|
std::string body = slurp(r.yaml_path);
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
g_rs.editing_idx = (int)i;
|
|
g_rs.edit_buf = body;
|
|
std::snprintf(g_rs.edit_textarea, sizeof(g_rs.edit_textarea),
|
|
"%s", body.c_str());
|
|
}
|
|
ImGui::SameLine();
|
|
if (ImGui::SmallButton("Delete")) delete_recipe(r.yaml_path);
|
|
ImGui::SameLine();
|
|
if (ImGui::SmallButton("Open in data_factory")) {
|
|
// placeholder — solo loguea
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
g_rs.status = "open in data_factory: " + r.name + " (not wired)";
|
|
}
|
|
ImGui::PopID();
|
|
}
|
|
ImGui::EndTable();
|
|
}
|
|
|
|
if (editing_idx >= 0 && editing_idx < (int)rows_copy.size()) {
|
|
ImGui::Separator();
|
|
ImGui::Text("Editing: %s", rows_copy[editing_idx].yaml_path.c_str());
|
|
ImGui::InputTextMultiline("##rec_edit", g_rs.edit_textarea,
|
|
sizeof(g_rs.edit_textarea),
|
|
ImVec2(-1, 220));
|
|
if (ImGui::Button(TI_DEVICE_FLOPPY " Save")) {
|
|
std::ofstream f(rows_copy[editing_idx].yaml_path, std::ios::binary);
|
|
if (f) {
|
|
f << g_rs.edit_textarea;
|
|
f.close();
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
g_rs.status = "Saved " + rows_copy[editing_idx].yaml_path;
|
|
g_rs.editing_idx = -1;
|
|
}
|
|
refresh_list();
|
|
}
|
|
ImGui::SameLine();
|
|
if (ImGui::Button("Cancel")) {
|
|
std::lock_guard<std::mutex> lk(g_rs.mu);
|
|
g_rs.editing_idx = -1;
|
|
}
|
|
}
|
|
|
|
ImGui::End();
|
|
}
|
|
|
|
} // namespace navegator
|