Skip to content

fix: Use correct SI units for speed test conversion - #629

Merged
HankYuLinksys merged 2 commits into
dev-2.0.0from
fix/speed-test-si-units
Feb 12, 2026
Merged

fix: Use correct SI units for speed test conversion#629
HankYuLinksys merged 2 commits into
dev-2.0.0from
fix/speed-test-si-units

Conversation

@AustinChangLinksys

Copy link
Copy Markdown
Collaborator

Summary

  • Change "Kb" to "kb" for kilo prefix (SI standard uses lowercase k)
  • Replace binary conversion (* 1024) with SI conversion (* 1000) for Kbps to bps
  • Update related test expectations

Details

Network speeds should use SI units (base 1000), not binary units (base 1024):

  • 1 kbps = 1,000 bps (not 1,024)
  • 1 Mbps = 1,000,000 bps (not 1,048,576)

The previous implementation caused displayed values to be ~2.4% higher than actual.

Test plan

  • Run flutter test test/utils_test.dart --name "formatBits" - all 17 tests pass
  • Verify speed test UI displays correct units (e.g., "100 kbps" instead of "100 Kbps")

🤖 Generated with Claude Code

@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Fix network speed calculations to use correct SI units

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Replace binary conversion (×1024) with SI conversion (×1000) for network speed calculations
• Change kilobit unit from "Kb" to "kb" per SI standard lowercase prefix
• Update test expectations to match corrected SI unit formatting
Diagram
flowchart LR
  A["Binary conversion ×1024"] -->|"Replace with"| B["SI conversion ×1000"]
  C["Unit: Kb"] -->|"Change to"| D["Unit: kb"]
  B --> E["Accurate speed display"]
  D --> E
Loading

Grey Divider

File Changes

1. lib/utils.dart 🐞 Bug fix +2/-2

Update bit formatting to use lowercase kb unit

• Updated formatBits() documentation example from "1 Kb" to "1 kb"
• Changed kilobit suffix in formatBitsWithUnit() from "Kb" to "kb" in suffixes array
• Maintains SI base-1000 conversion logic for bit formatting

lib/utils.dart


2. lib/page/health_check/services/health_check_service.dart 🐞 Bug fix +2/-2

Fix speed test bandwidth SI unit conversions

• Changed download bandwidth conversion from ×1024 to ×1000
• Changed upload bandwidth conversion from ×1024 to ×1000
• Ensures speed test results display with correct SI unit calculations

lib/page/health_check/services/health_check_service.dart


3. lib/page/health_check/providers/health_check_provider.dart 🐞 Bug fix +1/-1

Fix meter simulation to use SI conversion

• Updated random meter value simulation from ×1024 to ×1000
• Ensures simulated fluctuating meter uses consistent SI unit conversion

lib/page/health_check/providers/health_check_provider.dart


View more (2)
4. lib/page/health_check/shared_widgets/speed_test_widget.dart 🐞 Bug fix +2/-2

Fix speed test widget SI unit conversions

• Changed live meter value formatting from ×1024 to ×1000
• Updated meterValueMbps calculation from ÷1024 to ÷1000
• Ensures gauge display uses correct SI unit calculations

lib/page/health_check/shared_widgets/speed_test_widget.dart


5. test/utils_test.dart 🧪 Tests +4/-4

Update test expectations for lowercase kb unit

• Updated test expectations for kilobit formatting from "Kb" to "kb"
• Updated test comments to reflect lowercase kb unit notation
• Ensures test assertions match corrected SI unit output

test/utils_test.dart


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Feb 11, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Chart uses 1024 base 🐞 Bug ✓ Correctness
Description
After this PR’s switch to base-1000 conversions for speed-test bandwidth, the dashboard history
chart still divides Kbps by 1024 to get Mbps and uses a brittle unit-string fallback. This will make
historical chart values inconsistent with the live speed-test UI and can be plainly wrong when the
raw Kbps fields are absent/zero.
Code

lib/page/health_check/services/health_check_service.dart[R188-195]

  final download = NetworkUtils.formatBitsWithUnit(
-      (speedTestResult.downloadBandwidth ?? 0) * 1024,
+      (speedTestResult.downloadBandwidth ?? 0) * 1000,
    decimals: 1,
  );
  final upload = NetworkUtils.formatBitsWithUnit(
-      (speedTestResult.uploadBandwidth ?? 0) * 1024,
+      (speedTestResult.uploadBandwidth ?? 0) * 1000,
    decimals: 1,
  );
Evidence
The PR now formats bandwidth by converting Kbps→bps using *1000 and NetworkUtils explicitly defines
SI units as base-1000. But the dashboard history chart still converts Kbps→Mbps via /1024 and only
treats units equal to 'KBPS' (not 'kb') as needing conversion, so its scale no longer matches the
rest of the app and its fallback conversion can be skipped incorrectly.

lib/page/health_check/services/health_check_service.dart[176-209]
lib/utils.dart[330-352]
lib/page/dashboard/views/components/widgets/atomic/speed_test.dart[438-467]
lib/page/health_check/models/speed_test_ui_model.dart[31-35]
lib/page/health_check/models/speed_test_ui_model.dart[79-92]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The speed test UI now treats bandwidth as SI (base-1000) (e.g., Kbps→bps uses `* 1000`, and `NetworkUtils.formatBitsWithUnit` uses base-1000 and returns `unit` like `kb`). The dashboard history chart still converts Kbps→Mbps using `/ 1024.0` and only converts parsed values when `unit.toUpperCase() == 'KBPS'`, which does not match the unit strings actually stored/produced.
### Issue Context
This causes historical chart values to diverge from the live speed-test UI. It can also produce plainly wrong values in the fallback branch when `downloadBandwidthKbps/uploadBandwidthKbps` are null/0, because the chart may skip conversion and “assume Mbps”.
### Fix Focus Areas
- lib/page/dashboard/views/components/widgets/atomic/speed_test.dart[438-467]
- lib/utils.dart[330-352]
- lib/page/health_check/services/health_check_service.dart[176-209]
### Suggested fix
1. In the chart, change Kbps→Mbps conversion to divide by `1000.0` (not `1024.0`) for both download and upload.
2. Replace the `unit == 'KBPS'` fallback with logic that recognizes the actual units used by the app (case-insensitive):
- `kb` / `kbps` => divide by 1000
- `mb` / `mbps` => no change
- `gb` / `gbps` => multiply by 1000
- (optionally extend for Tb/Pb)
3. Consider centralizing this conversion into a small helper (e.g., `double toMbps(double value, String unit)`), and add a unit test for it.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

Comment on lines 188 to 195
final download = NetworkUtils.formatBitsWithUnit(
(speedTestResult.downloadBandwidth ?? 0) * 1024,
(speedTestResult.downloadBandwidth ?? 0) * 1000,
decimals: 1,
);
final upload = NetworkUtils.formatBitsWithUnit(
(speedTestResult.uploadBandwidth ?? 0) * 1024,
(speedTestResult.uploadBandwidth ?? 0) * 1000,
decimals: 1,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Chart uses 1024 base 🐞 Bug ✓ Correctness

After this PR’s switch to base-1000 conversions for speed-test bandwidth, the dashboard history
chart still divides Kbps by 1024 to get Mbps and uses a brittle unit-string fallback. This will make
historical chart values inconsistent with the live speed-test UI and can be plainly wrong when the
raw Kbps fields are absent/zero.
Agent Prompt
### Issue description
The speed test UI now treats bandwidth as SI (base-1000) (e.g., Kbps→bps uses `* 1000`, and `NetworkUtils.formatBitsWithUnit` uses base-1000 and returns `unit` like `kb`). The dashboard history chart still converts Kbps→Mbps using `/ 1024.0` and only converts parsed values when `unit.toUpperCase() == 'KBPS'`, which does not match the unit strings actually stored/produced.

### Issue Context
This causes historical chart values to diverge from the live speed-test UI. It can also produce plainly wrong values in the fallback branch when `downloadBandwidthKbps/uploadBandwidthKbps` are null/0, because the chart may skip conversion and “assume Mbps”.

### Fix Focus Areas
- lib/page/dashboard/views/components/widgets/atomic/speed_test.dart[438-467]
- lib/utils.dart[330-352]
- lib/page/health_check/services/health_check_service.dart[176-209]

### Suggested fix
1. In the chart, change Kbps→Mbps conversion to divide by `1000.0` (not `1024.0`) for both download and upload.
2. Replace the `unit == 'KBPS'` fallback with logic that recognizes the actual units used by the app (case-insensitive):
   - `kb` / `kbps` => divide by 1000
   - `mb` / `mbps` => no change
   - `gb` / `gbps` => multiply by 1000
   - (optionally extend for Tb/Pb)
3. Consider centralizing this conversion into a small helper (e.g., `double toMbps(double value, String unit)`), and add a unit test for it.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

- Change "Kb" to "kb" for kilo prefix (SI standard uses lowercase k)
- Replace binary conversion (* 1024) with SI conversion (* 1000) for Kbps to bps
- Fix dashboard history chart to use SI conversion (/ 1000.0) and handle unit variations
- Update related test expectations

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

@HankYuLinksys HankYuLinksys left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@HankYuLinksys
HankYuLinksys merged commit 8711f0a into dev-2.0.0 Feb 12, 2026
2 checks passed
@HankYuLinksys
HankYuLinksys deleted the fix/speed-test-si-units branch February 12, 2026 03:28
@AustinChangLinksys AustinChangLinksys linked an issue Feb 12, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[2.0.0] Merge 1.2.8 to 2.0.0

2 participants