Skip to content

Commit

Permalink
feat(alert/report): add 'not null' condition option to modal (#12077)
Browse files Browse the repository at this point in the history
  • Loading branch information
riahk committed Dec 17, 2020
1 parent 90ca49f commit 08b3ebe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
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 @@ -455,7 +463,9 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
> | 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 @@ -536,6 +546,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 @@ -566,7 +580,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 @@ -577,6 +595,10 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
} else if (currentAlert) {
// Create
createResource(data).then(response => {
if (!response) {
return;
}

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

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

const config = {
op,
threshold: currentAlert
Expand Down Expand Up @@ -851,8 +875,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 @@ -890,6 +915,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 @@ -913,9 +945,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 @@ -935,7 +969,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
sql: '',
type: isReport ? 'Report' : 'Alert',
validator_config_json: {},
validator_type: 'not null',
validator_type: '',
});

setNotificationSettings([]);
Expand All @@ -960,6 +994,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
currentAlert.chart,
contentType,
notificationSettings,
conditionNotNull,
]
: [],
);
Expand Down Expand Up @@ -1138,6 +1173,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

0 comments on commit 08b3ebe

Please sign in to comment.