Files
2026-02-25 23:01:42 +08:00

123 lines
4.9 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;
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