Skip to content

Commit

Permalink
[App Search] Continue polling empty engines even when they have a sch…
Browse files Browse the repository at this point in the history
…ema (#112915)
  • Loading branch information
byronhulcher authored Sep 27, 2021
1 parent 16aa9bf commit 40dbe1a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { DOCUMENTS_TITLE } from './constants';
import { SearchExperience } from './search_experience';

export const Documents: React.FC = () => {
const { isMetaEngine, isEngineEmpty } = useValues(EngineLogic);
const { isMetaEngine, hasNoDocuments } = useValues(EngineLogic);
const { myRole } = useValues(AppLogic);

return (
Expand All @@ -32,7 +32,7 @@ export const Documents: React.FC = () => {
rightSideItems:
myRole.canManageEngineDocuments && !isMetaEngine ? [<DocumentCreationButton />] : [],
}}
isEmptyState={isEngineEmpty}
isEmptyState={hasNoDocuments}
emptyState={<EmptyState />}
>
{isMetaEngine && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe('EngineLogic', () => {
dataLoading: true,
engine: {},
engineName: '',
isEngineEmpty: true,
isEngineSchemaEmpty: true,
hasNoDocuments: true,
hasEmptySchema: true,
isMetaEngine: false,
isSampleEngine: false,
hasSchemaErrors: false,
Expand All @@ -64,8 +64,8 @@ describe('EngineLogic', () => {
const DEFAULT_VALUES_WITH_ENGINE = {
...DEFAULT_VALUES,
engine: mockEngineData,
isEngineEmpty: false,
isEngineSchemaEmpty: false,
hasNoDocuments: false,
hasEmptySchema: false,
};

beforeEach(() => {
Expand Down Expand Up @@ -255,8 +255,8 @@ describe('EngineLogic', () => {
expect(EngineLogic.actions.onPollStart).toHaveBeenCalled();
});

it('polls for engine data if the current engine is empty', () => {
mount({ engine: {} });
it('polls for engine data if the current engine has no documents', () => {
mount({ engine: { ...mockEngineData, document_count: 0 } });
jest.spyOn(EngineLogic.actions, 'initializeEngine');

EngineLogic.actions.pollEmptyEngine();
Expand All @@ -267,8 +267,8 @@ describe('EngineLogic', () => {
expect(EngineLogic.actions.initializeEngine).toHaveBeenCalledTimes(2);
});

it('cancels the poll if the current engine changed from empty to non-empty', () => {
mount({ engine: mockEngineData });
it('cancels the poll if the current engine has documents', () => {
mount({ engine: { ...mockEngineData, document_count: 1 } });
jest.spyOn(EngineLogic.actions, 'stopPolling');
jest.spyOn(EngineLogic.actions, 'initializeEngine');

Expand Down Expand Up @@ -312,15 +312,15 @@ describe('EngineLogic', () => {
});

describe('selectors', () => {
describe('isEngineEmpty', () => {
describe('hasNoDocuments', () => {
it('returns true if the engine contains no documents', () => {
const engine = { ...mockEngineData, document_count: 0 };
mount({ engine });

expect(EngineLogic.values).toEqual({
...DEFAULT_VALUES_WITH_ENGINE,
engine,
isEngineEmpty: true,
hasNoDocuments: true,
});
});

Expand All @@ -329,20 +329,20 @@ describe('EngineLogic', () => {

expect(EngineLogic.values).toEqual({
...DEFAULT_VALUES,
isEngineEmpty: true,
hasNoDocuments: true,
});
});
});

describe('isEngineSchemaEmpty', () => {
describe('hasEmptySchema', () => {
it('returns true if the engine schema contains no fields', () => {
const engine = { ...mockEngineData, schema: {} };
mount({ engine });

expect(EngineLogic.values).toEqual({
...DEFAULT_VALUES_WITH_ENGINE,
engine,
isEngineSchemaEmpty: true,
hasEmptySchema: true,
});
});

Expand All @@ -351,7 +351,7 @@ describe('EngineLogic', () => {

expect(EngineLogic.values).toEqual({
...DEFAULT_VALUES,
isEngineSchemaEmpty: true,
hasEmptySchema: true,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ interface EngineValues {
dataLoading: boolean;
engine: Partial<EngineDetails>;
engineName: string;
isEngineEmpty: boolean;
isEngineSchemaEmpty: boolean;
hasNoDocuments: boolean;
hasEmptySchema: boolean;
isMetaEngine: boolean;
isSampleEngine: boolean;
hasSchemaErrors: boolean;
Expand Down Expand Up @@ -94,8 +94,8 @@ export const EngineLogic = kea<MakeLogicType<EngineValues, EngineActions>>({
],
},
selectors: ({ selectors }) => ({
isEngineEmpty: [() => [selectors.engine], (engine) => !engine.document_count],
isEngineSchemaEmpty: [
hasNoDocuments: [() => [selectors.engine], (engine) => !engine.document_count],
hasEmptySchema: [
() => [selectors.engine],
(engine) => Object.keys(engine.schema || {}).length === 0,
],
Expand Down Expand Up @@ -149,7 +149,7 @@ export const EngineLogic = kea<MakeLogicType<EngineValues, EngineActions>>({
if (values.intervalId) return; // Ensure we only have one poll at a time

const id = window.setInterval(() => {
if (values.isEngineEmpty && values.isEngineSchemaEmpty) {
if (values.hasNoDocuments) {
actions.initializeEngine(); // Re-fetch engine data when engine is empty
} else {
actions.stopPolling();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('EngineOverview', () => {
const values = {
dataLoading: false,
myRole: {},
isEngineEmpty: true,
hasNoDocuments: true,
isMetaEngine: false,
};

Expand All @@ -40,7 +40,7 @@ describe('EngineOverview', () => {

describe('EngineOverviewMetrics', () => {
it('renders when the engine has documents', () => {
setMockValues({ ...values, isEngineEmpty: false });
setMockValues({ ...values, hasNoDocuments: false });
const wrapper = shallow(<EngineOverview />);
expect(wrapper.find(EngineOverviewMetrics)).toHaveLength(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export const EngineOverview: React.FC = () => {
const {
myRole: { canManageEngineDocuments, canViewEngineCredentials },
} = useValues(AppLogic);
const { isEngineEmpty, isMetaEngine } = useValues(EngineLogic);
const { hasNoDocuments, isMetaEngine } = useValues(EngineLogic);

const canAddDocuments = canManageEngineDocuments && canViewEngineCredentials;
const showEngineOverview = !isEngineEmpty || !canAddDocuments || isMetaEngine;
const showEngineOverview = !hasNoDocuments || !canAddDocuments || isMetaEngine;

return showEngineOverview ? <EngineOverviewMetrics /> : <EmptyEngineOverview />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { SearchUILogic } from './search_ui_logic';

export const SearchUI: React.FC = () => {
const { loadFieldData } = useActions(SearchUILogic);
const { isEngineSchemaEmpty } = useValues(EngineLogic);
const { hasEmptySchema } = useValues(EngineLogic);

useEffect(() => {
loadFieldData();
Expand All @@ -34,7 +34,7 @@ export const SearchUI: React.FC = () => {
<AppSearchPageTemplate
pageChrome={getEngineBreadcrumbs([SEARCH_UI_TITLE])}
pageHeader={{ pageTitle: SEARCH_UI_TITLE }}
isEmptyState={isEngineSchemaEmpty}
isEmptyState={hasEmptySchema}
emptyState={<EmptyState />}
>
<EuiFlexGroup alignItems="flexStart">
Expand Down

0 comments on commit 40dbe1a

Please sign in to comment.