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

This commit is contained in:
Moriah Kreeger 2020-12-16 17:13:37 -08:00 committed by GitHub
parent 90ca49f3d6
commit 08b3ebea5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 14 deletions

View File

@ -74,6 +74,10 @@ const CONDITIONS = [
label: t('!= (Is Not Equal)'), label: t('!= (Is Not Equal)'),
value: '!=', value: '!=',
}, },
{
label: t('Not Null'),
value: 'not null',
},
]; ];
const RETENTION_OPTIONS = [ const RETENTION_OPTIONS = [
@ -215,6 +219,10 @@ export const StyledInputContainer = styled.div`
flex: 1 1 auto; flex: 1 1 auto;
} }
input[disabled] {
color: ${({ theme }) => theme.colors.grayscale.base};
}
textarea { textarea {
height: 160px; height: 160px;
resize: none; resize: none;
@ -455,7 +463,9 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
> | null>(); > | null>();
const [isHidden, setIsHidden] = useState<boolean>(true); const [isHidden, setIsHidden] = useState<boolean>(true);
const [contentType, setContentType] = useState<string>('dashboard'); const [contentType, setContentType] = useState<string>('dashboard');
// Dropdown options // Dropdown options
const [conditionNotNull, setConditionNotNull] = useState<boolean>(false);
const [sourceOptions, setSourceOptions] = useState<MetaObject[]>([]); const [sourceOptions, setSourceOptions] = useState<MetaObject[]>([]);
const [dashboardOptions, setDashboardOptions] = useState<MetaObject[]>([]); const [dashboardOptions, setDashboardOptions] = useState<MetaObject[]>([]);
const [chartOptions, setChartOptions] = useState<MetaObject[]>([]); const [chartOptions, setChartOptions] = useState<MetaObject[]>([]);
@ -536,6 +546,10 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
const data: any = { const data: any = {
...currentAlert, ...currentAlert,
validator_type: conditionNotNull ? 'not null' : 'operator',
validator_config_json: conditionNotNull
? {}
: currentAlert?.validator_config_json,
chart: contentType === 'chart' ? currentAlert?.chart?.value : undefined, chart: contentType === 'chart' ? currentAlert?.chart?.value : undefined,
dashboard: dashboard:
contentType === 'dashboard' contentType === 'dashboard'
@ -566,7 +580,11 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
delete data.last_value; delete data.last_value;
delete data.last_value_row_json; delete data.last_value_row_json;
updateResource(update_id, data).then(() => { updateResource(update_id, data).then(response => {
if (!response) {
return;
}
if (onAdd) { if (onAdd) {
onAdd(); onAdd();
} }
@ -577,6 +595,10 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
} else if (currentAlert) { } else if (currentAlert) {
// Create // Create
createResource(data).then(response => { createResource(data).then(response => {
if (!response) {
return;
}
if (onAdd) { if (onAdd) {
onAdd(response); onAdd(response);
} }
@ -787,6 +809,8 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
}; };
const onConditionChange = (op: Operator) => { const onConditionChange = (op: Operator) => {
setConditionNotNull(op === 'not null');
const config = { const config = {
op, op,
threshold: currentAlert threshold: currentAlert
@ -851,8 +875,9 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
} else if ( } else if (
!!currentAlert.database && !!currentAlert.database &&
currentAlert.sql?.length && currentAlert.sql?.length &&
!!currentAlert.validator_config_json?.op && (conditionNotNull || !!currentAlert.validator_config_json?.op) &&
currentAlert.validator_config_json?.threshold !== undefined (conditionNotNull ||
currentAlert.validator_config_json?.threshold !== undefined)
) { ) {
setDisableSave(false); setDisableSave(false);
} else { } else {
@ -890,6 +915,13 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
setNotificationSettings(settings); setNotificationSettings(settings);
setContentType(resource.chart ? 'chart' : 'dashboard'); 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({ setCurrentAlert({
...resource, ...resource,
chart: resource.chart chart: resource.chart
@ -913,9 +945,11 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
})), })),
// @ts-ignore: Type not assignable // @ts-ignore: Type not assignable
validator_config_json: validator_config_json:
typeof resource.validator_config_json === 'string' resource.validator_type === 'not null'
? JSON.parse(resource.validator_config_json) ? {
: resource.validator_config_json, op: 'not null',
}
: validatorConfig,
}); });
} }
}); });
@ -935,7 +969,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
sql: '', sql: '',
type: isReport ? 'Report' : 'Alert', type: isReport ? 'Report' : 'Alert',
validator_config_json: {}, validator_config_json: {},
validator_type: 'not null', validator_type: '',
}); });
setNotificationSettings([]); setNotificationSettings([]);
@ -960,6 +994,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
currentAlert.chart, currentAlert.chart,
contentType, contentType,
notificationSettings, notificationSettings,
conditionNotNull,
] ]
: [], : [],
); );
@ -1138,6 +1173,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
<input <input
type="number" type="number"
name="threshold" name="threshold"
disabled={conditionNotNull}
value={ value={
currentAlert && currentAlert.validator_config_json currentAlert && currentAlert.validator_config_json
? currentAlert.validator_config_json.threshold || '' ? currentAlert.validator_config_json.threshold || ''

View File

@ -38,7 +38,7 @@ export type MetaObject = {
value?: number | string; value?: number | string;
}; };
export type Operator = '<' | '>' | '<=' | '>=' | '==' | '!='; export type Operator = '<' | '>' | '<=' | '>=' | '==' | '!=' | 'not null';
export type AlertObject = { export type AlertObject = {
active?: boolean; active?: boolean;

View File

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