Skip to content

Commit

Permalink
Monitoring dashboards: Add text filter to variable dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoto committed Jul 7, 2021
1 parent 0af2b7a commit d0e3311
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';
import { connect } from 'react-redux';
import { match as RMatch } from 'react-router-dom';
import {
TimespanDropdown,
PollIntervalDropdown,
} from '@console/internal/components/monitoring/dashboards';
import { PollIntervalDropdown } from '@console/internal/components/monitoring/dashboards';
import TimespanDropdown from '@console/internal/components/monitoring/dashboards/timespan-dropdown';
import {
getURLSearchParams,
setQueryArguments,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as React from 'react';
import { shallow } from 'enzyme';
import { TFunction } from 'i18next';
import {
TimespanDropdown,
PollIntervalDropdown,
} from '@console/internal/components/monitoring/dashboards';
import { PollIntervalDropdown } from '@console/internal/components/monitoring/dashboards';
import TimespanDropdown from '@console/internal/components/monitoring/dashboards/timespan-dropdown';
import * as link from '@console/internal/components/utils';
import { Firehose } from '@console/internal/components/utils/firehose';
import { ResourceDropdown } from '@console/shared';
Expand Down
11 changes: 8 additions & 3 deletions frontend/public/components/monitoring/_monitoring.scss
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,14 @@ $screen-phone-landscape-min-width: 567px;
overflow-x: auto;
}

.monitoring-dashboards__variable-dropdown .pf-c-dropdown__menu {
max-height: 60vh;
overflow-y: auto;
.monitoring-dashboards__variable-dropdown {
.pf-c-dropdown__menu, .pf-c-select__menu {
max-height: 60vh;
overflow-y: auto;
}
.pf-m-search {
min-width: 140px;
}
}

.monitoring-dashboards__variables {
Expand Down
116 changes: 45 additions & 71 deletions frontend/public/components/monitoring/dashboards/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import * as _ from 'lodash-es';
import { Button, Dropdown, DropdownToggle, DropdownItem, Label } from '@patternfly/react-core';
import {
Button,
Dropdown,
DropdownItem,
DropdownToggle,
Label,
Select,
SelectOption,
} from '@patternfly/react-core';
import { AngleDownIcon, AngleRightIcon } from '@patternfly/react-icons';
import * as React from 'react';
import { Helmet } from 'react-helmet';
Expand Down Expand Up @@ -32,13 +40,12 @@ import { ErrorBoundaryFallback } from '../../error';
import { RootState } from '../../../redux';
import { getPrometheusURL, PrometheusEndpoint } from '../../graphs/helpers';
import { history, LoadingInline, useSafeFetch } from '../../utils';
import { formatPrometheusDuration, parsePrometheusDuration } from '../../utils/datetime';
import IntervalDropdown from '../poll-interval-dropdown';
import BarChart from './bar-chart';
import customTimeRangeModal from './custom-time-range-modal';
import Graph from './graph';
import SingleStat from './single-stat';
import Table from './table';
import TimespanDropdown from './timespan-dropdown';
import {
MONITORING_DASHBOARDS_DEFAULT_TIMESPAN,
MONITORING_DASHBOARDS_VARIABLE_ALL_OPTION_KEY,
Expand Down Expand Up @@ -105,7 +112,27 @@ const VariableDropdown: React.FC<VariableDropdownProps> = ({
}) => {
const { t } = useTranslation();

const [isOpen, toggleIsOpen, , setClosed] = useBoolean(false);
const [isOpen, , open, close] = useBoolean(false);
const [filterText, setFilterText] = React.useState<string>();

const onSelect = (e, value: string): void => {
onChange(value);
close();
setFilterText(undefined);
};

const onToggle = (isExpanded: boolean) => {
if (isExpanded) {
open();
} else {
close();
setFilterText(undefined);
}
};

const filteredItems = filterText
? _.pickBy(items, (v) => v.toLowerCase().includes(filterText))
: items;

return (
<div className="form-group monitoring-dashboards__dropdown-wrap">
Expand All @@ -125,25 +152,21 @@ const VariableDropdown: React.FC<VariableDropdownProps> = ({
}
/>
) : (
<Dropdown
dropdownItems={_.map(items, (name, key) => (
<DropdownItem component="button" key={key} onClick={() => onChange(key)}>
{name}
</DropdownItem>
))}
isOpen={isOpen}
onSelect={setClosed}
toggle={
<DropdownToggle
className="monitoring-dashboards__dropdown-button"
id={`${id}-dropdown`}
onToggle={toggleIsOpen}
>
{items[selectedKey]}
</DropdownToggle>
}
<Select
className="monitoring-dashboards__variable-dropdown"
/>
hasInlineFilter={_.size(items) > 1}
inlineFilterPlaceholderText={t('public~Filter options')}
onFilter={() => null}
isOpen={isOpen}
onSelect={onSelect}
onToggle={onToggle}
onTypeaheadInputChanged={(v) => setFilterText(v.toLowerCase())}
placeholderText={items[selectedKey]}
>
{_.map(filteredItems, (name, key) => (
<SelectOption key={key} value={name} />
))}
</Select>
)}
</div>
);
Expand Down Expand Up @@ -302,55 +325,6 @@ const DashboardDropdown = ({ items, onChange, selectedKey }) => {
);
};

const CUSTOM_TIME_RANGE_KEY = 'CUSTOM_TIME_RANGE_KEY';

export const TimespanDropdown = () => {
const { t } = useTranslation();

const timespan = useSelector(({ UI }: RootState) =>
UI.getIn(['monitoringDashboards', 'timespan']),
);
const endTime = useSelector(({ UI }: RootState) => UI.getIn(['monitoringDashboards', 'endTime']));

const dispatch = useDispatch();
const onChange = React.useCallback(
(v: string) => {
if (v === CUSTOM_TIME_RANGE_KEY) {
customTimeRangeModal({});
} else {
dispatch(monitoringDashboardsSetTimespan(parsePrometheusDuration(v)));
dispatch(monitoringDashboardsSetEndTime(null));
}
},
[dispatch],
);

const timespanOptions = {
[CUSTOM_TIME_RANGE_KEY]: t('public~Custom time range'),
'5m': t('public~Last {{count}} minute', { count: 5 }),
'15m': t('public~Last {{count}} minute', { count: 15 }),
'30m': t('public~Last {{count}} minute', { count: 30 }),
'1h': t('public~Last {{count}} hour', { count: 1 }),
'2h': t('public~Last {{count}} hour', { count: 2 }),
'6h': t('public~Last {{count}} hour', { count: 6 }),
'12h': t('public~Last {{count}} hour', { count: 12 }),
'1d': t('public~Last {{count}} day', { count: 1 }),
'2d': t('public~Last {{count}} day', { count: 2 }),
'1w': t('public~Last {{count}} week', { count: 1 }),
'2w': t('public~Last {{count}} week', { count: 2 }),
};

return (
<VariableDropdown
id="monitoring-time-range-dropdown"
items={timespanOptions}
label={t('public~Time range')}
onChange={onChange}
selectedKey={endTime ? CUSTOM_TIME_RANGE_KEY : formatPrometheusDuration(timespan)}
/>
);
};

export const PollIntervalDropdown = () => {
const { t } = useTranslation();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as _ from 'lodash';
import { Dropdown, DropdownToggle, DropdownItem } from '@patternfly/react-core';
import * as React from 'react';
import { useTranslation } from 'react-i18next';
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import { useDispatch, useSelector } from 'react-redux';

import {
monitoringDashboardsSetEndTime,
monitoringDashboardsSetTimespan,
} from '../../../actions/ui';
import { RootState } from '../../../redux';
import { formatPrometheusDuration, parsePrometheusDuration } from '../../utils/datetime';
import { useBoolean } from '../hooks/useBoolean';
import customTimeRangeModal from './custom-time-range-modal';

const CUSTOM_TIME_RANGE_KEY = 'CUSTOM_TIME_RANGE_KEY';

const TimespanDropdown = () => {
const { t } = useTranslation();

const [isOpen, toggleIsOpen, , setClosed] = useBoolean(false);

const timespan = useSelector(({ UI }: RootState) =>
UI.getIn(['monitoringDashboards', 'timespan']),
);
const endTime = useSelector(({ UI }: RootState) => UI.getIn(['monitoringDashboards', 'endTime']));

const dispatch = useDispatch();
const onChange = React.useCallback(
(v: string) => {
if (v === CUSTOM_TIME_RANGE_KEY) {
customTimeRangeModal({});
} else {
dispatch(monitoringDashboardsSetTimespan(parsePrometheusDuration(v)));
dispatch(monitoringDashboardsSetEndTime(null));
}
},
[dispatch],
);

const items = {
[CUSTOM_TIME_RANGE_KEY]: t('public~Custom time range'),
'5m': t('public~Last {{count}} minute', { count: 5 }),
'15m': t('public~Last {{count}} minute', { count: 15 }),
'30m': t('public~Last {{count}} minute', { count: 30 }),
'1h': t('public~Last {{count}} hour', { count: 1 }),
'2h': t('public~Last {{count}} hour', { count: 2 }),
'6h': t('public~Last {{count}} hour', { count: 6 }),
'12h': t('public~Last {{count}} hour', { count: 12 }),
'1d': t('public~Last {{count}} day', { count: 1 }),
'2d': t('public~Last {{count}} day', { count: 2 }),
'1w': t('public~Last {{count}} week', { count: 1 }),
'2w': t('public~Last {{count}} week', { count: 2 }),
};

return (
<div className="form-group monitoring-dashboards__dropdown-wrap">
<label
className="monitoring-dashboards__dropdown-title"
htmlFor="monitoring-time-range-dropdown"
>
{t('public~Time range')}
</label>
<Dropdown
className="monitoring-dashboards__variable-dropdown"
dropdownItems={_.map(items, (name, key) => (
<DropdownItem component="button" key={key} onClick={() => onChange(key)}>
{name}
</DropdownItem>
))}
isOpen={isOpen}
onSelect={setClosed}
toggle={
<DropdownToggle
className="monitoring-dashboards__dropdown-button"
id="monitoring-time-range-dropdown"
onToggle={toggleIsOpen}
>
{items[endTime ? CUSTOM_TIME_RANGE_KEY : formatPrometheusDuration(timespan)]}
</DropdownToggle>
}
/>
</div>
);
};

export default TimespanDropdown;
13 changes: 7 additions & 6 deletions frontend/public/locales/en/public.json
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,14 @@
"Custom time range": "Custom time range",
"To": "To",
"Error loading options": "Error loading options",
"Filter options": "Filter options",
"Dashboard": "Dashboard",
"Refresh interval": "Refresh interval",
"Inspect": "Inspect",
"Could not parse JSON data for dashboard \"{{dashboard}}\"": "Could not parse JSON data for dashboard \"{{dashboard}}\"",
"Metrics dashboards": "Metrics dashboards",
"Dashboards": "Dashboards",
"query results table": "query results table",
"Last {{count}} minute": "Last {{count}} minute",
"Last {{count}} minute_plural": "Last {{count}} minutes",
"Last {{count}} hour": "Last {{count}} hour",
Expand All @@ -883,12 +890,6 @@
"Last {{count}} week": "Last {{count}} week",
"Last {{count}} week_plural": "Last {{count}} weeks",
"Time range": "Time range",
"Refresh interval": "Refresh interval",
"Inspect": "Inspect",
"Could not parse JSON data for dashboard \"{{dashboard}}\"": "Could not parse JSON data for dashboard \"{{dashboard}}\"",
"Metrics dashboards": "Metrics dashboards",
"Dashboards": "Dashboards",
"query results table": "query results table",
"Add query": "Add query",
"Collapse all query tables": "Collapse all query tables",
"Expand all query tables": "Expand all query tables",
Expand Down

0 comments on commit d0e3311

Please sign in to comment.