Some performance optimizations for consumers history

pull/1000/head
Michael Zanetti 2023-03-29 23:19:14 +02:00
parent 1448e9b7f3
commit 089b0079e9
5 changed files with 74 additions and 3 deletions

View File

@ -36,6 +36,7 @@
#include <QSysInfo>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include "utils/qhashqml.h"
#include "libnymea-app-core.h"
#include "libnymea-app-airconditioning.h"
@ -183,6 +184,7 @@ int main(int argc, char *argv[])
qmlRegisterUncreatableType<DashboardWebViewItem>("Nymea", 1, 0, "DashboardWebViewItem", "");
qmlRegisterSingletonType<PrivacyPolicyHelper>("NymeaApp.Utils", 1, 0, "PrivacyPolicyHelper", PrivacyPolicyHelper::qmlProvider);
qmlRegisterType<QHashQml>("NymeaApp.Utils", 1, 0, "QHash");
qmlRegisterType<MouseObserver>("Nymea", 1, 0, "MouseObserver");

View File

@ -42,7 +42,8 @@ HEADERS += \
pushnotifications.h \
platformhelper.h \
ruletemplates/messages.h \
utils/privacypolicyhelper.h
utils/privacypolicyhelper.h \
utils/qhashqml.h
SOURCES += main.cpp \
configuredhostsmodel.cpp \
@ -56,7 +57,8 @@ SOURCES += main.cpp \
pushnotifications.cpp \
platformhelper.cpp \
platformintegration/generic/screenhelper.cpp \
utils/privacypolicyhelper.cpp
utils/privacypolicyhelper.cpp \
utils/qhashqml.cpp
RESOURCES += resources.qrc \
ruletemplates.qrc \

View File

@ -375,6 +375,8 @@ Item {
readonly property Thing thing: consumers.get(index)
property AreaSeries series: null
property QHash valueCache: QHash {}
function calculateBaseValue(timestamp) {
if (index > 0) {
return consumersRepeater.itemAt(index - 1).calculateValue(timestamp)
@ -383,6 +385,12 @@ Item {
}
function calculateValue(timestamp) {
var cached = valueCache.value(timestamp)
// print("ached:", cached)
if (cached !== undefined) {
return cached//valueCache.value(timestamp)
}
var ret = calculateBaseValue(timestamp)
var entry = logs.find(timestamp)
@ -390,6 +398,7 @@ Item {
ret += entry.currentPower;
}
valueCache.insert(timestamp, ret);
// print("calculating value for", thing.name, timestamp, ret)
return ret
}
@ -408,11 +417,21 @@ Item {
series.lowerSeries.removePoints(0, 1);
series.upperSeries.removePoints(0, 1);
var oldestTimestamp = null
var newestTimestamp = null
for (var i = 0; i < count; i++) {
var entry = logs.get(index + i)
// print("got thing entry", thing.name, entry.timestamp, entry.currentPower, index + i)
zeroSeries.ensureValue(entry.timestamp)
// zeroSeries.ensureValue(entry.timestamp)
if (oldestTimestamp == null || entry.timestamp < oldestTimestamp) {
oldestTimestamp = entry.timestamp;
}
if (newestTimestamp == null || entry.timestamp > newestTimestamp) {
newestTimestamp = entry.timestamp;
}
valueAxis.adjustMax(entry.currentPower)
insertEntry(index + i, entry)
@ -421,6 +440,9 @@ Item {
}
}
zeroSeries.ensureValue(oldestTimestamp)
zeroSeries.ensureValue(newestTimestamp)
// Add the leading 0-value entry back
series.lowerSeries.insert(0, series.upperSeries.at(0).x, 0)
series.upperSeries.insert(0, series.upperSeries.at(0).x, 0)

View File

@ -0,0 +1,22 @@
#include "qhashqml.h"
QHashQml::QHashQml(QObject *parent)
: QObject{parent}
{
}
void QHashQml::insert(int key, const QVariant &value)
{
m_hash.insert(key, value);
}
bool QHashQml::contains(int key)
{
return m_hash.contains(key);
}
QVariant QHashQml::value(int key)
{
return m_hash.value(key);
}

View File

@ -0,0 +1,23 @@
#ifndef QHASHQML_H
#define QHASHQML_H
#include <QObject>
#include <QHash>
#include <QVariant>
class QHashQml : public QObject
{
Q_OBJECT
public:
explicit QHashQml(QObject *parent = nullptr);
Q_INVOKABLE void insert(int key, const QVariant &value);
Q_INVOKABLE bool contains(int key);
Q_INVOKABLE QVariant value(int key);
private:
QHash<int, QVariant> m_hash;
};
#endif // QHASHQML_H