Skip to content

Commit

Permalink
refactor: simplify sources processing
Browse files Browse the repository at this point in the history
  • Loading branch information
arcan1s committed Mar 28, 2024
1 parent 0bcceef commit 8e8fac6
Show file tree
Hide file tree
Showing 29 changed files with 203 additions and 711 deletions.
25 changes: 18 additions & 7 deletions sources/extsysmonsources/abstractextsysmonsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@
#include <QObject>
#include <QRegularExpression>
#include <QVariant>
#include <ksysguard/systemstats/SensorInfo.h>


namespace KSysGuard
{
class SensorInfo;
}

class AbstractExtSysMonSource : public QObject
{
Q_OBJECT
Expand All @@ -36,15 +32,30 @@ class AbstractExtSysMonSource : public QObject
: QObject(_parent){};
~AbstractExtSysMonSource() override = default;
virtual QVariant data(const QString &_source) = 0;
[[nodiscard]] virtual KSysGuard::SensorInfo *initialData(const QString &_source) const = 0;
virtual void run() = 0;
[[nodiscard]] virtual QStringList sources() const = 0;
[[nodiscard]] virtual QHash<QString, KSysGuard::SensorInfo *> sources() const = 0;

// used by extensions
static int index(const QString &_source)
{
QRegularExpression rx("\\d+");
return rx.match(_source).captured().toInt();
}
static KSysGuard::SensorInfo *makeSensorInfo(const QString &_name, const QVariant::Type type,
const KSysGuard::Unit unit = KSysGuard::UnitNone, const double min = 0,
const double max = 9)
{
auto info = new KSysGuard::SensorInfo();
info->name = _name;
info->variantType = type;

info->unit = unit;

info->min = min;
info->max = max;

return info;
}

signals:
void dataReceived(const QVariantHash &);
Expand Down
144 changes: 27 additions & 117 deletions sources/extsysmonsources/batterysource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/


#include "batterysource.h"

#include <ksysguard/formatter/Unit.h>
#include <ksysguard/systemstats/SensorInfo.h>

#include <QDir>

#include <cmath>
Expand All @@ -35,44 +31,10 @@ BatterySource::BatterySource(QObject *_parent, const QStringList &_args)
qCDebug(LOG_ESS) << __PRETTY_FUNCTION__;

m_acpiPath = _args.at(0);
m_sources = getSources();
}


BatterySource::~BatterySource()
{
qCDebug(LOG_ESS) << __PRETTY_FUNCTION__;
}


QStringList BatterySource::getSources()
{
QStringList sources;
sources.append("ac");
sources.append("bat");
sources.append("batleft");
sources.append("batnow");
sources.append("batrate");
sources.append("battotal");

auto directory = QDir(m_acpiPath);

if (directory.exists()) {
m_batteriesCount
= directory.entryList(QStringList({"BAT*"}), QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name).count();
qCInfo(LOG_ESS) << "Init batteries count as" << m_batteriesCount;

for (int i = 0; i < m_batteriesCount; i++) {
sources.append(QString("bat%1").arg(i));
sources.append(QString("batleft%1").arg(i));
sources.append(QString("batnow%1").arg(i));
sources.append(QString("batrate%1").arg(i));
sources.append(QString("battotal%1").arg(i));
}
}

qCInfo(LOG_ESS) << "Sources list" << sources;
return sources;
m_batteriesCount = directory.entryList(QStringList({"BAT*"}), QDir::Dirs | QDir::NoDotAndDotDot).count();
qCInfo(LOG_ESS) << "Init batteries count as" << m_batteriesCount;
}


Expand All @@ -86,81 +48,6 @@ QVariant BatterySource::data(const QString &_source)
}


KSysGuard::SensorInfo *BatterySource::initialData(const QString &_source) const
{
qCDebug(LOG_ESS) << "Source" << _source;

auto data = new KSysGuard::SensorInfo();
if (_source == "ac") {
data->name = "Is AC online or not";
data->variantType = QVariant::Bool;
data->unit = KSysGuard::UnitNone;
} else if (_source == "bat") {
data->min = 0;
data->max = 100;
data->name = "Average battery usage";
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitPercent;
} else if (_source == "batleft") {
data->min = 0;
data->max = 0;
data->name = "Battery discharge time";
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitSecond;
} else if (_source == "batnow") {
data->min = 0;
data->max = 0;
data->name = "Current battery capacity";
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitNone;
} else if (_source == "batrate") {
data->min = 0;
data->max = 0;
data->name = "Average battery discharge rate";
data->variantType = QVariant::Double;
data->unit = KSysGuard::UnitRate;
} else if (_source == "battotal") {
data->min = 0;
data->max = 0;
data->name = "Full battery capacity";
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitNone;
} else if (_source.startsWith("batleft")) {
data->min = 0;
data->max = 0;
data->name = QString("Battery %1 discharge time").arg(index(_source));
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitSecond;
} else if (_source.startsWith("batnow")) {
data->min = 0;
data->max = 0;
data->name = QString("Battery %1 capacity").arg(index(_source));
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitNone;
} else if (_source.startsWith("battotal")) {
data->min = 0;
data->max = 0;
data->name = QString("Battery %1 full capacity").arg(index(_source));
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitNone;
} else if (_source.startsWith("batrate")) {
data->min = 0;
data->max = 0;
data->name = QString("Battery %1 discharge rate").arg(index(_source));
data->variantType = QVariant::Double;
data->unit = KSysGuard::UnitRate;
} else if (_source.startsWith("bat")) {
data->min = 0;
data->max = 100;
data->name = QString("Battery %1 usage").arg(index(_source));
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitPercent;
}

return data;
}


void BatterySource::run()
{
// adaptor
Expand Down Expand Up @@ -203,9 +90,32 @@ void BatterySource::run()
}


QStringList BatterySource::sources() const
QHash<QString, KSysGuard::SensorInfo *> BatterySource::sources() const
{
return m_sources;
auto result = QHash<QString, KSysGuard::SensorInfo *>();

// fixed fields
result.insert("ac", makeSensorInfo("Is AC online or not", QVariant::Bool));
result.insert("bat", makeSensorInfo("Average battery usage", QVariant::Int, KSysGuard::UnitPercent, 0, 100));
result.insert("batleft", makeSensorInfo("Battery discharge time", QVariant::Int, KSysGuard::UnitSecond));
result.insert("batnow", makeSensorInfo("Current battery capacity", QVariant::Int));
result.insert("batrate", makeSensorInfo("Average battery discharge rate", QVariant::Double, KSysGuard::UnitRate));
result.insert("battotal", makeSensorInfo("Full battery capacity", QVariant::Int));

// generators
for (auto i = 0; i < m_batteriesCount; i++) {
result.insert(QString("bat%1").arg(i), makeSensorInfo(QString("Battery %1 usage").arg(i), QVariant::Int,
KSysGuard::UnitPercent, 0, 100));
result.insert(QString("batleft%1").arg(i), makeSensorInfo(QString("Battery %1 discharge time").arg(i),
QVariant::Int, KSysGuard::UnitSecond));
result.insert(QString("batnow%1").arg(i), makeSensorInfo(QString("Battery %1 capacity").arg(i), QVariant::Int));
result.insert(QString("batrate%1").arg(i), makeSensorInfo(QString("Battery %1 discharge rate").arg(i),
QVariant::Double, KSysGuard::UnitRate));
result.insert(QString("battotal%1").arg(i),
makeSensorInfo(QString("Battery %1 full capacity").arg(i), QVariant::Int));
}

return result;
}


Expand Down
6 changes: 2 additions & 4 deletions sources/extsysmonsources/batterysource.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ class BatterySource : public AbstractExtSysMonSource
const int TREND_LIMIT = 20;

explicit BatterySource(QObject *_parent, const QStringList &_args);
~BatterySource() override;
QStringList getSources();
~BatterySource() override = default;
QVariant data(const QString &_source) override;
[[nodiscard]] KSysGuard::SensorInfo *initialData(const QString &_source) const override;
void run() override;
[[nodiscard]] QStringList sources() const override;
[[nodiscard]] QHash<QString, KSysGuard::SensorInfo *> sources() const override;

private:
static double approximate(const QList<int> &_trend);
Expand Down
39 changes: 6 additions & 33 deletions sources/extsysmonsources/customsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

#include "customsource.h"

#include <ksysguard/formatter/Unit.h>
#include <ksysguard/systemstats/SensorInfo.h>

#include "awdebug.h"
#include "extscript.h"

Expand All @@ -30,15 +27,8 @@ CustomSource::CustomSource(QObject *_parent, const QStringList &_args)
Q_ASSERT(_args.count() == 0);
qCDebug(LOG_ESS) << __PRETTY_FUNCTION__;

m_extScripts = new ExtItemAggregator<ExtScript>(nullptr, "scripts");
m_extScripts = new ExtItemAggregator<ExtScript>(this, "scripts");
m_extScripts->initSockets();
m_sources = getSources();
}


CustomSource::~CustomSource()
{
qCDebug(LOG_ESS) << __PRETTY_FUNCTION__;
}


Expand All @@ -51,30 +41,13 @@ QVariant CustomSource::data(const QString &_source)
}


KSysGuard::SensorInfo *CustomSource::initialData(const QString &_source) const
{
qCDebug(LOG_ESS) << "Source" << _source;

auto data = new KSysGuard::SensorInfo();
data->name = QString("Custom command '%1' output").arg(m_extScripts->itemByTagNumber(index(_source))->uniq());
data->variantType = QVariant::String;
data->unit = KSysGuard::UnitNone;

return data;
}


QStringList CustomSource::sources() const
QHash<QString, KSysGuard::SensorInfo *> CustomSource::sources() const
{
return m_sources;
}
auto result = QHash<QString, KSysGuard::SensorInfo *>();


QStringList CustomSource::getSources()
{
QStringList sources;
for (auto &item : m_extScripts->activeItems())
sources.append(item->tag("custom"));
result.insert(item->tag("custom"),
makeSensorInfo(QString("Custom command '%1' output").arg(item->uniq()), QVariant::String));

return sources;
return result;
}
7 changes: 2 additions & 5 deletions sources/extsysmonsources/customsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ class CustomSource : public AbstractExtSysMonSource

public:
explicit CustomSource(QObject *_parent, const QStringList &_args);
~CustomSource() override;
~CustomSource() override = default;
QVariant data(const QString &_source) override;
[[nodiscard]] KSysGuard::SensorInfo *initialData(const QString &_source) const override;
void run() override{};
[[nodiscard]] QStringList sources() const override;
[[nodiscard]] QHash<QString, KSysGuard::SensorInfo *> sources() const override;

private:
QStringList getSources();
// configuration and values
ExtItemAggregator<ExtScript> *m_extScripts = nullptr;
QStringList m_sources;
};
44 changes: 7 additions & 37 deletions sources/extsysmonsources/desktopsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include "desktopsource.h"

#include <KWindowSystem/KWindowSystem>
#include <ksysguard/formatter/Unit.h>
#include <ksysguard/systemstats/SensorInfo.h>
#include <taskmanager/virtualdesktopinfo.h>

#include "awdebug.h"
Expand Down Expand Up @@ -65,42 +63,14 @@ QVariant DesktopSource::data(const QString &_source)
}


KSysGuard::SensorInfo *DesktopSource::initialData(const QString &_source) const
QHash<QString, KSysGuard::SensorInfo *> DesktopSource::sources() const
{
qCDebug(LOG_ESS) << "Source" << _source;
auto result = QHash<QString, KSysGuard::SensorInfo *>();

auto data = new KSysGuard::SensorInfo();
if (_source == "name") {
data->name = "Current desktop name";
data->variantType = QVariant::String;
data->unit = KSysGuard::UnitNone;
} else if (_source == "number") {
data->min = 0;
data->name = "Current desktop number";
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitNone;
} else if (_source == "names") {
data->name = "All desktops by name";
data->variantType = QVariant::StringList;
data->unit = KSysGuard::UnitNone;
} else if (_source == "count") {
data->min = 0;
data->name = "Desktops count";
data->variantType = QVariant::Int;
data->unit = KSysGuard::UnitNone;
}

return data;
}


QStringList DesktopSource::sources() const
{
QStringList sources;
sources.append("name");
sources.append("number");
sources.append("names");
sources.append("count");
result.insert("name", makeSensorInfo("Current desktop name", QVariant::String));
result.insert("number", makeSensorInfo("Current desktop number", QVariant::Int));
result.insert("names", makeSensorInfo("All desktops by name", QVariant::StringList));
result.insert("count", makeSensorInfo("Desktops count", QVariant::Int));

return sources;
return result;
}
3 changes: 1 addition & 2 deletions sources/extsysmonsources/desktopsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ class DesktopSource : public AbstractExtSysMonSource
explicit DesktopSource(QObject *_parent, const QStringList &_args);
~DesktopSource() override;
QVariant data(const QString &_source) override;
[[nodiscard]] KSysGuard::SensorInfo *initialData(const QString &_source) const override;
void run() override{};
[[nodiscard]] QStringList sources() const override;
[[nodiscard]] QHash<QString, KSysGuard::SensorInfo *> sources() const override;

private:
TaskManager::VirtualDesktopInfo *m_vdi = nullptr;
Expand Down
Loading

0 comments on commit 8e8fac6

Please sign in to comment.