Skip to content

Commit

Permalink
[BUG][Discover] Check if the timestamp is already included to remove …
Browse files Browse the repository at this point in the history
…duplicate col (opensearch-project#6983)

* check if the timestamp is already included to remove duplicate col

---------

Signed-off-by: Anan Zhuang <ananzh@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
ananzh and opensearch-changeset-bot[bot] authored Jun 17, 2024
1 parent 190dab0 commit 0db5848
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6983.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Discover] Check if the timestamp is already included to remove duplicate col ([#6983](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6983))
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { getLegacyDisplayedColumns } from './helper';
import { IndexPattern } from '../../../opensearch_dashboards_services';

const mockGetFieldByName = jest.fn();

describe('getLegacyDisplayedColumns', () => {
let indexPattern: IndexPattern;

beforeEach(() => {
indexPattern = ({
getFieldByName: mockGetFieldByName,
timeFieldName: 'timestamp',
} as unknown) as IndexPattern;
mockGetFieldByName.mockReset();
});

it('should return correct column properties without time column', () => {
mockGetFieldByName.mockReturnValue({ sortable: true });
const result = getLegacyDisplayedColumns(['column1'], indexPattern, true, false);
expect(result).toEqual([
{
name: 'column1',
displayName: 'column1',
isSortable: true,
isRemoveable: true,
colLeftIdx: -1,
colRightIdx: -1,
},
]);
});

it('should prepend time column if not hidden, indexPattern has timeFieldName, and columns do not include timeFieldName', () => {
mockGetFieldByName.mockReturnValue({ sortable: true });
const result = getLegacyDisplayedColumns(['column1'], indexPattern, false, false);
expect(result).toEqual([
{
name: 'timestamp',
displayName: 'Time',
isSortable: true,
isRemoveable: false,
colLeftIdx: -1,
colRightIdx: -1,
},
{
name: 'column1',
displayName: 'column1',
isSortable: true,
isRemoveable: true,
colLeftIdx: -1,
colRightIdx: -1,
},
]);
});

it('should not prepend time column if hideTimeField is true', () => {
mockGetFieldByName.mockReturnValue({ sortable: true });
const result = getLegacyDisplayedColumns(['column1'], indexPattern, true, false);
expect(result).toEqual([
{
name: 'column1',
displayName: 'column1',
isSortable: true,
isRemoveable: true,
colLeftIdx: -1,
colRightIdx: -1,
},
]);
});

it('should not prepend time column if timeFieldName is included in columns', () => {
mockGetFieldByName.mockReturnValue({ sortable: true });
const result = getLegacyDisplayedColumns(['column1', 'timestamp'], indexPattern, false, false);
expect(result).toEqual([
{
name: 'column1',
displayName: 'column1',
isSortable: true,
isRemoveable: true,
colLeftIdx: -1,
colRightIdx: 1,
},
{
name: 'timestamp',
displayName: 'timestamp',
isSortable: true,
isRemoveable: true,
colLeftIdx: 0,
colRightIdx: -1,
},
]);
});

it('should shorten dotted string in displayName if isShortDots is true', () => {
mockGetFieldByName.mockReturnValue({ sortable: true });
const result = getLegacyDisplayedColumns(['column.with.dots'], indexPattern, false, true);
expect(result).toEqual([
{
name: 'timestamp',
displayName: 'Time',
isSortable: true,
isRemoveable: false,
colLeftIdx: -1,
colRightIdx: -1,
},
{
name: 'column.with.dots',
displayName: 'c.w.dots',
isSortable: true,
isRemoveable: true,
colLeftIdx: -1,
colRightIdx: -1,
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ export function getLegacyDisplayedColumns(
colRightIdx: idx + 1 >= columns.length ? -1 : idx + 1,
};
});
return !hideTimeField && indexPattern.timeFieldName
? [getTimeColumn(indexPattern.timeFieldName), ...columnProps]

const shouldIncludeTimeField =
!hideTimeField &&
typeof indexPattern.timeFieldName === 'string' &&
!columns.includes(indexPattern.timeFieldName);

return shouldIncludeTimeField
? [getTimeColumn(indexPattern.timeFieldName as string), ...columnProps]
: columnProps;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/

import React, { useRef, useEffect } from 'react';
import { DocViewRenderFn, DocViewRenderProps } from '../../../doc_views/doc_views_types';
import { DocViewRenderFn, DocViewRenderProps } from '../../doc_views/doc_views_types';

interface Props {
render: DocViewRenderFn;
Expand Down

0 comments on commit 0db5848

Please sign in to comment.