Fix some glitches in energy chats if the user changes the time zone
This commit is contained in:
parent
b9fd4b5961
commit
5fbabfa519
@ -184,19 +184,51 @@ EnergyLogEntry *EnergyLogs::get(int index) const
|
|||||||
return m_list.at(index);
|
return m_list.at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
EnergyLogEntry *EnergyLogs::find(const QDateTime ×tamp)
|
int EnergyLogs::indexOf(const QDateTime ×tamp)
|
||||||
{
|
{
|
||||||
if (m_list.isEmpty()) {
|
if (m_list.isEmpty()) {
|
||||||
return nullptr;
|
return -1;
|
||||||
}
|
}
|
||||||
QDateTime first = m_list.first()->timestamp();
|
QDateTime first = m_list.first()->timestamp();
|
||||||
|
|
||||||
int index = qRound(1.0 * first.secsTo(timestamp) / (m_sampleRate * 60));
|
int index = qRound(1.0 * first.secsTo(timestamp) / (m_sampleRate * 60));
|
||||||
if (index < 0 || index >= m_list.count()) {
|
if (index < 0 || index >= m_list.count()) {
|
||||||
// qWarning() << "finding:" << timestamp << index << first.toString() << "NOT FOUND" << m_list.last()->timestamp();
|
// qWarning() << "finding:" << timestamp << index << first.toString() << "NOT FOUND" << m_list.last()->timestamp();
|
||||||
return nullptr;
|
return -1;
|
||||||
}
|
}
|
||||||
// qWarning() << "finding:" << timestamp << index << first.toString() << m_list.at(index)->timestamp();
|
// qWarning() << "finding:" << timestamp << index << first.toString() << m_list.at(index)->timestamp();
|
||||||
|
|
||||||
|
|
||||||
|
// Normally, if the DB is in a consistent state, we can rely that the above finds the correct entry.
|
||||||
|
// However, if the user changes the timezone, during the lifetime, or other woes may appear like NTP
|
||||||
|
// changing time which may cause inconsistent entries like passing the same time twice, we could end up
|
||||||
|
// off by one. In order to compensate for that, we'll see if the next or previous entries may be closer
|
||||||
|
// In theory we could even be off by some more samples in very rare circumstances, but unlikely enough
|
||||||
|
// to not bother with that at this point.
|
||||||
|
QDateTime found = m_list.at(index)->timestamp();
|
||||||
|
QDateTime previous = index > 0 ? m_list.at(index-1)->timestamp() : found;
|
||||||
|
QDateTime next = index < m_list.count() - 1 ? m_list.at(index+1)->timestamp() : found;
|
||||||
|
|
||||||
|
int diffToFound = qAbs(timestamp.secsTo(found));
|
||||||
|
int diffToPrevious = qAbs(timestamp.secsTo(previous));
|
||||||
|
int diffToNext = qAbs(timestamp.secsTo(next));
|
||||||
|
if (diffToPrevious < diffToFound && diffToPrevious < diffToNext) {
|
||||||
|
// qWarning() << "Correcting to previous" << index << m_list.count() << found << previous << diffToPrevious << diffToFound;
|
||||||
|
return index - 1;
|
||||||
|
}
|
||||||
|
if (diffToNext < diffToFound) {
|
||||||
|
// qWarning() << "Correcting to next" << index << m_list.count() << found << next << diffToNext << diffToFound;
|
||||||
|
return index + 1;
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
EnergyLogEntry *EnergyLogs::find(const QDateTime ×tamp)
|
||||||
|
{
|
||||||
|
int index = indexOf(timestamp);
|
||||||
|
if (index < 0) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
return m_list.at(index);
|
return m_list.at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,7 +287,7 @@ void EnergyLogs::getLogsResponse(int commandId, const QVariantMap ¶ms)
|
|||||||
|
|
||||||
if (!entries.isEmpty()) {
|
if (!entries.isEmpty()) {
|
||||||
if (m_list.isEmpty()) {
|
if (m_list.isEmpty()) {
|
||||||
qCDebug(dcEnergyLogs()) << "Energy logs received";
|
// qCDebug(dcEnergyLogs()) << "Energy logs received" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson());
|
||||||
beginInsertRows(QModelIndex(), 0, entries.count());
|
beginInsertRows(QModelIndex(), 0, entries.count());
|
||||||
m_list.append(entries);
|
m_list.append(entries);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
|
|||||||
@ -85,6 +85,7 @@ public:
|
|||||||
double maxValue() const;
|
double maxValue() const;
|
||||||
|
|
||||||
Q_INVOKABLE EnergyLogEntry* get(int index) const;
|
Q_INVOKABLE EnergyLogEntry* get(int index) const;
|
||||||
|
Q_INVOKABLE int indexOf(const QDateTime ×tamp);
|
||||||
Q_INVOKABLE EnergyLogEntry* find(const QDateTime ×tamp);
|
Q_INVOKABLE EnergyLogEntry* find(const QDateTime ×tamp);
|
||||||
Q_INVOKABLE QList<EnergyLogEntry*> entries() const;
|
Q_INVOKABLE QList<EnergyLogEntry*> entries() const;
|
||||||
|
|
||||||
|
|||||||
@ -112,21 +112,6 @@ MainViewBase {
|
|||||||
rowSpacing: 0
|
rowSpacing: 0
|
||||||
columnSpacing: 0
|
columnSpacing: 0
|
||||||
|
|
||||||
|
|
||||||
// CurrentConsumptionBalancePieChart {
|
|
||||||
// Layout.fillWidth: true
|
|
||||||
// Layout.preferredHeight: width
|
|
||||||
// energyManager: energyManager
|
|
||||||
// visible: producers.count > 0
|
|
||||||
// animationsEnabled: Qt.application.active && root.isCurrentItem
|
|
||||||
// }
|
|
||||||
// CurrentProductionBalancePieChart {
|
|
||||||
// Layout.fillWidth: true
|
|
||||||
// Layout.preferredHeight: width
|
|
||||||
// energyManager: energyManager
|
|
||||||
// visible: producers.count > 0
|
|
||||||
// animationsEnabled: Qt.application.active && root.isCurrentItem
|
|
||||||
// }
|
|
||||||
CurrentPowerBalancePieChart {
|
CurrentPowerBalancePieChart {
|
||||||
Layout.fillWidth: true
|
Layout.fillWidth: true
|
||||||
Layout.preferredHeight: width
|
Layout.preferredHeight: width
|
||||||
@ -150,18 +135,6 @@ MainViewBase {
|
|||||||
producers: producers
|
producers: producers
|
||||||
}
|
}
|
||||||
|
|
||||||
// PowerConsumptionBalanceHistory {
|
|
||||||
// Layout.fillWidth: true
|
|
||||||
// Layout.preferredHeight: width
|
|
||||||
// visible: producers.count > 0
|
|
||||||
// }
|
|
||||||
|
|
||||||
// PowerProductionBalanceHistory {
|
|
||||||
// Layout.fillWidth: true
|
|
||||||
// Layout.preferredHeight: width
|
|
||||||
// visible: producers.count > 0
|
|
||||||
// }
|
|
||||||
|
|
||||||
ConsumersPieChart {
|
ConsumersPieChart {
|
||||||
Layout.fillWidth: true
|
Layout.fillWidth: true
|
||||||
Layout.preferredHeight: width
|
Layout.preferredHeight: width
|
||||||
|
|||||||
@ -107,7 +107,7 @@ StatsBase {
|
|||||||
// print("timestamp:", timestamp, "previous:", previousTimestamp)
|
// print("timestamp:", timestamp, "previous:", previousTimestamp)
|
||||||
var entry = thingPowerLogs.find(timestamp)
|
var entry = thingPowerLogs.find(timestamp)
|
||||||
var previousEntry = thingPowerLogs.find(previousTimestamp);
|
var previousEntry = thingPowerLogs.find(previousTimestamp);
|
||||||
if (entry && (previousEntry || !d.loading)) {
|
if (timestamp < upcomingTimestamp && entry && (previousEntry || !d.loading)) {
|
||||||
// print("found entry:", entry.timestamp, previousEntry)
|
// print("found entry:", entry.timestamp, previousEntry)
|
||||||
var consumption = entry.totalConsumption
|
var consumption = entry.totalConsumption
|
||||||
if (previousEntry) {
|
if (previousEntry) {
|
||||||
|
|||||||
@ -55,17 +55,21 @@ StatsBase {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var upcomingTimestamp = root.calculateTimestamp(d.config.startTime(), d.config.sampleRate, d.config.count)
|
var upcomingTimestamp = root.calculateTimestamp(d.config.startTime(), d.config.sampleRate, d.config.count)
|
||||||
// print("refreshing config start", d.config.startTime(), "upcoming:", upcomingTimestamp, "fetchPending", d.fetchPending)
|
// print("refreshing config start", d.config.startTime(), "upcoming:", upcomingTimestamp, "fetchPending", d.fetchPending)
|
||||||
for (var i = 0; i < d.config.count; i++) {
|
for (var i = 0; i < d.config.count; i++) {
|
||||||
var timestamp = root.calculateTimestamp(d.config.startTime(), d.config.sampleRate, d.startOffset + i + 1)
|
var timestamp = root.calculateTimestamp(d.config.startTime(), d.config.sampleRate, d.startOffset + i + 1)
|
||||||
var previousTimestamp = root.calculateTimestamp(timestamp, d.config.sampleRate, -1)
|
var previousTimestamp = root.calculateTimestamp(timestamp, d.config.sampleRate, -1)
|
||||||
var entry = powerBalanceLogs.find(timestamp)
|
var idx = powerBalanceLogs.indexOf(timestamp);
|
||||||
// print("timestamp:", timestamp, "found", (entry ? entry.timestamp : ""))
|
var entry = powerBalanceLogs.get(idx)
|
||||||
|
// print("timestamp:", timestamp, "previousTimestamp:", previousTimestamp)
|
||||||
var previousEntry = powerBalanceLogs.find(previousTimestamp);
|
var previousEntry = powerBalanceLogs.find(previousTimestamp);
|
||||||
if (entry && (previousEntry || !d.loading)) {
|
if (timestamp < upcomingTimestamp && entry && (previousEntry || !d.loading)) {
|
||||||
// print("found entry:", entry.timestamp, previousEntry)
|
// print("found entry:", entry.timestamp, entry.totalConsumption)
|
||||||
// print("Acquisition", entry.totalAcquisition)
|
// if (previousEntry) {
|
||||||
|
// print("found previous:", previousEntry.timestamp, previousEntry.totalConsumption)
|
||||||
|
// }
|
||||||
var consumption = entry.totalConsumption
|
var consumption = entry.totalConsumption
|
||||||
var production = entry.totalProduction
|
var production = entry.totalProduction
|
||||||
var acquisition = entry.totalAcquisition
|
var acquisition = entry.totalAcquisition
|
||||||
|
|||||||
Reference in New Issue
Block a user