From 30d5072a7b939477afefe0b9d550310cbf26eaf5 Mon Sep 17 00:00:00 2001 From: Spiros Date: Thu, 13 Jun 2024 21:01:28 +0300 Subject: [PATCH] Make the big if loop for reads/writes less shit --- src/mainwindow.cpp | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a4228ed..fdedd00 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -385,9 +385,11 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal QString firmwareVersion = localObj["firmware_version"].toString(); float userCapacityGB = localObj.value("user_capacity").toObject().value("bytes").toDouble() / 1e9; int temperatureInt = localObj["temperature"].toObject()["current"].toInt(); + int totalWritesInt = 0; + int totalReadsInt = 0; QString userCapacityString = QString::number(static_cast(userCapacityGB)) + "." + QString::number(static_cast((userCapacityGB - static_cast(userCapacityGB)) * 10)) + " GB"; - QString totalReads = "----"; - QString totalWrites = "----"; + QString totalReads; + QString totalWrites; QString percentage = ""; QString serialNumber = localObj["serial_number"].toString(); QJsonObject deviceObj = localObj["device"].toObject(); @@ -459,48 +461,49 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal totalWrites = QString::number(attrObj["raw"].toObject()["value"].toInt()) + " GB"; } else if (attrObj["name"] == "Host_Writes_32MiB") { double gigabytes = (attrObj["raw"].toObject()["value"].toInt() * 32 * 1024.0 * 1024.0) / 1e9; - totalWrites = QString::number(static_cast(gigabytes)) + " GB"; + totalWritesInt = static_cast(gigabytes); } else if (attrObj["name"] == "Total_LBAs_Written") { unsigned int logicalBlockSize = localObj["logical_block_size"].toInt(); unsigned long long lbaWritten = attrObj["raw"].toObject()["value"].toVariant().toLongLong(); unsigned long long oneGB = static_cast(std::pow(2, 30)); - unsigned long long totalGbWritten = (lbaWritten * logicalBlockSize) / oneGB; - totalWrites = QString::number(static_cast(totalGbWritten)) + " GB"; + unsigned long long gigabytes = (lbaWritten * logicalBlockSize) / oneGB; + totalWritesInt = static_cast(gigabytes); } else if (attrObj["name"] == "Host_Writes_GiB" || attrObj["name"] == "Lifetime_Writes_GiB") { double gibibytes = attrObj["raw"].toObject()["value"].toDouble(); double bytesPerGiB = static_cast(1ULL << 30); double bytesPerGB = 1e9; double conversionFactor = bytesPerGiB / bytesPerGB; double gigabytes = gibibytes * conversionFactor; - totalWrites = QString::number(static_cast(gigabytes)) + " GB"; + totalWritesInt = static_cast(gigabytes); } } else if (attrObj["id"] == 242) { if (attrObj["name"] == "Total_Reads_GB") { - totalReads = QString::number(attrObj["raw"].toObject()["value"].toInt()) + " GB"; + int gigabytes = attrObj["raw"].toObject()["value"].toInt(); + totalReadsInt = static_cast(gigabytes); } else if (attrObj["name"] == "Host_Reads_32MiB") { double gigabytes = (attrObj["raw"].toObject()["value"].toInt() * 32 * 1024.0 * 1024.0) / 1e9; - totalReads = QString::number(static_cast(gigabytes)) + " GB"; + totalReadsInt = static_cast(gigabytes); } else if (attrObj["name"] == "Total_LBAs_Read") { unsigned int logicalBlockSize = localObj["logical_block_size"].toInt(); unsigned long long lbaRead = attrObj["raw"].toObject()["value"].toVariant().toLongLong(); unsigned long long oneGB = static_cast(std::pow(2, 30)); - unsigned long long totalGbRead = (lbaRead * logicalBlockSize) / oneGB; - totalReads = QString::number(static_cast(totalGbRead)) + " GB"; + unsigned long long gigabytes = (lbaRead * logicalBlockSize) / oneGB; + totalReadsInt = static_cast(gigabytes); } else if (attrObj["name"] == "Host_Reads_GiB" || attrObj["name"] == "Lifetime_Reads_GiB") { double gibibytes = attrObj["raw"].toObject()["value"].toDouble(); double bytesPerGiB = static_cast(1ULL << 30); double bytesPerGB = 1e9; double conversionFactor = bytesPerGiB / bytesPerGB; double gigabytes = gibibytes * conversionFactor; - totalReads = QString::number(static_cast(gigabytes)) + " GB"; + totalReadsInt = static_cast(gigabytes); } } else if (attrObj["id"] == 246) { // MX500 if (attrObj["name"] == "Total_LBAs_Written") { unsigned int logicalBlockSize = localObj["logical_block_size"].toInt(); unsigned long long lbaWritten = attrObj["raw"].toObject()["value"].toVariant().toLongLong(); unsigned long long oneGB = static_cast(std::pow(2, 30)); - unsigned long long totalGbWritten = (lbaWritten * logicalBlockSize) / oneGB; - totalWrites = QString::number(static_cast(totalGbWritten)) + " GB"; + unsigned long long gigabytes = (lbaWritten * logicalBlockSize) / oneGB; + totalWritesInt = static_cast(gigabytes); } } else if (attrObj["name"] == "Remaining_Lifetime_Perc") { int percentageUsed = attrObj["raw"].toObject()["value"].toInt(); @@ -526,12 +529,12 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal QJsonValue value = smartItem.value(); if (key == "data_units_written") { double dataUnitsWritten = value.toDouble(); - double totalGbWritten = (dataUnitsWritten * 512) / 1'000'000; - totalWrites = QString::number(static_cast(totalGbWritten)) + " GB"; + double gigabytes = (dataUnitsWritten * 512) / 1'000'000; + totalWritesInt = static_cast(gigabytes); } else if (key == "data_units_read") { double dataUnitsRead = value.toDouble(); - double totalGbRead = (dataUnitsRead * 512) / 1'000'000; - totalReads = QString::number(static_cast(totalGbRead)) + " GB"; + double gigabytes = (dataUnitsRead * 512) / 1'000'000; + totalReadsInt = static_cast(gigabytes); } else if (key == "percentage_used") { int percentageUsed = 100 - value.toInt(); percentage = QString::number(percentageUsed) + " %"; @@ -539,11 +542,15 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal } } - if (totalReads == "0 GB") { + if (totalReadsInt) { + totalReads = QString::number(static_cast(totalReadsInt)) + " GB"; + } else { totalReads = "----"; } - if (totalWrites == "0 GB") { + if (totalWritesInt) { + totalWrites = QString::number(static_cast(totalWritesInt)) + " GB"; + } else { totalWrites = "----"; }