Skip to content

Commit

Permalink
feat: restore multithreaded processing
Browse files Browse the repository at this point in the history
  • Loading branch information
arcan1s committed Mar 31, 2024
1 parent ac52888 commit a20755a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
11 changes: 9 additions & 2 deletions sources/awesome-widget/plugin/awdataengineaggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,16 @@ void AWDataEngineAggregator::sensorRemoved(const QString &_sensor)
}


void AWDataEngineAggregator::updateData(KSysGuard::SensorDataList _data)
void AWDataEngineAggregator::updateData(const KSysGuard::SensorDataList &_data)
{
emit(dataUpdated(m_sensors, _data));
auto data
= std::accumulate(_data.cbegin(), _data.cend(), QList<QPair<KSysGuard::SensorInfo, KSysGuard::SensorData>>(),
[this](auto current, auto single) {
if (m_sensors.contains(single.sensorProperty))
current.append({m_sensors.value(single.sensorProperty), single});
return current;
});
emit(dataUpdated(data));
}


Expand Down
4 changes: 2 additions & 2 deletions sources/awesome-widget/plugin/awdataengineaggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class AWDataEngineAggregator : public QObject
void loadSources();

signals:
void dataUpdated(const QHash<QString, KSysGuard::SensorInfo> &_sensors, const KSysGuard::SensorDataList &_data);
void dataUpdated(const QList<QPair<KSysGuard::SensorInfo, KSysGuard::SensorData>> &_data);
void deviceAdded(const QString &_source);

public slots:
void dropSource(const QString &_source);
void sensorAdded(const QString &_sensor);
void sensorRemoved(const QString &_sensor);
void updateData(KSysGuard::SensorDataList _data);
void updateData(const KSysGuard::SensorDataList &_data);
void updateSensors(const QHash<QString, KSysGuard::SensorInfo> &_sensors);

private:
Expand Down
31 changes: 15 additions & 16 deletions sources/awesome-widget/plugin/awkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,13 @@ void AWKeys::editItem(const QString &_type)
}


void AWKeys::dataUpdated(const QHash<QString, KSysGuard::SensorInfo> &_sensors, const KSysGuard::SensorDataList &_data)
void AWKeys::dataUpdated(const QList<QPair<KSysGuard::SensorInfo, KSysGuard::SensorData>> &_data)
{
for (auto &single : _data) {
if (_sensors.contains(single.sensorProperty)) {
setDataBySource(single.sensorProperty, _sensors.value(single.sensorProperty), single);
}
// TODO use QtConcurrent::map or something like that
// QtConcurrent::run(m_threadPool, this, &AWKeys::setDataBySource, "ss", sensor);
}
auto result = QtConcurrent::map(m_threadPool, _data, [this](auto &pair) {
return setDataBySource(pair.first, pair.second);
});
// if not called, all object will be destroyed after exit leading to memory corruption
result.waitForFinished();
}


Expand Down Expand Up @@ -357,23 +355,24 @@ QString AWKeys::parsePattern(QString _pattern) const
}


void AWKeys::setDataBySource(const QString &_source, const KSysGuard::SensorInfo &_sensor,
const KSysGuard::SensorData &_data)
void AWKeys::setDataBySource(const KSysGuard::SensorInfo &_sensor, const KSysGuard::SensorData &_data)
{
qCDebug(LOG_AW) << "Source" << _source << _sensor.name << "with data" << _data.payload;
auto source = _data.sensorProperty;
auto value = _data.payload;
qCDebug(LOG_AW) << "Source" << source << _sensor.name << "with data" << value;

// first list init
auto tags = m_aggregator->keysFromSource(_source);
auto tags = m_aggregator->keysFromSource(source);
if (tags.isEmpty())
tags = m_aggregator->registerSource(_source, _sensor.unit, m_requiredKeys);
tags = m_aggregator->registerSource(source, _sensor.unit, m_requiredKeys);

// update data or drop source if there are no matches and exit
if (tags.isEmpty()) {
qCInfo(LOG_AW) << "Sensor" << _source << "not found";
return emit(dropSourceFromDataengine(_source));
qCInfo(LOG_AW) << "Sensor" << source << "not found";
return emit(dropSourceFromDataengine(source));
}

m_mutex.lock();
std::for_each(tags.cbegin(), tags.cend(), [this, &_data](const QString &tag) { m_values[tag] = _data.payload; });
std::for_each(tags.cbegin(), tags.cend(), [this, value](const QString &tag) { m_values[tag] = value; });
m_mutex.unlock();
}
5 changes: 2 additions & 3 deletions sources/awesome-widget/plugin/awkeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AWKeys : public QObject
Q_INVOKABLE void editItem(const QString &_type);

public slots:
void dataUpdated(const QHash<QString, KSysGuard::SensorInfo> &_sensors, const KSysGuard::SensorDataList &_data);
void dataUpdated(const QList<QPair<KSysGuard::SensorInfo, KSysGuard::SensorData>> &_data);

signals:
void dropSourceFromDataengine(const QString &_source);
Expand All @@ -70,8 +70,7 @@ private slots:
void calculateValues();
void createDBusInterface();
[[nodiscard]] QString parsePattern(QString _pattern) const;
void setDataBySource(const QString &_source, const KSysGuard::SensorInfo &_sensor,
const KSysGuard::SensorData &_data);
void setDataBySource(const KSysGuard::SensorInfo &_sensor, const KSysGuard::SensorData &_data);
// objects
AWDataAggregator *m_dataAggregator = nullptr;
AWDataEngineAggregator *m_dataEngineAggregator = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions sources/libraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
find_package(Gettext REQUIRED)

# main qt libraries
find_package(Qt6 6.6.0 REQUIRED COMPONENTS Core DBus Network Qml Test Widgets)
find_package(Qt6 6.6.0 REQUIRED COMPONENTS Core Concurrent DBus Network Qml Test Widgets)
add_definitions(
${Qt6Core_DEFINITIONS} ${Qt6DBus_DEFINITIONS} ${Qt6Network_DEFINITIONS}
${Qt6Qml_DEFINITIONS} ${Qt6Widgets_DEFINITIONS}
Expand All @@ -12,7 +12,7 @@ set(Qt_INCLUDE
${Qt6Qml_INCLUDE_DIRS} ${Qt6Widgets_INCLUDE_DIRS}
)
set(Qt_LIBRARIES
${Qt6Core_LIBRARIES} ${Qt6DBus_LIBRARIES} ${Qt6Network_LIBRARIES}
${Qt6Core_LIBRARIES} ${Qt6Concurrent_LIBRARIES} ${Qt6DBus_LIBRARIES} ${Qt6Network_LIBRARIES}
${Qt6Qml_LIBRARIES} ${Qt6Widgets_LIBRARIES}
)

Expand Down

0 comments on commit a20755a

Please sign in to comment.