plan_execute_t1/src/app.cpp

403 lines
13 KiB
C++
Raw Normal View History

2026-05-06 14:04:42 +00:00
#include "app.hpp"
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <ctime>
// ══════════════════════════════════════════════════════════════════════════════
// CmsEngine 构造与辅助
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*/
CmsEngine::CmsEngine()
: nextId_(0)
{
systemContext_.currentMode = RunMode::Idle;
systemContext_.consistencyMark = 0;
systemContext_.switchTime = std::chrono::system_clock::now();
systemContext_.contextSnapshot = R"({"mode":"Idle","version":"1.0.0"})";
}
/**
* @brief
*
* @return std::string "ID-00001"
*/
std::string CmsEngine::generateNextId()
{
std::ostringstream oss;
oss << "ID-" << std::setw(5) << std::setfill('0') << (++nextId_);
return oss.str();
}
// ══════════════════════════════════════════════════════════════════════════════
// 事件处理
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*
* ID
*
* @param event
* @return true
* @return false
*/
bool CmsEngine::ingestEvent(const EventRecord& event)
{
if (event.id.empty()) {
return false;
}
if (event.priority > 255) {
return false;
}
events_.push_back(event);
return true;
}
/**
* @brief
*
* Pending Generated
* TaskPlan
*
* @return size_t
*/
size_t CmsEngine::processPendingEvents()
{
size_t count = 0;
for (auto& evt : events_) {
if (evt.status == EventStatus::Pending) {
evt.status = EventStatus::Generated;
TaskPlan plan;
plan.id = generateNextId();
plan.name = "AutoPlan-" + evt.id;
plan.type = PlanType::Centralized;
plan.status = PlanStatus::Drafting;
plan.createTime = std::chrono::system_clock::now();
plan.relatedEventId = evt.id;
plan.resourceQuota = 0.5;
plan.constraints = "自动生成约束";
plans_.push_back(plan);
++count;
}
}
return count;
}
/**
* @brief ID
*
* @param eventId
* @return const EventRecord* nullptr
*/
const EventRecord* CmsEngine::findEventById(const std::string& eventId) const
{
for (const auto& evt : events_) {
if (evt.id == eventId) {
return &evt;
}
}
return nullptr;
}
// ══════════════════════════════════════════════════════════════════════════════
// 任务方案管理
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*
* ID
*
* @param plan id
* @return true
* @return false
*/
bool CmsEngine::createTaskPlan(TaskPlan& plan)
{
if (plan.name.empty()) {
return false;
}
plan.id = generateNextId();
plan.createTime = std::chrono::system_clock::now();
plans_.push_back(plan);
return true;
}
/**
* @brief
*
* @return const std::vector<TaskPlan>&
*/
const std::vector<TaskPlan>& CmsEngine::getAllPlans() const
{
return plans_;
}
/**
* @brief ID
*
* @param planId
* @return const TaskPlan* nullptr
*/
const TaskPlan* CmsEngine::findPlanById(const std::string& planId) const
{
for (const auto& plan : plans_) {
if (plan.id == planId) {
return &plan;
}
}
return nullptr;
}
/**
* @brief
*
* @param planId
* @param newStatus
* @return true
* @return false
*/
bool CmsEngine::updatePlanStatus(const std::string& planId, PlanStatus newStatus)
{
for (auto& plan : plans_) {
if (plan.id == planId) {
plan.status = newStatus;
return true;
}
}
return false;
}
// ══════════════════════════════════════════════════════════════════════════════
// 模板管理
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*
* @param tmpl
*/
void CmsEngine::registerTemplate(const TemplateInstance& tmpl)
{
templates_.push_back(tmpl);
}
/**
* @brief
*
*
*
* @param scenario
* @return const TemplateInstance* nullptr
*/
const TemplateInstance* CmsEngine::matchTemplate(const std::string& scenario) const
{
(void)scenario;
if (templates_.empty()) {
return nullptr;
}
const TemplateInstance* best = &templates_.front();
for (const auto& tmpl : templates_) {
if (tmpl.confidence > best->confidence) {
best = &tmpl;
}
}
return best;
}
// ══════════════════════════════════════════════════════════════════════════════
// 执行监控
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*
* nodeId
*
* @param status
*/
void CmsEngine::reportExecutionStatus(const ExecutionStatus& status)
{
nodeStatus_[status.nodeId] = status;
}
/**
* @brief
*
* < 0.5 != 0 > 30
*
* @return std::vector<ExecutionStatus>
*/
std::vector<ExecutionStatus> CmsEngine::checkHealth() const
{
std::vector<ExecutionStatus> unhealthy;
const auto now = std::chrono::system_clock::now();
for (const auto& [nodeId, status] : nodeStatus_) {
(void)nodeId;
bool isUnhealthy = false;
if (status.healthIndex < 0.5) {
isUnhealthy = true;
}
if (status.errorCode != 0) {
isUnhealthy = true;
}
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(
now - status.lastReport).count();
if (elapsed > 30) {
isUnhealthy = true;
}
if (isUnhealthy) {
unhealthy.push_back(status);
}
}
return unhealthy;
}
// ══════════════════════════════════════════════════════════════════════════════
// 用户会话管理
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*
* userId
*
* @param session
* @return true
* @return false ID
*/
bool CmsEngine::createSession(const UserSession& session)
{
if (session.userId.empty()) {
return false;
}
if (sessions_.find(session.userId) != sessions_.end()) {
return false;
}
sessions_[session.userId] = session;
return true;
}
/**
* @brief
*
*
*
* @param userId ID
* @param permissionMask
* @return true
* @return false
*/
bool CmsEngine::checkPermission(const std::string& userId,
uint64_t permissionMask) const
{
auto it = sessions_.find(userId);
if (it == sessions_.end()) {
return false;
}
return (it->second.permissionBitmap & permissionMask) == permissionMask;
}
// ══════════════════════════════════════════════════════════════════════════════
// 通知管理
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*
* @param msg
*/
void CmsEngine::pushNotification(const NotificationMessage& msg)
{
notifications_.push_back(msg);
}
/**
* @brief
*
* @return std::vector<NotificationMessage>
*/
std::vector<NotificationMessage> CmsEngine::getUnreadNotifications() const
{
std::vector<NotificationMessage> unread;
for (const auto& msg : notifications_) {
if (!msg.isRead) {
unread.push_back(msg);
}
}
return unread;
}
// ══════════════════════════════════════════════════════════════════════════════
// 系统状态管理
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief
*
*
*
* @param newMode
* @return true
* @return false
*/
bool CmsEngine::switchMode(RunMode newMode)
{
if (systemContext_.currentMode == RunMode::Degraded && newMode != RunMode::Degraded) {
// 降级模式下不允许退出降级(模拟安全策略)
return false;
}
systemContext_.currentMode = newMode;
systemContext_.switchTime = std::chrono::system_clock::now();
systemContext_.consistencyMark = nextId_;
return true;
}
/**
* @brief
*
* @return const SystemStateContext&
*/
const SystemStateContext& CmsEngine::getSystemContext() const
{
return systemContext_;
}
/**
* @brief
*
* @return std::string
*/
std::string CmsEngine::getSummary() const
{
std::ostringstream oss;
oss << "=== CMS Engine Summary ===\n"
<< " Events : " << events_.size() << "\n"
<< " Plans : " << plans_.size() << "\n"
<< " Templates : " << templates_.size() << "\n"
<< " Monitor Nodes : " << nodeStatus_.size() << "\n"
<< " Active Sessions : " << sessions_.size() << "\n"
<< " Notifications : " << notifications_.size() << "\n"
<< " Mode : ";
switch (systemContext_.currentMode) {
case RunMode::Idle: oss << "Idle"; break;
case RunMode::HumanLoop: oss << "HumanLoop"; break;
case RunMode::AutoExec: oss << "AutoExec"; break;
case RunMode::Degraded: oss << "Degraded"; break;
default: oss << "Unknown"; break;
}
oss << "\n============================";
return oss.str();
}