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

Issue Resolve
opensearch-project#4820

Signed-off-by: ananzh <ananzh@amazon.com>
  • Loading branch information
ananzh committed Aug 30, 2023
1 parent 2738131 commit 711509c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface DiscoverChartProps {
hits: number;
resetQuery: () => void;
showResetButton?: boolean;
timeField?: string;
isTimeBased?: boolean;
services: DiscoverServices;
}

Expand All @@ -37,7 +37,7 @@ export const DiscoverChart = ({
data,
hits,
resetQuery,
timeField,
isTimeBased,
services,
showResetButton = false,
}: DiscoverChartProps) => {
Expand Down Expand Up @@ -73,7 +73,7 @@ export const DiscoverChart = ({
onResetQuery={resetQuery}
/>
</EuiFlexItem>
{timeField && (
{isTimeBased && (
<EuiFlexItem className="dscChart__TimechartHeader">
<TimechartHeader
bucketInterval={bucketInterval}
Expand All @@ -85,7 +85,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
@@ -0,0 +1,56 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export enum OSD_FIELD_TYPES {
_SOURCE = '_source',
ATTACHMENT = 'attachment',
BOOLEAN = 'boolean',
DATE = 'date',
GEO_POINT = 'geo_point',
GEO_SHAPE = 'geo_shape',
IP = 'ip',
MURMUR3 = 'murmur3',
NUMBER = 'number',
STRING = 'string',
UNKNOWN = 'unknown',
CONFLICT = 'conflict',
OBJECT = 'object',
NESTED = 'nested',
HISTOGRAM = 'histogram',
}

export const osdJSON = 'osd-json';
export function getSchemaByKbnType(kbnType: string | undefined) {
// Default DataGrid schemas: boolean, numeric, datetime, json, currency, string
switch (kbnType) {
case OSD_FIELD_TYPES.IP:
case OSD_FIELD_TYPES.GEO_SHAPE:
case OSD_FIELD_TYPES.NUMBER:
return 'numeric';
case OSD_FIELD_TYPES.BOOLEAN:
return 'boolean';
case OSD_FIELD_TYPES.STRING:
return 'string';
case OSD_FIELD_TYPES.DATE:
return 'datetime';
default:
return osdJSON;
}
}

export function getSchemaDetectors() {
return [
{
type: osdJSON,
detector() {
return 0; // this schema is always explicitly defined
},
sortTextAsc: '',
sortTextDesc: '',
icon: '',
color: '',
},
];
}
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 || true}
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 @@ -20,7 +20,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 @@ -52,11 +52,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 @@ -66,12 +66,16 @@ export const TopNav = ({ opts }: TopNavProps) => {
};
}, [data.indexPatterns]);

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

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

0 comments on commit 711509c

Please sign in to comment.