fix: moved alerts and reports default values to config (#22880)

This commit is contained in:
Stepan 2023-03-31 11:58:59 +03:00 committed by GitHub
parent 500d90058f
commit 09757dc518
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 23 deletions

View File

@ -55,8 +55,10 @@ import {
MetaObject, MetaObject,
Operator, Operator,
Recipient, Recipient,
AlertsReportsConfig,
} from 'src/views/CRUD/alert/types'; } from 'src/views/CRUD/alert/types';
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls'; import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
import { useSelector } from 'react-redux';
import { AlertReportCronScheduler } from './components/AlertReportCronScheduler'; import { AlertReportCronScheduler } from './components/AlertReportCronScheduler';
import { NotificationMethod } from './components/NotificationMethod'; import { NotificationMethod } from './components/NotificationMethod';
@ -83,6 +85,10 @@ interface AlertReportModalProps {
show: boolean; show: boolean;
} }
const DEFAULT_WORKING_TIMEOUT = 3600;
const DEFAULT_CRON_VALUE = '0 * * * *'; // every hour
const DEFAULT_RETENTION = 90;
const DEFAULT_NOTIFICATION_METHODS: NotificationMethodOption[] = ['Email']; const DEFAULT_NOTIFICATION_METHODS: NotificationMethodOption[] = ['Email'];
const DEFAULT_NOTIFICATION_FORMAT = 'PNG'; const DEFAULT_NOTIFICATION_FORMAT = 'PNG';
const CONDITIONS = [ const CONDITIONS = [
@ -135,25 +141,6 @@ const RETENTION_OPTIONS = [
}, },
]; ];
const DEFAULT_RETENTION = 90;
const DEFAULT_WORKING_TIMEOUT = 3600;
const DEFAULT_CRON_VALUE = '0 * * * *'; // every hour
const DEFAULT_ALERT = {
active: true,
creation_method: 'alerts_reports',
crontab: DEFAULT_CRON_VALUE,
log_retention: DEFAULT_RETENTION,
working_timeout: DEFAULT_WORKING_TIMEOUT,
name: '',
owners: [],
recipients: [],
sql: '',
validator_config_json: {},
validator_type: '',
force_screenshot: false,
grace_period: undefined,
};
const StyledModal = styled(Modal)` const StyledModal = styled(Modal)`
max-width: 1200px; max-width: 1200px;
width: 100%; width: 100%;
@ -512,6 +499,38 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
); );
}; };
const {
ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT,
ALERT_REPORTS_DEFAULT_CRON_VALUE,
ALERT_REPORTS_DEFAULT_RETENTION,
} = useSelector<any, AlertsReportsConfig>(state => {
const conf = state.common?.conf;
return {
ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT:
conf?.ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT ?? DEFAULT_WORKING_TIMEOUT,
ALERT_REPORTS_DEFAULT_CRON_VALUE:
conf?.ALERT_REPORTS_DEFAULT_CRON_VALUE ?? DEFAULT_CRON_VALUE,
ALERT_REPORTS_DEFAULT_RETENTION:
conf?.ALERT_REPORTS_DEFAULT_RETENTION ?? DEFAULT_RETENTION,
};
});
const defaultAlert = {
active: true,
creation_method: 'alerts_reports',
crontab: ALERT_REPORTS_DEFAULT_CRON_VALUE,
log_retention: ALERT_REPORTS_DEFAULT_RETENTION,
working_timeout: ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT,
name: '',
owners: [],
recipients: [],
sql: '',
validator_config_json: {},
validator_type: '',
force_screenshot: false,
grace_period: undefined,
};
const updateNotificationSetting = ( const updateNotificationSetting = (
index: number, index: number,
setting: NotificationSetting, setting: NotificationSetting,
@ -549,7 +568,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
setIsHidden(true); setIsHidden(true);
onHide(); onHide();
setNotificationSettings([]); setNotificationSettings([]);
setCurrentAlert({ ...DEFAULT_ALERT }); setCurrentAlert({ ...defaultAlert });
setNotificationAddState('active'); setNotificationAddState('active');
}; };
@ -992,7 +1011,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
!isEditMode && !isEditMode &&
(!currentAlert || currentAlert.id || (isHidden && show)) (!currentAlert || currentAlert.id || (isHidden && show))
) { ) {
setCurrentAlert({ ...DEFAULT_ALERT }); setCurrentAlert({ ...defaultAlert });
setNotificationSettings([]); setNotificationSettings([]);
setNotificationAddState('active'); setNotificationAddState('active');
} }
@ -1299,7 +1318,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
<span className="required">*</span> <span className="required">*</span>
</StyledSectionTitle> </StyledSectionTitle>
<AlertReportCronScheduler <AlertReportCronScheduler
value={currentAlert?.crontab || DEFAULT_CRON_VALUE} value={currentAlert?.crontab || ALERT_REPORTS_DEFAULT_CRON_VALUE}
onChange={newVal => updateAlertState('crontab', newVal)} onChange={newVal => updateAlertState('crontab', newVal)}
/> />
<div className="control-label">{TRANSLATIONS.TIMEZONE_TEXT}</div> <div className="control-label">{TRANSLATIONS.TIMEZONE_TEXT}</div>
@ -1329,7 +1348,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
value={ value={
typeof currentAlert?.log_retention === 'number' typeof currentAlert?.log_retention === 'number'
? currentAlert?.log_retention ? currentAlert?.log_retention
: DEFAULT_RETENTION : ALERT_REPORTS_DEFAULT_RETENTION
} }
options={RETENTION_OPTIONS} options={RETENTION_OPTIONS}
sortComparator={propertyComparator('value')} sortComparator={propertyComparator('value')}

View File

@ -117,3 +117,8 @@ export enum RecipientIconName {
Email = 'Email', Email = 'Email',
Slack = 'Slack', Slack = 'Slack',
} }
export interface AlertsReportsConfig {
ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT: number;
ALERT_REPORTS_DEFAULT_RETENTION: number;
ALERT_REPORTS_DEFAULT_CRON_VALUE: string;
}

View File

@ -1264,6 +1264,10 @@ ALERT_REPORTS_WORKING_TIME_OUT_LAG = int(timedelta(seconds=10).total_seconds())
# if ALERT_REPORTS_WORKING_TIME_OUT_KILL is True, set a celery hard timeout # if ALERT_REPORTS_WORKING_TIME_OUT_KILL is True, set a celery hard timeout
# Equal to working timeout + ALERT_REPORTS_WORKING_SOFT_TIME_OUT_LAG # Equal to working timeout + ALERT_REPORTS_WORKING_SOFT_TIME_OUT_LAG
ALERT_REPORTS_WORKING_SOFT_TIME_OUT_LAG = int(timedelta(seconds=1).total_seconds()) ALERT_REPORTS_WORKING_SOFT_TIME_OUT_LAG = int(timedelta(seconds=1).total_seconds())
# Default values that user using when creating alert
ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT = 3600
ALERT_REPORTS_DEFAULT_RETENTION = 90
ALERT_REPORTS_DEFAULT_CRON_VALUE = "0 * * * *" # every hour
# If set to true no notification is sent, the worker will just log a message. # If set to true no notification is sent, the worker will just log a message.
# Useful for debugging # Useful for debugging
ALERT_REPORTS_NOTIFICATION_DRY_RUN = False ALERT_REPORTS_NOTIFICATION_DRY_RUN = False

View File

@ -116,6 +116,9 @@ FRONTEND_CONF_KEYS = (
"HTML_SANITIZATION_SCHEMA_EXTENSIONS", "HTML_SANITIZATION_SCHEMA_EXTENSIONS",
"WELCOME_PAGE_LAST_TAB", "WELCOME_PAGE_LAST_TAB",
"VIZ_TYPE_DENYLIST", "VIZ_TYPE_DENYLIST",
"ALERT_REPORTS_DEFAULT_CRON_VALUE",
"ALERT_REPORTS_DEFAULT_RETENTION",
"ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT",
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)