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

[Infra UI] Add date picker to asset details #164450

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Popover } from '../../common/popover';
import { useDateRangeProviderContext } from '../../../hooks/use_date_range';

type DataViewOrigin = 'logs' | 'metrics';
type BrushEndArgs = Parameters<NonNullable<LensEmbeddableInput['onBrushEnd']>>[0];

interface Props {
nodeName: string;
Expand Down Expand Up @@ -54,10 +55,7 @@ export const MetricsGrid = React.memo(
);

const handleBrushEnd = useCallback(
({
range,
preventDefault,
}: Parameters<NonNullable<LensEmbeddableInput['onBrushEnd']>>[0]) => {
({ range, preventDefault }: BrushEndArgs) => {
setDateRange({
from: new Date(range[0]).toISOString(),
to: new Date(range[1]).toISOString(),
Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugins/infra/public/components/asset_details/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export enum FlyoutTabIds {

export type TabIds = `${FlyoutTabIds}`;

export interface TabState {
dateRange?: TimeRange;
export interface OverridableTabState {
metadata?: {
query?: string;
showActionsColumn?: boolean;
Expand All @@ -46,6 +45,10 @@ export interface TabState {
};
}

export interface TabState extends OverridableTabState {
activeTabId?: TabIds;
dateRange: TimeRange;
}
export interface FlyoutProps {
closeFlyout: () => void;
mode: 'flyout';
Expand All @@ -70,7 +73,7 @@ export interface AssetDetailsProps {
dateRange: TimeRange;
tabs: Tab[];
activeTabId?: TabIds;
overrides?: TabState;
overrides?: OverridableTabState;
renderMode: RenderMode;
onTabsStateChange?: TabsStateChangeFn;
links?: LinkOptions[];
Expand All @@ -79,7 +82,9 @@ export interface AssetDetailsProps {
metricAlias: string;
}

export type TabsStateChangeFn = (state: TabState & { activeTabId?: TabIds }) => void;
export type TabsStateChangeFn = (
state: OverridableTabState & { activeTabId?: TabIds; dateRange: TimeRange }
) => void;

export interface ContentTemplateProps {
header: Pick<AssetDetailsProps, 'tabs' | 'links'>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const FlyoutWrapper = ({ node: { name }, closeFlyout }: Props) => {
<AssetDetails
asset={{ id: name, name }}
assetType="host"
dateRange={parsedDateRange}
dateRange={hostFlyoutState?.dateRange ?? parsedDateRange}
activeTabId={hostFlyoutState?.tabId}
overrides={{
metadata: {
Expand All @@ -44,6 +44,7 @@ export const FlyoutWrapper = ({ node: { name }, closeFlyout }: Props) => {
}}
onTabsStateChange={(state) =>
setHostFlyoutState({
dateRange: state.dateRange,
metadataSearch: state.metadata?.query,
processSearch: state.processes?.query,
logsSearch: state.logs?.query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const HostFlyoutStateRT = rt.intersection([
tabId: FlyoutTabIdRT,
}),
rt.partial({
dateRange: rt.type({
from: rt.string,
to: rt.string,
}),
processSearch: rt.string,
metadataSearch: rt.string,
logsSearch: rt.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const sortTableData =
export const useHostsTable = () => {
const [selectedItems, setSelectedItems] = useState<HostNodeRow[]>([]);
const { hostNodes } = useHostsViewContext();
const { searchCriteria } = useUnifiedSearchContext();
const { searchCriteria, parsedDateRange } = useUnifiedSearchContext();
const [{ pagination, sorting }, setProperties] = useHostsTableUrlState();
const {
services: {
Expand Down Expand Up @@ -244,7 +244,7 @@ export const useHostsTable = () => {
render: (title: HostNodeRow['title']) => (
<EntryTitle
title={title}
time={searchCriteria.dateRange}
time={parsedDateRange}
onClick={() => reportHostEntryClick(title)}
/>
),
Expand Down Expand Up @@ -351,7 +351,7 @@ export const useHostsTable = () => {
width: '120px',
},
],
[hostFlyoutState?.itemId, reportHostEntryClick, searchCriteria.dateRange, setHostFlyoutState]
[hostFlyoutState?.itemId, parsedDateRange, reportHostEntryClick, setHostFlyoutState]
);

const selection: EuiTableSelectionType<HostNodeRow> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type { TimeRange } from '@kbn/es-query';
import React, { useMemo } from 'react';
import React, { useCallback, useMemo } from 'react';
import { useLocation, useRouteMatch } from 'react-router-dom';
import { i18n } from '@kbn/i18n';
import { NoRemoteCluster } from '../../../components/empty_states';
Expand Down Expand Up @@ -70,26 +70,35 @@ export const AssetDetailPage = () => {
return queryParams.get('assetName') ?? undefined;
}, [search]);

const { parsedTimeRange, setTimeRange } = useMetricsTimeContext();
const { timeRange, setTimeRange } = useMetricsTimeContext();

const dateRange: TimeRange = useMemo(
() => ({
from: new Date(parsedTimeRange.from).toISOString(),
to: new Date(parsedTimeRange.to).toISOString(),
from:
typeof timeRange.from === 'number'
? new Date(timeRange.from).toISOString()
: timeRange.from,
to: typeof timeRange.to === 'number' ? new Date(timeRange.to).toISOString() : timeRange.to,
}),
[parsedTimeRange.from, parsedTimeRange.to]
[timeRange.from, timeRange.to]
);

// Retrocompatibility
const handleTabStateChange = ({ dateRange: newDateRange }: TabState) => {
if (newDateRange) {
setTimeRange({
from: newDateRange.from,
to: newDateRange.to,
interval: parsedTimeRange.interval,
});
}
};
const handleTabStateChange = useCallback(
({ dateRange: newDateRange }: TabState) => {
if (newDateRange) {
setTimeRange(
{
from: newDateRange.from,
to: newDateRange.to,
interval: timeRange.interval,
},
false
);
}
},
[setTimeRange, timeRange.interval]
);

const { metricIndicesExist, remoteClustersExist } = source?.status ?? {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ export const useMetricsTime = () => {
parseRange(urlState.time || DEFAULT_TIMERANGE)
);

const updateTimeRange = useCallback((range: MetricsTimeInput) => {
const updateTimeRange = useCallback((range: MetricsTimeInput, parseDate = true) => {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: maybe we can always pass a parsed date to this function so here we can simplify the logic, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't want to change this function so much. We won't use it after this #164147 is done

setTimeRange(range);
setParsedTimeRange(parseRange(range));
if (parseDate) {
setParsedTimeRange(parseRange(range));
}
}, []);

return {
Expand Down