split formatter into separated singleton classes

This commit is contained in:
2024-04-22 17:36:48 +03:00
parent 95572364c1
commit 3db85cb38f
54 changed files with 1538 additions and 292 deletions

View File

@ -21,6 +21,7 @@
#include "awdebug.h"
#include "awformatterhelper.h"
#include "formatters/formatters.h"
AWDataEngineMapper::AWDataEngineMapper(QObject *_parent, AWFormatterHelper *_custom)
@ -31,32 +32,32 @@ AWDataEngineMapper::AWDataEngineMapper(QObject *_parent, AWFormatterHelper *_cus
// default formatters
// memory
m_formatter["mem"] = AWKeysAggregator::FormatterType::Float;
m_formatter["memtotmb"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["memtotgb"] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter["mem"] = AWPluginFormatterFloat::instance();
m_formatter["memtotmb"] = AWPluginFormatterMemoryMB::instance();
m_formatter["memtotgb"] = AWPluginFormatterMemoryGB::instance();
// network
m_formatter["down"] = AWKeysAggregator::FormatterType::NetSmartFormat;
m_formatter["downkb"] = AWKeysAggregator::FormatterType::Integer;
m_formatter["downtot"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["downtotkb"] = AWKeysAggregator::FormatterType::Integer;
m_formatter["downunits"] = AWKeysAggregator::FormatterType::NetSmartUnits;
m_formatter["up"] = AWKeysAggregator::FormatterType::NetSmartFormat;
m_formatter["upkb"] = AWKeysAggregator::FormatterType::Integer;
m_formatter["uptot"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["uptotkb"] = AWKeysAggregator::FormatterType::Integer;
m_formatter["upunits"] = AWKeysAggregator::FormatterType::NetSmartUnits;
m_formatter["down"] = AWPluginFormatterNet::instance();
m_formatter["downkb"] = AWPluginFormatterMemory::instance();
m_formatter["downtot"] = AWPluginFormatterMemoryMB::instance();
m_formatter["downtotkb"] = AWPluginFormatterMemory::instance();
m_formatter["downunits"] = AWPluginFormatterNetUnits::instance();
m_formatter["up"] = AWPluginFormatterNet::instance();
m_formatter["upkb"] = AWPluginFormatterMemory::instance();
m_formatter["uptot"] = AWPluginFormatterMemoryMB::instance();
m_formatter["uptotkb"] = AWPluginFormatterMemory::instance();
m_formatter["upunits"] = AWPluginFormatterNetUnits::instance();
// swap
m_formatter["swap"] = AWKeysAggregator::FormatterType::Float;
m_formatter["swaptotmb"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["swaptotgb"] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter["swap"] = AWPluginFormatterFloat::instance();
m_formatter["swaptotmb"] = AWPluginFormatterMemoryMB::instance();
m_formatter["swaptotgb"] = AWPluginFormatterMemoryGB::instance();
}
AWKeysAggregator::FormatterType AWDataEngineMapper::formatter(const QString &_key) const
AWPluginFormaterInterface *AWDataEngineMapper::formatter(const QString &_key) const
{
qCDebug(LOG_AW) << "Get formatter for key" << _key;
return m_formatter.value(_key, AWKeysAggregator::FormatterType::NoFormat);
return m_formatter.value(_key, AWPluginFormatterNoFormat::instance());
}
@ -92,53 +93,55 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
if (_source == "extsysmon/battery/ac") {
// AC
m_map.insert(_source, "ac");
m_formatter["ac"] = AWKeysAggregator::FormatterType::ACFormat;
m_formatter["ac"] = AWPluginFormatterAC::instance();
} else if (_source.startsWith("extsysmon/battery/")) {
// battery stats
auto key = _source;
key.remove("extsysmon/battery/");
m_map.insert(_source, key);
m_formatter[key] = _source.contains("rate") ? AWKeysAggregator::FormatterType::Float
: AWKeysAggregator::FormatterType::IntegerThree;
if (_source.contains("rate"))
m_formatter[key] = AWPluginFormatterFloat::instance();
else
m_formatter[key] = AWPluginFormatterIntegerShort::instance();
} else if (_source == "cpu/all/usage") {
// cpu
m_map.insert(_source, "cpu");
m_formatter["cpu"] = AWKeysAggregator::FormatterType::Float;
m_formatter["cpu"] = AWPluginFormatterFloat::instance();
} else if (_source.contains(cpuRegExp)) {
// cpus
auto key = _source;
key.remove("cpu/").remove("/usage");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Float;
m_formatter[key] = AWPluginFormatterFloat::instance();
} else if (_source == "cpu/all/averageFrequency") {
// cpucl
m_map.insert(_source, "cpucl");
m_formatter["cpucl"] = AWKeysAggregator::FormatterType::Integer;
m_formatter["cpucl"] = AWPluginFormatterInteger::instance();
} else if (_source.contains(cpuclRegExp)) {
// cpucls
auto key = _source;
key.remove("cpu/cpu").remove("/frequency");
key = QString("cpucl%1").arg(key);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Integer;
m_formatter[key] = AWPluginFormatterInteger::instance();
} else if (_source.startsWith("extsysmon/custom")) {
// custom
auto key = _source;
key.remove("extsysmon/custom/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter[key] = AWPluginFormatterNoFormat::instance();
} else if (_source == "extsysmon/desktop/name") {
// current desktop name
m_map.insert(_source, "desktop");
m_formatter["desktop"] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter["desktop"] = AWPluginFormatterNoFormat::instance();
} else if (_source == "extsysmon/desktop/number") {
// current desktop number
m_map.insert(_source, "ndesktop");
m_formatter["ndesktop"] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter["ndesktop"] = AWPluginFormatterNoFormat::instance();
} else if (_source == "extsysmon/desktop/count") {
// desktop count
m_map.insert(_source, "tdesktops");
m_formatter["tdesktops"] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter["tdesktops"] = AWPluginFormatterNoFormat::instance();
} else if (_source.contains(hddrRegExp)) {
// read speed
auto device = _source;
@ -147,7 +150,7 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
if (index > -1) {
QString key = QString("hddr%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemKBFormat;
m_formatter[key] = AWPluginFormatterMemory::instance();
}
} else if (_source.contains(hddwRegExp)) {
// write speed
@ -157,12 +160,12 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
if (index > -1) {
QString key = QString("hddw%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemKBFormat;
m_formatter[key] = AWPluginFormatterMemory::instance();
}
} else if (_source == "gpu/all/usage") {
// gpu load
m_map.insert(_source, "gpu");
m_formatter["gpu"] = AWKeysAggregator::FormatterType::Float;
m_formatter["gpu"] = AWPluginFormatterFloat::instance();
} else if (_source.contains(gpuRegExp)) {
// gpus
auto device = _source;
@ -171,7 +174,7 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
if (index > -1) {
auto key = QString("gpu%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Float;
m_formatter[key] = AWPluginFormatterFloat::instance();
}
} else if (_source.contains(gpuTempRegExp)) {
// gpus temps
@ -181,7 +184,7 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
if (index > -1) {
auto key = QString("gputemp%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Temperature;
m_formatter[key] = AWPluginFormatterTemperature::instance();
}
} else if (_source.contains(mountFillRegExp)) {
// fill level
@ -191,10 +194,10 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
if (index > -1) {
auto key = QString("hdd%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Float;
m_formatter[key] = AWPluginFormatterFloat::instance();
// additional keys
m_formatter[QString("hddtotmb%1").arg(index)] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter[QString("hddtotgb%1").arg(index)] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter[QString("hddtotmb%1").arg(index)] = AWPluginFormatterMemoryMB::instance();
m_formatter[QString("hddtotgb%1").arg(index)] = AWPluginFormatterMemoryGB::instance();
}
} else if (_source.contains(mountFreeRegExp)) {
// free space
@ -205,11 +208,11 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
// mb
auto key = QString("hddfreemb%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter[key] = AWPluginFormatterMemoryMB::instance();
// gb
key = QString("hddfreegb%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter[key] = AWPluginFormatterMemoryGB::instance();
}
} else if (_source.contains(mountUsedRegExp)) {
// used
@ -220,11 +223,11 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
// mb
auto key = QString("hddmb%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter[key] = AWPluginFormatterMemoryMB::instance();
// gb
key = QString("hddgb%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter[key] = AWPluginFormatterMemoryGB::instance();
}
} else if (_source.startsWith("cpu/loadaverages/loadaverage")) {
// load average
@ -232,45 +235,45 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
time.remove("cpu/loadaverages/loadaverage");
auto key = QString("la%1").arg(time);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::FloatTwoSymbols;
m_formatter[key] = AWPluginFormatterFloatPrecise::instance();
} else if (_source == "memory/physical/application") {
// app memory
// mb
m_map.insert(_source, "memmb");
m_formatter["memmb"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["memmb"] = AWPluginFormatterMemoryMB::instance();
// gb
m_map.insert(_source, "memgb");
m_formatter["memgb"] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter["memgb"] = AWPluginFormatterMemoryGB::instance();
} else if (_source == "memory/physical/free") {
// free memory
// mb
m_map.insert(_source, "memfreemb");
m_formatter["memfreemb"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["memfreemb"] = AWPluginFormatterMemoryMB::instance();
// gb
m_map.insert(_source, "memfreegb");
m_formatter["memfreegb"] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter["memfreegb"] = AWPluginFormatterMemoryGB::instance();
} else if (_source == "memory/physical/used") {
// used memory
// mb
m_map.insert(_source, "memusedmb");
m_formatter["memusedmb"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["memusedmb"] = AWPluginFormatterMemoryMB::instance();
// gb
m_map.insert(_source, "memusedgb");
m_formatter["memusedgb"] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter["memusedgb"] = AWPluginFormatterMemoryGB::instance();
} else if (_source == "extsysmon/network/device") {
// network device
m_map.insert(_source, "netdev");
m_formatter["netdev"] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter["netdev"] = AWPluginFormatterNoFormat::instance();
} else if (_source == "extsysmon/network/ssid") {
// current ssid
m_map.insert(_source, "ssid");
m_formatter["ssid"] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter["ssid"] = AWPluginFormatterNoFormat::instance();
} else if (_source.startsWith("extsysmon/requests/response")) {
// network response
auto key = _source;
key.remove("extsysmon/requests/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter[key] = AWPluginFormatterNoFormat::instance();
} else if (_source.contains(netRegExp)) {
// network speed
auto type = _source.endsWith("download") ? "down" : "up";
@ -279,15 +282,15 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
// kb
auto key = QString("%1kb%2").arg(type).arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemKBFormat;
m_formatter[key] = AWPluginFormatterMemory::instance();
// smart
key = QString("%1%2").arg(type).arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::NetSmartFormat;
m_formatter[key] = AWPluginFormatterNet::instance();
// units
key = QString("%1units%2").arg(type).arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::NetSmartUnits;
m_formatter[key] = AWPluginFormatterNetUnits::instance();
}
} else if (_source.contains(netTotalRegExp)) {
// network data total
@ -297,58 +300,58 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
// kb
auto key = QString("%1totkb%2").arg(type).arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemKBFormat;
m_formatter[key] = AWPluginFormatterMemory::instance();
// mb
key = QString("%1tot%2").arg(type).arg(index);
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter[key] = AWPluginFormatterMemoryMB::instance();
}
} else if (_source.startsWith("extsysmon/upgrade")) {
// package manager
auto key = _source;
key.remove("extsysmon/upgrade/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::IntegerThree;
m_formatter[key] = AWPluginFormatterIntegerShort::instance();
} else if (_source.startsWith("extsysmon/player")) {
// player
auto key = _source;
key.remove("extsysmon/player/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter[key] = AWPluginFormatterNoFormat::instance();
} else if (_source == "extsysmon/ps/running") {
// running processes count
m_map.insert(_source, "pscount");
m_formatter["pscount"] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter["pscount"] = AWPluginFormatterNoFormat::instance();
} else if (_source == "extsysmon/ps/list") {
// list of running processes
m_map.insert(_source, "ps");
m_formatter["ps"] = AWKeysAggregator::FormatterType::List;
m_formatter["ps"] = AWPluginFormatterList::instance();
} else if (_source == "extsysmon/ps/count") {
// total processes count
m_map.insert(_source, "pstot");
m_formatter["pstot"] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter["pstot"] = AWPluginFormatterNoFormat::instance();
} else if (_source.startsWith("extsysmon/quotes")) {
// quotes
auto key = _source;
key.remove("extsysmon/quotes/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Quotes;
m_formatter[key] = AWPluginFormatterDouble::instance();
} else if (_source == "memory/swap/free") {
// free swap
// mb
m_map.insert(_source, "swapfreemb");
m_formatter["swapfreemb"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["swapfreemb"] = AWPluginFormatterMemoryMB::instance();
// gb
m_map.insert(_source, "swapfreegb");
m_formatter["swapfreegb"] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter["swapfreegb"] = AWPluginFormatterMemoryGB::instance();
} else if (_source == "memory/swap/used") {
// used swap
// mb
m_map.insert(_source, "swapmb");
m_formatter["swapmb"] = AWKeysAggregator::FormatterType::MemMBFormat;
m_formatter["swapmb"] = AWPluginFormatterMemoryMB::instance();
// gb
m_map.insert(_source, "swapgb");
m_formatter["swapgb"] = AWKeysAggregator::FormatterType::MemGBFormat;
m_formatter["swapgb"] = AWPluginFormatterMemoryGB::instance();
} else if (_source.startsWith("lmsensors/") || _source.contains(cpuTempRegExp)
|| _source == "cpu/all/averageTemperature") {
// temperature
@ -359,59 +362,58 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
if (index > -1) {
auto key = QString("temp%1").arg(index);
m_map.insert(_source, key);
m_formatter[key] = _units == KSysGuard::UnitCelsius ? AWKeysAggregator::FormatterType::Temperature
: AWKeysAggregator::FormatterType::Integer;
if (_units == KSysGuard::UnitCelsius)
m_formatter[key] = AWPluginFormatterTemperature::instance();
else
m_formatter[key] = AWPluginFormatterInteger::instance();
}
} else if (_source == "extsysmon/time/now") {
// time
m_map.insert(_source, "time");
m_formatter["time"] = AWKeysAggregator::FormatterType::Time;
m_formatter["time"] = AWPluginFormatterTime::instance();
// custom time
m_map.insert(_source, "ctime");
m_formatter["ctime"] = AWKeysAggregator::FormatterType::TimeCustom;
m_formatter["ctime"] = AWPluginFormatterTimeCustom::instance();
// ISO time
m_map.insert(_source, "isotime");
m_formatter["isotime"] = AWKeysAggregator::FormatterType::TimeISO;
m_formatter["isotime"] = AWPluginFormatterTimeISO::instance();
// long time
m_map.insert(_source, "longtime");
m_formatter["longtime"] = AWKeysAggregator::FormatterType::TimeLong;
m_formatter["longtime"] = AWPluginFormatterTimeLong::instance();
// short time
m_map.insert(_source, "shorttime");
m_formatter["shorttime"] = AWKeysAggregator::FormatterType::TimeShort;
m_formatter["shorttime"] = AWPluginFormatterTimeShort::instance();
// timestamp
m_map.insert(_source, "tstime");
m_formatter["tstime"] = AWKeysAggregator::FormatterType::Timestamp;
m_formatter["tstime"] = AWPluginFormatterNoFormat::instance();
} else if (_source == "extsysmon/system/brightness") {
m_map.insert(_source, "brightness");
m_formatter["brightness"] = AWKeysAggregator::FormatterType::IntegerThree;
m_formatter["brightness"] = AWPluginFormatterIntegerShort::instance();
} else if (_source == "extsysmon/system/volume") {
m_map.insert(_source, "volume");
m_formatter["volume"] = AWKeysAggregator::FormatterType::IntegerThree;
m_formatter["volume"] = AWPluginFormatterIntegerShort::instance();
} else if (_source == "os/system/uptime") {
// uptime
m_map.insert(_source, "uptime");
m_formatter["uptime"] = AWKeysAggregator::FormatterType::Uptime;
// custom uptime
m_map.insert(_source, "cuptime");
m_formatter["cuptime"] = AWKeysAggregator::FormatterType::UptimeCustom;
m_formatter["uptime"] = AWPluginFormatterUptime::instance();
} else if (_source.startsWith("extsysmon/weather/temperature")) {
// temperature
auto key = _source;
key.remove("extsysmon/weather/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Temperature;
m_formatter[key] = AWPluginFormatterTemperature::instance();
} else if (_source.startsWith("extsysmon/weather/")) {
// other weather
auto key = _source;
key.remove("extsysmon/weather/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::NoFormat;
m_formatter[key] = AWPluginFormatterNoFormat::instance();
} else if (_source.startsWith("extsysmon/load/load")) {
// load source
auto key = _source;
key.remove("extsysmon/load/");
m_map.insert(_source, key);
m_formatter[key] = AWKeysAggregator::FormatterType::Temperature;
m_formatter[key] = AWPluginFormatterTemperature::instance();
}
auto foundKeys = keysFromSource(_source);
@ -424,7 +426,7 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy
for (auto &key : foundKeys) {
if (!customFormattersKeys.contains(key))
continue;
m_formatter[key] = AWKeysAggregator::FormatterType::Custom;
m_formatter[key] = AWPluginFormatterCustom::instance();
}
// drop key from dictionary if no one user requested key required it