Skip to content

Commit

Permalink
feat: Adds Histogram chart migration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed Jun 4, 2024
1 parent 896fe85 commit 7a664af
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions superset/cli/viz_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class VizType(str, Enum):
DIST_BAR = "dist_bar"
DUAL_LINE = "dual_line"
HEATMAP = "heatmap"
HISTOGRAM = "histogram"
LINE = "line"
PIVOT_TABLE = "pivot_table"
SUNBURST = "sunburst"
Expand Down Expand Up @@ -85,6 +86,7 @@ def migrate(viz_type: VizType, is_downgrade: bool = False) -> None:
MigrateDistBarChart,
MigrateDualLine,
MigrateHeatmapChart,
MigrateHistogramChart,
MigrateLineChart,
MigratePivotTable,
MigrateSunburst,
Expand All @@ -98,6 +100,7 @@ def migrate(viz_type: VizType, is_downgrade: bool = False) -> None:
VizType.DIST_BAR: MigrateDistBarChart,
VizType.DUAL_LINE: MigrateDualLine,
VizType.HEATMAP: MigrateHeatmapChart,
VizType.HISTOGRAM: MigrateHistogramChart,
VizType.LINE: MigrateLineChart,
VizType.PIVOT_TABLE: MigratePivotTable,
VizType.SUNBURST: MigrateSunburst,
Expand Down
19 changes: 19 additions & 0 deletions superset/migrations/shared/migrate_viz/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,22 @@ class MigrateHeatmapChart(MigrateViz):

def _pre_action(self) -> None:
self.data["legend_type"] = "continuous"


class MigrateHistogramChart(MigrateViz):
source_viz_type = "histogram"
target_viz_type = "histogram_v2"
rename_keys = {
"x_axis_label": "x_axis_title",
"y_axis_label": "y_axis_title",
"normalized": "normalize",
}
remove_keys = {"all_columns_x", "link_length"}

def _pre_action(self) -> None:
all_columns_x = self.data.get("all_columns_x")
if all_columns_x and len(all_columns_x) > 0:
self.data["column"] = all_columns_x[0]

link_length = self.data.get("link_length")
self.data["bins"] = int(link_length) if link_length else 5
51 changes: 51 additions & 0 deletions tests/unit_tests/migrations/viz/histogram_v1_v2_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.
from typing import Any

from superset.migrations.shared.migrate_viz import MigrateHistogramChart
from tests.unit_tests.migrations.viz.utils import migrate_and_assert

SOURCE_FORM_DATA: dict[str, Any] = {
"all_columns_x": ["category"],
"adhoc_filters": [],
"cumulative": True,
"linear_color_scheme": "blue",
"link_length": "5",
"normalized": True,
"row_limit": 100,
"viz_type": "histogram",
"x_axis_label": "X",
"y_axis_label": "Y",
}

TARGET_FORM_DATA: dict[str, Any] = {
"adhoc_filters": [],
"bins": 5,
"column": "category",
"cumulative": True,
"form_data_bak": SOURCE_FORM_DATA,
"linear_color_scheme": "blue",
"normalize": True,
"row_limit": 100,
"viz_type": "histogram_v2",
"x_axis_title": "X",
"y_axis_title": "Y",
}


def test_migration() -> None:
migrate_and_assert(MigrateHistogramChart, SOURCE_FORM_DATA, TARGET_FORM_DATA)

0 comments on commit 7a664af

Please sign in to comment.