bito-code-review[bot] commented on code in PR #43863:
URL: https://github.com/apache/superset/pull/43863#discussion_r4052915925


##########
superset-frontend/plugins/plugin-chart-echarts/src/Candlestick/controlPanel.tsx:
##########
@@ -0,0 +1,427 @@
+/**
+ * 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 { t } from '@apache-superset/core/translation';
+import {
+  ensureIsArray,
+  getColumnLabel,
+  QueryFormColumn,
+  validateNonEmpty,
+} from '@superset-ui/core';
+import {
+  ControlPanelConfig,
+  ControlPanelsContainerProps,
+  ControlSubSectionHeader,
+  D3_TIME_FORMAT_DOCS,
+  DEFAULT_TIME_FORMAT,
+  getStandardizedControls,
+  sections,
+  sharedControls,
+} from '@superset-ui/chart-controls';
+import {
+  legendSection,
+  tooltipTimeFormatControl,
+  tooltipValuesFormatControl,
+  xAxisLabelInterval,
+  xAxisLabelRotation,
+} from '../controls';
+import {
+  CANDLESTICK_SERIES_NAME,
+  DEFAULT_DECREASE_COLOR,
+  DEFAULT_INCREASE_COLOR,
+  DEFAULT_SERIES_STYLE,
+} from './constants';
+import { MOVING_AVERAGE_PERIODS } from './utils';
+
+function hasSeriesDimension({
+  controls,
+}: ControlPanelsContainerProps): boolean {
+  return ensureIsArray(controls?.series?.value).length > 0;
+}
+
+type QueryRow = Record<string, unknown>;
+type ChartQueryResponse = { data?: QueryRow[] };
+
+const uniqueSeriesCountByResponse = new WeakMap<
+  ChartQueryResponse,
+  Map<string, number>
+>();
+const hasMultipleSeriesByProps = new WeakMap<
+  ControlPanelsContainerProps,
+  boolean
+>();
+
+function getSeriesColumnLabel(
+  props: ControlPanelsContainerProps,
+): string | undefined {
+  const [series] = ensureIsArray(props.controls?.series?.value);
+  if (series == null) {
+    return undefined;
+  }
+  return getColumnLabel(series as QueryFormColumn);
+}
+
+function getChartQueryResponse(
+  props: ControlPanelsContainerProps,
+): ChartQueryResponse | undefined {
+  return (props as { chart?: { queriesResponse?: ChartQueryResponse[] } 
}).chart
+    ?.queriesResponse?.[0];
+}
+
+function countUniqueSeriesValues(
+  props: ControlPanelsContainerProps,
+): number | undefined {
+  const seriesColumn = getSeriesColumnLabel(props);
+  if (!seriesColumn) {
+    return undefined;
+  }
+  const queryResponse = getChartQueryResponse(props);
+  const rows = queryResponse?.data;
+  if (!queryResponse || !rows) {
+    return undefined;
+  }
+  let cachedByColumn = uniqueSeriesCountByResponse.get(queryResponse);
+  if (!cachedByColumn) {
+    cachedByColumn = new Map<string, number>();
+    uniqueSeriesCountByResponse.set(queryResponse, cachedByColumn);
+  }
+  const cachedCount = cachedByColumn.get(seriesColumn);
+  if (cachedCount !== undefined) {
+    return cachedCount;
+  }
+  const count = new Set(
+    rows.map(row =>
+      row[seriesColumn] == null ? null : String(row[seriesColumn]),
+    ),
+  ).size;
+  cachedByColumn.set(seriesColumn, count);
+  return count;
+}
+
+function hasMultipleSeries(props: ControlPanelsContainerProps): boolean {
+  const cached = hasMultipleSeriesByProps.get(props);
+  if (cached !== undefined) {
+    return cached;
+  }
+  const result = (countUniqueSeriesValues(props) ?? 0) > 1;
+  hasMultipleSeriesByProps.set(props, result);
+  return result;

Review Comment:
   <!-- Bito Reply -->
   The suggestion to remove the `hasMultipleSeriesByProps` cache is correct. 
Since `countUniqueSeriesValues` already memoizes the result using 
`uniqueSeriesCountByResponse`, the outer cache is redundant and introduces 
unnecessary module-level state that relies on prop identity. Removing the outer 
cache simplifies the logic while maintaining identical behavior.
   
   
**superset-frontend/plugins/plugin-chart-echarts/src/Candlestick/controlPanel.tsx**
   ```
   function hasMultipleSeries(props: ControlPanelsContainerProps): boolean {
     return (countUniqueSeriesValues(props) ?? 0) > 1;
   }
   ```



-- 
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]

Reply via email to