From 557c47fe49f907cc5c89337303cb6bd69824980c Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 12 Sep 2023 16:47:46 +0200 Subject: [PATCH] Fix some issues with logs Fixes state charts sometimes requiring a refresh to paint completely Fixes log view lists inserting live entries on the wrong end --- libnymea-app/models/newlogsmodel.cpp | 84 +++++++++++++++++-------- nymea-app/ui/customviews/StateChart.qml | 36 +++++++---- 2 files changed, 83 insertions(+), 37 deletions(-) diff --git a/libnymea-app/models/newlogsmodel.cpp b/libnymea-app/models/newlogsmodel.cpp index 69db9584..bdd3dcd0 100644 --- a/libnymea-app/models/newlogsmodel.cpp +++ b/libnymea-app/models/newlogsmodel.cpp @@ -266,32 +266,64 @@ NewLogEntry *NewLogsModel::find(const QDateTime ×tamp) const return entry; } qint64 diff = timestamp.msecsTo(entry->timestamp()); - if (entry->timestamp() > timestamp) { -// qCDebug(dcLogEngine()) << "entry is newer than searched:" << entry->timestamp().toString() << timestamp.toString(); - if (idx == 0) { -// qCDebug(dcLogEngine()) << "Is oldest."; - return entry; + if (m_sortOrder == Qt::AscendingOrder) { + if (entry->timestamp() > timestamp) { +// qCDebug(dcLogEngine()) << "entry is newer than searched:" << entry->timestamp().toString() << timestamp.toString(); + if (idx == 0) { +// qCDebug(dcLogEngine()) << "Is oldest."; + return entry; + } + NewLogEntry *previousEntry = m_list.at(idx-1); + if (previousEntry->timestamp() < timestamp) { + qint64 previousDiff = timestamp.msecsTo(previousEntry->timestamp()); +// qCDebug(dcLogEngine()) << "time between this and previous:" << entry->timestamp().toString() << previousEntry->timestamp().toString() << (qAbs(previousDiff) < qAbs(diff) ? "next" : "this"); + return qAbs(previousDiff) < qAbs(diff) ? previousEntry : entry; + } + idx -= jump; + } else if (entry->timestamp() < timestamp) { +// qCDebug(dcLogEngine()) << "entry is older than searched:" << entry->timestamp().toString() << timestamp.toString(); + if (idx == m_list.count() - 1) { +// qCDebug(dcLogEngine()) << "Is newest."; + return entry; + } + NewLogEntry *nextEntry = m_list.at(idx+1); + if (nextEntry->timestamp() > timestamp) { + qint64 nextDiff = timestamp.msecsTo(nextEntry->timestamp()); +// qCDebug(dcLogEngine()) << "time between next and this:" << nextEntry->timestamp().toString() << "-" << entry->timestamp().toString() << (qAbs(nextDiff) > qAbs(diff) ? "prev" : "this"); + return qAbs(nextDiff) < qAbs(diff) ? nextEntry : entry; + } + idx += jump; } - NewLogEntry *previousEntry = m_list.at(idx-1); - if (previousEntry->timestamp() < timestamp) { - qint64 previousDiff = timestamp.msecsTo(previousEntry->timestamp()); -// qCDebug(dcLogEngine()) << "time between this and previous:" << entry->timestamp().toString() << previousEntry->timestamp().toString() << (qAbs(previousDiff) < qAbs(diff) ? "next" : "this"); - return qAbs(previousDiff) < qAbs(diff) ? previousEntry : entry; + } else { + if (entry->timestamp() > timestamp) { +// qCDebug(dcLogEngine()) << "entry is newer than searched:" << entry->timestamp().toString() << timestamp.toString(); + if (idx == m_list.count() - 1) { +// qCDebug(dcLogEngine()) << "Is newest."; + return entry; + } + NewLogEntry *previousEntry = m_list.at(idx+1); +// qCDebug(dcLogEngine) << "previous:" << previousEntry->timestamp().toString(); + if (previousEntry->timestamp() < timestamp) { + qint64 previousDiff = timestamp.msecsTo(previousEntry->timestamp()); +// qCDebug(dcLogEngine()) << "time between previous and this:" << previousEntry->timestamp().toString() << entry->timestamp().toString() << previousDiff; + return qAbs(previousDiff) < qAbs(diff) ? previousEntry : entry; + } + idx += jump; + } else if (entry->timestamp() < timestamp) { +// qCDebug(dcLogEngine()) << "entry is older than searched:" << entry->timestamp().toString() << timestamp.toString(); + if (idx == 0) { +// qCDebug(dcLogEngine()) << "Is oldest."; + return entry; + } + NewLogEntry *nextEntry = m_list.at(idx-1); +// qCDebug(dcLogEngine) << "next:" << nextEntry; + if (nextEntry->timestamp() > timestamp) { + qint64 nextDiff = timestamp.msecsTo(nextEntry->timestamp()); +// qCDebug(dcLogEngine()) << "time between this and next:" << entry->timestamp().toString() << nextEntry->timestamp().toString() << nextDiff; + return qAbs(nextDiff) < qAbs(diff) ? nextEntry : entry; + } + idx -= jump; } - idx -= jump; - } else if (entry->timestamp() < timestamp) { -// qCDebug(dcLogEngine()) << "entry is older than searched:" << entry->timestamp().toString() << timestamp.toString(); - if (idx == m_list.count() - 1) { -// qCDebug(dcLogEngine()) << "Is newest."; - return entry; - } - NewLogEntry *nextEntry = m_list.at(idx+1); - if (nextEntry->timestamp() > timestamp) { - qint64 nextDiff = timestamp.msecsTo(nextEntry->timestamp()); -// qCDebug(dcLogEngine()) << "time between next and this:" << nextEntry->timestamp().toString() << "-" << entry->timestamp().toString() << (qAbs(nextDiff) > qAbs(diff) ? "prev" : "this"); - return qAbs(nextDiff) < qAbs(diff) ? nextEntry : entry; - } - idx += jump; } jump = qMax(1, jump / 2); }; @@ -422,9 +454,11 @@ void NewLogsModel::newLogEntryReceived(const QVariantMap &map) QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(map.value("timestamp").toULongLong()); QVariantMap values = map.value("values").toMap(); + if (m_sources.contains(source) && m_sampleRate == SampleRateAny) { + qCritical() << "New entry!" << m_sources << source << m_sampleRate; NewLogEntry *entry = new NewLogEntry(source, timestamp, values, this); - if (m_sortOrder == Qt::AscendingOrder) { + if (m_sortOrder == Qt::DescendingOrder) { beginInsertRows(QModelIndex(), m_list.count(), m_list.count()); m_list.append(entry); endInsertRows(); diff --git a/nymea-app/ui/customviews/StateChart.qml b/nymea-app/ui/customviews/StateChart.qml index f12c2289..b7b0447c 100644 --- a/nymea-app/ui/customviews/StateChart.qml +++ b/nymea-app/ui/customviews/StateChart.qml @@ -59,13 +59,25 @@ Item { startTime: new Date(d.startTime.getTime() - d.range * 1.1 * 60000) endTime: new Date(d.endTime.getTime() + d.range * 1.1 * 60000) sampleRate: d.sampleRate + sortOrder: Qt.DescendingOrder Component.onCompleted: { - if (source != "") { + print("****** completed") + ready = true + update() + } + property bool ready: false + onSourceChanged: { + print("***** source changed") + update() + } + + function update() { + print("*********+ source", source, "start", startTime, "end", endTime, ready) + if (ready && source != "") { fetchLogs() } } - onSourceChanged: fetchLogs() property double minValue property double maxValue @@ -74,7 +86,7 @@ Item { print("**** entries added", index, count, "entries in series:", valueSeries.count, "in model", logsModel.count) for (var i = 0; i < count; i++) { var entry = logsModel.get(i) - // print("entry", entry.timestamp, entry.source, JSON.stringify(entry.values)) + print("entry", entry.timestamp, entry.source, JSON.stringify(entry.values)) zeroSeries.ensureValue(entry.timestamp) if (root.stateType.type.toLowerCase() == "bool") { @@ -86,8 +98,8 @@ Item { // for booleans, we'll insert the opposite value right before the new one so the position is doubled var insertIdx = (index + i) * 2 - valueSeries.insert(insertIdx, entry.timestamp.getTime() - 500, !value) - valueSeries.insert(insertIdx+1, entry.timestamp, value) + valueSeries.insert(insertIdx+1, entry.timestamp.getTime() - 500, !value) + valueSeries.insert(insertIdx, entry.timestamp, value) } else { var value = entry.values[root.stateType.name] @@ -105,9 +117,9 @@ Item { } if (root.stateType.type.toLowerCase() == "bool") { - var last = valueSeries.at(valueSeries.count-1); + var last = valueSeries.at(0); if (last.x < d.endTime) { - valueSeries.append(d.endTime, last.y) + valueSeries.insert(0, d.endTime, last.y) zeroSeries.ensureValue(d.endTime) } } @@ -333,22 +345,22 @@ Item { borderWidth: 2 lowerSeries: LineSeries { id: zeroSeries - XYPoint { x: dateTimeAxis.min.getTime(); y: 0 } XYPoint { x: dateTimeAxis.max.getTime(); y: 0 } + XYPoint { x: dateTimeAxis.min.getTime(); y: 0 } function ensureValue(timestamp) { if (count == 0) { append(timestamp, 0) } else if (count == 1) { if (timestamp.getTime() < at(0).x) { - insert(0, timestamp, 0) - } else { append(timestamp, 0) + } else { + insert(0, timestamp, 0) } } else { - if (timestamp.getTime() < at(0).x) { + if (timestamp.getTime() > at(0).x) { remove(0) insert(0, timestamp, 0) - } else if (timestamp.getTime() > at(1).x) { + } else if (timestamp.getTime() < at(1).x) { remove(1) append(timestamp, 0) }