Skip to content

Commit

Permalink
[Enterprise Search] Engine list page - fetch engines list with Backen…
Browse files Browse the repository at this point in the history
…d API call (#148495)

## Summary
### Engine list UI page

1. Replaced mocked values with Kibana API call in Fetch engines and added test cases
2. Replaced with the types from the `common ` directory suggestion based
on [PR
comment](#147399 (comment))
3. Removed documents column from UI


<img width="1675" alt="Screen Shot 2023-01-05 at 8 07 15 PM"
src="https://user-images.githubusercontent.com/55930906/210909095-3697fb62-758d-4dd1-89c1-2487f3b08db1.png">




### Checklist

Delete any items that are not applicable to this PR.


- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
saarikabhasi and kibanamachine authored Jan 10, 2023
1 parent f6adf64 commit 0da7113
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { mockHttpValues } from '../../../__mocks__/kea_logic';

import { nextTick } from '@kbn/test-jest-helpers';

import { fetchEngines } from './fetch_engines_api_logic';

describe('FetchEnginesAPILogic', () => {
const { http } = mockHttpValues;
beforeEach(() => {
jest.clearAllMocks();
});
describe('fetchEnginesAPILogic', () => {
it('request list engines api', async () => {
const promise = Promise.resolve({ result: 'result' });
http.get.mockReturnValue(promise);
const result = fetchEngines({
meta: { from: 0, size: 10, total: 0 },
});
await nextTick();
expect(http.get).toHaveBeenCalledWith('/internal/enterprise_search/engines', {
query: { from: 0, size: 10 },
});

await expect(result).resolves.toEqual({
result: 'result',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,29 @@
* 2.0.
*/

import { EnterpriseSearchEnginesResponse } from '../../../../../common/types/engines';

import { createApiLogic } from '../../../shared/api_logic/create_api_logic';
import { HttpLogic } from '../../../shared/http';

import { EngineListDetails, Meta } from '../../components/engines/types';
import { Meta } from '../../components/engines/types';

export interface EnginesListAPIResponse {
results: EngineListDetails[];
meta: Meta;
searchQuery?: string;
}
export interface EnginesListAPIArguments {
meta: Meta;
searchQuery?: string;
// searchQuery?: string;
}

const metaValue: Meta = {
from: 1,
size: 3,
total: 5,
};
// These are mocked values. To be changed as per the latest requirement when Backend API is ready
export const mockedEngines: EnginesListAPIResponse[] = [
{
meta: metaValue,
results: [
{
name: 'engine-name-1',
indices: ['index-18', 'index-23'],
last_updated: '21 March 2021',
document_count: 18,
},
{
name: 'engine-name-2',
indices: ['index-180', 'index-230', 'index-8', 'index-2'],
last_updated: '10 Jul 2018',
document_count: 10,
},

{
name: 'engine-name-3',
indices: ['index-2', 'index-3'],
last_updated: '21 December 2022',
document_count: 8,
},
],
},
];
export const fetchEngines = async () => {
// TODO replace with http call when backend is ready
return mockedEngines[0];
export const fetchEngines = async ({
meta,
}: EnginesListAPIArguments): Promise<EnterpriseSearchEnginesResponse> => {
const route = '/internal/enterprise_search/engines';
const query = {
from: meta.from,
size: meta.size,
};
return await HttpLogic.values.http.get<EnterpriseSearchEnginesResponse>(route, {
query,
});
};

export const FetchEnginesAPILogic = createApiLogic(['content', 'engines_api_logic'], fetchEngines);
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ import React from 'react';
import { CriteriaWithPagination, EuiBasicTable, EuiBasicTableColumn } from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import { FormattedNumber } from '@kbn/i18n-react';

import { EnterpriseSearchEngine } from '../../../../../../../common/types/engines';

import { DELETE_BUTTON_LABEL, MANAGE_BUTTON_LABEL } from '../../../../../shared/constants';

import { convertMetaToPagination, EngineListDetails, Meta } from '../../types';
import { convertMetaToPagination, Meta } from '../../types';

// add health status
interface EnginesListTableProps {
enginesList: EngineListDetails[];
enginesList: EnterpriseSearchEngine[];
loading: boolean;
meta: Meta;
isLoading?: boolean;
onChange: (criteria: CriteriaWithPagination<EngineListDetails>) => void;
onChange: (criteria: CriteriaWithPagination<EnterpriseSearchEngine>) => void;
}
export const EnginesListTable: React.FC<EnginesListTableProps> = ({
enginesList,
meta,
isLoading,
onChange,
}) => {
const columns: Array<EuiBasicTableColumn<EngineListDetails>> = [
const columns: Array<EuiBasicTableColumn<EnterpriseSearchEngine>> = [
{
field: 'name',
name: i18n.translate('xpack.enterpriseSearch.content.enginesList.table.column.name', {
Expand All @@ -44,14 +45,6 @@ export const EnginesListTable: React.FC<EnginesListTableProps> = ({
width: '100%',
},
},
{
field: 'document_count',
name: i18n.translate('xpack.enterpriseSearch.content.enginesList.table.column.documents', {
defaultMessage: 'Documents',
}),
dataType: 'number',
render: (number: number) => <FormattedNumber value={number} />,
},
{
field: 'last_updated',
name: i18n.translate('xpack.enterpriseSearch.content.enginesList.table.column.lastUpdated', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { LogicMounter, mockFlashMessageHelpers } from '../../../__mocks__/kea_lo
import { nextTick } from '@kbn/test-jest-helpers';

import { HttpError, Status } from '../../../../../common/types/api';
import { EnterpriseSearchEngine } from '../../../../../common/types/engines';

import { FetchEnginesAPILogic } from '../../api/engines/fetch_engines_api_logic';

import { EnginesListLogic } from './engines_list_logic';
import { DEFAULT_META, EngineListDetails } from './types';
import { DEFAULT_META } from './types';

const DEFAULT_VALUES = {
data: undefined,
Expand All @@ -26,25 +27,25 @@ const DEFAULT_VALUES = {

// sample engines list

const results: EngineListDetails[] = [
const results: EnterpriseSearchEngine[] = [
{
name: 'engine-name-1',
indices: ['index-18', 'index-23'],
last_updated: '21 March 2021',
document_count: 18,
// last_updated: '21 March 2021',
// document_count: 18,
},
{
name: 'engine-name-2',
indices: ['index-180', 'index-230', 'index-8', 'index-2'],
last_updated: '10 Jul 2018',
document_count: 10,
// last_updated: '10 Jul 2018',
// document_count: 10,
},

{
name: 'engine-name-3',
indices: ['index-2', 'index-3'],
last_updated: '21 December 2022',
document_count: 8,
// last_updated: '21 December 2022',
// document_count: 8,
},
];

Expand All @@ -65,13 +66,19 @@ describe('EnginesListLogic', () => {
describe('onPaginate', () => {
it('updates meta with newPageIndex', () => {
expect(EnginesListLogic.values).toEqual(DEFAULT_VALUES);
// This test does not work for now, test below code when Kibana GET API for pagination is ready
// EnginesListLogic.actions.onPaginate(2);
// test below code when pagination is ready
// EnginesListLogic.actions.onPaginate(1);
// expect(EnginesListLogic.values).toEqual({
// ...DEFAULT_VALUES,
// meta: {
// ...DEFAULT_META,
// from: 2,
// from: 1,
// },
// parameters: {
// meta: {
// ...DEFAULT_META,
// from: 1,
// },
// },
// });
});
Expand All @@ -89,19 +96,19 @@ describe('EnginesListLogic', () => {
EnginesListLogic.actions.apiSuccess({
meta: newPageMeta,
results,
searchQuery: 'k',
// searchQuery: 'k',
});
expect(EnginesListLogic.values).toEqual({
...DEFAULT_VALUES,
data: {
results,
meta: newPageMeta,
searchQuery: 'k',
// searchQuery: 'k',
},
meta: newPageMeta,
parameters: {
meta: newPageMeta,
searchQuery: 'k',
// searchQuery: 'k',
},
results,
status: Status.SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ import React from 'react';

import { shallow } from 'enzyme';

import { mockedEngines } from '../../api/engines/fetch_engines_api_logic';
import { Status } from '../../../../../common/types/api';

import { EnginesListTable } from './components/tables/engines_table';
import { EnginesList } from './engines_list';
import { DEFAULT_META } from './types';

const mockValues = {
enginesList: mockedEngines,
const DEFAULT_VALUES = {
data: undefined,
results: [],
meta: DEFAULT_META,
parameters: { meta: DEFAULT_META },
status: Status.IDLE,
};
const mockValues = { ...DEFAULT_VALUES };

const mockActions = {
fetchEngines: jest.fn(),
onPaginate: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const EnginesList: React.FC = () => {
meta,
searchQuery,
});
}, [meta, searchQuery]);
}, [meta.from, meta.size, searchQuery]);

return (
<EnterpriseSearchContentPageTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@

import { kea, MakeLogicType } from 'kea';

import {
EnterpriseSearchEngine,
EnterpriseSearchEnginesResponse,
} from '../../../../../common/types/engines';

import { Actions } from '../../../shared/api_logic/create_api_logic';

import {
EnginesListAPIArguments,
EnginesListAPIResponse,
FetchEnginesAPILogic,
} from '../../api/engines/fetch_engines_api_logic';

import { DEFAULT_META, EngineListDetails, Meta, updateMetaPageIndex } from './types';
import { DEFAULT_META, Meta, updateMetaPageIndex } from './types';

type EnginesListActions = Pick<
Actions<EnginesListAPIArguments, EnginesListAPIResponse>,
Actions<EnginesListAPIArguments, EnterpriseSearchEnginesResponse>,
'apiError' | 'apiSuccess' | 'makeRequest'
> & {
fetchEngines({ meta, searchQuery }: { meta: Meta; searchQuery?: string }): {
Expand All @@ -30,8 +34,8 @@ type EnginesListActions = Pick<
interface EngineListValues {
data: typeof FetchEnginesAPILogic.values.data;
meta: Meta;
results: EnterpriseSearchEngine[]; // stores engine list value from data
parameters: { meta: Meta; searchQuery?: string }; // Added this variable to store to the search Query value as well
results: EngineListDetails[]; // stores engine list value from data
status: typeof FetchEnginesAPILogic.values.status;
}

Expand All @@ -58,9 +62,8 @@ export const EnginesListLogic = kea<MakeLogicType<EngineListValues, EnginesListA
parameters: [
{ meta: DEFAULT_META },
{
apiSuccess: (_, { meta, searchQuery }) => ({
apiSuccess: (_, { meta }) => ({
meta,
searchQuery,
}),
onPaginate: (state, { pageNumber }) => ({
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ export interface Meta {
total: number;
}

export interface EngineListDetails {
name: string;
indices: string[];
last_updated: string;
document_count: number;
}
export const DEFAULT_META = {
from: 1,
size: 3,
from: 0,
size: 10,
total: 0,
};

Expand Down

0 comments on commit 0da7113

Please sign in to comment.