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

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

Merged
merged 10 commits into from
Mar 31, 2023
65 changes: 42 additions & 23 deletions superset-frontend/src/views/CRUD/alert/AlertReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ import {
MetaObject,
Operator,
Recipient,
AlertsReportsConfig,
} from 'src/views/CRUD/alert/types';
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
import { useSelector } from 'react-redux';
import { AlertReportCronScheduler } from './components/AlertReportCronScheduler';
import { NotificationMethod } from './components/NotificationMethod';

Expand All @@ -82,6 +84,10 @@ interface AlertReportModalProps {
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_FORMAT = 'PNG';
const CONDITIONS = [
Expand Down Expand Up @@ -134,25 +140,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)`
max-width: 1200px;
width: 100%;
Expand Down Expand Up @@ -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 = (
index: number,
setting: NotificationSetting,
Expand Down Expand Up @@ -549,7 +568,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
setIsHidden(true);
onHide();
setNotificationSettings([]);
setCurrentAlert({ ...DEFAULT_ALERT });
setCurrentAlert({ ...defaultAlert });
setNotificationAddState('active');
};

Expand Down Expand Up @@ -992,7 +1011,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
!isEditMode &&
(!currentAlert || currentAlert.id || (isHidden && show))
) {
setCurrentAlert({ ...DEFAULT_ALERT });
setCurrentAlert({ ...defaultAlert });
setNotificationSettings([]);
setNotificationAddState('active');
}
Expand Down Expand Up @@ -1299,7 +1318,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
<span className="required">*</span>
</StyledSectionTitle>
<AlertReportCronScheduler
value={currentAlert?.crontab || DEFAULT_CRON_VALUE}
value={currentAlert?.crontab || ALERT_REPORTS_DEFAULT_CRON_VALUE}
onChange={newVal => updateAlertState('crontab', newVal)}
/>
<div className="control-label">{TRANSLATIONS.TIMEZONE_TEXT}</div>
Expand Down Expand Up @@ -1329,7 +1348,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
value={
typeof currentAlert?.log_retention === 'number'
? currentAlert?.log_retention
: DEFAULT_RETENTION
: ALERT_REPORTS_DEFAULT_RETENTION
}
options={RETENTION_OPTIONS}
sortComparator={propertyComparator('value')}
Expand Down
5 changes: 5 additions & 0 deletions superset-frontend/src/views/CRUD/alert/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ export enum RecipientIconName {
Email = 'Email',
Slack = 'Slack',
}
export interface AlertsReportsConfig {
ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT: number;
ALERT_REPORTS_DEFAULT_RETENTION: number;
ALERT_REPORTS_DEFAULT_CRON_VALUE: string;
}
4 changes: 4 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@ def EMAIL_HEADER_MUTATOR( # pylint: disable=invalid-name,unused-argument
# 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
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.
# Useful for debugging
ALERT_REPORTS_NOTIFICATION_DRY_RUN = False
Expand Down
3 changes: 3 additions & 0 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
"HTML_SANITIZATION_SCHEMA_EXTENSIONS",
"WELCOME_PAGE_LAST_TAB",
"VIZ_TYPE_DENYLIST",
"ALERT_REPORTS_DEFAULT_CRON_VALUE",
"ALERT_REPORTS_DEFAULT_RETENTION",
"ALERT_REPORTS_DEFAULT_WORKING_TIMEOUT",
)

logger = logging.getLogger(__name__)
Expand Down