fix
This commit is contained in:
parent
d51cf330aa
commit
3188716329
|
|
@ -30,9 +30,17 @@ MonitorStatus DispatchMonitor::GetMonitorStatus() const {
|
|||
std::string DispatchMonitor::GetDashboardData() const {
|
||||
std::stringstream ss;
|
||||
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
||||
ss << "=== Dashboard ===\n"
|
||||
<< "Time: " << std::put_time(std::gmtime(&now), "%Y-%m-%d %H:%M:%S") << "\n"
|
||||
<< "Total Tasks: " << status_.totalTasks << "\n"
|
||||
ss << "=== Dashboard ===\n";
|
||||
|
||||
// Handle potential null pointer from gmtime
|
||||
std::tm* tm_info = std::gmtime(&now);
|
||||
if (tm_info != nullptr) {
|
||||
ss << "Time: " << std::put_time(tm_info, "%Y-%m-%d %H:%M:%S") << "\n";
|
||||
} else {
|
||||
ss << "Time: UNAVAILABLE\n";
|
||||
}
|
||||
|
||||
ss << "Total Tasks: " << status_.totalTasks << "\n"
|
||||
<< "Completed: " << status_.completedTasks << "\n"
|
||||
<< "In Progress: " << status_.inProgressTasks << "\n"
|
||||
<< "Failed: " << status_.failedTasks << "\n"
|
||||
|
|
|
|||
|
|
@ -5,6 +5,11 @@
|
|||
namespace battlefield {
|
||||
|
||||
bool EventProcessor::ReceiveEvent(const std::string& rawJson) {
|
||||
// 检查输入是否为空
|
||||
if (rawJson.empty()) {
|
||||
throw std::out_of_range("Input string is empty");
|
||||
}
|
||||
|
||||
// 模拟接收:将原始 JSON 转化为 Event 对象并加入列表
|
||||
Event evt = TransformEvent(rawJson);
|
||||
events_.push_back(evt);
|
||||
|
|
@ -18,6 +23,12 @@ Event EventProcessor::TransformEvent(const std::string& raw) {
|
|||
std::stringstream ss(raw);
|
||||
std::string token;
|
||||
std::getline(ss, token, ',');
|
||||
|
||||
// 检查输入是否有效:需要至少4个字符才能生成有效的ID
|
||||
if (token.empty() || token.size() < 4) {
|
||||
throw std::out_of_range("Input string too short to generate event ID");
|
||||
}
|
||||
|
||||
evt.id = "EVT-" + token.substr(0, 4);
|
||||
evt.type = EventType::INTEL;
|
||||
evt.priority = 5;
|
||||
|
|
|
|||
|
|
@ -5,48 +5,14 @@
|
|||
#include <memory>
|
||||
#include <chrono>
|
||||
|
||||
// 为保持测试独立可编译,此处提供依赖结构体的最小化定义
|
||||
// 实际项目中应直接包含对应头文件
|
||||
// 使用实际项目中的类型定义
|
||||
namespace battlefield {
|
||||
struct Plan {
|
||||
std::string id;
|
||||
std::string name;
|
||||
std::vector<std::string> subTasks;
|
||||
};
|
||||
struct CmdPacket {
|
||||
std::string packetId;
|
||||
std::string planId;
|
||||
std::string targetUnit;
|
||||
std::string payload;
|
||||
std::chrono::system_clock::time_point issueTime;
|
||||
};
|
||||
struct MonitorStatus {
|
||||
int32_t totalTasks = 0;
|
||||
int32_t completedTasks = 0;
|
||||
int32_t inProgressTasks = 0;
|
||||
int32_t failedTasks = 0;
|
||||
double progressPercent = 0.0;
|
||||
};
|
||||
struct DispatchLog {
|
||||
std::string logId;
|
||||
std::string planId;
|
||||
std::string action;
|
||||
std::chrono::system_clock::time_point timestamp;
|
||||
bool success;
|
||||
};
|
||||
|
||||
class DispatchMonitor {
|
||||
public:
|
||||
CmdPacket DispatchPlan(const Plan& plan, const std::string& targetUnit);
|
||||
MonitorStatus GetMonitorStatus() const;
|
||||
std::string GetDashboardData() const;
|
||||
void LogDispatch(const std::string& planId, const std::string& action, bool success);
|
||||
void AdvanceProgress(int32_t completed);
|
||||
private:
|
||||
std::vector<CmdPacket> issuedPackets_;
|
||||
std::vector<DispatchLog> logs_;
|
||||
MonitorStatus status_;
|
||||
};
|
||||
// 使用实际项目中的类型,通过已包含的头文件获取
|
||||
// struct Plan;
|
||||
// struct CmdPacket;
|
||||
// struct MonitorStatus;
|
||||
// struct DispatchLog;
|
||||
// class DispatchMonitor;
|
||||
} // namespace battlefield
|
||||
|
||||
// 测试夹具
|
||||
|
|
@ -64,7 +30,16 @@ protected:
|
|||
// ==================== DispatchPlan 测试 ====================
|
||||
TEST_F(DispatchMonitorTest, testDispatchPlanNormalInput) {
|
||||
// 正常输入:包含多个子任务的计划
|
||||
battlefield::Plan plan{"P001", "Attack Plan", {"Task1", "Task2", "Task3"}};
|
||||
battlefield::Plan plan;
|
||||
plan.id = "P001";
|
||||
plan.name = "Attack Plan";
|
||||
plan.description = "Test description";
|
||||
plan.subTasks = {
|
||||
{"Task1", "P001", "Task1", "Description 1", 0, ""},
|
||||
{"Task2", "P001", "Task2", "Description 2", 1, ""},
|
||||
{"Task3", "P001", "Task3", "Description 3", 2, ""}
|
||||
};
|
||||
plan.score = 0.0;
|
||||
std::string target = "UnitAlpha";
|
||||
|
||||
auto packet = monitor_->DispatchPlan(plan, target);
|
||||
|
|
@ -85,7 +60,12 @@ TEST_F(DispatchMonitorTest, testDispatchPlanNormalInput) {
|
|||
|
||||
TEST_F(DispatchMonitorTest, testDispatchPlanBoundaryZeroTasks) {
|
||||
// 边界值测试:空子任务列表
|
||||
battlefield::Plan plan{"P002", "Empty Plan", {}};
|
||||
battlefield::Plan plan;
|
||||
plan.id = "P002";
|
||||
plan.name = "Empty Plan";
|
||||
plan.description = "Test description";
|
||||
plan.subTasks = {};
|
||||
plan.score = 0.0;
|
||||
auto packet = monitor_->DispatchPlan(plan, "UnitBeta");
|
||||
|
||||
EXPECT_EQ(packet.packetId, "CMD-P002");
|
||||
|
|
@ -108,7 +88,15 @@ TEST_F(DispatchMonitorTest, testGetMonitorStatusInitialState) {
|
|||
// ==================== GetDashboardData 测试 ====================
|
||||
TEST_F(DispatchMonitorTest, testGetDashboardDataNormalFormat) {
|
||||
// 正常输入:执行部分操作后验证仪表盘格式
|
||||
battlefield::Plan plan{"P003", "Test Plan", {"T1", "T2"}};
|
||||
battlefield::Plan plan;
|
||||
plan.id = "P003";
|
||||
plan.name = "Test Plan";
|
||||
plan.description = "Test description";
|
||||
plan.subTasks = {
|
||||
{"T1", "P003", "Task 1", "Description 1", 0, ""},
|
||||
{"T2", "P003", "Task 2", "Description 2", 1, ""}
|
||||
};
|
||||
plan.score = 0.0;
|
||||
monitor_->DispatchPlan(plan, "UnitGamma");
|
||||
monitor_->AdvanceProgress(1);
|
||||
|
||||
|
|
@ -152,7 +140,17 @@ TEST_F(DispatchMonitorTest, testLogDispatchBoundaryEmptyStrings) {
|
|||
// ==================== AdvanceProgress 测试 ====================
|
||||
TEST_F(DispatchMonitorTest, testAdvanceProgressNormal) {
|
||||
// 正常输入:正常推进进度
|
||||
battlefield::Plan plan{"P005", "Progress Plan", {"A", "B", "C", "D"}};
|
||||
battlefield::Plan plan;
|
||||
plan.id = "P005";
|
||||
plan.name = "Progress Plan";
|
||||
plan.description = "Test description";
|
||||
plan.subTasks = {
|
||||
{"A", "P005", "Task A", "Description A", 0, ""},
|
||||
{"B", "P005", "Task B", "Description B", 1, ""},
|
||||
{"C", "P005", "Task C", "Description C", 2, ""},
|
||||
{"D", "P005", "Task D", "Description D", 3, ""}
|
||||
};
|
||||
plan.score = 0.0;
|
||||
monitor_->DispatchPlan(plan, "UnitDelta");
|
||||
monitor_->AdvanceProgress(2);
|
||||
|
||||
|
|
@ -164,7 +162,15 @@ TEST_F(DispatchMonitorTest, testAdvanceProgressNormal) {
|
|||
|
||||
TEST_F(DispatchMonitorTest, testAdvanceProgressBoundaryClamping) {
|
||||
// 边界值测试:完成数大于进行中任务数(触发截断)
|
||||
battlefield::Plan plan{"P006", "Clamp Plan", {"X", "Y"}};
|
||||
battlefield::Plan plan;
|
||||
plan.id = "P006";
|
||||
plan.name = "Clamp Plan";
|
||||
plan.description = "Test description";
|
||||
plan.subTasks = {
|
||||
{"X", "P006", "Task X", "Description X", 0, ""},
|
||||
{"Y", "P006", "Task Y", "Description Y", 1, ""}
|
||||
};
|
||||
plan.score = 0.0;
|
||||
monitor_->DispatchPlan(plan, "UnitEpsilon");
|
||||
monitor_->AdvanceProgress(10); // 10 > 2
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue