villebro commented on code in PR #32264: URL: https://github.com/apache/superset/pull/32264#discussion_r1956774951
########## 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: Ugh, sorry about our house bot giving you bad advice 🤦 Yes, please roll this back, I didn't quite understand how it would help in this context. So let's keep it simple and add memoization later if it's really needed. -- 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]
