task_plan/include/event_manager.hpp

161 lines
4.4 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef ETMS_EVENT_MANAGER_HPP
#define ETMS_EVENT_MANAGER_HPP
#include <string>
#include <vector>
#include <chrono>
#include <cstdint>
/**
* @brief 事件状态枚举
*
* 对应 t_event 表 status 字段的状态流转路径:
* Received → Processed → PendingTask → TaskGenerated
*/
enum class EventStatus {
Received, ///< 已接收,尚未处理
Processed, ///< 已处理完毕
PendingTask, ///< 待生成任务
TaskGenerated ///< 任务已生成
};
/**
* @brief 事件等级枚举
*
* 区分战场事件的紧急程度与重要性。
*/
enum class EventLevel {
Info = 0, ///< 信息级
Warning = 1, ///< 警告级
Critical = 2, ///< 危急级
Fatal = 3 ///< 致命级
};
/**
* @brief 战场事件数据结构
*
* 对应分析文档中 t_event 表的核心字段。
*/
struct Event {
uint64_t id; ///< 自增主键 ID
std::string eventId; ///< 事件唯一标识 (event_id)
std::string eventType; ///< 事件类型 (event_type)
int64_t timestamp; ///< 事件发生时间戳 (毫秒)
EventLevel level; ///< 事件等级
double longitude; ///< 经度
double latitude; ///< 纬度
std::string description; ///< 事件描述
EventStatus status; ///< 当前处理状态
int64_t createTime; ///< 记录创建时间戳 (毫秒)
};
/**
* @brief 分页查询结果
*/
struct PageResult {
std::vector<Event> items; ///< 当前页数据
size_t totalCount; ///< 总记录数
size_t pageIndex; ///< 当前页码 (从1开始)
size_t pageSize; ///< 每页大小
};
/**
* @brief 事件管理器
*
* 负责战场事件数据的接收、处理、存储与查询。
* 支持事件状态流转、分页查询、按等级过滤和自定义排序。
*/
class EventManager {
public:
/// @brief 默认构造函数
EventManager();
/// @brief 虚析构函数
virtual ~EventManager();
/// @brief 禁止拷贝
EventManager(const EventManager&) = delete;
EventManager& operator=(const EventManager&) = delete;
/// @brief 允许移动
EventManager(EventManager&&) noexcept;
EventManager& operator=(EventManager&&) noexcept;
/**
* @brief 接收并解析一条原始事件
*
* 模拟从消息队列MQTT/Kafka接收 JSON 事件后的处理入口。
*
* @param rawJson 原始 JSON 字符串(简化处理)
* @return true 解析成功,事件已入库
* @return false 格式无效或字段缺失
*/
bool receiveEvent(const std::string& rawJson);
/**
* @brief 更新指定事件的状态
*
* @param eventId 事件唯一 ID
* @param newStatus 目标状态
* @return true 更新成功
* @return false 未找到该事件
*/
bool updateStatus(const std::string& eventId, EventStatus newStatus);
/**
* @brief 分页查询事件列表
*
* @param page 页码从1开始
* @param pageSize 每页条数
* @param levelFilter 按等级过滤,传入 nullptr 表示不过滤
* @return PageResult 分页结果
*/
PageResult queryEvents(size_t page, size_t pageSize,
const EventLevel* levelFilter = nullptr) const;
/**
* @brief 获取当前事件总数
* @return size_t
*/
size_t totalEvents() const;
/**
* @brief 根据事件 ID 查找事件
* @param eventId 事件唯一 ID
* @return const Event* 找到返回指针,否则返回 nullptr
*/
const Event* findEvent(const std::string& eventId) const;
/**
* @brief 获取事件等级的字符串表示
* @param level 事件等级
* @return const char* 中文描述
*/
static const char* levelToString(EventLevel level) noexcept;
/**
* @brief 获取事件状态的字符串表示
* @param status 事件状态
* @return const char*
*/
static const char* statusToString(EventStatus status) noexcept;
private:
std::vector<Event> m_events; ///< 事件存储容器(模拟数据库 t_event 表)
uint64_t m_nextId = 1; ///< 自增 ID 计数器
/**
* @brief 生成下一个主键 ID
* @return uint64_t
*/
uint64_t nextId();
/**
* @brief 获取当前毫秒时间戳
* @return int64_t
*/
static int64_t nowMs() noexcept;
};
#endif // ETMS_EVENT_MANAGER_HPP