Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alert/report): add 'not null' condition option to modal #12077

Merged
merged 4 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions superset-frontend/src/views/CRUD/alert/AlertReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ const CONDITIONS = [
label: t('!= (Is Not Equal)'),
value: '!=',
},
{
label: t('Not Null'),
value: 'not null',
},
];

const RETENTION_OPTIONS = [
Expand Down Expand Up @@ -215,6 +219,10 @@ export const StyledInputContainer = styled.div`
flex: 1 1 auto;
}

input[disabled] {
color: ${({ theme }) => theme.colors.grayscale.base};
}

textarea {
height: 160px;
resize: none;
Expand Down Expand Up @@ -453,7 +461,9 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
const [currentAlert, setCurrentAlert] = useState<AlertObject | null>();
const [isHidden, setIsHidden] = useState<boolean>(true);
const [contentType, setContentType] = useState<string>('dashboard');

// Dropdown options
const [conditionNotNull, setConditionNotNull] = useState<boolean>(false);
const [sourceOptions, setSourceOptions] = useState<MetaObject[]>([]);
const [dashboardOptions, setDashboardOptions] = useState<MetaObject[]>([]);
const [chartOptions, setChartOptions] = useState<MetaObject[]>([]);
Expand Down Expand Up @@ -534,6 +544,10 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({

const data: any = {
...currentAlert,
validator_type: conditionNotNull ? 'not null' : 'operator',
validator_config_json: conditionNotNull
? {}
: currentAlert?.validator_config_json,
chart: contentType === 'chart' ? currentAlert?.chart?.value : undefined,
dashboard:
contentType === 'dashboard'
Expand Down Expand Up @@ -564,7 +578,11 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
delete data.last_value;
delete data.last_value_row_json;

updateResource(update_id, data).then(() => {
updateResource(update_id, data).then(response => {
if (!response) {
return;
}

if (onAdd) {
onAdd();
}
Expand All @@ -575,6 +593,10 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
} else if (currentAlert) {
// Create
createResource(data).then(response => {
if (!response) {
return;
}

if (onAdd) {
onAdd(response);
}
Expand Down Expand Up @@ -785,6 +807,8 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
};

const onConditionChange = (op: Operator) => {
setConditionNotNull(op === 'not null');

const config = {
op,
threshold: currentAlert
Expand Down Expand Up @@ -849,8 +873,9 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
} else if (
!!currentAlert.database &&
currentAlert.sql?.length &&
!!currentAlert.validator_config_json?.op &&
currentAlert.validator_config_json?.threshold !== undefined
(conditionNotNull || !!currentAlert.validator_config_json?.op) &&
(conditionNotNull ||
currentAlert.validator_config_json?.threshold !== undefined)
) {
setDisableSave(false);
} else {
Expand Down Expand Up @@ -888,6 +913,13 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
setNotificationSettings(settings);
setContentType(resource.chart ? 'chart' : 'dashboard');

const validatorConfig =
typeof resource.validator_config_json === 'string'
? JSON.parse(resource.validator_config_json)
: resource.validator_config_json;

setConditionNotNull(resource.validator_type === 'not null');

setCurrentAlert({
...resource,
chart: resource.chart
Expand All @@ -911,9 +943,11 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
})),
// @ts-ignore: Type not assignable
validator_config_json:
typeof resource.validator_config_json === 'string'
? JSON.parse(resource.validator_config_json)
: resource.validator_config_json,
resource.validator_type === 'not null'
? {
op: 'not null',
}
: validatorConfig,
});
}
});
Expand All @@ -933,7 +967,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
sql: '',
type: isReport ? 'Report' : 'Alert',
validator_config_json: {},
validator_type: 'not null',
validator_type: '',
});

setNotificationSettings([]);
Expand All @@ -958,6 +992,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
currentAlert.chart,
contentType,
notificationSettings,
conditionNotNull,
]
: [],
);
Expand Down Expand Up @@ -1136,6 +1171,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
<input
type="number"
name="threshold"
disabled={conditionNotNull}
value={
currentAlert && currentAlert.validator_config_json
? currentAlert.validator_config_json.threshold || ''
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/views/CRUD/alert/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type MetaObject = {
value?: number | string;
};

export type Operator = '<' | '>' | '<=' | '>=' | '==' | '!=';
export type Operator = '<' | '>' | '<=' | '>=' | '==' | '!=' | 'not null';

export type AlertObject = {
active?: boolean;
Expand Down
24 changes: 18 additions & 6 deletions superset-frontend/src/views/CRUD/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,23 @@ export function useSingleViewResource<D extends object = any>(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.id;
},
createErrorHandler(errMsg =>
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while creating %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
),
),
);

updateState({
error: errMsg,
});
}),
)
.finally(() => {
updateState({ loading: false });
Expand All @@ -287,18 +292,25 @@ export function useSingleViewResource<D extends object = any>(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.result;
},
createErrorHandler(errMsg =>
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while fetching %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
),
),
);

updateState({
error: errMsg,
});

return errMsg;
}),
)
.finally(() => {
updateState({ loading: false });
Expand Down