add multiple time filters for log filter
This commit is contained in:
parent
21ac449b3d
commit
fb4f1cf1a2
@ -754,12 +754,21 @@ StateDescriptor JsonTypes::unpackStateDescriptor(const QVariantMap &stateDescrip
|
|||||||
LogFilter JsonTypes::unpackLogFilter(const QVariantMap &logFilterMap)
|
LogFilter JsonTypes::unpackLogFilter(const QVariantMap &logFilterMap)
|
||||||
{
|
{
|
||||||
LogFilter filter;
|
LogFilter filter;
|
||||||
if (logFilterMap.contains("startTime")) {
|
if (logFilterMap.contains("timeFilters")) {
|
||||||
filter.setStartDate(QDateTime::fromMSecsSinceEpoch(logFilterMap.value("startTime").toInt()));
|
QVariantList timeFilters = logFilterMap.value("timeFilters").toList();
|
||||||
}
|
foreach (const QVariant &timeFilter, timeFilters) {
|
||||||
if (logFilterMap.contains("endTime")) {
|
QVariantMap timeFilterMap = timeFilter.toMap();
|
||||||
filter.setEndDate(QDateTime::fromMSecsSinceEpoch(logFilterMap.value("endTime").toInt()));
|
QDateTime startDate; QDateTime endDate;
|
||||||
|
if (timeFilterMap.contains("startDate")) {
|
||||||
|
startDate = QDateTime::fromTime_t(timeFilterMap.value("startDate").toInt());
|
||||||
|
}
|
||||||
|
if (timeFilterMap.contains("endDate")) {
|
||||||
|
endDate = QDateTime::fromTime_t(timeFilterMap.value("endDate").toInt());
|
||||||
|
}
|
||||||
|
filter.addTimeFilter(startDate, endDate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logFilterMap.contains("loggingSources")) {
|
if (logFilterMap.contains("loggingSources")) {
|
||||||
QVariantList loggingSources = logFilterMap.value("loggingSources").toList();
|
QVariantList loggingSources = logFilterMap.value("loggingSources").toList();
|
||||||
foreach (const QVariant &source, loggingSources) {
|
foreach (const QVariant &source, loggingSources) {
|
||||||
|
|||||||
@ -35,15 +35,18 @@ LoggingHandler::LoggingHandler(QObject *parent) :
|
|||||||
|
|
||||||
// Notifications
|
// Notifications
|
||||||
params.clear(); returns.clear();
|
params.clear(); returns.clear();
|
||||||
setDescription("LogEntryAdded", "Emitted whenever an entry is appended to the logging system.");
|
setDescription("LogEntryAdded", "Emitted whenever an entry is appended to the logging system. "
|
||||||
|
"The filters can be combinend.");
|
||||||
params.insert("logEntry", JsonTypes::logEntryRef());
|
params.insert("logEntry", JsonTypes::logEntryRef());
|
||||||
setParams("LogEntryAdded", params);
|
setParams("LogEntryAdded", params);
|
||||||
|
|
||||||
params.clear(); returns.clear();
|
params.clear(); returns.clear();
|
||||||
setDescription("GetLogEntries", "Get the LogEntries matching the given filter.");
|
setDescription("GetLogEntries", "Get the LogEntries matching the given filter.");
|
||||||
|
|
||||||
params.insert("o:startTime", JsonTypes::basicTypeToString(JsonTypes::Int));
|
QVariantMap timeFilter;
|
||||||
params.insert("o:endTime", JsonTypes::basicTypeToString(JsonTypes::Int));
|
timeFilter.insert("o:startDate", JsonTypes::basicTypeToString(JsonTypes::Int));
|
||||||
|
timeFilter.insert("o:endDate", JsonTypes::basicTypeToString(JsonTypes::Int));
|
||||||
|
params.insert("o:timeFilters", QVariantList() << timeFilter);
|
||||||
params.insert("o:loggingSources", QVariantList() << JsonTypes::loggingSourceRef());
|
params.insert("o:loggingSources", QVariantList() << JsonTypes::loggingSourceRef());
|
||||||
params.insert("o:loggingLevels", QVariantList() << JsonTypes::loggingLevelRef());
|
params.insert("o:loggingLevels", QVariantList() << JsonTypes::loggingLevelRef());
|
||||||
params.insert("o:eventTypes", QVariantList() << JsonTypes::loggingEventTypeRef());
|
params.insert("o:eventTypes", QVariantList() << JsonTypes::loggingEventTypeRef());
|
||||||
@ -73,7 +76,7 @@ void LoggingHandler::logEntryAdded(const LogEntry &logEntry)
|
|||||||
|
|
||||||
JsonReply* LoggingHandler::GetLogEntries(const QVariantMap ¶ms) const
|
JsonReply* LoggingHandler::GetLogEntries(const QVariantMap ¶ms) const
|
||||||
{
|
{
|
||||||
qCDebug(dcJsonRpc) << "asked for log entries" << params;
|
qCDebug(dcJsonRpc) << "Asked for log entries" << params;
|
||||||
|
|
||||||
LogFilter filter = JsonTypes::unpackLogFilter(params);
|
LogFilter filter = JsonTypes::unpackLogFilter(params);
|
||||||
|
|
||||||
|
|||||||
@ -29,7 +29,7 @@
|
|||||||
#include <QMetaEnum>
|
#include <QMetaEnum>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
#define DB_SCHEMA_VERSION 1
|
#define DB_SCHEMA_VERSION 2
|
||||||
|
|
||||||
namespace guhserver {
|
namespace guhserver {
|
||||||
|
|
||||||
@ -43,7 +43,6 @@ LogEngine::LogEngine(QObject *parent):
|
|||||||
qCWarning(dcLogEngine) << "Database not valid:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Database not valid:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_db.open()) {
|
if (!m_db.open()) {
|
||||||
qCWarning(dcLogEngine) << "Error opening log database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Error opening log database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
||||||
return;
|
return;
|
||||||
@ -69,7 +68,7 @@ QList<LogEntry> LogEngine::logEntries(const LogFilter &filter) const
|
|||||||
|
|
||||||
while (query.next()) {
|
while (query.next()) {
|
||||||
LogEntry entry(
|
LogEntry entry(
|
||||||
QDateTime::fromMSecsSinceEpoch(query.value("timestamp").toLongLong()),
|
QDateTime::fromTime_t(query.value("timestamp").toLongLong()),
|
||||||
(Logging::LoggingLevel)query.value("loggingLevel").toInt(),
|
(Logging::LoggingLevel)query.value("loggingLevel").toInt(),
|
||||||
(Logging::LoggingSource)query.value("sourceType").toInt(),
|
(Logging::LoggingSource)query.value("sourceType").toInt(),
|
||||||
query.value("errorCode").toInt());
|
query.value("errorCode").toInt());
|
||||||
@ -150,7 +149,7 @@ void LogEngine::logRuleActiveChanged(const Rule &rule)
|
|||||||
void LogEngine::appendLogEntry(const LogEntry &entry)
|
void LogEngine::appendLogEntry(const LogEntry &entry)
|
||||||
{
|
{
|
||||||
QString queryString = QString("INSERT INTO entries (timestamp, loggingEventType, loggingLevel, sourceType, typeId, deviceId, value, active, errorCode) values ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9');")
|
QString queryString = QString("INSERT INTO entries (timestamp, loggingEventType, loggingLevel, sourceType, typeId, deviceId, value, active, errorCode) values ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9');")
|
||||||
.arg(entry.timestamp().toMSecsSinceEpoch())
|
.arg(entry.timestamp().toTime_t())
|
||||||
.arg(entry.eventType())
|
.arg(entry.eventType())
|
||||||
.arg(entry.level())
|
.arg(entry.level())
|
||||||
.arg(entry.source())
|
.arg(entry.source())
|
||||||
@ -186,7 +185,7 @@ void LogEngine::initDB()
|
|||||||
if (version != DB_SCHEMA_VERSION) {
|
if (version != DB_SCHEMA_VERSION) {
|
||||||
qCWarning(dcLogEngine) << "Log schema version not matching! Schema upgrade not implemented yet. Logging might fail.";
|
qCWarning(dcLogEngine) << "Log schema version not matching! Schema upgrade not implemented yet. Logging might fail.";
|
||||||
} else {
|
} else {
|
||||||
qCDebug(dcLogEngine) << "Log database schema version matches";
|
qCDebug(dcLogEngine) << "Log database schema version" << DB_SCHEMA_VERSION << "matches";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qCWarning(dcLogEngine) << "Broken log database. Version not found in metadata table.";
|
qCWarning(dcLogEngine) << "Broken log database. Version not found in metadata table.";
|
||||||
@ -194,7 +193,7 @@ void LogEngine::initDB()
|
|||||||
|
|
||||||
if (!m_db.tables().contains("sourceTypes")) {
|
if (!m_db.tables().contains("sourceTypes")) {
|
||||||
query.exec("CREATE TABLE sourceTypes (id int, name varchar(20), PRIMARY KEY(id));");
|
query.exec("CREATE TABLE sourceTypes (id int, name varchar(20), PRIMARY KEY(id));");
|
||||||
qCDebug(dcLogEngine) << query.lastError().databaseText();
|
//qCDebug(dcLogEngine) << query.lastError().databaseText();
|
||||||
QMetaEnum logTypes = Logging::staticMetaObject.enumerator(Logging::staticMetaObject.indexOfEnumerator("LoggingSource"));
|
QMetaEnum logTypes = Logging::staticMetaObject.enumerator(Logging::staticMetaObject.indexOfEnumerator("LoggingSource"));
|
||||||
Q_ASSERT_X(logTypes.isValid(), "LogEngine", "Logging has no enum LoggingSource");
|
Q_ASSERT_X(logTypes.isValid(), "LogEngine", "Logging has no enum LoggingSource");
|
||||||
for (int i = 0; i < logTypes.keyCount(); i++) {
|
for (int i = 0; i < logTypes.keyCount(); i++) {
|
||||||
@ -204,7 +203,7 @@ void LogEngine::initDB()
|
|||||||
|
|
||||||
if (!m_db.tables().contains("loggingEventTypes")) {
|
if (!m_db.tables().contains("loggingEventTypes")) {
|
||||||
query.exec("CREATE TABLE loggingEventTypes (id int, name varchar(20), PRIMARY KEY(id));");
|
query.exec("CREATE TABLE loggingEventTypes (id int, name varchar(20), PRIMARY KEY(id));");
|
||||||
qCDebug(dcLogEngine) << query.lastError().databaseText();
|
//qCDebug(dcLogEngine) << query.lastError().databaseText();
|
||||||
QMetaEnum logTypes = Logging::staticMetaObject.enumerator(Logging::staticMetaObject.indexOfEnumerator("LoggingEventType"));
|
QMetaEnum logTypes = Logging::staticMetaObject.enumerator(Logging::staticMetaObject.indexOfEnumerator("LoggingEventType"));
|
||||||
Q_ASSERT_X(logTypes.isValid(), "LogEngine", "Logging has no enum LoggingEventType");
|
Q_ASSERT_X(logTypes.isValid(), "LogEngine", "Logging has no enum LoggingEventType");
|
||||||
for (int i = 0; i < logTypes.keyCount(); i++) {
|
for (int i = 0; i < logTypes.keyCount(); i++) {
|
||||||
|
|||||||
@ -159,6 +159,7 @@ int LogEntry::errorCode() const
|
|||||||
QDebug operator<<(QDebug dbg, const LogEntry &entry)
|
QDebug operator<<(QDebug dbg, const LogEntry &entry)
|
||||||
{
|
{
|
||||||
dbg.nospace() << "LogEntry (count:" << entry.timestamp().toString() << endl;
|
dbg.nospace() << "LogEntry (count:" << entry.timestamp().toString() << endl;
|
||||||
|
dbg.nospace() << " time stamp: " << entry.timestamp().toTime_t() << endl;
|
||||||
dbg.nospace() << " DeviceId: " << entry.deviceId().toString() << endl;
|
dbg.nospace() << " DeviceId: " << entry.deviceId().toString() << endl;
|
||||||
dbg.nospace() << " type id: " << entry.typeId().toString() << endl;
|
dbg.nospace() << " type id: " << entry.typeId().toString() << endl;
|
||||||
dbg.nospace() << " source: " << entry.sourceString() << endl;
|
dbg.nospace() << " source: " << entry.sourceString() << endl;
|
||||||
|
|||||||
@ -19,6 +19,7 @@
|
|||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
#include "logfilter.h"
|
#include "logfilter.h"
|
||||||
|
#include "loggingcategories.h"
|
||||||
|
|
||||||
namespace guhserver {
|
namespace guhserver {
|
||||||
|
|
||||||
@ -69,24 +70,16 @@ QString LogFilter::queryString() const
|
|||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogFilter::setStartDate(const QDateTime &startDate)
|
void LogFilter::addTimeFilter(const QDateTime &startDate, const QDateTime &endDate)
|
||||||
{
|
{
|
||||||
m_startDate = startDate;
|
QPair<QDateTime, QDateTime> timeFilter(startDate, endDate);
|
||||||
|
if (!m_timeFilters.contains(timeFilter))
|
||||||
|
m_timeFilters.append(timeFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
QDateTime LogFilter::startDate() const
|
QList<QPair<QDateTime, QDateTime> > LogFilter::timeFilters() const
|
||||||
{
|
{
|
||||||
return m_startDate;
|
return m_timeFilters;
|
||||||
}
|
|
||||||
|
|
||||||
void LogFilter::setEndDate(const QDateTime &endDate)
|
|
||||||
{
|
|
||||||
m_endDate = endDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
QDateTime LogFilter::endDate() const
|
|
||||||
{
|
|
||||||
return m_endDate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogFilter::addLoggingSource(const Logging::LoggingSource &source)
|
void LogFilter::addLoggingSource(const Logging::LoggingSource &source)
|
||||||
@ -157,8 +150,7 @@ QList<QString> LogFilter::values() const
|
|||||||
|
|
||||||
bool LogFilter::isEmpty() const
|
bool LogFilter::isEmpty() const
|
||||||
{
|
{
|
||||||
return m_endDate.isNull() &&
|
return m_timeFilters.isEmpty() &&
|
||||||
m_startDate.isNull() &&
|
|
||||||
m_sources.isEmpty() &&
|
m_sources.isEmpty() &&
|
||||||
m_levels.isEmpty() &&
|
m_levels.isEmpty() &&
|
||||||
m_eventTypes.isEmpty() &&
|
m_eventTypes.isEmpty() &&
|
||||||
@ -170,22 +162,50 @@ bool LogFilter::isEmpty() const
|
|||||||
QString LogFilter::createDateString() const
|
QString LogFilter::createDateString() const
|
||||||
{
|
{
|
||||||
QString query;
|
QString query;
|
||||||
if (m_startDate.isValid() && !m_endDate.isValid()) {
|
if (!m_timeFilters.isEmpty()) {
|
||||||
|
if (m_timeFilters.count() == 1) {
|
||||||
|
QPair<QDateTime, QDateTime> timeFilter = m_timeFilters.first();
|
||||||
|
query.append(createTimeFilterString(timeFilter));
|
||||||
|
} else {
|
||||||
|
query.append("( ");
|
||||||
|
QPair<QDateTime, QDateTime> timeFilter;
|
||||||
|
foreach (timeFilter, m_timeFilters) {
|
||||||
|
query.append(createTimeFilterString(timeFilter));
|
||||||
|
if (timeFilter != m_timeFilters.last())
|
||||||
|
query.append("OR ");
|
||||||
|
}
|
||||||
|
query.append(") ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString LogFilter::createTimeFilterString(QPair<QDateTime, QDateTime> timeFilter) const
|
||||||
|
{
|
||||||
|
QString query;
|
||||||
|
QDateTime startDate = timeFilter.first;
|
||||||
|
QDateTime endDate = timeFilter.second;
|
||||||
|
|
||||||
|
qCDebug(dcLogEngine) << "create timefiler for" << startDate.toString() << endDate.toString();
|
||||||
|
|
||||||
|
query.append("( ");
|
||||||
|
if (startDate.isValid() && !endDate.isValid()) {
|
||||||
// only start date is valid
|
// only start date is valid
|
||||||
query.append(QString("timestamp BETWEEN '%1' AND '%2' ")
|
query.append(QString("timestamp BETWEEN '%1' AND '%2' ")
|
||||||
.arg(m_startDate.toMSecsSinceEpoch())
|
.arg(startDate.toTime_t())
|
||||||
.arg(QDateTime::currentDateTime().toMSecsSinceEpoch()));
|
.arg(QDateTime::currentDateTime().toTime_t()));
|
||||||
} else if (!m_startDate.isValid() && m_endDate.isValid()) {
|
} else if (!startDate.isValid() && endDate.isValid()) {
|
||||||
// only end date is valid
|
// only end date is valid
|
||||||
query.append(QString("timestamp NOT BETWEEN '%1' AND '%2' ")
|
query.append(QString("timestamp NOT BETWEEN '%1' AND '%2' ")
|
||||||
.arg(m_endDate.toMSecsSinceEpoch())
|
.arg(endDate.toTime_t())
|
||||||
.arg(QDateTime::currentDateTime().toMSecsSinceEpoch()));
|
.arg(QDateTime::currentDateTime().toTime_t()));
|
||||||
} else if (m_startDate.isValid() && m_endDate.isValid()) {
|
} else if (startDate.isValid() && endDate.isValid()) {
|
||||||
// both dates are valid
|
// both dates are valid
|
||||||
query.append(QString("timestamp BETWEEN '%1' AND '%2' ")
|
query.append(QString("timestamp BETWEEN '%1' AND '%2' ")
|
||||||
.arg(m_startDate.toMSecsSinceEpoch())
|
.arg(startDate.toTime_t())
|
||||||
.arg(m_endDate.toMSecsSinceEpoch()));
|
.arg(endDate.toTime_t()));
|
||||||
}
|
}
|
||||||
|
query.append(") ");
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,7 @@
|
|||||||
#ifndef LOGFILTER_H
|
#ifndef LOGFILTER_H
|
||||||
#define LOGFILTER_H
|
#define LOGFILTER_H
|
||||||
|
|
||||||
|
#include <QPair>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
@ -36,11 +37,9 @@ public:
|
|||||||
|
|
||||||
QString queryString() const;
|
QString queryString() const;
|
||||||
|
|
||||||
void setStartDate(const QDateTime &startDate);
|
|
||||||
QDateTime startDate() const;
|
|
||||||
|
|
||||||
void setEndDate(const QDateTime &endDate);
|
void addTimeFilter(const QDateTime &startDate = QDateTime(), const QDateTime &endDate = QDateTime());
|
||||||
QDateTime endDate() const;
|
QList<QPair<QDateTime, QDateTime> > timeFilters() const;
|
||||||
|
|
||||||
void addLoggingSource(const Logging::LoggingSource &source) ;
|
void addLoggingSource(const Logging::LoggingSource &source) ;
|
||||||
QList<Logging::LoggingSource> loggingSources() const;
|
QList<Logging::LoggingSource> loggingSources() const;
|
||||||
@ -66,9 +65,7 @@ public:
|
|||||||
bool isEmpty() const;
|
bool isEmpty() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QDateTime m_startDate;
|
QList<QPair<QDateTime, QDateTime > > m_timeFilters;
|
||||||
QDateTime m_endDate;
|
|
||||||
|
|
||||||
QList<Logging::LoggingSource> m_sources;
|
QList<Logging::LoggingSource> m_sources;
|
||||||
QList<Logging::LoggingLevel> m_levels;
|
QList<Logging::LoggingLevel> m_levels;
|
||||||
QList<Logging::LoggingEventType> m_eventTypes;
|
QList<Logging::LoggingEventType> m_eventTypes;
|
||||||
@ -77,6 +74,7 @@ private:
|
|||||||
QList<QString> m_values;
|
QList<QString> m_values;
|
||||||
|
|
||||||
QString createDateString() const;
|
QString createDateString() const;
|
||||||
|
QString createTimeFilterString(QPair<QDateTime, QDateTime> timeFilter) const;
|
||||||
QString createSourcesString() const;
|
QString createSourcesString() const;
|
||||||
QString createLevelsString() const;
|
QString createLevelsString() const;
|
||||||
QString createEventTypesString() const;
|
QString createEventTypesString() const;
|
||||||
|
|||||||
@ -278,6 +278,30 @@
|
|||||||
"Logging.GetLogEntries": {
|
"Logging.GetLogEntries": {
|
||||||
"description": "Get the LogEntries matching the given filter.",
|
"description": "Get the LogEntries matching the given filter.",
|
||||||
"params": {
|
"params": {
|
||||||
|
"o:deviceIds": [
|
||||||
|
"Uuid"
|
||||||
|
],
|
||||||
|
"o:eventTypes": [
|
||||||
|
"$ref:LoggingEventType"
|
||||||
|
],
|
||||||
|
"o:loggingLevels": [
|
||||||
|
"$ref:LoggingLevel"
|
||||||
|
],
|
||||||
|
"o:loggingSources": [
|
||||||
|
"$ref:LoggingSource"
|
||||||
|
],
|
||||||
|
"o:timeFilters": [
|
||||||
|
{
|
||||||
|
"o:endDate": "Int",
|
||||||
|
"o:startDate": "Int"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"o:typeIds": [
|
||||||
|
"Uuid"
|
||||||
|
],
|
||||||
|
"o:values": [
|
||||||
|
"Variant"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"returns": {
|
"returns": {
|
||||||
"loggingError": "$ref:LoggingError",
|
"loggingError": "$ref:LoggingError",
|
||||||
@ -434,7 +458,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Logging.LogEntryAdded": {
|
"Logging.LogEntryAdded": {
|
||||||
"description": "Emitted whenever an entry is appended to the logging system.",
|
"description": "Emitted whenever an entry is appended to the logging system. The filters can be combinend.",
|
||||||
"params": {
|
"params": {
|
||||||
"logEntry": "$ref:LogEntry"
|
"logEntry": "$ref:LogEntry"
|
||||||
}
|
}
|
||||||
@ -603,7 +627,8 @@
|
|||||||
},
|
},
|
||||||
"LoggingError": [
|
"LoggingError": [
|
||||||
"LoggingErrorNoError",
|
"LoggingErrorNoError",
|
||||||
"LoggingErrorLogEntryNotFound"
|
"LoggingErrorLogEntryNotFound",
|
||||||
|
"LoggingErrorInvalidFilterParameter"
|
||||||
],
|
],
|
||||||
"LoggingEventType": [
|
"LoggingEventType": [
|
||||||
"LoggingEventTypeTrigger",
|
"LoggingEventTypeTrigger",
|
||||||
|
|||||||
Reference in New Issue
Block a user