villebro commented on code in PR #32264: URL: https://github.com/apache/superset/pull/32264#discussion_r1956635507
########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createNetworkNumberFormatter.ts: ########## @@ -0,0 +1,173 @@ +/* + * 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 { format as d3Format } from 'd3-format'; +import NumberFormatter from '../NumberFormatter'; +import NumberFormats from '../NumberFormats'; + +const float2PointFormatter = d3Format(`.2~f`); +const float4PointFormatter = d3Format(`.4~f`); + +const bytesSILabels = ['Bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']; +const bytesIECLabels = [ + 'Bytes', + 'KiB', + 'MiB', + 'GiB', + 'TiB', + 'PiB', + 'EiB', + 'ZiB', +]; +const byterateSILabels = [ + 'Bytes/s', + 'kB/s', + 'MB/s', + 'GB/s', + 'TB/s', + 'PB/s', + 'EB/s', + 'ZB/s', +]; +const byterateIECLabels = [ + 'Bytes/s', + 'KiB/s', + 'MiB/s', + 'GiB/s', + 'TiB/s', + 'PiB/s', + 'EiB/s', + 'ZiB/s', +]; + +const MAX_CACHE_SIZE = 100; +const formatterCache = new Map<number, Function>(); + +function siFormatter(decimals: number) { + if (formatterCache.size >= MAX_CACHE_SIZE) { + const iterator = formatterCache.keys(); + const first = iterator.next(); + if (!first.done) { + formatterCache.delete(first.value); + } + } + + try { + if (!formatterCache.has(decimals)) { + formatterCache.set(decimals, d3Format(`.${decimals}s`)); + } + return formatterCache.get(decimals); + } catch (error) { + throw new Error( + `Failed to create SI formatter for ${decimals} decimals: ${error.message}`, + { cause: error }, + ); + } +} Review Comment: Is this performance optimization needed? Was there performance issues without this? ########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createNetworkNumberFormatter.ts: ########## @@ -0,0 +1,173 @@ +/* + * 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 { format as d3Format } from 'd3-format'; +import NumberFormatter from '../NumberFormatter'; +import NumberFormats from '../NumberFormats'; + +const float2PointFormatter = d3Format(`.2~f`); +const float4PointFormatter = d3Format(`.4~f`); + +const bytesSILabels = ['Bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']; +const bytesIECLabels = [ + 'Bytes', + 'KiB', + 'MiB', + 'GiB', + 'TiB', + 'PiB', + 'EiB', + 'ZiB', +]; +const byterateSILabels = [ + 'Bytes/s', + 'kB/s', + 'MB/s', + 'GB/s', + 'TB/s', + 'PB/s', + 'EB/s', + 'ZB/s', +]; +const byterateIECLabels = [ + 'Bytes/s', + 'KiB/s', + 'MiB/s', + 'GiB/s', + 'TiB/s', + 'PiB/s', + 'EiB/s', + 'ZiB/s', +]; Review Comment: To avoid duplication, can we just use the bytes above and then slap on a `/s` after them? ########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createNetworkNumberFormatter.ts: ########## @@ -0,0 +1,173 @@ +/* + * 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 { format as d3Format } from 'd3-format'; +import NumberFormatter from '../NumberFormatter'; +import NumberFormats from '../NumberFormats'; + +const float2PointFormatter = d3Format(`.2~f`); +const float4PointFormatter = d3Format(`.4~f`); + +const bytesSILabels = ['Bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']; +const bytesIECLabels = [ + 'Bytes', + 'KiB', + 'MiB', + 'GiB', + 'TiB', + 'PiB', + 'EiB', + 'ZiB', +]; +const byterateSILabels = [ + 'Bytes/s', + 'kB/s', + 'MB/s', + 'GB/s', + 'TB/s', + 'PB/s', + 'EB/s', + 'ZB/s', +]; +const byterateIECLabels = [ + 'Bytes/s', + 'KiB/s', + 'MiB/s', + 'GiB/s', + 'TiB/s', + 'PiB/s', + 'EiB/s', + 'ZiB/s', +]; + +const MAX_CACHE_SIZE = 100; +const formatterCache = new Map<number, Function>(); + +function siFormatter(decimals: number) { + if (formatterCache.size >= MAX_CACHE_SIZE) { + const iterator = formatterCache.keys(); + const first = iterator.next(); + if (!first.done) { + formatterCache.delete(first.value); + } + } + + try { + if (!formatterCache.has(decimals)) { + formatterCache.set(decimals, d3Format(`.${decimals}s`)); + } + return formatterCache.get(decimals); + } catch (error) { + throw new Error( + `Failed to create SI formatter for ${decimals} decimals: ${error.message}`, + { cause: error }, + ); + } +} + +function formatValue( + value: number, + labels: string[], + base: number, + decimals: number, +) { + if (value === 0) { + return `0 ${labels[0]}`; + } + const absoluteValue = Math.abs(value); + if (absoluteValue >= 1000) { + const i = Math.min( + Math.floor(Math.log(absoluteValue) / Math.log(base)), + labels.length - 1, + ); + const parsedVal = parseFloat( + (absoluteValue / Math.pow(base, i)).toFixed(decimals), + ); + return `${value < 0 ? '-' : ''}${parsedVal} ${labels[i]}`; + } + if (absoluteValue >= 1) { + return `${float2PointFormatter(value)} ${labels[0]}`; + } + if (absoluteValue >= 0.001) { + return `${float4PointFormatter(value)} ${labels[0]}`; + } + if (absoluteValue > 0.000001) { + return `${siFormatter(decimals)(value * 1000000)}ยต ${labels[0]}`; + } + return `${float4PointFormatter(value)} ${labels[0]}`; +} + +function formatBytesSI(value: number, decimals: number) { + return formatValue(value, bytesSILabels, 1000, decimals); +} Review Comment: There's a fair amount of logic here that we'd want to have unit tests for. Can we add those to get full test coverage? ########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createNetworkNumberFormatter.ts: ########## @@ -0,0 +1,156 @@ +// @ts-nocheck + +import { format as d3Format } from 'd3-format'; +import NumberFormatter from '../NumberFormatter'; +import NumberFormats from '../NumberFormats'; + +const float2PointFormatter = d3Format(`.2~f`); +const float4PointFormatter = d3Format(`.4~f`); + +const bytesSILabels = ['Bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']; +const bytesIECLabels = [ + 'Bytes', + 'KiB', + 'MiB', + 'GiB', + 'TiB', + 'PiB', + 'EiB', + 'ZiB', +]; Review Comment: Is there overlap here with the code in `createMemoryFormatter.ts`? I think ideally we want to avoid duplication and double down on the same terms. Some quick observations: - Here we're talking about `Bytes`, while in memory formatter we have `B` - Here we're only going to `ZiB`/`ZB`, while in memory formatter we're going all the way to `YiB` and for SI to `QB`. -- 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]
