codeant-ai-for-open-source[bot] commented on code in PR #40954:
URL: https://github.com/apache/superset/pull/40954#discussion_r3391462137


##########
superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts:
##########
@@ -319,9 +322,12 @@ export function transformSeries(
    * same as the original series, otherwise uses separate colors
    * */
   const itemStyle: ItemStyleOption = {
-    color: timeShiftColor
-      ? colorScale(colorScaleKey, sliceId)
-      : colorScale(seriesKey || forecastSeries.name, sliceId),
+    color: isAnomaly
+      ? (theme?.colorError ??
+        colorScale(seriesKey || forecastSeries.name, sliceId))
+      : timeShiftColor
+        ? colorScale(colorScaleKey, sliceId)
+        : colorScale(seriesKey || forecastSeries.name, sliceId),

Review Comment:
   **Suggestion:** The new anomaly color assignment does not actually guarantee 
red anomaly points when `colorByPrimaryAxis` is enabled, because this chart 
path bypasses series-level `itemStyle` and applies per-point colors instead. 
This breaks the intended behavior ("anomalies are red") in a valid chart 
configuration. Ensure anomaly series bypass `applyColorByPrimaryAxis` coloring 
(or force red in that branch) so anomaly markers stay red consistently. [logic 
error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Timeseries anomaly points not reliably highlighted in red.
   - ⚠️ Color-by-X-axis charts misrepresent anomaly visual semantics.
   - ⚠️ Users may overlook anomalies due to inconsistent coloring.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Configure a Time-series ECharts chart (e.g., Regular Line) that uses the 
Timeseries
   plugin so that `transformProps()` at
   
`superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts:220-307`
   is used to build the series list and call `transformSeries()` for each 
metric column.
   
   2. In the chart control panel, enable anomaly detection so the backend 
produces
   `metric__anomaly` columns (verified by tests in
   `superset/tests/unit_tests/pandas_postprocessing/test_anomaly.py`) and 
enable the "Color
   by X-axis / primary axis" option so `colorByPrimaryAxis` is `true` in the
   `EchartsTimeseriesFormData` passed to `transformProps()` and forwarded into
   `transformSeries()` (`transformProps.ts:256-293`).
   
   3. At runtime, for the anomaly series, `transformSeries()` in
   
`superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts:270-275`
   sets `forecastSeries.type === ForecastSeriesEnum.Anomaly`, making 
`isAnomaly` true and
   computing a red series-level `itemStyle.color` using `theme?.colorError` in 
the snippet at
   lines 324-333; however, because `colorByPrimaryAxis` is true and 
`Array.isArray(data)` is
   true, the returned series has its `data` replaced by the result of
   `applyColorByPrimaryAxis()` (`transformers.ts:379-387`) and omits the 
`itemStyle` entirely
   via `...(colorByPrimaryAxis ? {} : { itemStyle })` at line 397.
   
   4. `applyColorByPrimaryAxis()` (`transformers.ts:172-195`) colors each point 
by its
   primary-axis value using `colorScale(colorKey, sliceId)` and ignores 
anomaly-specific
   styling, so in the rendered chart anomaly points are colored by X-axis 
category rather
   than consistently red, diverging from the intended "red anomaly markers" 
behavior even
   though the anomaly path is exercised through normal chart configuration.
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=0ba6085e1a714987bfae29a7a6d2c563&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=0ba6085e1a714987bfae29a7a6d2c563&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/plugins/plugin-chart-echarts/src/Timeseries/transformers.ts
   **Line:** 325:330
   **Comment:**
        *Logic Error: The new anomaly color assignment does not actually 
guarantee red anomaly points when `colorByPrimaryAxis` is enabled, because this 
chart path bypasses series-level `itemStyle` and applies per-point colors 
instead. This breaks the intended behavior ("anomalies are red") in a valid 
chart configuration. Ensure anomaly series bypass `applyColorByPrimaryAxis` 
coloring (or force red in that branch) so anomaly markers stay red consistently.
   
   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%2F40954&comment_hash=0854226ae046ab47b648129bbb32d74a9348e3a010685abcf19b6f1045418784&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40954&comment_hash=0854226ae046ab47b648129bbb32d74a9348e3a010685abcf19b6f1045418784&reaction=dislike'>👎</a>



##########
superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts:
##########
@@ -26,14 +26,25 @@ import {
 } from '../types';
 import { sanitizeHtml } from './series';
 
-const seriesTypeRegex = new RegExp(
+const forecastSuffixRegex = new RegExp(
   
`(.+)(${ForecastSeriesEnum.ForecastLower}|${ForecastSeriesEnum.ForecastTrend}|${ForecastSeriesEnum.ForecastUpper})$`,
 );
 export const extractForecastSeriesContext = (
   seriesName: OptionName,
 ): ForecastSeriesContext => {
   const name = seriesName as string;
-  const regexMatch = seriesTypeRegex.exec(name);
+
+  // Check for anomaly suffix first, then resolve nested forecast suffix
+  if (name.endsWith(ForecastSeriesEnum.Anomaly)) {
+    const stripped = name.slice(0, -ForecastSeriesEnum.Anomaly.length);
+    const forecastMatch = forecastSuffixRegex.exec(stripped);
+    return {
+      name: forecastMatch ? forecastMatch[1] : stripped,
+      type: ForecastSeriesEnum.Anomaly,
+    };

Review Comment:
   **Suggestion:** The anomaly parsing now collapses nested forecast anomaly 
series (for example `metric__yhat__anomaly`) to the same base key as regular 
anomaly series (`metric__anomaly`). When both exist (such as forecast enabled 
with zero future periods), tooltip aggregation merges them into one entry and 
overwrites values/colors, producing incorrect tooltip output. Preserve distinct 
keys for nested anomaly series (e.g., keep `stripped` as the name) and only 
normalize for display later. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Timeseries charts can drop one of two anomaly series.
   - ⚠️ Tooltips cannot distinguish original vs forecast anomalies.
   - ⚠️ Users may misinterpret which values were anomalous.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Enable Prophet forecast and anomaly detection together for a timeseries 
metric in a
   chart that uses the ECharts Timeseries plugin; configure forecast so that 
the original
   metric has no NaNs in the returned dataframe (e.g., zero or no future 
periods). Backend
   tests in
   
`superset/tests/unit_tests/pandas_postprocessing/test_anomaly.py:test_anomaly_detection_with_forecast`
   confirm this produces both `metric__anomaly` and `metric__yhat__anomaly` 
columns.
   
   2. On the frontend, `transformProps()` in
   
`superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts:73-75`
   calls `rebaseForecastDatum(data, verboseMap)`
   
(`superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts:138-170`).
 Inside
   `rebaseForecastDatum`, each column key is passed through 
`extractForecastSeriesContext()`
   (`forecast.ts:32-53`); for both `metric__anomaly` and 
`metric__yhat__anomaly`, the code at
   lines 38-44 detects the `__anomaly` suffix, strips it, optionally strips any 
`__yhat*`
   suffix via `forecastSuffixRegex`, and returns `ForecastSeriesContext { name: 
'metric',
   type: ForecastSeriesEnum.Anomaly }` for both columns.
   
   3. Still in `rebaseForecastDatum`, when `verboseMap` has an entry for the 
base metric key
   `'metric'`, both anomaly columns compute the same `verboseKey`
   `${verboseMap['metric']}${ForecastSeriesEnum.Anomaly}` 
(`forecast.ts:147-151`). The second
   anomaly column processed (typically `metric__yhat__anomaly`, as in the 
backend test column
   order) overwrites the first in `newRow`, so only one anomaly column remains 
in the rebased
   data and the other anomaly series' values are silently lost before 
`extractSeries()`
   builds the chart series
   
(`superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts:70-103`).
   
   4. When hovering the chart, the tooltip formatter in
   
`superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts:1015-1079`
   calls `extractForecastValuesFromTooltipParams()` (`forecast.ts:68-97`), 
which again uses
   `extractForecastSeriesContext(seriesId)` and aggregates by `context.name`. 
Because both
   base and forecast anomaly series are normalized to the same `name` 
`'metric'`, any
   remaining cases where both anomaly series survive (e.g., if `verboseMap` 
lacked the base
   key or in other consumers such as 
`MixedTimeseries/transformProps.ts:790-797`) will be
   merged into a single `ForecastValue` entry, causing one series' anomaly 
value/color to
   overwrite the other and preventing tooltips from distinguishing base vs 
forecast
   anomalies.
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=1728d023c2bd4454a0962a94a0b03644&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=1728d023c2bd4454a0962a94a0b03644&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/plugins/plugin-chart-echarts/src/utils/forecast.ts
   **Line:** 38:44
   **Comment:**
        *Logic Error: The anomaly parsing now collapses nested forecast anomaly 
series (for example `metric__yhat__anomaly`) to the same base key as regular 
anomaly series (`metric__anomaly`). When both exist (such as forecast enabled 
with zero future periods), tooltip aggregation merges them into one entry and 
overwrites values/colors, producing incorrect tooltip output. Preserve distinct 
keys for nested anomaly series (e.g., keep `stripped` as the name) and only 
normalize for display later.
   
   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%2F40954&comment_hash=b31d42ab423e69c95cccb51294d63ee06ba7e1b3c48a5110484a4b5ec567bab4&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40954&comment_hash=b31d42ab423e69c95cccb51294d63ee06ba7e1b3c48a5110484a4b5ec567bab4&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]

Reply via email to