You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/07/03 06:07:29 UTC

(superset) 01/01: feat(time-pivot): migrate time_pivot chart to v1 chart data API

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch legacy-viz-t2-time-pivot
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 786c7d5e07d146f398990083f51af5cc08ca5159
Author: Claude Code <no...@anthropic.com>
AuthorDate: Thu Jul 2 23:06:58 2026 -0700

    feat(time-pivot): migrate time_pivot chart to v1 chart data API
    
    Adds a buildQuery mirroring the legacy NVD3TimePivotViz.query_obj (a
    timeseries query with the single metric) and ports the get_data reshape
    into a transformData helper: timestamps are bucketed by a pandas-style
    offset rollback (AS/A, MS/M, W and W-XXX, D, H, T aliases; multiplier
    prefixes keep the same anchor), periods ranked most-recent-first as
    current/-1/-2, points shifted onto the latest period's time axis, and
    one series emitted per period with the legacy rank/perc metadata. A
    TimePivot-specific transformProps reshapes v1 records before delegating
    to the shared nvd3 transformProps; legacy payloads pass through.
    
    No DB migration needed: viz_type is unchanged and legacy
    granularity_sqla/time_range form data flows through extractExtras.
    
    Co-Authored-By: Claude Fable 5 <no...@anthropic.com>
---
 .../src/TimePivot/buildQuery.ts                    |  34 +++++
 .../src/TimePivot/index.ts                         |   6 +-
 .../src/TimePivot/transformData.ts                 | 164 +++++++++++++++++++++
 .../src/TimePivot/transformProps.ts                |  39 +++++
 .../test/TimePivot.test.ts                         |  96 ++++++++++++
 5 files changed, 336 insertions(+), 3 deletions(-)

diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/buildQuery.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/buildQuery.ts
new file mode 100644
index 00000000000..76a788b6e69
--- /dev/null
+++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/buildQuery.ts
@@ -0,0 +1,34 @@
+/**
+ * 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 { buildQueryContext, QueryFormData } from '@superset-ui/core';
+
+/**
+ * Mirrors the legacy NVD3TimePivotViz.query_obj: a timeseries query with
+ * the single metric.
+ */
+export default function buildQuery(formData: QueryFormData) {
+  const { metric } = formData;
+  return buildQueryContext(formData, baseQueryObject => [
+    {
+      ...baseQueryObject,
+      is_timeseries: true,
+      metrics: metric ? [metric] : [],
+    },
+  ]);
+}
diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/index.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/index.ts
index bf98ec9a44e..f903849837f 100644
--- a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/index.ts
+++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/index.ts
@@ -18,7 +18,7 @@
  */
 import { t } from '@apache-superset/core/translation';
 import { ChartMetadata, ChartPlugin } from '@superset-ui/core';
-import transformProps from '../transformProps';
+import transformProps from './transformProps';
 import thumbnail from './images/thumbnail.png';
 import thumbnailDark from './images/thumbnail-dark.png';
 import example from './images/example.jpg';
@@ -33,16 +33,16 @@ const metadata = new ChartMetadata({
   ),
   exampleGallery: [{ url: example, urlDark: exampleDark }],
   name: t('Time-series Period Pivot'),
-  tags: [t('Legacy'), t('Time'), t('nvd3')],
+  tags: [t('Time'), t('nvd3')],
   thumbnail,
   thumbnailDark,
-  useLegacyApi: true,
 });
 
 export default class TimePivotChartPlugin extends ChartPlugin {
   constructor() {
     super({
       loadChart: () => import('../ReactNVD3'),
+      loadBuildQuery: () => import('./buildQuery'),
       metadata,
       transformProps,
       controlPanel,
diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/transformData.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/transformData.ts
new file mode 100644
index 00000000000..8279478c982
--- /dev/null
+++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/transformData.ts
@@ -0,0 +1,164 @@
+/**
+ * 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 { DTTM_ALIAS } from '@superset-ui/core';
+
+const DAY_MS = 24 * 60 * 60 * 1000;
+const WEEKDAYS = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'];
+
+/**
+ * Rolls a timestamp back to the start of its period the way pandas
+ * offset.rollback(normalize=True) does for the offset aliases the
+ * frequency control offers (AS/A, MS/M, W and W-XXX, D, H, T/MIN).
+ * The multiplier prefix (e.g. 52W-MON) does not change the anchor.
+ */
+export const rollback = (timestamp: number, freq: string): number => {
+  const match = /^\d*(AS|YS|A|Y|MS|M|W(?:-([A-Z]{3}))?|D|H|T|MIN)$/i.exec(
+    (freq || 'W-MON').trim(),
+  );
+  const date = new Date(timestamp);
+  const midnight = Date.UTC(
+    date.getUTCFullYear(),
+    date.getUTCMonth(),
+    date.getUTCDate(),
+  );
+  if (!match) {
+    return midnight; // freeform rules fall back to day boundaries
+  }
+  const unit = match[1].toUpperCase();
+  if (unit === 'AS' || unit === 'YS') {
+    return Date.UTC(date.getUTCFullYear(), 0, 1);
+  }
+  if (unit === 'A' || unit === 'Y') {
+    // most recent Dec 31 at or before the timestamp
+    const yearEnd = Date.UTC(date.getUTCFullYear(), 11, 31);
+    return midnight >= yearEnd
+      ? yearEnd
+      : Date.UTC(date.getUTCFullYear() - 1, 11, 31);
+  }
+  if (unit === 'MS') {
+    return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1);
+  }
+  if (unit === 'M') {
+    // most recent month end at or before the timestamp
+    const monthEnd = Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 0);
+    return midnight >= monthEnd
+      ? monthEnd
+      : Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 0);
+  }
+  if (unit.startsWith('W')) {
+    const anchor = match[2] ? WEEKDAYS.indexOf(match[2].toUpperCase()) : 6; // W defaults to W-SUN
+    // JS getUTCDay: 0=SUN..6=SAT; WEEKDAYS index: 0=MON..6=SUN
+    const jsAnchor = anchor === 6 ? 0 : anchor + 1;
+    const diff = (date.getUTCDay() - jsAnchor + 7) % 7;
+    return midnight - diff * DAY_MS;
+  }
+  if (unit === 'D') {
+    return midnight;
+  }
+  if (unit === 'H') {
+    return timestamp - (timestamp % (60 * 60 * 1000));
+  }
+  return timestamp - (timestamp % (60 * 1000)); // T / MIN
+};
+
+export interface TimePivotSeries {
+  key: string;
+  values: { x: number; y: number | null }[];
+  rank: number;
+  perc: number;
+}
+
+/**
+ * Ports the legacy NVD3TimePivotViz.get_data reshape: timestamps are
+ * bucketed into periods by the freq offset, ranked most-recent-first
+ * ("current", "-1", "-2", ...), shifted onto the latest period's time
+ * axis, and pivoted into one series per period with rank/perc metadata.
+ */
+export default function transformData(
+  records: Record<string, unknown>[],
+  metricLabel: string,
+  freq: string,
+): TimePivotSeries[] {
+  const rows = records
+    .filter(record => record[DTTM_ALIAS] != null)
+    .map(record => {
+      const timestamp = record[DTTM_ALIAS] as number;
+      return {
+        timestamp,
+        period: rollback(timestamp, freq),
+        value: record[metricLabel] as number | null,
+      };
+    });
+  if (rows.length === 0) {
+    return [];
+  }
+
+  const periods = Array.from(new Set(rows.map(row => row.period))).sort(
+    (a, b) => b - a,
+  );
+  const maxPeriod = periods[0];
+  const rankOf = new Map(periods.map((period, index) => [period, index]));
+  const maxRank = periods.length - 1;
+
+  const seriesOf = (rank: number) => (rank === 0 ? 'current' : `-${rank}`);
+
+  // shift every point onto the latest period's time axis
+  const values = new Map<string, Map<number, number | null>>();
+  const shiftedTimestamps = new Set<number>();
+  rows.forEach(({ timestamp, period, value }) => {
+    const series = seriesOf(rankOf.get(period)!);
+    const shifted = timestamp + (maxPeriod - period);
+    shiftedTimestamps.add(shifted);
+    if (!values.has(series)) {
+      values.set(series, new Map());
+    }
+    values.get(series)!.set(shifted, value);
+  });
+
+  const timestamps = Array.from(shiftedTimestamps).sort((a, b) => a - b);
+  // pandas pivot sorts the series labels lexicographically
+  const seriesLabels = Array.from(values.keys()).sort();
+
+  const chartData: TimePivotSeries[] = [];
+  seriesLabels.forEach(series => {
+    const seriesValues = values.get(series)!;
+    let nonNullCount = 0;
+    const points = timestamps.map(timestamp => {
+      let y = seriesValues.has(timestamp) ? seriesValues.get(timestamp)! : null;
+      if (typeof y === 'number' && Number.isNaN(y)) {
+        y = null;
+      }
+      if (y != null) {
+        nonNullCount += 1;
+      }
+      return { x: timestamp, y };
+    });
+    if (nonNullCount === 0) {
+      return;
+    }
+    const rank = series === 'current' ? 0 : Number(series.slice(1));
+    chartData.push({
+      key: series,
+      values: points,
+      rank,
+      perc: 1 - rank / (maxRank + 1),
+    });
+  });
+  return chartData;
+}
diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/transformProps.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/transformProps.ts
new file mode 100644
index 00000000000..62739c13f5d
--- /dev/null
+++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/TimePivot/transformProps.ts
@@ -0,0 +1,39 @@
+/**
+ * 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 { ChartProps, getMetricLabel } from '@superset-ui/core';
+import nvd3TransformProps from '../transformProps';
+import transformData from './transformData';
+
+export default function transformProps(chartProps: ChartProps) {
+  const { formData, queriesData } = chartProps;
+  const rawData = queriesData[0].data;
+  // v1 responses arrive as flat records; the legacy explore_json endpoint
+  // delivered the period-pivoted series directly.
+  const data = Array.isArray(rawData)
+    ? transformData(
+        rawData as Record<string, unknown>[],
+        getMetricLabel(formData.metric),
+        formData.freq as string,
+      )
+    : rawData;
+  return nvd3TransformProps({
+    ...chartProps,
+    queriesData: [{ ...queriesData[0], data }],
+  } as ChartProps);
+}
diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/test/TimePivot.test.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/test/TimePivot.test.ts
new file mode 100644
index 00000000000..774b2da3e9d
--- /dev/null
+++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/test/TimePivot.test.ts
@@ -0,0 +1,96 @@
+/**
+ * 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 { QueryFormData } from '@superset-ui/core';
+import buildQuery from '../src/TimePivot/buildQuery';
+import transformData, { rollback } from '../src/TimePivot/transformData';
+
+describe('TimePivot buildQuery', () => {
+  test('builds a timeseries query with the single metric', () => {
+    const formData: QueryFormData = {
+      datasource: '5__table',
+      granularity_sqla: 'ds',
+      time_range: 'Last quarter',
+      viz_type: 'time_pivot',
+      metric: 'sum__num',
+      freq: 'W-MON',
+    };
+    const [query] = buildQuery(formData).queries;
+    expect(query.metrics).toEqual(['sum__num']);
+    expect(query.is_timeseries).toBe(true);
+  });
+});
+
+describe('TimePivot rollback', () => {
+  const wed = Date.UTC(2024, 0, 10, 15, 30); // Wednesday 2024-01-10
+  test.each([
+    ['W-MON', Date.UTC(2024, 0, 8)],
+    ['52W-MON', Date.UTC(2024, 0, 8)],
+    ['W-SUN', Date.UTC(2024, 0, 7)],
+    ['D', Date.UTC(2024, 0, 10)],
+    ['AS', Date.UTC(2024, 0, 1)],
+    ['MS', Date.UTC(2024, 0, 1)],
+  ])('rolls back to the %s period start', (freq, expected) => {
+    expect(rollback(wed, freq as string)).toEqual(expected);
+  });
+
+  test('rolls back to the most recent month end for M', () => {
+    expect(rollback(wed, 'M')).toEqual(Date.UTC(2023, 11, 31));
+    expect(rollback(Date.UTC(2024, 0, 31), 'M')).toEqual(Date.UTC(2024, 0, 31));
+  });
+});
+
+describe('TimePivot transformData', () => {
+  const mon1 = Date.UTC(2024, 0, 1); // Monday
+  const tue1 = Date.UTC(2024, 0, 2);
+  const mon2 = Date.UTC(2024, 0, 8); // next Monday
+  const tue2 = Date.UTC(2024, 0, 9);
+
+  test('pivots periods onto the latest period axis with ranks', () => {
+    const data = transformData(
+      [
+        { __timestamp: mon1, sum__num: 1 },
+        { __timestamp: tue1, sum__num: 2 },
+        { __timestamp: mon2, sum__num: 3 },
+        { __timestamp: tue2, sum__num: 4 },
+      ],
+      'sum__num',
+      'W-MON',
+    );
+    expect(data.map(series => series.key)).toEqual(['-1', 'current']);
+    const previous = data[0];
+    const current = data[1];
+    expect(previous.rank).toEqual(1);
+    expect(previous.perc).toEqual(0.5);
+    expect(current.rank).toEqual(0);
+    expect(current.perc).toEqual(1);
+    // the older week is shifted onto the current week's timestamps
+    expect(previous.values).toEqual([
+      { x: mon2, y: 1 },
+      { x: tue2, y: 2 },
+    ]);
+    expect(current.values).toEqual([
+      { x: mon2, y: 3 },
+      { x: tue2, y: 4 },
+    ]);
+  });
+
+  test('returns an empty list for empty input', () => {
+    expect(transformData([], 'sum__num', 'W-MON')).toEqual([]);
+  });
+});