Skip to content

Commit

Permalink
Merge branch 'main' into fix/x-pack-test-types
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Sep 27, 2023
2 parents b0b8631 + e522f0a commit b0673b7
Show file tree
Hide file tree
Showing 53 changed files with 1,144 additions and 330 deletions.
2 changes: 1 addition & 1 deletion packages/shared-ux/markdown/mocks/storybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class MarkdownStorybookMock extends AbstractStorybookMock<
};
}

getServices() {
getServices(): MarkdownProps {
return { ...this.getProps() };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class EditPanelAction implements Action<ActionContext> {
const canEditEmbeddable = Boolean(
embeddable &&
embeddable.getOutput().editable &&
!embeddable.getOutput().inlineEditable &&
(embeddable.getOutput().editUrl ||
(embeddable.getOutput().editApp && embeddable.getOutput().editPath) ||
embeddable.getOutput().editableWithExplicitInput)
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/embeddable/public/lib/embeddables/i_embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface EmbeddableOutput {
title?: string;
description?: string;
editable?: boolean;
// set this to true if the embeddable allows inline editing
inlineEditable?: boolean;
// Whether the embeddable can be edited inline by re-requesting the explicit input from the user
editableWithExplicitInput?: boolean;
savedObjectId?: string;
Expand Down
45 changes: 45 additions & 0 deletions src/plugins/unified_histogram/public/__mocks__/lens_adapters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { UnifiedHistogramChartLoadEvent } from '../types';

export const lensAdaptersMock = {
tables: {
tables: {
default: {
columns: [
{
id: 'col-0-1',
meta: {
dimensionName: 'Slice size',
type: 'number',
},
name: 'Field 1',
},
{
id: 'col-0-2',
meta: {
dimensionName: 'Slice',
type: 'number',
},
name: 'Field 2',
},
],
rows: [
{
'col-0-1': 0,
'col-0-2': 0,
'col-0-3': 0,
'col-0-4': 0,
},
],
type: 'datatable',
},
},
},
} as unknown as UnifiedHistogramChartLoadEvent['adapters'];

This file was deleted.

18 changes: 12 additions & 6 deletions src/plugins/unified_histogram/public/chart/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React, { ReactElement, useMemo, useState, useEffect, useCallback, memo } from 'react';
import type { Observable } from 'rxjs';
import {
EuiButtonIcon,
EuiContextMenu,
Expand All @@ -17,8 +18,11 @@ import {
EuiProgress,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import type { EmbeddableComponentProps, Suggestion } from '@kbn/lens-plugin/public';
import type { Datatable } from '@kbn/expressions-plugin/common';
import type {
EmbeddableComponentProps,
Suggestion,
LensEmbeddableOutput,
} from '@kbn/lens-plugin/public';
import { DataView, DataViewField, DataViewType } from '@kbn/data-views-plugin/public';
import type { LensEmbeddableInput } from '@kbn/lens-plugin/public';
import type { AggregateQuery, Filter, Query, TimeRange } from '@kbn/es-query';
Expand Down Expand Up @@ -69,7 +73,8 @@ export interface ChartProps {
disableTriggers?: LensEmbeddableInput['disableTriggers'];
disabledActions?: LensEmbeddableInput['disabledActions'];
input$?: UnifiedHistogramInput$;
lensTablesAdapter?: Record<string, Datatable>;
lensAdapters?: UnifiedHistogramChartLoadEvent['adapters'];
lensEmbeddableOutput$?: Observable<LensEmbeddableOutput>;
isOnHistogramMode?: boolean;
isChartLoading?: boolean;
onResetChartHeight?: () => void;
Expand Down Expand Up @@ -107,7 +112,8 @@ export function Chart({
disableTriggers,
disabledActions,
input$: originalInput$,
lensTablesAdapter,
lensAdapters,
lensEmbeddableOutput$,
isOnHistogramMode,
isChartLoading,
onResetChartHeight,
Expand Down Expand Up @@ -463,8 +469,8 @@ export function Chart({
{...{
services,
lensAttributesContext,
dataView,
lensTablesAdapter,
lensAdapters,
lensEmbeddableOutput$,
currentSuggestion,
isFlyoutVisible,
setIsFlyoutVisible,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { act } from 'react-dom/test-utils';
import { setTimeout } from 'timers/promises';
import { dataViewWithTimefieldMock } from '../__mocks__/data_view_with_timefield';
import { unifiedHistogramServicesMock } from '../__mocks__/services';
import { lensTablesAdapterMock } from '../__mocks__/lens_table_adapter';
import { lensAdaptersMock } from '../__mocks__/lens_adapters';
import { ChartConfigPanel } from './chart_config_panel';
import type { LensAttributesContext } from './utils/get_lens_attributes';

Expand All @@ -34,7 +34,7 @@ describe('ChartConfigPanel', () => {
isFlyoutVisible: true,
setIsFlyoutVisible: jest.fn(),
isPlainRecord: true,
lensTablesAdapter: lensTablesAdapterMock,
lensAdapters: lensAdaptersMock,
query: {
esql: 'from test',
},
Expand Down
35 changes: 17 additions & 18 deletions src/plugins/unified_histogram/public/chart/chart_config_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
* Side Public License, v 1.
*/
import React, { useCallback, useEffect, useRef, useState } from 'react';
import type { Observable } from 'rxjs';
import type { AggregateQuery, Query } from '@kbn/es-query';
import { isEqual } from 'lodash';
import type { Suggestion } from '@kbn/lens-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { LensEmbeddableOutput, Suggestion } from '@kbn/lens-plugin/public';
import type { Datatable } from '@kbn/expressions-plugin/common';

import type { UnifiedHistogramServices } from '../types';
import type { UnifiedHistogramServices, UnifiedHistogramChartLoadEvent } from '../types';
import type { LensAttributesContext } from './utils/get_lens_attributes';

export function ChartConfigPanel({
services,
lensAttributesContext,
dataView,
lensTablesAdapter,
lensAdapters,
lensEmbeddableOutput$,
currentSuggestion,
isFlyoutVisible,
setIsFlyoutVisible,
Expand All @@ -29,10 +29,10 @@ export function ChartConfigPanel({
}: {
services: UnifiedHistogramServices;
lensAttributesContext: LensAttributesContext;
dataView: DataView;
isFlyoutVisible: boolean;
setIsFlyoutVisible: (flag: boolean) => void;
lensTablesAdapter?: Record<string, Datatable>;
lensAdapters?: UnifiedHistogramChartLoadEvent['adapters'];
lensEmbeddableOutput$?: Observable<LensEmbeddableOutput>;
currentSuggestion?: Suggestion;
isPlainRecord?: boolean;
query?: Query | AggregateQuery;
Expand All @@ -49,26 +49,25 @@ export function ChartConfigPanel({
...(datasourceState && { datasourceState }),
...(visualizationState && { visualizationState }),
} as Suggestion;
if (!isEqual(updatedSuggestion, currentSuggestion)) {
onSuggestionChange?.(updatedSuggestion);
}
onSuggestionChange?.(updatedSuggestion);
},
[currentSuggestion, onSuggestionChange]
);

useEffect(() => {
const tablesAdapters = lensAdapters?.tables?.tables;
const dataHasChanged =
Boolean(lensTablesAdapter) &&
!isEqual(previousAdapters.current, lensTablesAdapter) &&
Boolean(tablesAdapters) &&
!isEqual(previousAdapters.current, tablesAdapters) &&
query !== previousQuery?.current;
async function fetchLensConfigComponent() {
const Component = await services.lens.EditLensConfigPanelApi();
const panel = (
<Component
attributes={lensAttributesContext.attributes}
dataView={dataView}
adaptersTables={lensTablesAdapter}
updateAll={updateSuggestion}
updatePanelState={updateSuggestion}
lensAdapters={lensAdapters}
output$={lensEmbeddableOutput$}
closeFlyout={() => {
setIsFlyoutVisible(false);
}}
Expand All @@ -78,7 +77,7 @@ export function ChartConfigPanel({
);
setEditLensConfigPanel(panel);
previousSuggestion.current = currentSuggestion;
previousAdapters.current = lensTablesAdapter;
previousAdapters.current = tablesAdapters;
if (dataHasChanged) {
previousQuery.current = query;
}
Expand All @@ -92,14 +91,14 @@ export function ChartConfigPanel({
}, [
lensAttributesContext.attributes,
services.lens,
dataView,
updateSuggestion,
isPlainRecord,
currentSuggestion,
query,
isFlyoutVisible,
lensTablesAdapter,
setIsFlyoutVisible,
lensAdapters,
lensEmbeddableOutput$,
]);

return isPlainRecord ? editLensConfigPanel : null;
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/unified_histogram/public/chart/histogram.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { Histogram } from './histogram';
import React from 'react';
import { of } from 'rxjs';
import { unifiedHistogramServicesMock } from '../__mocks__/services';
import { dataViewWithTimefieldMock } from '../__mocks__/data_view_with_timefield';
import { createDefaultInspectorAdapters } from '@kbn/expressions-plugin/common';
Expand Down Expand Up @@ -166,24 +167,25 @@ describe('Histogram', () => {
jest
.spyOn(adapters.requests, 'getRequests')
.mockReturnValue([{ response: { json: { rawResponse } } } as any]);
onLoad(true, undefined);
const embeddableOutput$ = jest.fn().mockReturnValue(of('output$'));
onLoad(true, undefined, embeddableOutput$);
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.loading,
undefined
);
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters: {} });
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters: {}, embeddableOutput$ });
expect(buildBucketInterval.buildBucketInterval).not.toHaveBeenCalled();
expect(useTimeRange.useTimeRange).toHaveBeenLastCalledWith(
expect.objectContaining({ bucketInterval: undefined })
);
act(() => {
onLoad(false, adapters);
onLoad(false, adapters, embeddableOutput$);
});
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.complete,
100
);
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters });
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters, embeddableOutput$ });
expect(buildBucketInterval.buildBucketInterval).toHaveBeenCalled();
expect(useTimeRange.useTimeRange).toHaveBeenLastCalledWith(
expect.objectContaining({ bucketInterval: mockBucketInterval })
Expand Down
14 changes: 11 additions & 3 deletions src/plugins/unified_histogram/public/chart/histogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import type { DefaultInspectorAdapters, Datatable } from '@kbn/expressions-plugi
import type { IKibanaSearchResponse } from '@kbn/data-plugin/public';
import type { estypes } from '@elastic/elasticsearch';
import type { TimeRange } from '@kbn/es-query';
import type { EmbeddableComponentProps, LensEmbeddableInput } from '@kbn/lens-plugin/public';
import type {
EmbeddableComponentProps,
LensEmbeddableInput,
LensEmbeddableOutput,
} from '@kbn/lens-plugin/public';
import { RequestStatus } from '@kbn/inspector-plugin/public';
import type { Observable } from 'rxjs';
import {
Expand Down Expand Up @@ -118,7 +122,11 @@ export function Histogram({
}, [attributes, containerHeight, containerWidth]);

const onLoad = useStableCallback(
(isLoading: boolean, adapters: Partial<DefaultInspectorAdapters> | undefined) => {
(
isLoading: boolean,
adapters: Partial<DefaultInspectorAdapters> | undefined,
lensEmbeddableOutput$?: Observable<LensEmbeddableOutput>
) => {
const lensRequest = adapters?.requests?.getRequests()[0];
const requestFailed = lensRequest?.status === RequestStatus.ERROR;
const json = lensRequest?.response?.json as
Expand Down Expand Up @@ -155,7 +163,7 @@ export function Histogram({
setBucketInterval(newBucketInterval);
}

onChartLoad?.({ adapters: adapters ?? {} });
onChartLoad?.({ adapters: adapters ?? {}, embeddableOutput$: lensEmbeddableOutput$ });
}
);

Expand Down
Loading

0 comments on commit b0673b7

Please sign in to comment.