Compare commits

...

6 Commits

Author SHA1 Message Date
AAfghahi
3014da13f1 feat: adding logging to validation (#16527)
* adding logging to validation

* Update superset-frontend/src/views/CRUD/data/database/DatabaseModal/index.tsx

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>
(cherry picked from commit 376c685838)
2021-09-15 19:31:27 -07:00
David Aaron Suddjian
3187c66c3a chore: Add option to set a custom color scheme as default (#16540)
* upgrade superset-ui for new flag

* configurable default categorical scheme

* dry refactor

* doc example

* Update superset-frontend/src/setup/setupColors.ts

Co-authored-by: Geido <60598000+geido@users.noreply.github.com>

* re-adding # type: ignore (suggestion from ville)

* ugh... missed a space.

Co-authored-by: Geido <60598000+geido@users.noreply.github.com>
Co-authored-by: Evan Rusackas <evan@preset.io>
(cherry picked from commit 540277ebb1)
2021-09-15 19:30:27 -07:00
Daniel Vaz Gaspar
5cf4d5bb4b feat: feature flag configurable custom backend (#16618)
* feat: feature flag configurable custom backend

* fix lint

* simpler approach

* fix tests

* revert dependency updates

* Update superset/utils/feature_flag_manager.py

Co-authored-by: Ville Brofeldt <33317356+villebro@users.noreply.github.com>

* Update superset/config.py

Co-authored-by: Ville Brofeldt <33317356+villebro@users.noreply.github.com>

Co-authored-by: Ville Brofeldt <33317356+villebro@users.noreply.github.com>
(cherry picked from commit f2bc139e35)
2021-09-14 11:01:28 -07:00
Ville Brofeldt
5d8e1f5d3a fix(explore): make clicked dnd filters unique (#16700)
(cherry picked from commit 1d890f8913)
2021-09-14 11:01:09 -07:00
Ville Brofeldt
7d35a91642 fix(dnd): make clicked dnd metrics unique (#16632)
(cherry picked from commit 9dfa33fedf)
2021-09-10 16:31:04 -07:00
Elizabeth Thompson
cc821bb747 fix: params in sql lab are jumpy in the ace editor (#16536)
* fix jumpy params

* remove cypress tests

(cherry picked from commit 519baa6f00)
2021-09-10 16:30:33 -07:00
12 changed files with 157 additions and 71 deletions

View File

@@ -613,7 +613,7 @@ describe('async actions', () => {
describe('queryEditorSetSql', () => { describe('queryEditorSetSql', () => {
describe('with backend persistence flag on', () => { describe('with backend persistence flag on', () => {
it('does not update the tab state in the backend', () => { it('updates the tab state in the backend', () => {
expect.assertions(2); expect.assertions(2);
const sql = 'SELECT * '; const sql = 'SELECT * ';
@@ -629,7 +629,7 @@ describe('async actions', () => {
}); });
}); });
describe('with backend persistence flag off', () => { describe('with backend persistence flag off', () => {
it('updates the tab state in the backend', () => { it('does not update the tab state in the backend', () => {
const backendPersistenceOffMock = jest const backendPersistenceOffMock = jest
.spyOn(featureFlags, 'isFeatureEnabled') .spyOn(featureFlags, 'isFeatureEnabled')
.mockImplementation( .mockImplementation(

View File

@@ -949,6 +949,11 @@ export function queryEditorSetQueryLimit(queryEditor, queryLimit) {
export function queryEditorSetTemplateParams(queryEditor, templateParams) { export function queryEditorSetTemplateParams(queryEditor, templateParams) {
return function (dispatch) { return function (dispatch) {
dispatch({
type: QUERY_EDITOR_SET_TEMPLATE_PARAMS,
queryEditor,
templateParams,
});
const sync = isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE) const sync = isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE)
? SupersetClient.put({ ? SupersetClient.put({
endpoint: encodeURI(`/tabstateview/${queryEditor.id}`), endpoint: encodeURI(`/tabstateview/${queryEditor.id}`),
@@ -956,15 +961,7 @@ export function queryEditorSetTemplateParams(queryEditor, templateParams) {
}) })
: Promise.resolve(); : Promise.resolve();
return sync return sync.catch(() =>
.then(() =>
dispatch({
type: QUERY_EDITOR_SET_TEMPLATE_PARAMS,
queryEditor,
templateParams,
}),
)
.catch(() =>
dispatch( dispatch(
addDangerToast( addDangerToast(
t( t(

View File

@@ -25,3 +25,13 @@ export interface DatasourcePanelDndItem {
value: DndItemValue; value: DndItemValue;
type: DndItemType; type: DndItemType;
} }
export function isDatasourcePanelDndItem(
item: any,
): item is DatasourcePanelDndItem {
return item?.value && item?.type;
}
export function isSavedMetric(item: any): item is Metric {
return item?.metric_name;
}

View File

@@ -45,10 +45,12 @@ import AdhocMetric from 'src/explore/components/controls/MetricControl/AdhocMetr
import { import {
DatasourcePanelDndItem, DatasourcePanelDndItem,
DndItemValue, DndItemValue,
isSavedMetric,
} from 'src/explore/components/DatasourcePanel/types'; } from 'src/explore/components/DatasourcePanel/types';
import { DndItemType } from 'src/explore/components/DndItemType'; import { DndItemType } from 'src/explore/components/DndItemType';
import { ControlComponentProps } from 'src/explore/components/Control'; import { ControlComponentProps } from 'src/explore/components/Control';
const EMPTY_OBJECT = {};
const DND_ACCEPTED_TYPES = [ const DND_ACCEPTED_TYPES = [
DndItemType.Column, DndItemType.Column,
DndItemType.Metric, DndItemType.Metric,
@@ -78,7 +80,9 @@ export const DndFilterSelect = (props: DndFilterSelectProps) => {
); );
const [partitionColumn, setPartitionColumn] = useState(undefined); const [partitionColumn, setPartitionColumn] = useState(undefined);
const [newFilterPopoverVisible, setNewFilterPopoverVisible] = useState(false); const [newFilterPopoverVisible, setNewFilterPopoverVisible] = useState(false);
const [droppedItem, setDroppedItem] = useState<DndItemValue | null>(null); const [droppedItem, setDroppedItem] = useState<
DndItemValue | typeof EMPTY_OBJECT
>({});
const optionsForSelect = ( const optionsForSelect = (
columns: ColumnMeta[], columns: ColumnMeta[],
@@ -342,12 +346,12 @@ export const DndFilterSelect = (props: DndFilterSelectProps) => {
); );
const handleClickGhostButton = useCallback(() => { const handleClickGhostButton = useCallback(() => {
setDroppedItem(null); setDroppedItem({});
togglePopover(true); togglePopover(true);
}, [togglePopover]); }, [togglePopover]);
const adhocFilter = useMemo(() => { const adhocFilter = useMemo(() => {
if (droppedItem?.metric_name) { if (isSavedMetric(droppedItem)) {
return new AdhocFilter({ return new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL, expressionType: EXPRESSION_TYPES.SQL,
clause: CLAUSES.HAVING, clause: CLAUSES.HAVING,

View File

@@ -34,7 +34,10 @@ import { usePrevious } from 'src/common/hooks/usePrevious';
import AdhocMetric from 'src/explore/components/controls/MetricControl/AdhocMetric'; import AdhocMetric from 'src/explore/components/controls/MetricControl/AdhocMetric';
import AdhocMetricPopoverTrigger from 'src/explore/components/controls/MetricControl/AdhocMetricPopoverTrigger'; import AdhocMetricPopoverTrigger from 'src/explore/components/controls/MetricControl/AdhocMetricPopoverTrigger';
import MetricDefinitionValue from 'src/explore/components/controls/MetricControl/MetricDefinitionValue'; import MetricDefinitionValue from 'src/explore/components/controls/MetricControl/MetricDefinitionValue';
import { DatasourcePanelDndItem } from 'src/explore/components/DatasourcePanel/types'; import {
DatasourcePanelDndItem,
isDatasourcePanelDndItem,
} from 'src/explore/components/DatasourcePanel/types';
import { DndItemType } from 'src/explore/components/DndItemType'; import { DndItemType } from 'src/explore/components/DndItemType';
import DndSelectLabel from 'src/explore/components/controls/DndColumnSelectControl/DndSelectLabel'; import DndSelectLabel from 'src/explore/components/controls/DndColumnSelectControl/DndSelectLabel';
import { savedMetricType } from 'src/explore/components/controls/MetricControl/types'; import { savedMetricType } from 'src/explore/components/controls/MetricControl/types';
@@ -143,9 +146,9 @@ export const DndMetricSelect = (props: any) => {
const [value, setValue] = useState<ValueType[]>( const [value, setValue] = useState<ValueType[]>(
coerceAdhocMetrics(props.value), coerceAdhocMetrics(props.value),
); );
const [droppedItem, setDroppedItem] = useState<DatasourcePanelDndItem | null>( const [droppedItem, setDroppedItem] = useState<
null, DatasourcePanelDndItem | typeof EMPTY_OBJECT
); >({});
const [newMetricPopoverVisible, setNewMetricPopoverVisible] = useState(false); const [newMetricPopoverVisible, setNewMetricPopoverVisible] = useState(false);
const prevColumns = usePrevious(columns); const prevColumns = usePrevious(columns);
const prevSavedMetrics = usePrevious(savedMetrics); const prevSavedMetrics = usePrevious(savedMetrics);
@@ -323,13 +326,16 @@ export const DndMetricSelect = (props: any) => {
); );
const handleClickGhostButton = useCallback(() => { const handleClickGhostButton = useCallback(() => {
setDroppedItem(null); setDroppedItem({});
togglePopover(true); togglePopover(true);
}, [togglePopover]); }, [togglePopover]);
const adhocMetric = useMemo(() => { const adhocMetric = useMemo(() => {
if (droppedItem?.type === DndItemType.Column) { if (
const itemValue = droppedItem?.value as ColumnMeta; isDatasourcePanelDndItem(droppedItem) &&
droppedItem.type === DndItemType.Column
) {
const itemValue = droppedItem.value as ColumnMeta;
const config: Partial<AdhocMetric> = { const config: Partial<AdhocMetric> = {
column: { column_name: itemValue?.column_name }, column: { column_name: itemValue?.column_name },
}; };

View File

@@ -31,44 +31,43 @@ import {
SequentialScheme, SequentialScheme,
} from '@superset-ui/core'; } from '@superset-ui/core';
import superset from '@superset-ui/core/lib/color/colorSchemes/categorical/superset'; import superset from '@superset-ui/core/lib/color/colorSchemes/categorical/superset';
import ColorSchemeRegistry from '@superset-ui/core/lib/color/ColorSchemeRegistry';
function registerColorSchemes(
registry: ColorSchemeRegistry<unknown>,
colorSchemes: (CategoricalScheme | SequentialScheme)[],
standardDefaultKey: string,
) {
colorSchemes.forEach(scheme => {
registry.registerValue(scheme.id, scheme);
});
const defaultKey =
colorSchemes.find(scheme => scheme.isDefault)?.id || standardDefaultKey;
registry.setDefaultKey(defaultKey);
}
export default function setupColors( export default function setupColors(
extraCategoricalColorSchemas: CategoricalScheme[] = [], extraCategoricalColorSchemes: CategoricalScheme[] = [],
extraSequentialColorSchemes: SequentialScheme[] = [], extraSequentialColorSchemes: SequentialScheme[] = [],
) { ) {
// Register color schemes registerColorSchemes(
const categoricalSchemeRegistry = getCategoricalSchemeRegistry(); getCategoricalSchemeRegistry(),
[
if (extraCategoricalColorSchemas?.length > 0) { ...superset,
extraCategoricalColorSchemas.forEach(scheme => { ...airbnb,
categoricalSchemeRegistry.registerValue(scheme.id, scheme); ...categoricalD3,
}); ...echarts,
} ...google,
...lyft,
[superset, airbnb, categoricalD3, echarts, google, lyft, preset].forEach( ...preset,
group => { ...extraCategoricalColorSchemes,
group.forEach(scheme => { ],
categoricalSchemeRegistry.registerValue(scheme.id, scheme); 'supersetColors',
});
},
); );
categoricalSchemeRegistry.setDefaultKey('supersetColors'); registerColorSchemes(
getSequentialSchemeRegistry(),
const sequentialSchemeRegistry = getSequentialSchemeRegistry(); [...sequentialCommon, ...sequentialD3, ...extraSequentialColorSchemes],
'superset_seq_1',
if (extraSequentialColorSchemes?.length > 0) {
extraSequentialColorSchemes.forEach(scheme => {
sequentialSchemeRegistry.registerValue(
scheme.id,
new SequentialScheme(scheme),
); );
});
}
[sequentialCommon, sequentialD3].forEach(group => {
group.forEach(scheme => {
sequentialSchemeRegistry.registerValue(scheme.id, scheme);
});
});
sequentialSchemeRegistry.setDefaultKey('superset_seq_1');
} }

View File

@@ -908,14 +908,14 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
/> />
); );
} }
const message: Array<string> =
const message: Array<string> = Object.values(dbErrors); typeof dbErrors === 'object' ? Object.values(dbErrors) : [];
return ( return (
<Alert <Alert
type="error" type="error"
css={(theme: SupersetTheme) => antDErrorAlertStyles(theme)} css={(theme: SupersetTheme) => antDErrorAlertStyles(theme)}
message="Database Creation Error" message="Database Creation Error"
description={message[0]} description={message?.[0] || dbErrors}
/> />
); );
}; };

View File

@@ -427,7 +427,14 @@ FEATURE_FLAGS: Dict[str, bool] = {}
# feature_flags_dict['some_feature'] = g.user and g.user.get_id() == 5 # feature_flags_dict['some_feature'] = g.user and g.user.get_id() == 5
# return feature_flags_dict # return feature_flags_dict
GET_FEATURE_FLAGS_FUNC: Optional[Callable[[Dict[str, bool]], Dict[str, bool]]] = None GET_FEATURE_FLAGS_FUNC: Optional[Callable[[Dict[str, bool]], Dict[str, bool]]] = None
# A function that receives a feature flag name and an optional default value.
# Has a similar utility to GET_FEATURE_FLAGS_FUNC but it's useful to not force the
# evaluation of all feature flags when just evaluating a single one.
#
# Note that the default `get_feature_flags` will evaluate each feature with this
# callable when the config key is set, so don't use both GET_FEATURE_FLAGS_FUNC
# and IS_FEATURE_ENABLED_FUNC in conjunction.
IS_FEATURE_ENABLED_FUNC: Optional[Callable[[str, Optional[bool]], bool]] = None
# A function that expands/overrides the frontend `bootstrap_data.common` object. # A function that expands/overrides the frontend `bootstrap_data.common` object.
# Can be used to implement custom frontend functionality, # Can be used to implement custom frontend functionality,
# or dynamically change certain configs. # or dynamically change certain configs.
@@ -449,6 +456,7 @@ COMMON_BOOTSTRAP_OVERRIDES_FUNC: Callable[
# "id": 'myVisualizationColors', # "id": 'myVisualizationColors',
# "description": '', # "description": '',
# "label": 'My Visualization Colors', # "label": 'My Visualization Colors',
# "isDefault": True,
# "colors": # "colors":
# ['#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77', # ['#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77',
# '#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD'] # '#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD']
@@ -483,6 +491,7 @@ THEME_OVERRIDES: Dict[str, Any] = {}
# "description": '', # "description": '',
# "isDiverging": True, # "isDiverging": True,
# "label": 'My custom warm to hot', # "label": 'My custom warm to hot',
# "isDefault": True,
# "colors": # "colors":
# ['#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD', # ['#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD',
# '#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77'] # '#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77']

View File

@@ -72,6 +72,10 @@ class TestConnectionDatabaseCommand(BaseCommand):
database.db_engine_spec.mutate_db_for_connection_test(database) database.db_engine_spec.mutate_db_for_connection_test(database)
username = self._actor.username if self._actor is not None else None username = self._actor.username if self._actor is not None else None
engine = database.get_sqla_engine(user_name=username) engine = database.get_sqla_engine(user_name=username)
event_logger.log_with_context(
action="test_connection_attempt",
engine=database.db_engine_spec.__name__,
)
with closing(engine.raw_connection()) as conn: with closing(engine.raw_connection()) as conn:
try: try:
alive = engine.dialect.do_ping(conn) alive = engine.dialect.do_ping(conn)

View File

@@ -33,6 +33,7 @@ from superset.databases.dao import DatabaseDAO
from superset.db_engine_specs import get_engine_specs from superset.db_engine_specs import get_engine_specs
from superset.db_engine_specs.base import BasicParametersMixin from superset.db_engine_specs.base import BasicParametersMixin
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.extensions import event_logger
from superset.models.core import Database from superset.models.core import Database
BYPASS_VALIDATION_ENGINES = {"bigquery"} BYPASS_VALIDATION_ENGINES = {"bigquery"}
@@ -89,6 +90,7 @@ class ValidateDatabaseParametersCommand(BaseCommand):
self._properties.get("parameters", {}) self._properties.get("parameters", {})
) )
if errors: if errors:
event_logger.log_with_context(action="validation_error", engine=engine)
raise InvalidParametersError(errors) raise InvalidParametersError(errors)
serialized_encrypted_extra = self._properties.get("encrypted_extra", "{}") serialized_encrypted_extra = self._properties.get("encrypted_extra", "{}")

View File

@@ -24,24 +24,36 @@ class FeatureFlagManager:
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self._get_feature_flags_func = None self._get_feature_flags_func = None
self._is_feature_enabled_func = None
self._feature_flags: Dict[str, Any] = {} self._feature_flags: Dict[str, Any] = {}
def init_app(self, app: Flask) -> None: def init_app(self, app: Flask) -> None:
self._get_feature_flags_func = app.config["GET_FEATURE_FLAGS_FUNC"] self._get_feature_flags_func = app.config["GET_FEATURE_FLAGS_FUNC"]
self._is_feature_enabled_func = app.config["IS_FEATURE_ENABLED_FUNC"]
self._feature_flags = app.config["DEFAULT_FEATURE_FLAGS"] self._feature_flags = app.config["DEFAULT_FEATURE_FLAGS"]
self._feature_flags.update(app.config["FEATURE_FLAGS"]) self._feature_flags.update(app.config["FEATURE_FLAGS"])
def get_feature_flags(self) -> Dict[str, Any]: def get_feature_flags(self) -> Dict[str, Any]:
if self._get_feature_flags_func: if self._get_feature_flags_func:
return self._get_feature_flags_func(deepcopy(self._feature_flags)) return self._get_feature_flags_func(deepcopy(self._feature_flags))
if callable(self._is_feature_enabled_func):
return dict(
map(
lambda kv: (kv[0], self._is_feature_enabled_func(kv[0], kv[1])),
self._feature_flags.items(),
)
)
return self._feature_flags return self._feature_flags
def is_feature_enabled(self, feature: str) -> bool: def is_feature_enabled(self, feature: str) -> bool:
"""Utility function for checking whether a feature is turned on""" """Utility function for checking whether a feature is turned on"""
if self._is_feature_enabled_func:
return (
self._is_feature_enabled_func(feature, self._feature_flags[feature])
if feature in self._feature_flags
else False
)
feature_flags = self.get_feature_flags() feature_flags = self.get_feature_flags()
if feature_flags and feature in feature_flags: if feature_flags and feature in feature_flags:
return feature_flags[feature] return feature_flags[feature]
return False return False

View File

@@ -16,10 +16,16 @@
# under the License. # under the License.
from unittest.mock import patch from unittest.mock import patch
from superset import is_feature_enabled from parameterized import parameterized
from superset import get_feature_flags, is_feature_enabled
from tests.integration_tests.base_tests import SupersetTestCase from tests.integration_tests.base_tests import SupersetTestCase
def dummy_is_feature_enabled(feature_flag_name: str, default: bool = True) -> bool:
return True if feature_flag_name.startswith("True_") else default
class TestFeatureFlag(SupersetTestCase): class TestFeatureFlag(SupersetTestCase):
@patch.dict( @patch.dict(
"superset.extensions.feature_flag_manager._feature_flags", "superset.extensions.feature_flag_manager._feature_flags",
@@ -38,3 +44,40 @@ class TestFeatureFlag(SupersetTestCase):
def test_feature_flags(self): def test_feature_flags(self):
self.assertEqual(is_feature_enabled("foo"), "bar") self.assertEqual(is_feature_enabled("foo"), "bar")
self.assertEqual(is_feature_enabled("super"), "set") self.assertEqual(is_feature_enabled("super"), "set")
@patch.dict(
"superset.extensions.feature_flag_manager._feature_flags",
{"True_Flag1": False, "True_Flag2": True, "Flag3": False, "Flag4": True},
clear=True,
)
class TestFeatureFlagBackend(SupersetTestCase):
@parameterized.expand(
[
("True_Flag1", True),
("True_Flag2", True),
("Flag3", False),
("Flag4", True),
("True_DoesNotExist", False),
]
)
@patch(
"superset.extensions.feature_flag_manager._is_feature_enabled_func",
dummy_is_feature_enabled,
)
def test_feature_flags_override(self, feature_flag_name, expected):
self.assertEqual(is_feature_enabled(feature_flag_name), expected)
@patch(
"superset.extensions.feature_flag_manager._is_feature_enabled_func",
dummy_is_feature_enabled,
)
@patch(
"superset.extensions.feature_flag_manager._get_feature_flags_func", None,
)
def test_get_feature_flags(self):
feature_flags = get_feature_flags()
self.assertEqual(
feature_flags,
{"True_Flag1": True, "True_Flag2": True, "Flag3": False, "Flag4": True},
)