Commit daafb56d authored by Arjen Hiemstra's avatar Arjen Hiemstra
Browse files

SensorDataModel: Cache minimum and maximum values

The minimum and maxmimum properties only change when the sensor metadata
changes, so we can cache them and avoid some extra work when they get
queried repeatedly.


(cherry picked from commit 57e3ba00)
parent 6d727895
......@@ -18,11 +18,14 @@
Boston, MA 02110-1301, USA.
*/
#include "SensorDataModel.h"
#include <optional>
#include <QMetaEnum>
#include "Formatter.h"
#include "SensorDaemonInterface_p.h"
#include "SensorDataModel.h"
#include "SensorInfo_p.h"
#include "sensors_logging.h"
......@@ -54,6 +57,9 @@ public:
bool loaded = false;
bool enabled = true;
std::optional<qreal> minimum;
std::optional<qreal> maximum;
private:
SensorDataModel *q;
};
......@@ -208,8 +214,13 @@ qreal SensorDataModel::minimum() const
return 0;
}
if (d->minimum.has_value()) {
return d->minimum.value();
}
auto result = std::min_element(d->sensorInfos.cbegin(), d->sensorInfos.cend(), [](const SensorInfo &first, const SensorInfo &second) { return first.min < second.min; });
return (*result).min;
d->minimum = (*result).min;
return d->minimum.value();
}
qreal SensorDataModel::maximum() const
......@@ -218,8 +229,13 @@ qreal SensorDataModel::maximum() const
return 0;
}
if (d->maximum.has_value()) {
return d->maximum.value();
}
auto result = std::max_element(d->sensorInfos.cbegin(), d->sensorInfos.cend(), [](const SensorInfo &first, const SensorInfo &second) { return first.max < second.max; });
return (*result).max;
d->maximum = (*result).max;
return d->maximum.value();
}
QStringList SensorDataModel::sensors() const
......@@ -386,6 +402,9 @@ void SensorDataModel::onMetaDataChanged(const QString &sensorId, const SensorInf
d->sensorData[sensorId] = QVariant{};
endInsertColumns();
d->minimum.reset();
d->maximum.reset();
SensorDaemonInterface::instance()->requestValue(sensorId);
emit sensorMetaDataChanged();
Q_EMIT readyChanged();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment