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

[BUG][Discover] Check if the timestamp is already included to remove duplicate col #6983

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Collaborator

Choose a reason for hiding this comment

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

How did this build if this wasn't imported correctly?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do see a type error when I debug the issue

Cannot find module '../../../doc_views/doc_views_types' or its corresponding type declarations.ts(2307)

I believe this is related to an old discussion #1660, which highlights that the standard build scripts in the project do not fail on or show TypeScript errors. This has led to a situation where TypeScript compilation errors (like the one I encountered) do not cause the build to fail, allowing many such errors to creep into the project unnoticed.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe we don't have the bandwidth to address this issue right now, but we should consider implementing a way to prevent new type errors from being added. This will help ensure the problem doesn't grow larger.

CC: @AMoo-Miki @ashwin-pc

Copy link
Member

Choose a reason for hiding this comment

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

Yeah typescript error right now rely on the IDE or us manually running tsc and validating the diff. I have a work in progress PR to help catch this in the CI but its not complete yet. #6586


interface Props {
render: DocViewRenderFn;
Expand Down
Loading