From 93531c1aaf7443f496e364a0755aacf57c2d3dec Mon Sep 17 00:00:00 2001 From: acetone Date: Fri, 25 Nov 2022 15:42:30 +0300 Subject: [PATCH] top list hr strings --- src/g.cpp | 2 +- src/webpage.cpp | 81 ++++++++++++++++++++++++++++++++++++++----------- src/webpage.h | 1 + 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/g.cpp b/src/g.cpp index 3a953cc..0cd48db 100644 --- a/src/g.cpp +++ b/src/g.cpp @@ -31,7 +31,7 @@ namespace g { namespace c { const QString SOFTWARE_NAME = "3proxy-eagle"; - const QString SOFTWARE_VERSION = "0.0.1a"; + const QString SOFTWARE_VERSION = "0.0.1"; const QString COPYRIGHT = "GPLv3 (c) acetone, 2022"; const qint64 DB_DAILY_TOP_TABLE_ACTUALIZE_MINIMAL_INTERVAL_MS = 300000; // 5 min const qint64 CACHE_ACTUALIZE_TOP_LISTS_FROM_DB_MINIMAL_INTERVAL_MS = 5000; // 5 sec diff --git a/src/webpage.cpp b/src/webpage.cpp index 70c7f3a..75f10f6 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -319,7 +319,7 @@ QString WebPage::topDestinationItem(const QString &destination, quint64 count) { QString result {m_topDestinationItem}; result.replace("{{VALUE}}", destination); - result.replace("{{COUNT}}", QString::number(count)); + result.replace("{{COUNT}}", counterToHumanReadableString(count)); return result; } @@ -342,47 +342,47 @@ HumanReadableValue WebPage::bytesToHumanReadableString(quint64 bytes) { HumanReadableValue result; - constexpr const quint64 BYTE = 1024; + constexpr const quint64 KBYTE = 1024; - if (bytes < BYTE) + if (bytes < KBYTE) { result.integer = QString::number(bytes); result.decimal = "0"; result.measure = "B"; } - else if (bytes <= BYTE*BYTE) + else if (bytes <= KBYTE*KBYTE) { - result.integer = QString::number(bytes/BYTE); - result.decimal = QString::number(bytes%BYTE); + result.integer = QString::number(bytes/KBYTE); + result.decimal = QString::number(bytes%KBYTE); result.measure = "KiB"; } - else if (bytes <= BYTE*BYTE*BYTE) + else if (bytes <= KBYTE*KBYTE*KBYTE) { - result.integer = QString::number(bytes /BYTE/BYTE); - result.decimal = QString::number(bytes% (BYTE*BYTE)); + result.integer = QString::number(bytes /KBYTE/KBYTE); + result.decimal = QString::number(bytes% (KBYTE*KBYTE)); result.measure = "MiB"; } - else if (bytes <= BYTE*BYTE*BYTE*BYTE) + else if (bytes <= KBYTE*KBYTE*KBYTE*KBYTE) { - result.integer = QString::number(bytes /BYTE/BYTE/BYTE); - result.decimal = QString::number(bytes% (BYTE*BYTE*BYTE)); + result.integer = QString::number(bytes /KBYTE/KBYTE/KBYTE); + result.decimal = QString::number(bytes% (KBYTE*KBYTE*KBYTE)); result.measure = "GiB"; } - else if (bytes <= BYTE*BYTE*BYTE*BYTE*BYTE) + else if (bytes <= KBYTE*KBYTE*KBYTE*KBYTE*KBYTE) { - result.integer = QString::number(bytes /BYTE/BYTE/BYTE/BYTE); - result.decimal = QString::number(bytes% (BYTE*BYTE*BYTE*BYTE)); + result.integer = QString::number(bytes /KBYTE/KBYTE/KBYTE/KBYTE); + result.decimal = QString::number(bytes% (KBYTE*KBYTE*KBYTE*KBYTE)); result.measure = "TiB"; } else { - result.integer = QString::number(bytes /BYTE/BYTE/BYTE/BYTE/BYTE); - result.decimal = QString::number(bytes% (BYTE*BYTE*BYTE*BYTE*BYTE)); + result.integer = QString::number(bytes /KBYTE/KBYTE/KBYTE/KBYTE/KBYTE); + result.decimal = QString::number(bytes% (KBYTE*KBYTE*KBYTE*KBYTE*KBYTE)); result.integer = "PiB"; } @@ -399,3 +399,50 @@ HumanReadableValue WebPage::bytesToHumanReadableString(quint64 bytes) return result; } + +QString WebPage::counterToHumanReadableString(quint64 count) +{ + constexpr const quint64 THOUSAND = 1000; + + QString result; + + if (count < THOUSAND) + { + result = QString::number(count); + } + + else if (count <= THOUSAND*THOUSAND) + { + result = QString::number(count/THOUSAND); + auto decimal = count%THOUSAND; + if (decimal) + { + result += "." + QString::number(decimal).mid(0,1); + } + result += " K"; + } + + else if (count <= THOUSAND*THOUSAND*THOUSAND) + { + result = QString::number(count /THOUSAND/THOUSAND); + auto decimal = count% (THOUSAND*THOUSAND); + if (decimal) + { + result += "." + QString::number(decimal).mid(0,1); + } + result += " M"; + } + + else if (count <= THOUSAND*THOUSAND*THOUSAND*THOUSAND) + { + result = QString::number(count /THOUSAND/THOUSAND/THOUSAND); + auto decimal = count% (THOUSAND*THOUSAND*THOUSAND); + if (decimal) + { + result += "." + QString::number(decimal).mid(0,1); + } + result += " B"; + } + + return result; +} diff --git a/src/webpage.h b/src/webpage.h index ca98c38..10d3e98 100644 --- a/src/webpage.h +++ b/src/webpage.h @@ -45,6 +45,7 @@ private: static QString topDestinationItem(const QString& destination, quint64 count); static QString blockedDestinationItem(const QString& destination, const QStringList& addresses); static HumanReadableValue bytesToHumanReadableString(quint64 bytes); + static QString counterToHumanReadableString(quint64 count); static const QString m_lastDestinationItemAllowed; static const QString m_lastDestinationItemBlocked;