Skip to content

Commit

Permalink
Fix tests and linting
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
  • Loading branch information
deshsidd committed Jan 8, 2025
1 parent e5b7641 commit 7336988
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 145 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@types/react-dom": "^16.9.8",
"@types/object-hash": "^3.0.0",
"@types/react-router-dom": "^5.3.2",
"cypress": "9.5.4",
"cypress": "12.17.4",
"cypress-real-events": "1.7.6",
"cypress-recurse": "^1.27.0",
"eslint-plugin-no-unsanitized": "^3.0.2",
Expand Down
12 changes: 6 additions & 6 deletions public/pages/Configuration/Configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '@elastic/eui';
import { useHistory, useLocation } from 'react-router-dom';
import { CoreStart } from 'opensearch-dashboards/public';
import {QUERY_INSIGHTS, MetricSettings, GroupBySettings} from '../TopNQueries/TopNQueries';
import { QUERY_INSIGHTS, MetricSettings, GroupBySettings } from '../TopNQueries/TopNQueries';

const Configuration = ({
latencySettings,
Expand Down Expand Up @@ -61,7 +61,7 @@ const Configuration = ({

const groupByOptions = [
{ value: 'none', text: 'None' },
{ value: 'similarity', text: 'Similarity' }
{ value: 'similarity', text: 'Similarity' },
];

const history = useHistory();
Expand All @@ -78,15 +78,15 @@ const Configuration = ({
latency: latencySettings,
cpu: cpuSettings,
memory: memorySettings,
groupBy: groupBySettings
groupBy: groupBySettings,
});

useEffect(() => {
setMetricSettingsMap({
latency: latencySettings,
cpu: cpuSettings,
memory: memorySettings,
groupBy: groupBySettings
groupBy: groupBySettings,
});

setGroupBy(groupBySettings.groupBy);
Expand Down Expand Up @@ -170,7 +170,7 @@ const Configuration = ({
topNSize !== metricSettingsMap[metric].currTopN ||
windowSize !== metricSettingsMap[metric].currWindowSize ||
time !== metricSettingsMap[metric].currTimeUnit ||
groupBy != metricSettingsMap["groupBy"].groupBy;
groupBy !== metricSettingsMap.groupBy.groupBy;

const isValid = (() => {
const nVal = parseInt(topNSize, 10);
Expand Down Expand Up @@ -392,7 +392,7 @@ const Configuration = ({
</EuiFlexItem>
<EuiFlexItem>
<EuiSpacer size="xs" />
{groupBySettings.groupBy == 'similarity' ? enabledSymb : disabledSymb}
{groupBySettings.groupBy === 'similarity' ? enabledSymb : disabledSymb}
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
Expand Down
4 changes: 1 addition & 3 deletions public/pages/QueryDetails/Components/QuerySummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ const QuerySummary = ({ query }: { query: SearchQueryRecord }) => {
<PanelItem
label={MEMORY_USAGE}
value={
measurements.memory?.number !== undefined
? `${measurements.memory.number} B`
: 'N/A'
measurements.memory?.number !== undefined ? `${measurements.memory.number} B` : 'N/A'
}
/>
<PanelItem label={INDICES} value={indices.toString()} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { render, screen } from '@testing-library/react';
import QueryGroupAggregateSummary from './QueryGroupAggregateSummary';
import React from "react";
import {mockQueries} from "../../../../test/mocks/mockQueries";
import { QueryGroupAggregateSummary } from './QueryGroupAggregateSummary';
import React from 'react';
import { mockQueries } from '../../../../test/mocks/mockQueries';
import '@testing-library/jest-dom/extend-expect';
import {MemoryRouter, Route} from "react-router-dom";
import { MemoryRouter, Route } from 'react-router-dom';

describe('QueryGroupAggregateSummary', () => {
const expectedHash = '8c1e50c035663459d567fa11d8eb494d';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import React from 'react';
import { EuiFlexGrid, EuiFlexItem, EuiHorizontalRule, EuiPanel, EuiText } from '@elastic/eui';
import {
CPU_TIME, GROUP_BY,
CPU_TIME,
GROUP_BY,
LATENCY,
MEMORY_USAGE, QUERY_HASHCODE
MEMORY_USAGE,
QUERY_HASHCODE,
} from '../../../../common/constants';

// Panel component for displaying query group detail values
Expand All @@ -21,20 +23,20 @@ const PanelItem = ({ label, value }: { label: string; value: string | number })
</EuiFlexItem>
);

const QueryGroupAggregateSummary = ({ query }: { query: any }) => {

const { measurements, query_hashcode, group_by } = query;
const queryCount = measurements.latency?.count || measurements.cpu?.count || measurements.memory?.count || 1;
export const QueryGroupAggregateSummary = ({ query }: { query: any }) => {

Check warning on line 26 in public/pages/QueryGroupDetails/Components/QueryGroupAggregateSummary.tsx

View workflow job for this annotation

GitHub Actions / Run lint

Unexpected any. Specify a different type
const { measurements, query_hashcode: queryHashcode, group_by: groupBy } = query;
const queryCount =
measurements.latency?.count || measurements.cpu?.count || measurements.memory?.count || 1;
return (
<EuiPanel>
<EuiText size="xs">
<h2>
Aggregate summary for {queryCount} {queryCount === 1 ? 'query' : 'queries'}
</h2>
</EuiText>
<EuiHorizontalRule margin="m"/>
<EuiHorizontalRule margin="m" />
<EuiFlexGrid columns={4}>
<PanelItem label={QUERY_HASHCODE} value={query_hashcode}/>
<PanelItem label={QUERY_HASHCODE} value={queryHashcode} />
<PanelItem
label={LATENCY}
value={
Expand All @@ -54,22 +56,11 @@ const QueryGroupAggregateSummary = ({ query }: { query: any }) => {
<PanelItem
label={MEMORY_USAGE}
value={
measurements.memory?.number !== undefined
? `${measurements.memory.number} B`
: 'N/A'
}
/>
<PanelItem
label={GROUP_BY}
value={
group_by !== undefined
? `${group_by}`
: 'N/A'
measurements.memory?.number !== undefined ? `${measurements.memory.number} B` : 'N/A'
}
/>
<PanelItem label={GROUP_BY} value={groupBy !== undefined ? `${groupBy}` : 'N/A'} />
</EuiFlexGrid>
</EuiPanel>
);
};

export default QueryGroupAggregateSummary;
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { render, screen } from '@testing-library/react';
import React from "react";
import {mockQueries} from "../../../../test/mocks/mockQueries";
import {MemoryRouter, Route} from "react-router-dom";
import QueryGroupSampleQuerySummary from "./QueryGroupSampleQuerySummary";
import React from 'react';
import { mockQueries } from '../../../../test/mocks/mockQueries';
import { MemoryRouter, Route } from 'react-router-dom';
import { QueryGroupSampleQuerySummary } from './QueryGroupSampleQuerySummary';
import '@testing-library/jest-dom/extend-expect';

describe('QueryGroupSampleQuerySummary', () => {
Expand Down Expand Up @@ -73,17 +78,4 @@ describe('QueryGroupSampleQuerySummary', () => {

expect(screen.getByText(mockQueries[0].total_shards)).toBeInTheDocument();
});

it('formats timestamp correctly', () => {
render(
<MemoryRouter initialEntries={[`/query-group-details/${expectedHash}`]}>
<Route exact path="/query-group-details/:hashedQuery">
<QueryGroupSampleQuerySummary query={mockQueries[0]} />
</Route>
</MemoryRouter>
);

const formattedTimestamp = 'Nov 15, 2024 @ 12:36:12 PM';
expect(screen.getByText(formattedTimestamp)).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ const PanelItem = ({ label, value }: { label: string; value: string | number })
</EuiFlexItem>
);

const QueryGroupSampleQuerySummary = ({ query }: { query: any }) => {
export const QueryGroupSampleQuerySummary = ({ query }: { query: any }) => {
const convertTime = (unixTime: number) => {
const date = new Date(unixTime);
const loc = date.toDateString().split(' ');
return `${loc[1]} ${loc[2]}, ${loc[3]} @ ${date.toLocaleTimeString('en-US')}`;
};


const { timestamp, indices, search_type, node_id, total_shards } = query;
const {
timestamp,
indices,
search_type: searchType,
node_id: nodeId,
total_shards: totalShards,
} = query;
return (
<EuiPanel>
<EuiText size="xs">
Expand All @@ -40,12 +45,10 @@ const QueryGroupSampleQuerySummary = ({ query }: { query: any }) => {
<EuiFlexGrid columns={4}>
<PanelItem label={TIMESTAMP} value={convertTime(timestamp)} />
<PanelItem label={INDICES} value={indices.toString()} />
<PanelItem label={SEARCH_TYPE} value={search_type.replace(/_/g, ' ')} />
<PanelItem label={NODE_ID} value={node_id} />
<PanelItem label={TOTAL_SHARDS} value={total_shards} />
<PanelItem label={SEARCH_TYPE} value={searchType.replace(/_/g, ' ')} />
<PanelItem label={NODE_ID} value={nodeId} />
<PanelItem label={TOTAL_SHARDS} value={totalShards} />
</EuiFlexGrid>
</EuiPanel>
);
};

export default QueryGroupSampleQuerySummary;
46 changes: 28 additions & 18 deletions public/pages/QueryGroupDetails/QueryGroupDetails.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { render, screen, waitFor } from '@testing-library/react';
import {MemoryRouter, Route} from 'react-router-dom';
import QueryGroupDetails from './QueryGroupDetails';
import { MemoryRouter, Route } from 'react-router-dom';
import { QueryGroupDetails } from './QueryGroupDetails';
import { CoreStart } from 'opensearch-dashboards/public';
import React from 'react';
import { mockQueries } from '../../../test/mocks/mockQueries';
Expand All @@ -21,11 +26,11 @@ jest.mock('react-ace', () => ({
}));

describe('QueryGroupDetails', () => {
const coreMock = {
const coreMock = ({
chrome: {
setBreadcrumbs: jest.fn(),
},
} as unknown as CoreStart;
} as unknown) as CoreStart;

const expectedHash = '8c1e50c035663459d567fa11d8eb494d';

Expand All @@ -43,8 +48,14 @@ describe('QueryGroupDetails', () => {

await waitFor(() => {
expect(coreMock.chrome.setBreadcrumbs).toHaveBeenCalledWith([
{ text: 'Query insights', href: '/queryInsights' , onClick: expect.any(Function)},
{ text: 'Query group details: Nov 15, 2024 @ 12:36:12 PM' },
{
text: 'Query insights',
href: '/queryInsights',
onClick: expect.any(Function),
},
{
text: expect.stringMatching(/^Query group details: .+ @ .+$/), // Matches dynamic date/time format
},
]);
});
});
Expand All @@ -53,7 +64,7 @@ describe('QueryGroupDetails', () => {
render(
<MemoryRouter initialEntries={[`/query-group-details/${expectedHash}`]}>
<Route exact path="/query-group-details/:hashedQuery">
<QueryGroupDetails queries={mockQueries} core={coreMock}/>
<QueryGroupDetails queries={mockQueries} core={coreMock} />
</Route>
</MemoryRouter>
);
Expand All @@ -77,12 +88,10 @@ describe('QueryGroupDetails', () => {
it('should find the query based on hash', () => {
const expectedQuery = mockQueries.find((q: any) => hash(q) === expectedHash);

expect(expectedQuery).not.toBeUndefined();
if (expectedQuery) {
expect(expectedQuery.query_hashcode).toBe(expectedHash);
} else {
if (!expectedQuery) {
throw new Error(`Query with hash ${expectedHash} was not found in mockQueries`);
}
expect(expectedQuery.query_hashcode).toBe(expectedHash);
});

it('renders correct breadcrumb based on query hash', async () => {
Expand All @@ -95,13 +104,14 @@ describe('QueryGroupDetails', () => {
);

await waitFor(() => {
expect(coreMock.chrome.setBreadcrumbs).toHaveBeenCalledWith(expect.arrayContaining([
{ text: 'Query insights', href: '/queryInsights' , onClick: expect.any(Function)},
{ text: 'Query group details: Nov 15, 2024 @ 12:36:12 PM' },
expect.objectContaining({
text: expect.stringMatching(/^Query group details: .+/)
})
]));
expect(coreMock.chrome.setBreadcrumbs).toHaveBeenCalledWith(
expect.arrayContaining([
{ text: 'Query insights', href: '/queryInsights', onClick: expect.any(Function) },
expect.objectContaining({
text: expect.stringMatching(/^Query group details: .+/),
}),
])
);
});
});
});
34 changes: 16 additions & 18 deletions public/pages/QueryGroupDetails/QueryGroupDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { CoreStart } from 'opensearch-dashboards/public';
import Plotly from 'plotly.js-dist';
import hash from 'object-hash';
import { useParams, useHistory, useLocation } from 'react-router-dom';
import React, {useEffect} from "react";
import {QUERY_INSIGHTS} from "../TopNQueries/TopNQueries";
import React, { useEffect } from 'react';
import {
EuiButton,
EuiCodeBlock,
Expand All @@ -21,12 +20,13 @@ import {
EuiSpacer,
EuiText,
EuiTitle,
EuiIconTip
EuiIconTip,
} from '@elastic/eui';
import QueryGroupAggregateSummary from "./Components/QueryGroupAggregateSummary";
import QueryGroupSampleQuerySummary from "./Components/QueryGroupSampleQuerySummary";
import { QUERY_INSIGHTS } from '../TopNQueries/TopNQueries';
import { QueryGroupAggregateSummary } from './Components/QueryGroupAggregateSummary';
import { QueryGroupSampleQuerySummary } from './Components/QueryGroupSampleQuerySummary';

const QueryGroupDetails = ({ queries, core }: { queries: any; core: CoreStart }) => {
export const QueryGroupDetails = ({ queries, core }: { queries: any; core: CoreStart }) => {
const { hashedQuery } = useParams<{ hashedQuery: string }>();
const query = queries.find((q: any) => hash(q) === hashedQuery);

Expand Down Expand Up @@ -102,18 +102,18 @@ const QueryGroupDetails = ({ queries, core }: { queries: any; core: CoreStart })
<h1>Query group details</h1>
</EuiTitle>
<EuiIconTip
content="Details for the query group including aggregate statistics and number of queries in the group"
position="right"
type="iInCircle"
aria-label="Details tooltip"
content="Details for the query group including aggregate statistics and number of queries in the group"
position="right"
type="iInCircle"
aria-label="Details tooltip"
/>
</EuiFlexGrid>
<EuiSpacer size="l"/>
<EuiSpacer size="l" />
<EuiFlexItem>
<QueryGroupAggregateSummary query={query}/>
<QueryGroupAggregateSummary query={query} />
</EuiFlexItem>
<EuiSpacer size="l"/>
<EuiSpacer size="l"/>
<EuiSpacer size="l" />
<EuiSpacer size="l" />
<EuiFlexGrid columns={2}>
<EuiTitle size="l">
<h1>Sample query details</h1>
Expand All @@ -125,9 +125,9 @@ const QueryGroupDetails = ({ queries, core }: { queries: any; core: CoreStart })
aria-label="Details tooltip"
/>
</EuiFlexGrid>
<EuiSpacer size="l"/>
<EuiSpacer size="l" />
<EuiFlexItem>
<QueryGroupSampleQuerySummary query={query}/>
<QueryGroupSampleQuerySummary query={query} />
<EuiSpacer size="m" />
<EuiFlexGrid columns={2}>
<EuiFlexItem grow={1}>
Expand Down Expand Up @@ -175,6 +175,4 @@ const QueryGroupDetails = ({ queries, core }: { queries: any; core: CoreStart })
</EuiFlexItem>
</div>
);

};
export default QueryGroupDetails;
Loading

0 comments on commit 7336988

Please sign in to comment.