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

[ML] Anomaly Detection: Adds never expire option to forecast creation modal #195151

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,14 @@ export class ForecastsTable extends Component {
name: i18n.translate('xpack.ml.jobsList.jobDetails.forecastsTable.expiresLabel', {
defaultMessage: 'Expires',
}),
render: timeFormatter,
render: (value) => {
if (value === undefined) {
return i18n.translate('xpack.ml.jobsList.jobDetails.forecastsTable.neverExpiresLabel', {
defaultMessage: 'Never expires',
});
}
return timeFormatter(value);
},
textOnly: true,
sortable: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,15 @@ export function forecastServiceFactory(mlApi: MlApi) {
);
}
// Runs a forecast
function runForecast(jobId: string, duration?: string) {
function runForecast(jobId: string, duration?: string, neverExpires?: boolean) {
// eslint-disable-next-line no-console
console.log('ML forecast service run forecast with duration:', duration);
return new Promise((resolve, reject) => {
mlApi
.forecast({
jobId,
duration,
neverExpires,
})
.then((resp) => {
resolve(resp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,18 @@ export function mlApiProvider(httpService: HttpService) {
});
},

forecast({ jobId, duration }: { jobId: string; duration?: string }) {
forecast({
jobId,
duration,
neverExpires,
}: {
jobId: string;
duration?: string;
neverExpires?: boolean;
}) {
const body = JSON.stringify({
...(duration !== undefined ? { duration } : {}),
...(neverExpires === true ? { expires_in: '0' } : {}),
});

return httpService.http<any>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function getDefaultState() {
newForecastDuration: '1d',
isNewForecastDurationValid: true,
newForecastDurationErrors: [],
neverExpires: false,
messages: [],
};
}
Expand Down Expand Up @@ -104,6 +105,12 @@ export class ForecastingModal extends Component {
this.closeModal();
};

onNeverExpiresChange = (event) => {
this.setState({
neverExpires: event.target.checked,
});
};

onNewForecastDurationChange = (event) => {
const newForecastDurationErrors = [];
let isNewForecastDurationValid = true;
Expand Down Expand Up @@ -258,7 +265,7 @@ export class ForecastingModal extends Component {
const durationInSeconds = parseInterval(this.state.newForecastDuration).asSeconds();

this.mlForecastService
.runForecast(this.props.job.job_id, `${durationInSeconds}s`)
.runForecast(this.props.job.job_id, `${durationInSeconds}s`, this.state.neverExpires)
.then((resp) => {
// Endpoint will return { acknowledged:true, id: <now timestamp> } before forecast is complete.
// So wait for results and then refresh the dashboard to the end of the forecast.
Expand Down Expand Up @@ -551,6 +558,8 @@ export class ForecastingModal extends Component {
runForecast={this.checkJobStateAndRunForecast}
newForecastDuration={this.state.newForecastDuration}
onNewForecastDurationChange={this.onNewForecastDurationChange}
onNeverExpiresChange={this.onNeverExpiresChange}
neverExpires={this.state.neverExpires}
isNewForecastDurationValid={this.state.isNewForecastDurationValid}
newForecastDurationErrors={this.state.newForecastDurationErrors}
isForecastRequested={this.state.isForecastRequested}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
EuiForm,
EuiFormRow,
EuiSpacer,
EuiSwitch,
EuiText,
EuiToolTip,
} from '@elastic/eui';
Expand Down Expand Up @@ -82,6 +83,8 @@ export function RunControls({
newForecastDuration,
isNewForecastDurationValid,
newForecastDurationErrors,
neverExpires,
onNeverExpiresChange,
onNewForecastDurationChange,
runForecast,
isForecastRequested,
Expand Down Expand Up @@ -134,34 +137,60 @@ export function RunControls({
<EuiSpacer size="s" />
<EuiForm>
<EuiFlexGroup>
<EuiFlexItem>
<EuiFormRow
label={
<FormattedMessage
id="xpack.ml.timeSeriesExplorer.runControls.durationLabel"
defaultMessage="Duration"
/>
}
fullWidth
isInvalid={!isNewForecastDurationValid}
error={newForecastDurationErrors}
helpText={
<FormattedMessage
id="xpack.ml.timeSeriesExplorer.runControls.forecastMaximumLengthHelpText"
defaultMessage="Length of forecast, up to a maximum of {maximumForecastDurationDays} days.
<EuiFlexItem grow={false}>
<EuiFlexGroup direction="column">
<EuiFlexItem grow={false}>
<EuiFormRow
label={
<FormattedMessage
id="xpack.ml.timeSeriesExplorer.runControls.durationLabel"
defaultMessage="Duration"
/>
}
fullWidth
isInvalid={!isNewForecastDurationValid}
error={newForecastDurationErrors}
helpText={
<FormattedMessage
id="xpack.ml.timeSeriesExplorer.runControls.forecastMaximumLengthHelpText"
defaultMessage="Length of forecast, up to a maximum of {maximumForecastDurationDays} days.
Use s for seconds, m for minutes, h for hours, d for days, w for weeks."
values={{ maximumForecastDurationDays: FORECAST_DURATION_MAX_DAYS }}
/>
}
>
{disabledState.isDisabledToolTipText === undefined ? (
durationInput
) : (
<EuiToolTip position="right" content={disabledState.isDisabledToolTipText}>
{durationInput}
</EuiToolTip>
)}
</EuiFormRow>
values={{ maximumForecastDurationDays: FORECAST_DURATION_MAX_DAYS }}
/>
}
>
{disabledState.isDisabledToolTipText === undefined ? (
durationInput
) : (
<EuiToolTip position="right" content={disabledState.isDisabledToolTipText}>
{durationInput}
</EuiToolTip>
)}
</EuiFormRow>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiFormRow
helpText={i18n.translate(
'xpack.ml.timeSeriesExplorer.runControls.neverExpireHelpText',
{
defaultMessage:
'When not selected, created forecasts will expire after 14 days.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or When not selected, forecast results will be deleted after 14 days. @szabosteve is there anything we can do here to switch the logic around to say what will happen if the switch is selected?

Screenshot 2024-10-07 at 10 21 10

Copy link
Contributor

@szabosteve szabosteve Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if indefinitely is technically correct. Is there an explicit hard limit for retaining forecasts? @peteharverson Let me know what you think, please.

Suggested change
'When not selected, created forecasts will expire after 14 days.',
'When selected, forecast results will be retained indefinitely.',

OR

Suggested change
'When not selected, created forecasts will expire after 14 days.',
'When selected, forecast results will be retained indefinitely. If disabled, forecasts will be retained for 14 days.',

}
)}
>
<EuiSwitch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to disable this switch if the user doesn't have permission to run forecasts? Or I wonder if just hide the entire 'Run a new forecast section'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch is disabled if the 'Run' is disabled. That is existing behavior. If we want to hide it completely that would require an additional change in the parent component. 🤔 I'm not sure we should change it since it has been available to be viewed until now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good. Just wanted to make sure that the 'Never expire' switch was enabled as well as the 'Run' button.

label={i18n.translate(
'xpack.ml.timeSeriesExplorer.runControls.neverExpireLabel',
{
defaultMessage: 'Never expire',
}
)}
checked={neverExpires}
onChange={onNeverExpiresChange}
/>
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiFormRow hasEmptyLabelSpace>
Expand Down Expand Up @@ -193,7 +222,9 @@ RunControls.propType = {
newForecastDuration: PropTypes.string,
isNewForecastDurationValid: PropTypes.bool,
newForecastDurationErrors: PropTypes.array,
neverExpires: PropTypes.bool.isRequired,
onNewForecastDurationChange: PropTypes.func.isRequired,
onNeverExpiresChange: PropTypes.func.isRequired,
runForecast: PropTypes.func.isRequired,
isForecastRequested: PropTypes.bool,
forecastProgress: PropTypes.number,
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/ml/server/routes/anomaly_detectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,10 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) {
routeGuard.fullLicenseAPIGuard(async ({ mlClient, request, response }) => {
try {
const jobId = request.params.jobId;
const duration = request.body.duration;
const body = await mlClient.forecast({
job_id: jobId,
body: {
duration,
...request.body,
},
});
return response.ok({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ export const updateModelSnapshotBodySchema = schema.object({
retain: schema.maybe(schema.boolean()),
});

export const forecastAnomalyDetector = schema.object({ duration: schema.any() });
export const forecastAnomalyDetector = schema.object({
duration: schema.any(),
expires_in: schema.maybe(schema.any()),
});

export const forceQuerySchema = schema.object({
force: schema.maybe(schema.boolean()),
Expand Down