拓扑图

This commit is contained in:
2026-08-10 00:08:47 +08:00
parent 14ad97c7c8
commit 39966da920
10 changed files with 387 additions and 436 deletions
+202 -198
View File
@@ -6,6 +6,7 @@
#include "adminive/adapters/nlohmann_json.hpp"
#include <algorithm>
#include <charconv>
#include <cmath>
#include <cstdint>
#include <map>
#include <memory>
@@ -28,11 +29,6 @@ enum class Data_Feed_Type {
Data_Feed_TCP_Client,
Data_Feed_UDP_Client
};
enum class Source_Feed_Relation_Type {
One_to_One_Relation,
Name_One_To_Many_Relation,
First_Source_To_All_Feed_Relation
};
struct Data_Source_Row {
std::string key;
Data_Source_Type type = Data_Source_Type::TCP_Client_Data_Source;
@@ -56,6 +52,7 @@ struct Data_Feed_Row {
std::string key;
Data_Feed_Type type = Data_Feed_Type::Data_Feed_TCP_Server;
bool enable{};
std::string source_key;
Output_Format output_format;
Output_Data_Format format = Output_Data_Format::BIN;
std::uint16_t port{};
@@ -64,14 +61,6 @@ struct Data_Feed_Row {
std::size_t connect_system_buffer_size = 409600;
std::string state;
};
struct Source_Feed_Relation_Row {
std::string key;
Source_Feed_Relation_Type type = Source_Feed_Relation_Type::One_to_One_Relation;
bool enable{};
std::string source_key;
std::string feed_key;
std::string feed_name;
};
struct External_Resource_Row {
std::string name;
std::string description;
@@ -145,7 +134,7 @@ struct Type_View_Descriptor<ecap::adminive_config::Data_Source_Row> {
.create_title("添加数据源")
.edit_title("编辑")
.delete_title("删除")
.delete_confirmation("确定删除该数据源?存在关系依赖时后端会拒绝删除。")
.delete_confirmation("确定删除该数据源?仍有数据馈送连接时后端会拒绝删除。")
.reorderable()
.create_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>()), use<&T::config>(), grid(2, use<&T::ip>(), use<&T::port>(), use<&T::port_name>(), use<&T::baud_rate>(), use<&T::file_path>(), use<&T::data_type>(), use<&T::play_mode>(), use<&T::library_path>(), use<&T::function_name>(), use<&T::buffer_size>(), use<&T::shared_memory_name>(), use<&T::shared_memory_size>())))
.edit_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>(), use<&T::state>()), use<&T::config>(), grid(2, use<&T::ip>(), use<&T::port>(), use<&T::port_name>(), use<&T::baud_rate>(), use<&T::file_path>(), use<&T::data_type>(), use<&T::play_mode>(), use<&T::library_path>(), use<&T::function_name>(), use<&T::buffer_size>(), use<&T::shared_memory_name>(), use<&T::shared_memory_size>())));
@@ -165,6 +154,7 @@ struct Type_Descriptor<ecap::adminive_config::Data_Feed_Row> {
ADMINIVE_FIELD_LABEL(T, key, "名称").creatable().required().text_input(),
type,
ADMINIVE_FIELD_LABEL(T, enable, "启用").creatable().editable().boolean_input(),
ADMINIVE_FIELD_LABEL(T, source_key, "连接的数据源").creatable().editable().select_input(),
ADMINIVE_FIELD_LABEL(T, output_format, "输出格式").creatable().editable(),
ADMINIVE_FIELD_LABEL(T, format, "数据格式").select_input(),
ADMINIVE_FIELD_LABEL(T, port, "端口").creatable().editable().number_input(),
@@ -178,50 +168,16 @@ template <>
struct Type_View_Descriptor<ecap::adminive_config::Data_Feed_Row> {
static Table_View table() {
using T = ecap::adminive_config::Data_Feed_Row;
return table_view<T>(column<&T::key>("名称").search(), column<&T::type>("类型").filter(), column<&T::format>("输出格式").filter(), column<&T::port>("端口").sort(), column<&T::enable>("启用").filter(), column<&T::state>("状态"))
return table_view<T>(column<&T::key>("名称").search(), column<&T::type>("类型").filter(), column<&T::source_key>("连接的数据源").filter(), column<&T::format>("输出格式").filter(), column<&T::port>("端口").sort(), column<&T::enable>("启用").filter(), column<&T::state>("状态"))
.titled("数据馈送")
.id_title("序号")
.create_title("添加数据馈送")
.edit_title("编辑")
.delete_title("删除")
.delete_confirmation("确定删除该数据馈送?存在关系依赖时后端会拒绝删除。")
.delete_confirmation("确定删除该数据馈送?对应拓扑连接会一并删除。")
.reorderable()
.create_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>()), use<&T::output_format>(), grid(2, use<&T::url>(), use<&T::port>(), use<&T::connect_user_buffer_size>(), use<&T::connect_system_buffer_size>())))
.edit_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>(), use<&T::state>()), use<&T::output_format>(), grid(2, use<&T::url>(), use<&T::port>(), use<&T::connect_user_buffer_size>(), use<&T::connect_system_buffer_size>())));
}
};
template <>
struct Type_Descriptor<ecap::adminive_config::Source_Feed_Relation_Row> {
static auto get() {
using T = ecap::adminive_config::Source_Feed_Relation_Row;
using E = ecap::adminive_config::Source_Feed_Relation_Type;
auto type = ADMINIVE_FIELD_LABEL(T, type, "关系类型").creatable().required().select_input();
type = type.enum_label<E::One_to_One_Relation>("一对一")
.enum_label<E::Name_One_To_Many_Relation>("按名称一对多")
.enum_label<E::First_Source_To_All_Feed_Relation>("默认:首个数据源到未匹配馈送");
return object<T>("source_feed_relation", "数据源-数据馈送关系",
ADMINIVE_FIELD_LABEL(T, key, "关系名称").creatable().required().text_input(),
type,
ADMINIVE_FIELD_LABEL(T, enable, "启用").creatable().editable().boolean_input(),
ADMINIVE_FIELD_LABEL(T, source_key, "数据源名称").creatable().editable().text_input().visible_on("${$self.type == 'One_to_One_Relation' || $self.type == 'Name_One_To_Many_Relation'}"),
ADMINIVE_FIELD_LABEL(T, feed_key, "数据馈送名称").creatable().editable().text_input().visible_on("${$self.type == 'One_to_One_Relation'}"),
ADMINIVE_FIELD_LABEL(T, feed_name, "数据馈送名称前缀").description("按名称扫描全部数据馈送;为空时使用数据源名称作为前缀。默认关系只接管前面规则没有匹配到的数据馈送。").creatable().editable().text_input().visible_on("${$self.type == 'Name_One_To_Many_Relation'}"));
}
};
template <>
struct Type_View_Descriptor<ecap::adminive_config::Source_Feed_Relation_Row> {
static Table_View table() {
using T = ecap::adminive_config::Source_Feed_Relation_Row;
return table_view<T>(column<&T::key>("关系名称").search(), column<&T::type>("类型").filter(), column<&T::source_key>("数据源"), column<&T::feed_key>("数据馈送"), column<&T::feed_name>("名称前缀"), column<&T::enable>("启用").filter())
.titled("数据源-数据馈送关系")
.id_title("序号")
.create_title("添加关系")
.edit_title("编辑")
.delete_title("删除")
.delete_confirmation("确定删除该关系?")
.reorderable()
.create_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>()), grid(2, use<&T::source_key>(), use<&T::feed_key>(), use<&T::feed_name>())))
.edit_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>()), grid(2, use<&T::source_key>(), use<&T::feed_key>(), use<&T::feed_name>())));
.create_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>(), use<&T::source_key>()), use<&T::output_format>(), grid(2, use<&T::url>(), use<&T::port>(), use<&T::connect_user_buffer_size>(), use<&T::connect_system_buffer_size>())))
.edit_layout(vertical(grid(2, use<&T::key>(), use<&T::type>(), use<&T::enable>(), use<&T::source_key>(), use<&T::state>()), use<&T::output_format>(), grid(2, use<&T::url>(), use<&T::port>(), use<&T::connect_user_buffer_size>(), use<&T::connect_system_buffer_size>())));
}
};
template <>
@@ -264,6 +220,7 @@ struct Type_View_Descriptor<MLAT_Config_Data> {
namespace ecap::adminive_config {
namespace {
using Json = nlohmann::json;
const std::string data_topology_path = "/api/adminive/config/data_topology";
class Conflict_Error final : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
@@ -330,21 +287,10 @@ adminive::Resource_Transaction<Map_View_Config> map_view_transaction(Global* glo
return transaction;
}
void save_data_sources(Global* global) {
auto patch = Psc::JSON::object();
patch.append({"data_source", global->mode_acs.data_source_config.to_json()});
Config::merge(Config_Section::mode_acs, patch);
Config::save(Config_Section::mode_acs, global->mode_acs.to_base_json());
}
void save_data_feeds(Global* global) {
auto feed = Psc::JSON::object();
feed.append({"list", global->mode_acs.data_feed_config.list()});
auto patch = Psc::JSON::object();
patch.append({"data_feed", feed});
Config::merge(Config_Section::mode_acs, patch);
}
void save_source_feed_relations(Global* global) {
auto patch = Psc::JSON::object();
patch.append({"source_feed_relation", global->mode_acs.source_feed_relation_config.to_json()});
Config::merge(Config_Section::mode_acs, patch);
Config::save(Config_Section::mode_acs, global->mode_acs.to_base_json());
}
template <class Function>
adminive::Http_Response<Json> business_response(Function&& function) noexcept {
@@ -373,6 +319,48 @@ Json read_request_body(std::string_view body) {
throw Bad_Request_Error(error.what());
}
}
Data_Topology_View_Config read_topology_view(Json input, Global* global) {
if(input.value("protocol", std::string{}) != "ecap.data-topology-view" || input.value("protocol_version", 0) != 1)
throw std::invalid_argument("数据拓扑视图协议或版本不受支持");
const auto finite_number = [&input](std::string_view name) {
const auto field = input.find(std::string(name));
if(field == input.end() || !field->is_number())
throw std::invalid_argument("数据拓扑视图缺少数值字段: " + std::string(name));
const auto value = field->get<double>();
if(!std::isfinite(value))
throw std::invalid_argument("数据拓扑视图字段不是有限数值: " + std::string(name));
return value;
};
Data_Topology_View_Config result;
result.initialized = true;
result.zoom = finite_number("zoom");
result.pan_x = finite_number("pan_x");
result.pan_y = finite_number("pan_y");
if(result.zoom <= 0.0 || result.zoom > 100.0)
throw std::invalid_argument("数据拓扑视图参数无效");
std::map<std::string, bool> valid_nodes;
for(const auto& source : global->mode_acs.data_source_config.map.list())
valid_nodes.emplace("data_source:" + source->key, true);
for(const auto& feed : global->mode_acs.data_feed_config.map.list())
valid_nodes.emplace("data_feed:" + feed->key, true);
const auto positions = input.find("node_positions");
if(positions == input.end() || !positions->is_object())
throw std::invalid_argument("节点位置必须是对象");
for(const auto& [id, position] : positions->items()) {
if(!valid_nodes.contains(id) || !position.is_object())
continue;
const auto x_field = position.find("x");
const auto y_field = position.find("y");
if(x_field == position.end() || y_field == position.end() || !x_field->is_number() || !y_field->is_number())
throw std::invalid_argument("节点位置缺少数值坐标: " + id);
const auto x = x_field->get<double>();
const auto y = y_field->get<double>();
if(!std::isfinite(x) || !std::isfinite(y))
throw std::invalid_argument("节点位置不是有限数值: " + id);
result.node_positions.emplace(id, Data_Topology_Node_Position{x, y});
}
return result;
}
template <class Function>
void complete_business_request(std::function<void(const drogon::HttpResponsePtr&)>& callback, Function&& function) noexcept {
callback(adminive::make_drogon_response(business_response(std::forward<Function>(function))));
@@ -444,9 +432,6 @@ std::string source_type_name(Data_Source_Type value) {
std::string feed_type_name(Data_Feed_Type value) {
return std::string(magic_enum::enum_name(value));
}
std::string relation_type_name(Source_Feed_Relation_Type value) {
return std::string(magic_enum::enum_name(value));
}
Data_Source_Type source_type(std::string_view value) {
const auto result = magic_enum::enum_cast<Data_Source_Type>(value);
if(!result)
@@ -459,12 +444,6 @@ Data_Feed_Type feed_type(std::string_view value) {
throw std::invalid_argument("未知数据馈送类型: " + std::string(value));
return *result;
}
Source_Feed_Relation_Type relation_type(std::string_view value) {
const auto result = magic_enum::enum_cast<Source_Feed_Relation_Type>(value);
if(!result)
throw std::invalid_argument("未知关系类型: " + std::string(value));
return *result;
}
Data_Source_Row source_row(const std::shared_ptr<Data_Source>& source) {
Data_Source_Row row;
row.key = source->key;
@@ -548,6 +527,7 @@ Data_Feed_Row feed_row(const std::shared_ptr<Data_Feed>& feed) {
row.key = feed->key;
row.type = feed_type(feed->type);
row.enable = feed->enabled();
row.source_key = feed->source_key;
feed->output_format.read([&](const auto& value) {
row.output_format = value;
row.format = value.type;
@@ -578,6 +558,7 @@ Data_Feed_Row feed_row(const std::shared_ptr<Data_Feed>& feed) {
}
void apply_feed_row(const std::shared_ptr<Data_Feed>& feed, const Data_Feed_Row& row) {
feed->set_enabled(row.enable);
feed->source_key = row.source_key;
feed->output_format.write([&](auto& value) {
value = row.output_format;
});
@@ -603,26 +584,6 @@ void apply_feed_row(const std::shared_ptr<Data_Feed>& feed, const Data_Feed_Row&
});
}
}
Source_Feed_Relation_Row relation_row(const std::shared_ptr<Source_Feed_Relation>& relation) {
Source_Feed_Relation_Row row;
row.key = relation->key;
row.type = relation_type(relation->type);
relation->settings.read([&](const auto& value) {
row.enable = value.enable;
row.source_key = value.source_key;
row.feed_key = value.feed_key;
row.feed_name = value.feed_name;
});
return row;
}
void apply_relation_row(const std::shared_ptr<Source_Feed_Relation>& relation, const Source_Feed_Relation_Row& row) {
relation->settings.write([&](auto& value) {
value.enable = row.enable;
value.source_key = row.source_key;
value.feed_key = row.feed_key;
value.feed_name = row.feed_name;
});
}
template <class Row>
adminive::Http_Response<Json> list_response(std::string path, std::vector<Row> rows, adminive::Collection_Query query) {
adminive::Collection_Service<Row, Json> collection(std::move(path), std::move(rows));
@@ -668,11 +629,43 @@ std::vector<Data_Feed_Row> feed_rows(Global* global) {
rows.push_back(feed_row(feed));
return rows;
}
std::vector<Source_Feed_Relation_Row> relation_rows(Global* global) {
std::vector<Source_Feed_Relation_Row> rows;
for(const auto& relation : global->mode_acs.source_feed_relation_config.map.list())
rows.push_back(relation_row(relation));
return rows;
Json data_topology_view(Global* global) {
auto result = global->mode_acs.data_topology_view.read([](const auto& value) {
return adminive::parse_json<Json>(value.to_json().to_json_string());
});
result["protocol"] = "ecap.data-topology-view";
result["protocol_version"] = 1;
return result;
}
Json data_topology(Global* global) {
const auto sources = global->mode_acs.data_source_config.map.list();
const auto feeds = global->mode_acs.data_feed_config.map.list();
std::map<std::string, std::shared_ptr<Data_Source>> source_by_key;
Json nodes = Json::array();
Json edges = Json::array();
for(std::size_t index = 0; index < sources.size(); ++index) {
const auto& source = sources[index];
const auto id = "data_source:" + source->key;
source_by_key.emplace(source->key, source);
nodes.push_back(Json{{"id", id}, {"kind", "data_source"}, {"entity_id", index + 1},
{"key", source->key}, {"label", source->key}, {"type", source->type},
{"enabled", source->enabled()}, {"status_api", data_topology_path + "/status/data_source/" + std::to_string(index + 1)}});
}
for(std::size_t index = 0; index < feeds.size(); ++index) {
const auto& feed = feeds[index];
const auto id = "data_feed:" + feed->key;
nodes.push_back(Json{{"id", id}, {"kind", "data_feed"}, {"entity_id", index + 1},
{"key", feed->key}, {"label", feed->key}, {"type", feed->type},
{"enabled", feed->enabled()}, {"status_api", data_topology_path + "/status/data_feed/" + std::to_string(index + 1)}});
const auto source = source_by_key.find(feed->source_key);
if(source != source_by_key.end()) {
edges.push_back(Json{{"id", "source-feed:" + feed->key},
{"source", "data_source:" + feed->source_key}, {"target", id},
{"active", source->second->enabled() && feed->enabled()}});
}
}
return Json{{"protocol", "ecap.data-topology"}, {"protocol_version", 1},
{"nodes", std::move(nodes)}, {"edges", std::move(edges)}, {"view", data_topology_view(global)}};
}
std::vector<External_Resource_Row> external_resource_rows(Global* global) {
std::map<std::string, External_Resource_Status> status_by_name;
@@ -705,11 +698,55 @@ std::vector<External_Resource_Row> external_resource_rows(Global* global) {
Json source_amis_schema() {
return adminive::to_amis_table_schema<Json, Data_Source_Row>("/api/adminive/config/data_sources", adminive::describe_table_view<Data_Source_Row>());
}
Json feed_amis_schema() {
return adminive::to_amis_table_schema<Json, Data_Feed_Row>("/api/adminive/config/data_feeds", adminive::describe_table_view<Data_Feed_Row>());
void set_select_options(Json& value, std::string_view field_name, const Json& options) {
if(value.is_object()) {
const auto name = value.find("name");
if(name != value.end() && name->is_string() && name->get<std::string>() == field_name) {
value["type"] = "select";
value["options"] = options;
value["clearable"] = true;
value["placeholder"] = "未连接";
}
for(auto& [key, child] : value.items()) {
static_cast<void>(key);
set_select_options(child, field_name, options);
}
} else if(value.is_array()) {
for(auto& child : value)
set_select_options(child, field_name, options);
}
}
Json relation_amis_schema() {
return adminive::to_amis_table_schema<Json, Source_Feed_Relation_Row>("/api/adminive/config/source_feed_relations", adminive::describe_table_view<Source_Feed_Relation_Row>());
Json feed_amis_schema(Global* global) {
auto schema = adminive::to_amis_table_schema<Json, Data_Feed_Row>("/api/adminive/config/data_feeds", adminive::describe_table_view<Data_Feed_Row>());
Json options = Json::array();
for(const auto& source : global->mode_acs.data_source_config.map.list())
options.push_back(Json{{"label", source->key}, {"value", source->key}});
set_select_options(schema, "source_key", options);
return schema;
}
Json data_topology_schema(Global* global) {
return Json{
{"title", "数据源与数据馈送拓扑"},
{"data_api", data_topology_path},
{"view_api", data_topology_path + "/view"},
{"graph", Json{
{"height", 620},
{"layout", Json{{"type", "dagre"}, {"rankdir", "LR"}, {"align", "UL"}, {"nodesep", 34}, {"ranksep", 100}}},
{"behaviors", Json::array({"drag-canvas", "zoom-canvas", "drag-element"})},
{"active_edge_color", "#52c41a"},
{"inactive_edge_color", "#bfbfbf"}
}},
{"node_kinds", Json::array({
Json{{"kind", "data_source"}, {"title", "数据源"}, {"fill", "#e6f4ff"}, {"stroke", "#1677ff"}},
Json{{"kind", "data_feed"}, {"title", "数据馈送"}, {"fill", "#f6ffed"}, {"stroke", "#52c41a"}}
})},
{"status", Json{{"title", "运行状态"}, {"poll_interval_ms", 1000},
{"collapsed_hint", "展开后才向后台轮询,收起立即停止"}}},
{"editors", Json::array({
Json{{"kind", "data_source"}, {"title", "数据源管理"}, {"button_label", "管理数据源"}, {"schema", source_amis_schema()}},
Json{{"kind", "data_feed"}, {"title", "数据馈送管理"}, {"button_label", "管理数据馈送"}, {"schema", feed_amis_schema(global)}}
})}
};
}
Json external_resource_amis_schema() {
auto schema = adminive::to_amis_table_schema<Json, External_Resource_Row>("/api/adminive/config/external_resources", adminive::describe_table_view<External_Resource_Row>());
@@ -843,7 +880,7 @@ public:
cesium_graphics_resource_.bind(app);
bind_data_sources(app);
bind_data_feeds(app);
bind_relations(app);
bind_data_topology(app);
bind_external_resources(app);
bind_backend_config(app);
app.registerHandler(manifest_path, [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
@@ -851,6 +888,51 @@ public:
}, {drogon::Get});
}
private:
void bind_data_topology(drogon::HttpAppFramework& app) {
const auto get = adminive::make_drogon_constraints(drogon::Get, {});
const auto put = adminive::make_drogon_constraints(drogon::Put, {});
app.registerHandler(data_topology_path + "/schema", [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
complete_business_request(callback, [this] {
std::lock_guard lock(topology_mutex_);
return adminive::make_http_success<Json>(data_topology_schema(global_));
});
}, get);
app.registerHandler(data_topology_path, [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
complete_business_request(callback, [this] {
std::lock_guard lock(topology_mutex_);
return adminive::make_http_success<Json>(data_topology(global_));
});
}, get);
app.registerHandler(data_topology_path + "/view", [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
complete_business_request(callback, [this] {
std::lock_guard lock(topology_mutex_);
return adminive::make_http_success<Json>(data_topology_view(global_));
});
}, get);
app.registerHandler(data_topology_path + "/view", [this](const drogon::HttpRequestPtr& request, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
complete_business_request(callback, [this, body = std::string(request->getBody())] {
std::lock_guard lock(topology_mutex_);
auto value = read_topology_view(read_request_body(body), global_);
global_->mode_acs.data_topology_view.write([&](auto& current) { current = std::move(value); });
Config::save(Config_Section::mode_acs, global_->mode_acs.to_base_json());
return adminive::make_http_success<Json>(data_topology_view(global_), "saved");
});
}, put);
app.registerHandler(data_topology_path + "/status/data_source/{1:id}", [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback, std::uint64_t id) {
complete_business_request(callback, [this, id] {
std::lock_guard lock(topology_mutex_);
const auto source = item_by_id(global_->mode_acs.data_source_config.map.list(), id);
return adminive::make_http_success<Json>(adminive::parse_json<Json>(source->get_state().to_json_string()));
});
}, get);
app.registerHandler(data_topology_path + "/status/data_feed/{1:id}", [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback, std::uint64_t id) {
complete_business_request(callback, [this, id] {
std::lock_guard lock(topology_mutex_);
const auto feed = item_by_id(global_->mode_acs.data_feed_config.map.list(), id);
return adminive::make_http_success<Json>(adminive::parse_json<Json>(feed->get_state_json().to_json_string()));
});
}, get);
}
void bind_backend_config(drogon::HttpAppFramework& app) {
const auto get = adminive::make_drogon_constraints(drogon::Get, {});
app.registerHandler(backend_config_path, [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
@@ -885,7 +967,7 @@ private:
source->key = row.key;
apply_source_row(source, row);
global_->mode_acs.data_source_config.map.push_back(source);
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_sources(global_);
return adminive::make_http_success<Json>(adminive::to_frontend_json<Json>(source_row(source)), "created");
});
@@ -901,7 +983,7 @@ private:
if(!result.success)
return adminive::make_http_error<Json>(422, result);
apply_source_row(source, row);
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_sources(global_);
return adminive::make_http_success<Json>(adminive::to_frontend_json<Json>(source_row(source)), result.message);
});
@@ -913,12 +995,12 @@ private:
std::lock_guard lock(topology_mutex_);
const auto list = global_->mode_acs.data_source_config.map.list();
auto source = item_by_id(list, id);
if(global_->mode_acs.source_feed_relation_config.source_has_dependency(source->key))
throw Conflict_Error("数据源存在数据馈送关系依赖,不能删除: " + source->key);
if(global_->mode_acs.data_feed_config.source_has_connection(source->key))
throw Conflict_Error("数据源仍被数据馈送连接,不能删除: " + source->key);
source->async_stop();
if(!global_->mode_acs.data_source_config.map.remove(id - 1))
throw std::out_of_range("record not found");
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_sources(global_);
return adminive::make_http_success<Json>(adminive::json_object<Json>(), "deleted");
});
@@ -930,7 +1012,7 @@ private:
const auto keys = ordered_keys(list, read_order_ids(body));
if(!global_->mode_acs.data_source_config.map.set_order(keys))
throw std::invalid_argument("invalid order");
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_sources(global_);
return adminive::make_http_success<Json>(adminive::json_object<Json>(), "reordered");
});
@@ -943,7 +1025,7 @@ private:
const auto remove = adminive::make_drogon_constraints(drogon::Delete, {});
bind_collection_read_handlers<Data_Feed_Row>(app, data_feeds_path,
[] { return adminive::to_descriptor_json<Json, Data_Feed_Row>(); },
[this] { return feed_rows(global_); }, [] { return feed_amis_schema(); }, true);
[this] { return feed_rows(global_); }, [this] { return feed_amis_schema(global_); }, true);
app.registerHandler(data_feeds_path, [this](const drogon::HttpRequestPtr& request, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
complete_business_request(callback, [this, body = std::string(request->getBody())] {
std::lock_guard lock(topology_mutex_);
@@ -954,11 +1036,13 @@ private:
return adminive::make_http_error<Json>(422, result);
if(global_->mode_acs.data_feed_config.map.get(row.key))
return adminive::make_http_error<Json>(409, "数据馈送名称已存在: " + row.key);
if(!row.source_key.empty() && !global_->mode_acs.data_source_config.map.get(row.source_key))
throw std::invalid_argument("连接的数据源不存在: " + row.source_key);
auto feed = create_data_feed_from_type(feed_type_name(row.type));
feed->key = row.key;
apply_feed_row(feed, row);
global_->mode_acs.data_feed_config.map.push_back(feed);
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_feeds(global_);
return adminive::make_http_success<Json>(adminive::to_frontend_json<Json>(feed_row(feed)), "created");
});
@@ -973,8 +1057,10 @@ private:
const auto result = adminive::apply_frontend_patch<Json>(row, input);
if(!result.success)
return adminive::make_http_error<Json>(422, result);
if(!row.source_key.empty() && !global_->mode_acs.data_source_config.map.get(row.source_key))
throw std::invalid_argument("连接的数据源不存在: " + row.source_key);
apply_feed_row(feed, row);
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_feeds(global_);
return adminive::make_http_success<Json>(adminive::to_frontend_json<Json>(feed_row(feed)), result.message);
});
@@ -986,12 +1072,10 @@ private:
std::lock_guard lock(topology_mutex_);
const auto list = global_->mode_acs.data_feed_config.map.list();
auto feed = item_by_id(list, id);
if(global_->mode_acs.source_feed_relation_config.feed_has_dependency(feed->key))
throw Conflict_Error("数据馈送存在数据源关系依赖,不能删除: " + feed->key);
feed->async_stop();
if(!global_->mode_acs.data_feed_config.map.remove(id - 1))
throw std::out_of_range("record not found");
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_feeds(global_);
return adminive::make_http_success<Json>(adminive::json_object<Json>(), "deleted");
});
@@ -1003,93 +1087,17 @@ private:
const auto keys = ordered_keys(list, read_order_ids(body));
if(!global_->mode_acs.data_feed_config.map.set_order(keys))
throw std::invalid_argument("invalid order");
Source_Feed_Relation_Config::set_need_refresh();
Data_feed_Config::set_connections_need_refresh();
save_data_feeds(global_);
return adminive::make_http_success<Json>(adminive::json_object<Json>(), "reordered");
});
}, post);
}
void bind_relations(drogon::HttpAppFramework& app) {
const auto post = adminive::make_drogon_constraints(drogon::Post, {});
const auto put = adminive::make_drogon_constraints(drogon::Put, {});
const auto patch = adminive::make_drogon_constraints(drogon::Patch, {});
const auto remove = adminive::make_drogon_constraints(drogon::Delete, {});
bind_collection_read_handlers<Source_Feed_Relation_Row>(app, relations_path,
[] { return adminive::to_descriptor_json<Json, Source_Feed_Relation_Row>(); },
[this] { return relation_rows(global_); }, [] { return relation_amis_schema(); }, true);
app.registerHandler(relations_path, [this](const drogon::HttpRequestPtr& request, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
complete_business_request(callback, [this, body = std::string(request->getBody())] {
std::lock_guard lock(topology_mutex_);
auto input = read_request_body(body);
Source_Feed_Relation_Row row;
const auto result = adminive::apply_frontend_create<Json>(row, input);
if(!result.success)
return adminive::make_http_error<Json>(422, result);
auto relation = create_source_feed_relation_from_type(relation_type_name(row.type));
relation->key = row.key;
apply_relation_row(relation, row);
global_->mode_acs.source_feed_relation_config.validate(*relation);
global_->mode_acs.source_feed_relation_config.map.push_back(relation);
Source_Feed_Relation_Config::set_need_refresh();
save_source_feed_relations(global_);
return adminive::make_http_success<Json>(adminive::to_frontend_json<Json>(relation_row(relation)), "created");
});
}, post);
const auto update = [this](const drogon::HttpRequestPtr& request, std::function<void(const drogon::HttpResponsePtr&)>&& callback, std::uint64_t id) {
complete_business_request(callback, [this, id, body = std::string(request->getBody())] {
std::lock_guard lock(topology_mutex_);
const auto list = global_->mode_acs.source_feed_relation_config.map.list();
auto relation = item_by_id(list, id);
auto input = read_request_body(body);
auto row = relation_row(relation);
const auto result = adminive::apply_frontend_patch<Json>(row, input);
if(!result.success)
return adminive::make_http_error<Json>(422, result);
auto candidate = create_source_feed_relation_from_type(relation->type);
candidate->key = relation->key;
apply_relation_row(candidate, row);
global_->mode_acs.source_feed_relation_config.validate(*candidate, relation->key);
apply_relation_row(relation, row);
Source_Feed_Relation_Config::set_need_refresh();
save_source_feed_relations(global_);
return adminive::make_http_success<Json>(adminive::to_frontend_json<Json>(relation_row(relation)), result.message);
});
};
app.registerHandler(relations_path + "/{1:id}", update, put);
app.registerHandler(relations_path + "/{1:id}", update, patch);
app.registerHandler(relations_path + "/{1:id}", [this](const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback, std::uint64_t id) {
complete_business_request(callback, [this, id] {
std::lock_guard lock(topology_mutex_);
const auto list = global_->mode_acs.source_feed_relation_config.map.list();
item_by_id(list, id);
if(!global_->mode_acs.source_feed_relation_config.map.remove(id - 1))
throw std::out_of_range("record not found");
Source_Feed_Relation_Config::set_need_refresh();
save_source_feed_relations(global_);
return adminive::make_http_success<Json>(adminive::json_object<Json>(), "deleted");
});
}, remove);
app.registerHandler(relations_path + "/order", [this](const drogon::HttpRequestPtr& request, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
complete_business_request(callback, [this, body = std::string(request->getBody())] {
std::lock_guard lock(topology_mutex_);
const auto list = global_->mode_acs.source_feed_relation_config.map.list();
const auto keys = ordered_keys(list, read_order_ids(body));
if(!global_->mode_acs.source_feed_relation_config.map.set_order(keys))
throw std::invalid_argument("invalid order");
Source_Feed_Relation_Config::set_need_refresh();
save_source_feed_relations(global_);
return adminive::make_http_success<Json>(adminive::json_object<Json>(), "reordered");
});
}, post);
}
Json manifest() const {
const auto view = adminive::composition_view("ecap_config", adminive::compose::frontend(adminive::compose::slot("mode_acs"), adminive::compose::slot("data_feed_settings"), adminive::compose::slot("data_sources"), adminive::compose::slot("data_feeds"), adminive::compose::slot("source_feed_relations"), adminive::compose::slot("mlat"), adminive::compose::slot("map_view"), adminive::compose::slot("cesium_graphics"), adminive::compose::slot("external_resources"), adminive::compose::slot("archive_operations"), adminive::compose::slot("cesium_models"), adminive::compose::slot("backend_config"), adminive::compose::slot("system_actions"))).titled("ECAP 配置");
const auto view = adminive::composition_view("ecap_config", adminive::compose::frontend(adminive::compose::slot("mode_acs"), adminive::compose::slot("data_feed_settings"), adminive::compose::slot("mlat"), adminive::compose::slot("map_view"), adminive::compose::slot("cesium_graphics"), adminive::compose::slot("external_resources"), adminive::compose::slot("archive_operations"), adminive::compose::slot("cesium_models"), adminive::compose::slot("backend_config"), adminive::compose::slot("system_actions"))).titled("ECAP 配置");
const std::vector<adminive::Composition_Slot_Contract> contracts = {
{"mode_acs", "amis_schema", mode_acs_path + "/descriptor", mode_acs_path + "/view", mode_acs_path + "/data", mode_acs_path + "/amis"},
{"data_feed_settings", "amis_schema", data_feed_settings_path + "/descriptor", data_feed_settings_path + "/view", data_feed_settings_path + "/data", data_feed_settings_path + "/amis"},
{"data_sources", "amis_schema", data_sources_path + "/descriptor", data_sources_path + "/view", data_sources_path, data_sources_path + "/amis"},
{"data_feeds", "amis_schema", data_feeds_path + "/descriptor", data_feeds_path + "/view", data_feeds_path, data_feeds_path + "/amis"},
{"source_feed_relations", "amis_schema", relations_path + "/descriptor", relations_path + "/view", relations_path, relations_path + "/amis"},
{"mlat", "amis_schema", mlat_path + "/descriptor", mlat_path + "/view", mlat_path + "/data", mlat_path + "/amis"},
{"map_view", "amis_schema", map_view_path + "/descriptor", map_view_path + "/view", map_view_path + "/data", map_view_path + "/amis"},
{"cesium_graphics", "amis_schema", cesium_graphics_path + "/descriptor", cesium_graphics_path + "/view", cesium_graphics_path + "/data", cesium_graphics_path + "/amis"},
@@ -1102,9 +1110,6 @@ private:
const std::map<std::string, Json> schemas = {
{"mode_acs", mode_acs_resource_.amis_schema()},
{"data_feed_settings", data_feed_settings_resource_.amis_schema()},
{"data_sources", source_amis_schema()},
{"data_feeds", feed_amis_schema()},
{"source_feed_relations", relation_amis_schema()},
{"mlat", mlat_resource_.amis_schema()},
{"map_view", map_view_resource_.amis_schema()},
{"cesium_graphics", cesium_graphics_resource_.amis_schema()},
@@ -1121,7 +1126,6 @@ private:
static inline const std::string map_view_path = "/api/adminive/config/map_view";
static inline const std::string data_sources_path = "/api/adminive/config/data_sources";
static inline const std::string data_feeds_path = "/api/adminive/config/data_feeds";
static inline const std::string relations_path = "/api/adminive/config/source_feed_relations";
static inline const std::string external_resources_path = "/api/adminive/config/external_resources";
static inline const std::string backend_config_path = "/api/adminive/config/backend_config";
static inline const std::string mlat_path = "/api/adminive/config/mlat";