168 lines
5.7 KiB
C++
168 lines
5.7 KiB
C++
#ifndef QDATAQUALITY_H
|
|
#define QDATAQUALITY_H
|
|
|
|
#include <QObject>
|
|
#include <QMap>
|
|
#include <QSet>
|
|
#include <QDateTime>
|
|
#include <QMutex>
|
|
#include <QTimer>
|
|
#include "BZStruct.h"
|
|
|
|
// 数据质量指标
|
|
struct DataQualityMetrics {
|
|
double completeness = 100.0; // 完整性百分比
|
|
double timeliness = 100.0; // 及时性百分比
|
|
double accuracy = 100.0; // 准确性百分比
|
|
double consistency = 100.0; // 一致性百分比
|
|
double availability = 100.0; // 可用性百分比
|
|
|
|
int totalPackets = 0;
|
|
int validPackets = 0;
|
|
int delayedPackets = 0;
|
|
int corruptedPackets = 0;
|
|
int missingPackets = 0;
|
|
|
|
QDateTime lastUpdate;
|
|
|
|
void updateMetrics(int total, int valid, int delayed, int corrupted, int missing) {
|
|
totalPackets = total;
|
|
validPackets = valid;
|
|
delayedPackets = delayed;
|
|
corruptedPackets = corrupted;
|
|
missingPackets = missing;
|
|
|
|
completeness = total > 0 ? (static_cast<double>(valid) / total) * 100.0 : 100.0;
|
|
timeliness = total > 0 ? (static_cast<double>(total - delayed) / total) * 100.0 : 100.0;
|
|
accuracy = total > 0 ? (static_cast<double>(total - corrupted) / total) * 100.0 : 100.0;
|
|
consistency = total > 0 ? (static_cast<double>(valid) / total) * 100.0 : 100.0;
|
|
availability = total > 0 ? (static_cast<double>(total - missing) / total) * 100.0 : 100.0;
|
|
|
|
lastUpdate = QDateTime::currentDateTime();
|
|
}
|
|
};
|
|
|
|
// 异常类型
|
|
enum class ExceptionType {
|
|
DataDelay, // 数据延迟
|
|
DataCorruption, // 数据损坏
|
|
DataMissing, // 数据缺失
|
|
ConnectionLoss, // 连接丢失
|
|
MemoryOverflow, // 内存溢出
|
|
ProcessingTimeout, // 处理超时
|
|
SystemError // 系统错误
|
|
};
|
|
|
|
// 异常记录
|
|
struct ExceptionRecord {
|
|
ExceptionType type;
|
|
QString description;
|
|
QString stockCode;
|
|
QDateTime timestamp;
|
|
int severity; // 1-10, 10为最严重
|
|
bool resolved = false;
|
|
QDateTime resolvedTime;
|
|
|
|
ExceptionRecord(ExceptionType t, const QString& desc, const QString& code = "", int sev = 5)
|
|
: type(t), description(desc), stockCode(code), timestamp(QDateTime::currentDateTime()), severity(sev) {}
|
|
};
|
|
|
|
// 告警配置
|
|
struct AlertConfig {
|
|
double completenessThreshold = 95.0; // 完整性阈值
|
|
double timelinessThreshold = 98.0; // 及时性阈值
|
|
double accuracyThreshold = 99.0; // 准确性阈值
|
|
int maxConsecutiveErrors = 5; // 最大连续错误数
|
|
int maxExceptionsPerMinute = 10; // 每分钟最大异常数
|
|
bool enableEmailAlerts = false;
|
|
bool enableSoundAlerts = true;
|
|
bool enableLogAlerts = true;
|
|
|
|
QString emailRecipient;
|
|
QString smtpServer;
|
|
};
|
|
|
|
class QDataQuality : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
static QDataQuality* instance();
|
|
|
|
// 配置管理
|
|
void setAlertConfig(const AlertConfig& config);
|
|
AlertConfig getAlertConfig() const;
|
|
|
|
// 数据质量检查
|
|
bool validateOrderBookData(const Qot_UpdateOrderBook::Response& data);
|
|
bool validateQuoteData(const Qot_UpdateBasicQot::Response& data);
|
|
bool validateTickData(const Qot_UpdateTicker::Response& data);
|
|
bool validateKLData(const Qot_UpdateKL::Response& data);
|
|
|
|
// 异常检测
|
|
void checkDataDelay(const QString& stockCode, const QDateTime& dataTime);
|
|
void checkDataConsistency(const QString& stockCode, DataPacketType type);
|
|
void checkSystemHealth();
|
|
|
|
// 异常管理
|
|
void reportException(ExceptionType type, const QString& description, const QString& stockCode = "", int severity = 5);
|
|
QList<ExceptionRecord> getActiveExceptions() const;
|
|
QList<ExceptionRecord> getExceptionHistory(int hours = 24) const;
|
|
void resolveException(const QString& exceptionId);
|
|
void resolveAllExceptions();
|
|
|
|
// 质量指标
|
|
DataQualityMetrics getOverallMetrics() const;
|
|
DataQualityMetrics getMetricsForStock(const QString& stockCode) const;
|
|
DataQualityMetrics getMetricsForType(DataPacketType type) const;
|
|
|
|
// 告警控制
|
|
void enableAlerts(bool enabled);
|
|
bool areAlertsEnabled() const;
|
|
void setAlertThresholds(double completeness, double timeliness, double accuracy);
|
|
|
|
signals:
|
|
void dataQualityAlert(const QString& message, int severity);
|
|
void exceptionDetected(const ExceptionRecord& exception);
|
|
void exceptionResolved(const ExceptionRecord& exception);
|
|
void qualityMetricsUpdated(const DataQualityMetrics& metrics);
|
|
void systemHealthChanged(bool healthy);
|
|
|
|
private:
|
|
explicit QDataQuality(QObject *parent = nullptr);
|
|
~QDataQuality();
|
|
|
|
void initializeValidationRules();
|
|
bool checkBusinessRules(const Qot_UpdateOrderBook::Response& data);
|
|
bool checkNumericalBounds(const Qot_UpdateOrderBook::Response& data);
|
|
bool checkTemporalConsistency(const QString& stockCode, DataPacketType type);
|
|
|
|
void updateMetrics();
|
|
void checkAlertConditions();
|
|
void sendAlert(const QString& message, int severity);
|
|
|
|
// 数据存储
|
|
mutable QMutex m_mutex;
|
|
QMap<QString, DataQualityMetrics> m_stockMetrics;
|
|
QMap<DataPacketType, DataQualityMetrics> m_typeMetrics;
|
|
DataQualityMetrics m_overallMetrics;
|
|
|
|
// 异常管理
|
|
QMap<QString, ExceptionRecord> m_activeExceptions;
|
|
QList<ExceptionRecord> m_exceptionHistory;
|
|
|
|
// 配置
|
|
AlertConfig m_alertConfig;
|
|
bool m_alertsEnabled = true;
|
|
|
|
// 统计
|
|
QMap<QString, QDateTime> m_lastDataTime;
|
|
QMap<QString, int> m_consecutiveErrors;
|
|
QMap<QString, QList<QDateTime>> m_exceptionTimestamps;
|
|
|
|
// 定时器
|
|
QTimer* m_metricsTimer;
|
|
QTimer* m_healthCheckTimer;
|
|
};
|
|
|
|
#endif // QDATAQUALITY_H
|