some fixes in the graph series adapter

This commit is contained in:
Michael Zanetti 2021-04-16 16:56:08 +02:00
parent 3974cdad2e
commit 80bd748d0d
6 changed files with 72 additions and 23 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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:

View File

@ -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<int>(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<int>(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++;

View File

@ -70,7 +70,7 @@ private:
public:
QDateTime timestamp; // The timestamp where this sample *ends*
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;
QtCharts::QXYSeries* m_series = nullptr;

View File

@ -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 <QMetaEnum>
#include <QJsonDocument>
@ -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");
}