From 80bd748d0d69f5f1191362ba3e760322c9e58479 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 16 Apr 2021 16:56:08 +0200 Subject: [PATCH] some fixes in the graph series adapter --- libnymea-app/connection/awsclient.cpp | 4 +-- libnymea-app/models/logsmodel.cpp | 38 +++++++++++++++++++++ libnymea-app/models/logsmodel.h | 1 + libnymea-app/models/xyseriesadapter.cpp | 45 ++++++++++++++----------- libnymea-app/models/xyseriesadapter.h | 2 +- libnymea-app/rulemanager.cpp | 5 ++- 6 files changed, 72 insertions(+), 23 deletions(-) diff --git a/libnymea-app/connection/awsclient.cpp b/libnymea-app/connection/awsclient.cpp index 29af21ec..7f697dc7 100644 --- a/libnymea-app/connection/awsclient.cpp +++ b/libnymea-app/connection/awsclient.cpp @@ -627,8 +627,8 @@ void AWSClient::getId() QJsonDocument jsonDoc = QJsonDocument::fromVariant(params); QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact); -// qDebug() << "Posting:" << request.url().toString(); -// qDebug() << "Payload:" << payload; + qCDebug(dcCloud()) << "Posting:" << request.url().toString(); + qDebug(dcCloud()) << "Payload:" << payload; QNetworkReply *reply = m_nam->post(request, payload); connect(reply, &QNetworkReply::finished, this, [this, reply]() { reply->deleteLater(); diff --git a/libnymea-app/models/logsmodel.cpp b/libnymea-app/models/logsmodel.cpp index c9020549..2fa965ba 100644 --- a/libnymea-app/models/logsmodel.cpp +++ b/libnymea-app/models/logsmodel.cpp @@ -211,6 +211,44 @@ LogEntry *LogsModel::get(int index) const return nullptr; } +LogEntry *LogsModel::findClosest(const QDateTime &dateTime) +{ +// qWarning() << "********************Finding closest for:" << dateTime.time().toString(); + int newest = 0; + int oldest = m_list.count() - 1; + LogEntry *entry = nullptr; + int step = 0; + while (oldest > newest && step < m_list.count()) { + LogEntry *oldestEntry = m_list.at(oldest); + LogEntry *newestEntry = m_list.at(newest); + int middle = (oldest - newest) / 2 + newest; + LogEntry *middleEntry = m_list.at(middle); +// qWarning() << "Oldest:" << oldestEntry->timestamp().time().toString() << "Middle:" << middleEntry->timestamp().time().toString() << "Newest:" << newestEntry->timestamp().time().toString() << ":" << (oldest - newest); + if (dateTime <= oldestEntry->timestamp()) { + return oldestEntry; + } + if (dateTime >= newestEntry->timestamp()) { + return newestEntry; + } + + if (dateTime == middleEntry->timestamp()) { + return middleEntry; + } + + if (dateTime < middleEntry->timestamp()) { + newest = middle; + } else { + oldest = middle; + } + + if (oldest - newest == 1) { + return newestEntry; + } + step++; + } + return entry; +} + void LogsModel::logsReply(int /*commandId*/, const QVariantMap &data) { int offset = data.value("offset").toInt(); diff --git a/libnymea-app/models/logsmodel.h b/libnymea-app/models/logsmodel.h index aebf4ef4..f52179d2 100644 --- a/libnymea-app/models/logsmodel.h +++ b/libnymea-app/models/logsmodel.h @@ -97,6 +97,7 @@ public: void setViewStartTime(const QDateTime &viewStartTime); Q_INVOKABLE LogEntry* get(int index) const; + Q_INVOKABLE LogEntry* findClosest(const QDateTime &dateTime); signals: diff --git a/libnymea-app/models/xyseriesadapter.cpp b/libnymea-app/models/xyseriesadapter.cpp index 3727e472..5d431ee8 100644 --- a/libnymea-app/models/xyseriesadapter.cpp +++ b/libnymea-app/models/xyseriesadapter.cpp @@ -50,12 +50,12 @@ void XYSeriesAdapter::setBaseSeries(QtCharts::QXYSeries *series) m_series->replace(index, m_series->at(index).x(), value); if (value < m_minValue) { m_minValue = value; - qDebug() << "New min:" << m_minValue; +// qDebug() << "New min:" << m_minValue; emit minValueChanged(); } if (value > m_maxValue) { m_maxValue = value; - qDebug() << "New max:" << m_maxValue; +// qDebug() << "New max:" << m_maxValue; emit maxValueChanged(); } } @@ -66,12 +66,12 @@ void XYSeriesAdapter::setBaseSeries(QtCharts::QXYSeries *series) m_series->replace(index, m_series->at(index).x(), value); if (value < m_minValue) { m_minValue = value; - qDebug() << "New min:" << m_minValue; +// qDebug() << "New min:" << m_minValue; emit minValueChanged(); } if (value > m_maxValue) { m_maxValue = value; - qDebug() << "New max:" << m_maxValue; +// qDebug() << "New max:" << m_maxValue; emit maxValueChanged(); } } @@ -117,6 +117,7 @@ qreal XYSeriesAdapter::minValue() const void XYSeriesAdapter::ensureSamples(const QDateTime &from, const QDateTime &to) { +// qWarning() << "Ensuring samples:" << from.toString("yyyy-MM-dd hh:mm:ss") << to.toString("yyyy-MM-dd hh:mm:ss"); if (!m_series) { return; } @@ -153,6 +154,7 @@ void XYSeriesAdapter::logEntryAdded(LogEntry *entry) return; } + ensureSamples(entry->timestamp(), entry->timestamp()); int idx = entry->timestamp().secsTo(m_newestSample) / m_sampleRate; @@ -161,19 +163,12 @@ void XYSeriesAdapter::logEntryAdded(LogEntry *entry) return; } Sample *sample = m_samples.at(static_cast(idx)); + LogEntry *oldLast = sample->entries.count() > 0 ? sample->entries.last() : nullptr; sample->entries.append(entry); - for (int i = idx; i > 0; i--) { - Sample *nextSample = m_samples.at(i); - if (!nextSample->last) { - nextSample->last = entry; - m_series->replace(i, nextSample->timestamp.toMSecsSinceEpoch(), calculateSampleValue(i)); - } else { - break; - } - } qreal value = calculateSampleValue(idx); m_series->replace(idx, sample->timestamp.toMSecsSinceEpoch(), value); +// qWarning() << "sample value added" << idx << entry->timestamp().time().toString("hh:mm:ss") << value; if (value < m_minValue) { m_minValue = value; @@ -185,6 +180,20 @@ void XYSeriesAdapter::logEntryAdded(LogEntry *entry) // qDebug() << "New max:" << m_maxValue; emit maxValueChanged(); } + + // check if we need to update more samples + for (int i = idx - 1; i >= 0; i--) { + Sample *nextSample = m_samples.at(i); + if (nextSample->startingPoint == oldLast) { + nextSample->startingPoint = entry; + qreal value = calculateSampleValue(i); +// qWarning() << "Updating" << i << value; + m_series->replace(i, nextSample->timestamp.toMSecsSinceEpoch(), value); + + } else { + break; + } + } } qreal XYSeriesAdapter::calculateSampleValue(int index) @@ -192,13 +201,11 @@ qreal XYSeriesAdapter::calculateSampleValue(int index) Sample *sample = m_samples.at(index); qreal value = 0; int count = 0; - if (m_samples.length() > index + 1) { - Sample *previousSample = m_samples.at(static_cast(index) + 1); - if (previousSample->last) { - value = previousSample->last->value().toDouble(); - count++; - } + if (sample->startingPoint) { + value = sample->startingPoint->value().toDouble(); + count++; } + foreach (LogEntry *entry, sample->entries) { value += entry->value().toDouble(); count++; diff --git a/libnymea-app/models/xyseriesadapter.h b/libnymea-app/models/xyseriesadapter.h index feb114aa..001b6d12 100644 --- a/libnymea-app/models/xyseriesadapter.h +++ b/libnymea-app/models/xyseriesadapter.h @@ -70,7 +70,7 @@ private: public: QDateTime timestamp; // The timestamp where this sample *ends* QList entries; // all log entries in this sample, that is, from timestamp - m_sampleRate - LogEntry *last = nullptr; + LogEntry *startingPoint = nullptr; // the starting point for the same. Normally the last entry of the previous sample }; LogsModel* m_model = nullptr; QtCharts::QXYSeries* m_series = nullptr; diff --git a/libnymea-app/rulemanager.cpp b/libnymea-app/rulemanager.cpp index 4ebf6c7c..6e019aee 100644 --- a/libnymea-app/rulemanager.cpp +++ b/libnymea-app/rulemanager.cpp @@ -47,6 +47,9 @@ #include "types/repeatingoption.h" #include "types/calendaritems.h" #include "types/calendaritem.h" +#include "logging.h" + +NYMEA_LOGGING_CATEGORY(dcRuleManager, "RuleManager") #include #include @@ -92,7 +95,7 @@ int RuleManager::addRule(const QVariantMap params) int RuleManager::addRule(Rule *rule) { QVariantMap params = packRule(rule); - qDebug() << "packed rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented)); + qCDebug(dcRuleManager) << "packed rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented)); return m_jsonClient->sendCommand("Rules.AddRule", params, this, "onAddRuleReply"); }