Skip to content

Commit

Permalink
[ML] Add commonly used ranges to date picker (#96501)
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 authored Apr 8, 2021
1 parent 767567f commit 4a54188
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,20 @@ jest.mock('../../../contexts/kibana', () => ({
useMlKibana: () => {
return {
services: {
uiSettings: { get: jest.fn() },
uiSettings: {
get: jest.fn().mockReturnValue([
{
from: 'now/d',
to: 'now/d',
display: 'Today',
},
{
from: 'now/w',
to: 'now/w',
display: 'This week',
},
]),
},
data: {
query: {
timefilter: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
* 2.0.
*/

import React, { FC, useCallback, useEffect, useState } from 'react';
import React, { FC, useCallback, useEffect, useMemo, useState } from 'react';
import { Subscription } from 'rxjs';
import { debounce } from 'lodash';

import { EuiSuperDatePicker, OnRefreshProps } from '@elastic/eui';
import { TimeHistoryContract, TimeRange } from 'src/plugins/data/public';
import { UI_SETTINGS } from '../../../../../../../../src/plugins/data/common';

import { mlTimefilterRefresh$ } from '../../../services/timefilter_refresh_service';
import { useUrlState } from '../../../util/url_state';
import { useMlKibana } from '../../../contexts/kibana';

interface TimePickerQuickRange {
from: string;
to: string;
display: string;
}

interface Duration {
start: string;
end: string;
Expand Down Expand Up @@ -71,6 +78,19 @@ export const DatePickerWrapper: FC = () => {
);

const dateFormat = config.get('dateFormat');
const timePickerQuickRanges = config.get<TimePickerQuickRange[]>(
UI_SETTINGS.TIMEPICKER_QUICK_RANGES
);

const commonlyUsedRanges = useMemo(
() =>
timePickerQuickRanges.map(({ from, to, display }) => ({
start: from,
end: to,
label: display,
})),
[timePickerQuickRanges]
);

useEffect(() => {
const subscriptions = new Subscription();
Expand Down Expand Up @@ -141,6 +161,7 @@ export const DatePickerWrapper: FC = () => {
onRefreshChange={updateInterval}
recentlyUsedRanges={recentlyUsedRanges}
dateFormat={dateFormat}
commonlyUsedRanges={commonlyUsedRanges}
/>
</div>
) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* 2.0.
*/

import { dataPluginMock } from '../../../../../../../../src/plugins/data/public/mocks';

export const kibanaContextMock = {
services: {
uiSettings: { get: jest.fn() },
chrome: { recentlyAccessed: { add: jest.fn() } },
application: { navigateToApp: jest.fn() },
http: {
Expand All @@ -17,6 +20,7 @@ export const kibanaContextMock = {
share: {
urlGenerators: { getUrlGenerator: jest.fn() },
},
data: dataPluginMock.createStartContract(),
},
};

Expand Down

0 comments on commit 4a54188

Please sign in to comment.