-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] Data Frame Analytics custom URLs: adds ability to set custom time range in urls #155337
Merged
alvarezmelissa87
merged 8 commits into
elastic:main
from
alvarezmelissa87:ml-custom-urls-add-timepicker-for-dfa
Apr 24, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f8faa8f
create custom time range picker component for dfa custom urls
alvarezmelissa87 e5da7e6
fix jest tests
alvarezmelissa87 6dd3e69
Remove duplicate translation id
alvarezmelissa87 b4350ca
Merge branch 'main' into ml-custom-urls-add-timepicker-for-dfa
alvarezmelissa87 879af45
only show customTimeRangePicker if dataview has timefield
alvarezmelissa87 eca357a
move custom time range determination to util function
alvarezmelissa87 b355d3a
Merge branch 'main' into ml-custom-urls-add-timepicker-for-dfa
kibanamachine aad4207
Merge branch 'main' into ml-custom-urls-add-timepicker-for-dfa
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
.../public/application/components/custom_urls/custom_url_editor/custom_time_range_picker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { FC, useMemo, useState } from 'react'; | ||
import moment, { type Moment } from 'moment'; | ||
import { | ||
EuiDatePicker, | ||
EuiDatePickerRange, | ||
EuiFlexItem, | ||
EuiFlexGroup, | ||
EuiFormRow, | ||
EuiIconTip, | ||
EuiSpacer, | ||
EuiSwitch, | ||
} from '@elastic/eui'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { useMlKibana } from '../../../contexts/kibana'; | ||
|
||
interface CustomUrlTimeRangePickerProps { | ||
onCustomTimeRangeChange: (customTimeRange?: { start: Moment; end: Moment }) => void; | ||
customTimeRange?: { start: Moment; end: Moment }; | ||
} | ||
|
||
/* | ||
* React component for the form for adding a custom time range. | ||
*/ | ||
export const CustomTimeRangePicker: FC<CustomUrlTimeRangePickerProps> = ({ | ||
onCustomTimeRangeChange, | ||
customTimeRange, | ||
}) => { | ||
const [showCustomTimeRangeSelector, setShowCustomTimeRangeSelector] = useState<boolean>(false); | ||
const { | ||
services: { | ||
data: { | ||
query: { | ||
timefilter: { timefilter }, | ||
}, | ||
}, | ||
}, | ||
} = useMlKibana(); | ||
|
||
const onCustomTimeRangeSwitchChange = (checked: boolean) => { | ||
if (checked === false) { | ||
// Clear the custom time range so it isn't persisted | ||
onCustomTimeRangeChange(undefined); | ||
} | ||
setShowCustomTimeRangeSelector(checked); | ||
}; | ||
|
||
// If the custom time range is not set, default to the timefilter settings | ||
const currentTimeRange = useMemo( | ||
() => | ||
customTimeRange ?? { | ||
start: moment(timefilter.getAbsoluteTime().from), | ||
end: moment(timefilter.getAbsoluteTime().to), | ||
}, | ||
[customTimeRange, timefilter] | ||
); | ||
|
||
const handleStartChange = (date: moment.Moment) => { | ||
onCustomTimeRangeChange({ ...currentTimeRange, start: date }); | ||
}; | ||
const handleEndChange = (date: moment.Moment) => { | ||
onCustomTimeRangeChange({ ...currentTimeRange, end: date }); | ||
}; | ||
|
||
const { start, end } = currentTimeRange; | ||
|
||
return ( | ||
<> | ||
<EuiSpacer size="s" /> | ||
<EuiFlexGroup gutterSize={'none'}> | ||
<EuiFlexItem grow={false}> | ||
<EuiIconTip | ||
content={i18n.translate('xpack.ml.customUrlsEditor.customTimeRangeTooltip', { | ||
defaultMessage: 'If not set, time range defaults to global settings.', | ||
})} | ||
position="top" | ||
type="iInCircle" | ||
/> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiFormRow | ||
display="columnCompressedSwitch" | ||
label={ | ||
<FormattedMessage | ||
id="xpack.ml.customUrlsEditor.customTimeRangeSwitch" | ||
defaultMessage="Add custom time range?" | ||
/> | ||
} | ||
> | ||
<EuiSwitch | ||
showLabel={false} | ||
label={ | ||
<FormattedMessage | ||
id="xpack.ml.customUrlsEditor.addCustomTimeRangeSwitchLabel" | ||
defaultMessage="Add custom time range switch" | ||
/> | ||
} | ||
checked={showCustomTimeRangeSelector} | ||
onChange={(e) => onCustomTimeRangeSwitchChange(e.target.checked)} | ||
compressed | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
{showCustomTimeRangeSelector ? ( | ||
<> | ||
<EuiSpacer size="s" /> | ||
<EuiFormRow | ||
label={ | ||
<FormattedMessage | ||
id="xpack.ml.customUrlsEditor.customTimeRangeLabel" | ||
defaultMessage="Custom time range" | ||
/> | ||
} | ||
> | ||
<EuiDatePickerRange | ||
data-test-subj={`mlCustomUrlsDateRange`} | ||
isInvalid={start > end} | ||
startDateControl={ | ||
<EuiDatePicker | ||
selected={start} | ||
onChange={handleStartChange} | ||
startDate={start} | ||
endDate={end} | ||
aria-label={i18n.translate('xpack.ml.customUrlsEditor.customTimeRangeStartDate', { | ||
defaultMessage: 'Start date', | ||
})} | ||
showTimeSelect | ||
/> | ||
} | ||
endDateControl={ | ||
<EuiDatePicker | ||
selected={end} | ||
onChange={handleEndChange} | ||
startDate={start} | ||
endDate={end} | ||
aria-label={i18n.translate('xpack.ml.customUrlsEditor.customTimeRangeEndDate', { | ||
defaultMessage: 'End date', | ||
})} | ||
showTimeSelect | ||
/> | ||
} | ||
/> | ||
</EuiFormRow> | ||
</> | ||
) : null} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the destination data view of the job has a timestamp field, and the target page is either Dashboard or Discover with a data view that has a timestamp field, it would be good to include an extra option here as well as the custom time range option, where the user can specify an interval. The action here would be to open the target page showing
interval
either side of the time of the row the action is being run on, similar to the option we have for anomaly detection job custom URLs:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This item is part of the overall meta issue #150375 and can be done in a separate PR 👍