Merge PR #588: some fixes in the graph series adapter

This commit is contained in:
Jenkins nymea 2021-04-16 17:49:51 +02:00
commit 97214a71bb
6 changed files with 72 additions and 23 deletions

View File

@ -627,8 +627,8 @@ void AWSClient::getId()
QJsonDocument jsonDoc = QJsonDocument::fromVariant(params); QJsonDocument jsonDoc = QJsonDocument::fromVariant(params);
QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact); QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact);
// qDebug() << "Posting:" << request.url().toString(); qCDebug(dcCloud()) << "Posting:" << request.url().toString();
// qDebug() << "Payload:" << payload; qDebug(dcCloud()) << "Payload:" << payload;
QNetworkReply *reply = m_nam->post(request, payload); QNetworkReply *reply = m_nam->post(request, payload);
connect(reply, &QNetworkReply::finished, this, [this, reply]() { connect(reply, &QNetworkReply::finished, this, [this, reply]() {
reply->deleteLater(); reply->deleteLater();

View File

@ -211,6 +211,44 @@ LogEntry *LogsModel::get(int index) const
return nullptr; 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) void LogsModel::logsReply(int /*commandId*/, const QVariantMap &data)
{ {
int offset = data.value("offset").toInt(); int offset = data.value("offset").toInt();

View File

@ -97,6 +97,7 @@ public:
void setViewStartTime(const QDateTime &viewStartTime); void setViewStartTime(const QDateTime &viewStartTime);
Q_INVOKABLE LogEntry* get(int index) const; Q_INVOKABLE LogEntry* get(int index) const;
Q_INVOKABLE LogEntry* findClosest(const QDateTime &dateTime);
signals: signals:

View File

@ -50,12 +50,12 @@ void XYSeriesAdapter::setBaseSeries(QtCharts::QXYSeries *series)
m_series->replace(index, m_series->at(index).x(), value); m_series->replace(index, m_series->at(index).x(), value);
if (value < m_minValue) { if (value < m_minValue) {
m_minValue = value; m_minValue = value;
qDebug() << "New min:" << m_minValue; // qDebug() << "New min:" << m_minValue;
emit minValueChanged(); emit minValueChanged();
} }
if (value > m_maxValue) { if (value > m_maxValue) {
m_maxValue = value; m_maxValue = value;
qDebug() << "New max:" << m_maxValue; // qDebug() << "New max:" << m_maxValue;
emit maxValueChanged(); emit maxValueChanged();
} }
} }
@ -66,12 +66,12 @@ void XYSeriesAdapter::setBaseSeries(QtCharts::QXYSeries *series)
m_series->replace(index, m_series->at(index).x(), value); m_series->replace(index, m_series->at(index).x(), value);
if (value < m_minValue) { if (value < m_minValue) {
m_minValue = value; m_minValue = value;
qDebug() << "New min:" << m_minValue; // qDebug() << "New min:" << m_minValue;
emit minValueChanged(); emit minValueChanged();
} }
if (value > m_maxValue) { if (value > m_maxValue) {
m_maxValue = value; m_maxValue = value;
qDebug() << "New max:" << m_maxValue; // qDebug() << "New max:" << m_maxValue;
emit maxValueChanged(); emit maxValueChanged();
} }
} }
@ -117,6 +117,7 @@ qreal XYSeriesAdapter::minValue() const
void XYSeriesAdapter::ensureSamples(const QDateTime &from, const QDateTime &to) 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) { if (!m_series) {
return; return;
} }
@ -153,6 +154,7 @@ void XYSeriesAdapter::logEntryAdded(LogEntry *entry)
return; return;
} }
ensureSamples(entry->timestamp(), entry->timestamp()); ensureSamples(entry->timestamp(), entry->timestamp());
int idx = entry->timestamp().secsTo(m_newestSample) / m_sampleRate; int idx = entry->timestamp().secsTo(m_newestSample) / m_sampleRate;
@ -161,19 +163,12 @@ void XYSeriesAdapter::logEntryAdded(LogEntry *entry)
return; return;
} }
Sample *sample = m_samples.at(static_cast<int>(idx)); Sample *sample = m_samples.at(static_cast<int>(idx));
LogEntry *oldLast = sample->entries.count() > 0 ? sample->entries.last() : nullptr;
sample->entries.append(entry); 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); qreal value = calculateSampleValue(idx);
m_series->replace(idx, sample->timestamp.toMSecsSinceEpoch(), value); 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) { if (value < m_minValue) {
m_minValue = value; m_minValue = value;
@ -185,6 +180,20 @@ void XYSeriesAdapter::logEntryAdded(LogEntry *entry)
// qDebug() << "New max:" << m_maxValue; // qDebug() << "New max:" << m_maxValue;
emit maxValueChanged(); 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) qreal XYSeriesAdapter::calculateSampleValue(int index)
@ -192,13 +201,11 @@ qreal XYSeriesAdapter::calculateSampleValue(int index)
Sample *sample = m_samples.at(index); Sample *sample = m_samples.at(index);
qreal value = 0; qreal value = 0;
int count = 0; int count = 0;
if (m_samples.length() > index + 1) { if (sample->startingPoint) {
Sample *previousSample = m_samples.at(static_cast<int>(index) + 1); value = sample->startingPoint->value().toDouble();
if (previousSample->last) { count++;
value = previousSample->last->value().toDouble();
count++;
}
} }
foreach (LogEntry *entry, sample->entries) { foreach (LogEntry *entry, sample->entries) {
value += entry->value().toDouble(); value += entry->value().toDouble();
count++; count++;

View File

@ -70,7 +70,7 @@ private:
public: public:
QDateTime timestamp; // The timestamp where this sample *ends* QDateTime timestamp; // The timestamp where this sample *ends*
QList<LogEntry*> entries; // all log entries in this sample, that is, from timestamp - m_sampleRate QList<LogEntry*> 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; LogsModel* m_model = nullptr;
QtCharts::QXYSeries* m_series = nullptr; QtCharts::QXYSeries* m_series = nullptr;

View File

@ -47,6 +47,9 @@
#include "types/repeatingoption.h" #include "types/repeatingoption.h"
#include "types/calendaritems.h" #include "types/calendaritems.h"
#include "types/calendaritem.h" #include "types/calendaritem.h"
#include "logging.h"
NYMEA_LOGGING_CATEGORY(dcRuleManager, "RuleManager")
#include <QMetaEnum> #include <QMetaEnum>
#include <QJsonDocument> #include <QJsonDocument>
@ -92,7 +95,7 @@ int RuleManager::addRule(const QVariantMap params)
int RuleManager::addRule(Rule *rule) int RuleManager::addRule(Rule *rule)
{ {
QVariantMap params = packRule(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"); return m_jsonClient->sendCommand("Rules.AddRule", params, this, "onAddRuleReply");
} }