Skip to content

Commit

Permalink
[Data Explorer]Fix display for index pattern without a default time f…
Browse files Browse the repository at this point in the history
…ield (#4821) (#4901)

Issue Resolve
#4820


(cherry picked from commit 83cd7f6)

Signed-off-by: ananzh <ananzh@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 15350bd commit a47e612
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface DiscoverChartProps {
hits: number;
resetQuery: () => void;
showResetButton?: boolean;
timeField?: string;
isTimeBased?: boolean;
services: DiscoverServices;
}

Expand All @@ -39,7 +39,7 @@ export const DiscoverChart = ({
data,
hits,
resetQuery,
timeField,
isTimeBased,
services,
showResetButton = false,
}: DiscoverChartProps) => {
Expand Down Expand Up @@ -75,7 +75,7 @@ export const DiscoverChart = ({
onResetQuery={resetQuery}
/>
</EuiFlexItem>
{timeField && (
{isTimeBased && (
<EuiFlexItem className="dscChart__TimechartHeader">
<TimechartHeader
bucketInterval={bucketInterval}
Expand All @@ -87,7 +87,7 @@ export const DiscoverChart = ({
/>
</EuiFlexItem>
)}
{timeField && chartData && (
{isTimeBased && chartData && (
<EuiFlexItem grow={false}>
<section
aria-label={i18n.translate('discover.histogramOfFoundDocumentsAriaLabel', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React, { useMemo, Fragment } from 'react';
import { useCallback } from 'react';
import { SurrDocType } from './context/api/context';
import { ActionBar } from './context/components/action_bar/action_bar';
import { CONTEXT_STEP_SETTING } from '../../../../common';
import { CONTEXT_STEP_SETTING, DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../common';
import { DiscoverViewServices } from '../../../build_services';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { LOADING_STATUS } from './context/utils/context_query_state';
Expand Down Expand Up @@ -77,6 +77,11 @@ export function ContextApp({
return [[indexPattern.timeFieldName!, SortDirection.desc]];
}, [indexPattern]);

const displayTimeColumn = useMemo(
() => !uiSettings.get(DOC_HIDE_TIME_COLUMN_SETTING, false) && indexPattern?.isTimeBased(),
[indexPattern, uiSettings]
);

return (
<Fragment>
<ActionBar
Expand All @@ -100,7 +105,7 @@ export function ContextApp({
onSort={() => {}}
sort={sort}
rows={rows}
displayTimeColumn={true}
displayTimeColumn={displayTimeColumn}
services={services}
isToolbarVisible={false}
isContextView={true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import './discover_chart_container.scss';
import React from 'react';
import React, { useMemo } from 'react';
import { DiscoverViewServices } from '../../../build_services';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { useDiscoverContext } from '../context';
Expand All @@ -16,27 +16,26 @@ export const DiscoverChartContainer = ({ hits, bucketInterval, chartData }: Sear
const { uiSettings, data } = services;
const { indexPattern, savedSearch } = useDiscoverContext();

const timeField = indexPattern?.timeFieldName;

if (!hits || !bucketInterval || !chartData) {
// TODO: handle better
return null;
}
const isTimeBased = useMemo(() => (indexPattern ? indexPattern.isTimeBased() : false), [
indexPattern,
]);

return (
<DiscoverChart
bucketInterval={bucketInterval}
chartData={chartData}
config={uiSettings}
data={data}
hits={hits}
timeField={timeField}
resetQuery={() => {
window.location.href = `#/view/${savedSearch?.id}`;
window.location.reload();
}}
services={services}
showResetButton={!!savedSearch && !!savedSearch.id}
/>
hits && (
<DiscoverChart
bucketInterval={bucketInterval}
chartData={chartData}
config={uiSettings}
data={data}
hits={hits}
resetQuery={() => {
window.location.href = `#/view/${savedSearch?.id}`;
window.location.reload();
}}
services={services}
showResetButton={!!savedSearch && !!savedSearch.id}
isTimeBased={isTimeBased}
/>
)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState, useEffect, useCallback } from 'react';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { History } from 'history';
import { DiscoverViewServices } from '../../../build_services';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
Expand All @@ -21,14 +21,20 @@ import { ResultStatus, SearchData } from '../utils/use_search';
import { IndexPatternField, opensearchFilters } from '../../../../../data/public';
import { DocViewFilterFn } from '../../doc_views/doc_views_types';
import { SortOrder } from '../../../saved_searches/types';
import { DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../common';

interface Props {
history: History;
}

export const DiscoverTable = ({ history }: Props) => {
const { services } = useOpenSearchDashboards<DiscoverViewServices>();
const { filterManager } = services.data.query;
const {
uiSettings,
data: {
query: { filterManager },
},
} = services;
const { data$, refetch$, indexPattern } = useDiscoverContext();
const [fetchState, setFetchState] = useState<SearchData>({
status: data$.getValue().status,
Expand Down Expand Up @@ -57,6 +63,10 @@ export const DiscoverTable = ({ history }: Props) => {
},
[filterManager, indexPattern]
);
const displayTimeColumn = useMemo(
() => !!(!uiSettings.get(DOC_HIDE_TIME_COLUMN_SETTING, false) && indexPattern?.isTimeBased()),
[indexPattern, uiSettings]
);

const { rows } = fetchState || {};

Expand Down Expand Up @@ -93,7 +103,7 @@ export const DiscoverTable = ({ history }: Props) => {
onSort={onSetSort}
sort={sort}
rows={rows}
displayTimeColumn={true}
displayTimeColumn={displayTimeColumn}
services={services}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useEffect, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import { AppMountParameters } from '../../../../../../core/public';
import { NEW_DISCOVER_APP, PLUGIN_ID } from '../../../../common';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
Expand All @@ -21,7 +21,7 @@ export interface TopNavProps {

export const TopNav = ({ opts }: TopNavProps) => {
const { services } = useOpenSearchDashboards<DiscoverViewServices>();
const { inspectorAdapters, savedSearch } = useDiscoverContext();
const { inspectorAdapters, savedSearch, indexPattern } = useDiscoverContext();
const [indexPatterns, setIndexPatterns] = useState<IndexPattern[] | undefined>(undefined);

const {
Expand Down Expand Up @@ -54,11 +54,11 @@ export const TopNav = ({ opts }: TopNavProps) => {
let isMounted = true;
const getDefaultIndexPattern = async () => {
await data.indexPatterns.ensureDefaultIndexPattern();
const indexPattern = await data.indexPatterns.getDefault();
const defaultIndexPattern = await data.indexPatterns.getDefault();

if (!isMounted) return;

setIndexPatterns(indexPattern ? [indexPattern] : undefined);
setIndexPatterns(defaultIndexPattern ? [defaultIndexPattern] : undefined);
};

getDefaultIndexPattern();
Expand All @@ -82,11 +82,16 @@ export const TopNav = ({ opts }: TopNavProps) => {
}
}, [chrome, getUrlForApp, savedSearch?.id, savedSearch?.title]);

const showDatePicker = useMemo(() => (indexPattern ? indexPattern.isTimeBased() : false), [
indexPattern,
]);

return (
<TopNavMenu
appName={PLUGIN_ID}
config={topNavLinks}
showSearchBar
showDatePicker={showDatePicker}
showSaveQuery
useDefaultBehaviors
setMenuMountPoint={opts.setHeaderActionMenu}
Expand Down

0 comments on commit a47e612

Please sign in to comment.