From 313b9161e5f6249bce096c160a182f1957bb1284 Mon Sep 17 00:00:00 2001 From: Nova Date: Thu, 8 May 2025 02:27:30 +0330 Subject: [PATCH] improve data view --- core/server/internal/utils.go | 20 ++++++++++++++++++++ core/server/test_utils.go | 9 +++++---- include/ui/mainwindow.h | 1 + src/ui/mainwindow.cpp | 5 +++++ 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 core/server/internal/utils.go diff --git a/core/server/internal/utils.go b/core/server/internal/utils.go new file mode 100644 index 0000000..562b9e9 --- /dev/null +++ b/core/server/internal/utils.go @@ -0,0 +1,20 @@ +package internal + +import "fmt" + +const ( + Gb = 1000 * Mb + Mb = 1000 * Kb + Kb = 1000 +) + +func BrateToStr(brate float64) string { + brate *= 8 + if brate >= Gb { + return fmt.Sprintf("%.2f%s", brate/Gb, "Gbps") + } + if brate >= Mb { + return fmt.Sprintf("%.2f%s", brate/Mb, "Mbps") + } + return fmt.Sprintf("%.2f%s", brate/Kb, "Kbps") +} diff --git a/core/server/test_utils.go b/core/server/test_utils.go index 13a1c6c..16a6290 100644 --- a/core/server/test_utils.go +++ b/core/server/test_utils.go @@ -8,6 +8,7 @@ import ( "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing/common/metadata" "github.com/sagernet/sing/service" + "nekobox_core/internal" "nekobox_core/internal/boxbox" "net" "net/http" @@ -225,16 +226,16 @@ func speedTestWithDialer(ctx context.Context, dialer func(ctx context.Context, n for { select { case <-done: - res.DlSpeed = srv[0].DLSpeed.String() - res.UlSpeed = srv[0].ULSpeed.String() + res.DlSpeed = internal.BrateToStr(float64(srv[0].DLSpeed)) + res.UlSpeed = internal.BrateToStr(float64(srv[0].ULSpeed)) res.Latency = int32(srv[0].Latency.Milliseconds()) SpTQuerier.storeResult(res) return nil case <-ctx.Done(): return nil case <-ticker.C: - res.DlSpeed = speedtest.ByteRate(srv[0].Context.GetEWMADownloadRate()).String() - res.UlSpeed = speedtest.ByteRate(srv[0].Context.GetEWMAUploadRate()).String() + res.DlSpeed = internal.BrateToStr(srv[0].Context.GetEWMADownloadRate()) + res.UlSpeed = internal.BrateToStr(srv[0].Context.GetEWMAUploadRate()) SpTQuerier.storeResult(res) } } diff --git a/include/ui/mainwindow.h b/include/ui/mainwindow.h index f0d49ef..5ebf6fa 100644 --- a/include/ui/mainwindow.h +++ b/include/ui/mainwindow.h @@ -200,6 +200,7 @@ private: SpeedWidget *speedChartWidget; // // for data view + QDateTime lastUpdated = QDateTime::currentDateTime(); QString currentSptProfileName; bool showSpeedtestData = false; bool showDownloadData = false; diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 12c5120..8685070 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -930,6 +930,10 @@ void MainWindow::neko_set_spmode_vpn(bool enable, bool save) { void MainWindow::UpdateDataView() { + if (lastUpdated.msecsTo(QDateTime::currentDateTime()) < 100) + { + return; + } QString html; if (showDownloadData) { @@ -959,6 +963,7 @@ void MainWindow::UpdateDataView() currentTestResult.server_name().c_str()); } ui->data_view->setHtml(html); + lastUpdated = QDateTime::currentDateTime(); } void MainWindow::setDownloadReport(const DownloadProgressReport& report, bool show)