Skip to content

Commit

Permalink
Add paginated_update to migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje committed May 16, 2023
1 parent 156431c commit c7a5764
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
12 changes: 6 additions & 6 deletions superset-frontend/src/dashboard/actions/dashboardInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export function dashboardInfoChanged(newInfo: { metadata: any }) {

return { type: DASHBOARD_INFO_UPDATED, newInfo };
}
export const SET_CHART_CONFIG_BEGIN = 'SET_CHART_CONFIG_BEGIN';
export const SET_CHART_CONFIG_COMPLETE = 'SET_CHART_CONFIG_COMPLETE';
export const SET_CHART_CONFIG_FAIL = 'SET_CHART_CONFIG_FAIL';
export const SAVE_CHART_CONFIG_BEGIN = 'SAVE_CHART_CONFIG_BEGIN';
export const SAVE_CHART_CONFIG_COMPLETE = 'SAVE_CHART_CONFIG_COMPLETE';
export const SAVE_CHART_CONFIG_FAIL = 'SAVE_CHART_CONFIG_FAIL';

export const saveChartConfiguration =
({
Expand All @@ -81,7 +81,7 @@ export const saveChartConfiguration =
}) =>
async (dispatch: Dispatch, getState: () => RootState) => {
dispatch({
type: SET_CHART_CONFIG_BEGIN,
type: SAVE_CHART_CONFIG_BEGIN,
chartConfiguration,
globalChartConfiguration,
});
Expand Down Expand Up @@ -112,13 +112,13 @@ export const saveChartConfiguration =
}),
);
dispatch({
type: SET_CHART_CONFIG_COMPLETE,
type: SAVE_CHART_CONFIG_COMPLETE,
chartConfiguration,
globalChartConfiguration,
});
} catch (err) {
dispatch({
type: SET_CHART_CONFIG_FAIL,
type: SAVE_CHART_CONFIG_FAIL,
chartConfiguration,
globalChartConfiguration,
});
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/dashboard/actions/dashboardState.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { UPDATE_COMPONENTS_PARENTS_LIST } from './dashboardLayout';
import {
saveChartConfiguration,
dashboardInfoChanged,
SET_CHART_CONFIG_COMPLETE,
SAVE_CHART_CONFIG_COMPLETE,
} from './dashboardInfo';
import { fetchDatasourceMetadata } from './datasources';
import {
Expand Down Expand Up @@ -327,7 +327,7 @@ export function saveDashboardRequest(data, id, saveType) {
);
if (metadata.chart_configuration) {
dispatch({
type: SET_CHART_CONFIG_COMPLETE,
type: SAVE_CHART_CONFIG_COMPLETE,
chartConfiguration: metadata.chart_configuration,
});
}
Expand Down
16 changes: 12 additions & 4 deletions superset-frontend/src/dashboard/util/charts/useChartIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
* specific language governing permissions and limitations
* under the License.
*/
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import isEqual from 'lodash/isEqual';
import { createSelector } from '@reduxjs/toolkit';
import { RootState } from 'src/dashboard/types';
import { useMemoCompare } from 'src/hooks/useMemoCompare';

const chartIdsSelector = createSelector(
(state: RootState) => state.charts,
charts => Object.values(charts).map(chart => chart.id),
);

export const useChartIds = () => {
const chartIds = useSelector<RootState, number[]>(state =>
Object.values(state.charts).map(chart => chart.id),
const chartIds = useSelector<RootState, number[]>(chartIdsSelector);
return useMemoCompare(
chartIds,
(prev, next) => prev === next || isEqual(prev, next),
);
return useMemo(() => chartIds, [chartIds]);
};
35 changes: 35 additions & 0 deletions superset-frontend/src/hooks/useMemoCompare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 { useEffect, useRef } from 'react';

export const useMemoCompare = <T>(
next: T,
compare: (prev: T | undefined, next: T) => boolean,
) => {
const previousRef = useRef<T>();
const previous = previousRef.current;
const isEqual = compare(previous, next);
useEffect(() => {
if (!isEqual) {
previousRef.current = next;
}
});
return isEqual ? previous : next;
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from sqlalchemy.ext.declarative import declarative_base

from superset import db
from superset.migrations.shared.utils import paginated_update

Base = declarative_base()

Expand All @@ -48,7 +49,7 @@ class Dashboard(Base):
def upgrade():
bind = op.get_bind()
session = db.Session(bind=bind)
for dashboard in session.query(Dashboard).all():
for dashboard in paginated_update(session.query(Dashboard).all()):
try:
json_metadata = json.loads(dashboard.json_metadata)
new_chart_configuration = {}
Expand Down Expand Up @@ -82,7 +83,7 @@ def downgrade():
bind = op.get_bind()
session = db.Session(bind=bind)

for dashboard in session.query(Dashboard).all():
for dashboard in paginated_update(session.query(Dashboard).all()):
try:
json_metadata = json.loads(dashboard.json_metadata)
new_chart_configuration = {}
Expand Down

0 comments on commit c7a5764

Please sign in to comment.