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

[Discover] Migrate angular context app controllers to react #100530

Merged
merged 53 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6e26af1
[Discover] migrate remaining context files from js to ts
dimaanj May 3, 2021
fae2360
[Discover] get rid of any types
dimaanj May 3, 2021
5dd8d10
Merge branch 'master' into js-ts-migration
kibanamachine May 3, 2021
5c9b64a
[Discover] replace constants with enums, update imports
dimaanj May 4, 2021
dbd8859
[Discover] use unknown instead of any, correct types
dimaanj May 4, 2021
cbff694
Merge branch 'master' of https://github.com/Dmitriynj/kibana into js-…
dimaanj May 5, 2021
902473a
[Discover] skip any type for tests
dimaanj May 5, 2021
89f10ba
[Discover] add euiDataGrid view
dimaanj May 6, 2021
d90750b
Merge branch 'master' into eui-data-grid-surr-docs
dimaanj May 7, 2021
35abe5b
[Discover] add support dataGrid columns, provide ability to do not ch…
dimaanj May 7, 2021
63493d5
[Discover] update context_legacy test and types
dimaanj May 7, 2021
0060ed0
Merge branch 'master' into eui-data-grid-surr-docs
kibanamachine May 10, 2021
a6e5d65
Merge branch 'master' into eui-data-grid-surr-docs
kibanamachine May 12, 2021
18ad181
[Discover] update unit tests, add context header
dimaanj May 12, 2021
233be90
[Discover] update unit and functional tests
dimaanj May 13, 2021
8780907
[Discover] remove docTable from context test which uses new data grid
dimaanj May 13, 2021
c49026b
Merge branch 'master' into eui-data-grid-surr-docs
kibanamachine May 19, 2021
3a70b98
Merge branch 'master' into eui-data-grid-surr-docs
kibanamachine May 20, 2021
6388555
[Discover] update EsHitRecord type, use it for context app. add no pa…
dimaanj May 21, 2021
720b613
[Discover] resolve type error in test
dimaanj May 21, 2021
6411879
Merge branch 'master' into eui-data-grid-surr-docs
kibanamachine May 24, 2021
b81a4e2
[Discover] move fetching methods
dimaanj May 25, 2021
7f69c92
[Discover] complete fetching part
dimaanj May 26, 2021
f6049d6
Merge branch 'master' into migrate-angular-ctrls
kibanamachine May 28, 2021
43a9966
[Discover] remove redundant controller
dimaanj May 31, 2021
640b75d
[Discover] split up context state and components
dimaanj May 31, 2021
c1dbb3e
Merge branch 'master' of https://github.com/elastic/kibana into migra…
dimaanj Jun 2, 2021
33ae8a5
[Discover] revert redundant css class
dimaanj Jun 2, 2021
ffdf1ec
[Discover] rename component
dimaanj Jun 3, 2021
ceec6be
[Discover] revert to upstream changes
dimaanj Jun 3, 2021
133c797
[Discover] return upstream changes
dimaanj Jun 3, 2021
a19bd7c
[Discover] refactoring, context test update
dimaanj Jun 4, 2021
b1a92e8
Merge branch 'master' of https://github.com/Dmitriynj/kibana into mig…
dimaanj Jun 4, 2021
1ff3ec4
[Discover] add tests for fetching methods, remove redundant files
dimaanj Jun 7, 2021
b68573c
Merge branch 'master' into migrate-angular-ctrls
kibanamachine Jun 7, 2021
358201e
[Discover] remove redundant angular utils, add filter test
dimaanj Jun 8, 2021
5440411
[Discover] refactor error feedback
dimaanj Jun 8, 2021
81f9968
[Discover] fix functional test
dimaanj Jun 8, 2021
9414231
Merge branch 'master' into migrate-angular-ctrls
kibanamachine Jun 8, 2021
1939a44
[Discover] provide defaultSize
dimaanj Jun 8, 2021
67b1bc1
[Discover] clean up code
dimaanj Jun 8, 2021
057e030
[Discover] fix eslint error
dimaanj Jun 8, 2021
2ac1b8a
[Discover] fiix context settings
dimaanj Jun 8, 2021
49a5ed6
[Discover] return tieBreaker field check
dimaanj Jun 8, 2021
5c0d8a2
Merge branch 'master' into migrate-angular-ctrls
kibanamachine Jun 9, 2021
105d04a
[Discover] optimize things
dimaanj Jun 9, 2021
2b53b32
[Discover] optimize rerenders
dimaanj Jun 10, 2021
ec7a51d
Update src/plugins/discover/public/application/components/context_app…
dimaanj Jun 10, 2021
19dc507
[Discover] resolve comments
dimaanj Jun 10, 2021
48b99fa
Merge branch 'master' into migrate-angular-ctrls
kibanamachine Jun 10, 2021
1421458
Merge branch 'master' into migrate-angular-ctrls
kibanamachine Jun 14, 2021
19e6840
[Discover] replace url instead of pushing to history. refactoring
dimaanj Jun 16, 2021
694e17c
Merge branch 'master' into migrate-angular-ctrls
kibanamachine Jun 16, 2021
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
3 changes: 2 additions & 1 deletion src/plugins/discover/public/__mocks__/index_patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export const indexPatternsMock = ({
return indexPatternMock;
}
},
} as unknown) as IndexPatternsService;
updateSavedObject: jest.fn(),
} as unknown) as jest.Mocked<IndexPatternsService>;
12 changes: 11 additions & 1 deletion src/plugins/discover/public/__mocks__/ui_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
*/

import { IUiSettingsClient } from 'kibana/public';
import { DEFAULT_COLUMNS_SETTING, DOC_TABLE_LEGACY, SAMPLE_SIZE_SETTING } from '../../common';
import {
CONTEXT_TIE_BREAKER_FIELDS_SETTING,
DEFAULT_COLUMNS_SETTING,
DOC_TABLE_LEGACY,
SAMPLE_SIZE_SETTING,
SEARCH_FIELDS_FROM_SOURCE,
} from '../../common';

export const uiSettingsMock = ({
get: (key: string) => {
Expand All @@ -17,6 +23,10 @@ export const uiSettingsMock = ({
return ['default_column'];
} else if (key === DOC_TABLE_LEGACY) {
return true;
} else if (key === CONTEXT_TIE_BREAKER_FIELDS_SETTING) {
return ['_doc'];
} else if (key === SEARCH_FIELDS_FROM_SOURCE) {
return false;
}
},
} as unknown) as IUiSettingsClient;
13 changes: 4 additions & 9 deletions src/plugins/discover/public/application/angular/context.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<context-app
anchor-id="contextAppRoute.anchorId"
columns="contextAppRoute.state.columns"
<context-app-legacy
index-pattern="contextAppRoute.indexPattern"
app-state="contextAppRoute.state"
state-container="contextAppRoute.stateContainer"
filters="contextAppRoute.filters"
predecessor-count="contextAppRoute.state.predecessorCount"
successor-count="contextAppRoute.state.successorCount"
sort="contextAppRoute.state.sort"></context-app>
index-pattern-id="contextAppRoute.indexPatternId"
anchor-id="contextAppRoute.anchorId">
</context-app-legacy>
65 changes: 6 additions & 59 deletions src/plugins/discover/public/application/angular/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
* Side Public License, v 1.
*/

import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { CONTEXT_DEFAULT_SIZE_SETTING } from '../../../common';
import { getAngularModule, getServices } from '../../kibana_services';
import './context_app';
import { getState } from './context_state';
import contextAppRouteTemplate from './context.html';
import { getRootBreadcrumbs } from '../helpers/breadcrumbs';

Expand All @@ -28,9 +24,14 @@ const k7Breadcrumbs = () => {

getAngularModule().config(($routeProvider) => {
$routeProvider.when('/context/:indexPatternId/:id*', {
controller: ContextAppRouteController,
controller: function ($routeParams, $scope, $route) {
this.indexPattern = $route.current.locals.indexPattern.ip;
this.anchorId = $routeParams.id;
this.indexPatternId = $route.current.params.indexPatternId;
},
k7Breadcrumbs,
controllerAs: 'contextAppRoute',
reloadOnSearch: false,
resolve: {
indexPattern: ($route, Promise) => {
const indexPattern = getServices().indexPatterns.get($route.current.params.indexPatternId);
Expand All @@ -40,57 +41,3 @@ getAngularModule().config(($routeProvider) => {
template: contextAppRouteTemplate,
});
});

function ContextAppRouteController($routeParams, $scope, $route) {
const filterManager = getServices().filterManager;
const indexPattern = $route.current.locals.indexPattern.ip;
const stateContainer = getState({
defaultStepSize: getServices().uiSettings.get(CONTEXT_DEFAULT_SIZE_SETTING),
timeFieldName: indexPattern.timeFieldName,
storeInSessionStorage: getServices().uiSettings.get('state:storeInSessionStorage'),
history: getServices().history(),
toasts: getServices().core.notifications.toasts,
uiSettings: getServices().core.uiSettings,
});
const {
startSync: startStateSync,
stopSync: stopStateSync,
appState,
getFilters,
setFilters,
setAppState,
flushToUrl,
} = stateContainer;
this.stateContainer = stateContainer;
this.state = { ...appState.getState() };
this.anchorId = $routeParams.id;
this.indexPattern = indexPattern;
filterManager.setFilters(_.cloneDeep(getFilters()));
startStateSync();

// take care of parameter changes in UI
$scope.$watchGroup(
[
'contextAppRoute.state.columns',
'contextAppRoute.state.predecessorCount',
'contextAppRoute.state.successorCount',
],
(newValues) => {
const [columns, predecessorCount, successorCount] = newValues;
if (Array.isArray(columns) && predecessorCount >= 0 && successorCount >= 0) {
setAppState({ columns, predecessorCount, successorCount });
flushToUrl(true);
}
}
);
// take care of parameter filter changes
const filterObservable = filterManager.getUpdates$().subscribe(() => {
setFilters(filterManager);
$route.reload();
});

$scope.$on('$destroy', () => {
stopStateSync();
filterObservable.unsubscribe();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import moment from 'moment';
import { get, last } from 'lodash';
import { createIndexPatternsStub, createContextSearchSourceStub } from './_stubs';
import { EsHitRecordList, fetchContextProvider } from './context';
import { EsHitRecordList, fetchContextProvider, SurrDocType } from './context';
import { setServices, SortDirection } from '../../../../kibana_services';
import { EsHitRecord } from './context';
import { Query } from '../../../../../../data/public';
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('context app', function () {
};

return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs(
'predecessors',
SurrDocType.PREDECESSORS,
indexPatternId,
anchor as EsHitRecord,
timeField,
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('context app', function () {
};

return fetchContextProvider(createIndexPatternsStub(), true).fetchSurroundingDocs(
'predecessors',
SurrDocType.PREDECESSORS,
indexPatternId,
anchor as EsHitRecord,
timeField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { get, last } from 'lodash';
import { createIndexPatternsStub, createContextSearchSourceStub } from './_stubs';
import { setServices, SortDirection } from '../../../../kibana_services';
import { Query } from '../../../../../../data/public';
import { EsHitRecordList, fetchContextProvider } from './context';
import { EsHitRecordList, fetchContextProvider, SurrDocType } from './context';
import { EsHitRecord } from './context';
import { DiscoverServices } from '../../../../build_services';

Expand Down Expand Up @@ -73,7 +73,7 @@ describe('context app', function () {
};

return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs(
'successors',
SurrDocType.SUCCESSORS,
indexPatternId,
anchor as EsHitRecord,
timeField,
Expand Down Expand Up @@ -268,7 +268,7 @@ describe('context app', function () {
};

return fetchContextProvider(createIndexPatternsStub(), true).fetchSurroundingDocs(
'successors',
SurrDocType.SUCCESSORS,
indexPatternId,
anchor as EsHitRecord,
timeField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import { getEsQuerySearchAfter } from './utils/get_es_query_search_after';
import { getEsQuerySort } from './utils/get_es_query_sort';
import { getServices } from '../../../../kibana_services';

export type SurrDocType = 'successors' | 'predecessors';
export enum SurrDocType {
SUCCESSORS = 'successors',
PREDECESSORS = 'predecessors',
}

export type EsHitRecord = Required<
Pick<
estypes.SearchResponse['hits']['hits'][number],
Expand Down Expand Up @@ -68,7 +72,7 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract, useNewFields
}
const indexPattern = await indexPatterns.get(indexPatternId);
const searchSource = await createSearchSource(indexPattern, filters);
const sortDirToApply = type === 'successors' ? sortDir : reverseSortDir(sortDir);
const sortDirToApply = type === SurrDocType.SUCCESSORS ? sortDir : reverseSortDir(sortDir);

const nanos = indexPattern.isTimeNanosBased() ? extractNanos(anchor.fields[timeField][0]) : '';
const timeValueMillis =
Expand Down Expand Up @@ -108,7 +112,9 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract, useNewFields
);

documents =
type === 'successors' ? [...documents, ...hits] : [...hits.slice().reverse(), ...documents];
type === SurrDocType.SUCCESSORS
? [...documents, ...hits]
: [...hits.slice().reverse(), ...documents];
}

return documents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { SortDirection } from '../../../../../../../data/public';
import { SurrDocType } from '../context';

export type IntervalValue = number | null;

Expand All @@ -31,12 +32,12 @@ export function* asPairs(iterable: Iterable<IntervalValue>): IterableIterator<In
export function generateIntervals(
offsets: number[],
startTime: number,
type: string,
type: SurrDocType,
sort: SortDirection
): IterableIterator<IntervalValue[]> {
const offsetSign =
(sort === SortDirection.asc && type === 'successors') ||
(sort === SortDirection.desc && type === 'predecessors')
(sort === SortDirection.asc && type === SurrDocType.SUCCESSORS) ||
(sort === SortDirection.desc && type === SurrDocType.PREDECESSORS)
? 1
: -1;
// ending with `null` opens the last interval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function getEsQuerySearchAfter(
): EsQuerySearchAfter {
if (documents.length) {
// already surrounding docs -> first or last record is used
const afterTimeRecIdx = type === 'successors' && documents.length ? documents.length - 1 : 0;
const afterTimeRecIdx =
type === SurrDocType.SUCCESSORS && documents.length ? documents.length - 1 : 0;
const afterTimeDoc = documents[afterTimeRecIdx];
let afterTimeValue = afterTimeDoc.sort[0] as string | number;
if (nanoSeconds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import React from 'react';
import { mountWithIntl } from '@kbn/test/jest';
import { ActionBar, ActionBarProps } from './action_bar';
import { findTestSubject } from '@elastic/eui/lib/test';
import { MAX_CONTEXT_SIZE, MIN_CONTEXT_SIZE } from '../../query_parameters/constants';
import {
MAX_CONTEXT_SIZE,
MIN_CONTEXT_SIZE,
} from '../../../../components/context_app/utils/constants';
import { SurrDocType } from '../../api/context';

describe('Test Discover Context ActionBar for successor | predecessor records', () => {
['successors', 'predecessors'].forEach((type) => {
[SurrDocType.SUCCESSORS, SurrDocType.PREDECESSORS].forEach((type) => {
const onChangeCount = jest.fn();
const props = {
defaultStepSize: 5,
Expand All @@ -31,7 +35,7 @@ describe('Test Discover Context ActionBar for successor | predecessor records',

test(`${type}: Load button click`, () => {
btn.simulate('click');
expect(onChangeCount).toHaveBeenCalledWith(25);
expect(onChangeCount).toHaveBeenCalledWith(type, 25);
});

test(`${type}: Load button click doesnt submit when MAX_CONTEXT_SIZE was reached`, () => {
Expand All @@ -44,13 +48,13 @@ describe('Test Discover Context ActionBar for successor | predecessor records',
test(`${type}: Count input change submits on blur`, () => {
input.simulate('change', { target: { valueAsNumber: 123 } });
input.simulate('blur');
expect(onChangeCount).toHaveBeenCalledWith(123);
expect(onChangeCount).toHaveBeenCalledWith(type, 123);
});

test(`${type}: Count input change submits on return`, () => {
input.simulate('change', { target: { valueAsNumber: 124 } });
input.simulate('submit');
expect(onChangeCount).toHaveBeenCalledWith(124);
expect(onChangeCount).toHaveBeenCalledWith(type, 124);
});

test(`${type}: Count input doesnt submits values higher than MAX_CONTEXT_SIZE `, () => {
Expand All @@ -68,7 +72,7 @@ describe('Test Discover Context ActionBar for successor | predecessor records',
});

test(`${type}: Warning about limitation of additional records`, () => {
if (type === 'predecessors') {
if (type === SurrDocType.PREDECESSORS) {
expect(findTestSubject(wrapper, 'predecessorsWarningMsg').text()).toBe(
'No documents newer than the anchor could be found.'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
} from '@elastic/eui';
import { ActionBarWarning } from './action_bar_warning';
import { SurrDocType } from '../../api/context';
import { MAX_CONTEXT_SIZE, MIN_CONTEXT_SIZE } from '../../query_parameters/constants';
import {
MAX_CONTEXT_SIZE,
MIN_CONTEXT_SIZE,
} from '../../../../components/context_app/utils/constants';

export interface ActionBarProps {
/**
Expand All @@ -45,9 +48,10 @@ export interface ActionBarProps {
isLoading: boolean;
/**
* is triggered when the input containing count is changed
* @param type
* @param count
*/
onChangeCount: (count: number) => void;
onChangeCount: (type: SurrDocType, count: number) => void;
/**
* can be `predecessors` or `successors`, usage in context:
* predecessors action bar + records (these are newer records)
Expand All @@ -67,13 +71,13 @@ export function ActionBar({
type,
}: ActionBarProps) {
const showWarning = !isDisabled && !isLoading && docCountAvailable < docCount;
const isSuccessor = type === 'successors';
const isSuccessor = type === SurrDocType.SUCCESSORS;
const [newDocCount, setNewDocCount] = useState(docCount);
const isValid = (value: number) => value >= MIN_CONTEXT_SIZE && value <= MAX_CONTEXT_SIZE;
const onSubmit = (ev: React.FormEvent<HTMLFormElement>) => {
ev.preventDefault();
if (newDocCount !== docCount && isValid(newDocCount)) {
onChangeCount(newDocCount);
onChangeCount(type, newDocCount);
}
};
useEffect(() => {
Expand All @@ -100,7 +104,7 @@ export function ActionBar({
const value = newDocCount + defaultStepSize;
if (isValid(value)) {
setNewDocCount(value);
onChangeCount(value);
onChangeCount(type, value);
}
}}
flush="right"
Expand Down Expand Up @@ -131,7 +135,7 @@ export function ActionBar({
}}
onBlur={() => {
if (newDocCount !== docCount && isValid(newDocCount)) {
onChangeCount(newDocCount);
onChangeCount(type, newDocCount);
}
}}
type="number"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EuiCallOut } from '@elastic/eui';
import { SurrDocType } from '../../api/context';

export function ActionBarWarning({ docCount, type }: { docCount: number; type: SurrDocType }) {
if (type === 'predecessors') {
if (type === SurrDocType.PREDECESSORS) {
return (
<EuiCallOut
color="warning"
Expand Down
Loading