codeant-ai-for-open-source[bot] commented on code in PR #42437: URL: https://github.com/apache/superset/pull/42437#discussion_r3652499871
########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createThroughputFormatter.ts: ########## @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import NumberFormatter from '../NumberFormatter'; +import { NumberFormatFunction } from '../types'; + +const BITS_PER_BYTE = 8; +const BASE = 1000; +const SUFFIXES = [ + 'bps', + 'kbps', + 'Mbps', + 'Gbps', + 'Tbps', + 'Pbps', + 'Ebps', + 'Zbps', + 'Ybps', + 'Rbps', + 'Qbps', +]; + +function formatThroughput( + decimals: number, + fromBytes: boolean, +): NumberFormatFunction { + return value => { + if (value === 0) { + return `0${SUFFIXES[0]}`; + } + + const sign = value > 0 ? '' : '-'; + const magnitude = Math.abs(value); + const bits = fromBytes ? magnitude * BITS_PER_BYTE : magnitude; + const i = Math.max( + 0, + Math.min( + SUFFIXES.length - 1, + Math.floor(Math.log(bits) / Math.log(BASE)), + ), + ); + const scaled = parseFloat((bits / Math.pow(BASE, i)).toFixed(decimals)); Review Comment: **Suggestion:** The suffix is selected before rounding, so values near a unit boundary render with an invalid-looking 1000-unit value instead of advancing to the next suffix. For example, `999999` becomes `1000kbps` with the default precision rather than `1Mbps`, and the same issue occurs for byte conversion. Select the suffix after accounting for rounding or normalize a rounded value of 1000 into the next unit. [logic error] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Throughput labels display non-normalized boundary values. - ⚠️ Network charts can show `1000kbps` instead of `1Mbps`. - ⚠️ Byte-to-bit throughput formatting has the same boundary defect. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Create a formatter through `createThroughputFormatter()` at `superset-frontend/packages/superset-ui-core/src/number-format/factories/createThroughputFormatter.ts:64-80`, using the default `decimals = 2` and `fromBytes = false`. 2. Pass the value `999999` through the returned `NumberFormatter`; its formatting callback is created at lines 75-77 and enters `formatThroughput()` at lines 39-43. 3. At lines 51-57, `bits = 999999` selects suffix index `1` (`kbps`) because the logarithm is floored before rounding. 4. At line 58, `999999 / 1000` becomes `999.999`, which rounds to `1000.00` and parses as `1000`; line 60 therefore returns `1000kbps` instead of the normalized `1Mbps`. The same behavior occurs for byte-source values near the conversion boundary, such as `124999.9` bytes/s with `fromBytes: true`, because line 50 multiplies the value by eight before the same suffix calculation. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=faff76ae18a44a5cb4a76dc532b4fc00&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=faff76ae18a44a5cb4a76dc532b4fc00&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/packages/superset-ui-core/src/number-format/factories/createThroughputFormatter.ts **Line:** 51:58 **Comment:** *Logic Error: The suffix is selected before rounding, so values near a unit boundary render with an invalid-looking 1000-unit value instead of advancing to the next suffix. For example, `999999` becomes `1000kbps` with the default precision rather than `1Mbps`, and the same issue occurs for byte conversion. Select the suffix after accounting for rounding or normalize a rounded value of 1000 into the next unit. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42437&comment_hash=3a7b1b634e9c5ba9f6617038fc722b4243191f0d720920bfe517936717fa7e6f&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42437&comment_hash=3a7b1b634e9c5ba9f6617038fc722b4243191f0d720920bfe517936717fa7e6f&reaction=dislike'>👎</a> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
