Skip to content

Commit

Permalink
🏷️ Fix remaining types
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Sep 24, 2024
1 parent 5452dd4 commit f6e6df0
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 46 deletions.
7 changes: 3 additions & 4 deletions src/plugins/unified_histogram/public/chart/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import React, { memo, ReactElement, useCallback, useEffect, useMemo, useState } from 'react';
import type { Observable } from 'rxjs';
import { Subject } from 'rxjs';
import useObservable from 'react-use/lib/useObservable';
import { IconButtonGroup, type IconButtonGroupProps } from '@kbn/shared-ux-button-toolbar';
Expand Down Expand Up @@ -69,7 +68,7 @@ export interface ChartProps {
disabledActions?: LensEmbeddableInput['disabledActions'];
input$?: UnifiedHistogramInput$;
lensAdapters?: UnifiedHistogramChartLoadEvent['adapters'];
lensEmbeddableOutput$?: Observable<LensEmbeddableOutput>;
dataLoading$?: LensEmbeddableOutput['dataLoading'];
isChartLoading?: boolean;
onChartHiddenChange?: (chartHidden: boolean) => void;
onTimeIntervalChange?: (timeInterval: string) => void;
Expand Down Expand Up @@ -103,7 +102,7 @@ export function Chart({
disabledActions,
input$: originalInput$,
lensAdapters,
lensEmbeddableOutput$,
dataLoading$,
isChartLoading,
onChartHiddenChange,
onTimeIntervalChange,
Expand Down Expand Up @@ -392,7 +391,7 @@ export function Chart({
services={services}
visContext={visContext}
lensAdapters={lensAdapters}
lensEmbeddableOutput$={lensEmbeddableOutput$}
dataLoading$={dataLoading$}
isFlyoutVisible
setIsFlyoutVisible={setIsFlyoutVisible}
isPlainRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import React, { ComponentProps, useCallback, useEffect, useRef, useState } from 'react';
import type { Observable } from 'rxjs';
import type { AggregateQuery, Query } from '@kbn/es-query';
import { isEqual, isObject } from 'lodash';
import type { LensEmbeddableOutput, Suggestion } from '@kbn/lens-plugin/public';
Expand All @@ -28,7 +27,7 @@ export function ChartConfigPanel({
services,
visContext,
lensAdapters,
lensEmbeddableOutput$,
dataLoading$,
currentSuggestionContext,
isFlyoutVisible,
setIsFlyoutVisible,
Expand All @@ -41,7 +40,7 @@ export function ChartConfigPanel({
isFlyoutVisible: boolean;
setIsFlyoutVisible: (flag: boolean) => void;
lensAdapters?: UnifiedHistogramChartLoadEvent['adapters'];
lensEmbeddableOutput$?: Observable<LensEmbeddableOutput>;
dataLoading$?: LensEmbeddableOutput['dataLoading'];
currentSuggestionContext: UnifiedHistogramSuggestionContext;
isPlainRecord?: boolean;
query?: Query | AggregateQuery;
Expand Down Expand Up @@ -107,7 +106,7 @@ export function ChartConfigPanel({
updateSuggestion={updateSuggestion}
updatePanelState={updatePanelState}
lensAdapters={lensAdapters}
output$={lensEmbeddableOutput$}
dataLoading$={dataLoading$}
displayFlyoutHeader
closeFlyout={() => {
setIsFlyoutVisible(false);
Expand Down Expand Up @@ -140,7 +139,7 @@ export function ChartConfigPanel({
isFlyoutVisible,
setIsFlyoutVisible,
lensAdapters,
lensEmbeddableOutput$,
dataLoading$,
currentSuggestionType,
]);

Expand Down
20 changes: 10 additions & 10 deletions src/plugins/unified_histogram/public/chart/histogram.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { Histogram } from './histogram';
import React from 'react';
import { of, Subject } from 'rxjs';
import { BehaviorSubject, Subject } from 'rxjs';
import { unifiedHistogramServicesMock } from '../__mocks__/services';
import { getLensVisMock } from '../__mocks__/lens_vis';
import { dataViewWithTimefieldMock } from '../__mocks__/data_view_with_timefield';
Expand Down Expand Up @@ -172,25 +172,25 @@ describe('Histogram', () => {
jest
.spyOn(adapters.requests, 'getRequests')
.mockReturnValue([{ response: { json: { rawResponse } } } as any]);
const embeddableOutput$ = jest.fn().mockReturnValue(of('output$'));
onLoad(true, undefined, embeddableOutput$);
const dataLoading$ = new BehaviorSubject<boolean | undefined>(false);
onLoad?.(true, undefined, dataLoading$);
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.loading,
undefined
);
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters: {}, embeddableOutput$ });
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters: {}, dataLoading$ });
expect(buildBucketInterval.buildBucketInterval).not.toHaveBeenCalled();
expect(useTimeRange.useTimeRange).toHaveBeenLastCalledWith(
expect.objectContaining({ bucketInterval: undefined })
);
act(() => {
onLoad(false, adapters, embeddableOutput$);
onLoad?.(false, adapters, dataLoading$);
});
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.complete,
100
);
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters, embeddableOutput$ });
expect(props.onChartLoad).toHaveBeenLastCalledWith({ adapters, dataLoading$ });
expect(buildBucketInterval.buildBucketInterval).toHaveBeenCalled();
expect(useTimeRange.useTimeRange).toHaveBeenLastCalledWith(
expect.objectContaining({ bucketInterval: mockBucketInterval })
Expand All @@ -205,7 +205,7 @@ describe('Histogram', () => {
jest
.spyOn(adapters.requests, 'getRequests')
.mockReturnValue([{ status: RequestStatus.ERROR } as any]);
onLoad(false, adapters);
onLoad?.(false, adapters);
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.error,
undefined
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('Histogram', () => {
.spyOn(adapters.requests, 'getRequests')
.mockReturnValue([{ response: { json: { rawResponse } } } as any]);
act(() => {
onLoad(false, adapters);
onLoad?.(false, adapters);
});
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.complete,
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('Histogram', () => {
],
} as any;
act(() => {
onLoad(false, adapters);
onLoad?.(false, adapters);
});
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.complete,
Expand Down Expand Up @@ -309,7 +309,7 @@ describe('Histogram', () => {
],
} as any;
act(() => {
onLoad(false, adapters);
onLoad?.(false, adapters);
});
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.complete,
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/unified_histogram/public/layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import { EuiSpacer, useEuiTheme, useIsWithinBreakpoints } from '@elastic/eui';
import React, { PropsWithChildren, ReactElement, useEffect, useMemo, useState } from 'react';
import { Observable } from 'rxjs';
import useObservable from 'react-use/lib/useObservable';
import { createHtmlPortalNode, InPortal, OutPortal } from 'react-reverse-portal';
import { css } from '@emotion/css';
Expand Down Expand Up @@ -99,7 +98,7 @@ export interface UnifiedHistogramLayoutProps extends PropsWithChildren<unknown>
*/
hits?: UnifiedHistogramHitsContext;
lensAdapters?: UnifiedHistogramChartLoadEvent['adapters'];
lensEmbeddableOutput$?: Observable<LensEmbeddableOutput>;
dataLoading$?: LensEmbeddableOutput['dataLoading'];
/**
* Context object for the chart -- leave undefined to hide the chart
*/
Expand Down Expand Up @@ -214,7 +213,7 @@ export const UnifiedHistogramLayout = ({
request,
hits,
lensAdapters,
lensEmbeddableOutput$,
dataLoading$,
chart: originalChart,
breakdown,
container,
Expand Down Expand Up @@ -372,7 +371,7 @@ export const UnifiedHistogramLayout = ({
onFilter={onFilter}
onBrushEnd={onBrushEnd}
lensAdapters={lensAdapters}
lensEmbeddableOutput$={lensEmbeddableOutput$}
dataLoading$={dataLoading$}
withDefaultActions={withDefaultActions}
/>
</InPortal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, { useEffect, useMemo } from 'react';
import { unmountComponentAtNode } from 'react-dom';
import type { LensApi } from '@kbn/lens-plugin/public';
import { toMountPoint } from '@kbn/react-kibana-mount';
import { useStateFromPublishingSubject } from '@kbn/presentation-publishing';
import { apiPublishesTimeRange, useStateFromPublishingSubject } from '@kbn/presentation-publishing';
import { ActionWrapper } from './action_wrapper';
import type { CasesActionContextProps, Services } from './types';
import type { CaseUI } from '../../../../common';
Expand All @@ -30,7 +30,9 @@ const AddExistingCaseModalWrapper: React.FC<Props> = ({ lensApi, onClose, onSucc
});

const timeRange = useStateFromPublishingSubject(lensApi.timeRange$);
const parentTimeRange = useStateFromPublishingSubject(lensApi.parentApi?.timeRange$);
const parentTimeRange = useStateFromPublishingSubject(
apiPublishesTimeRange(lensApi.parentApi) ? lensApi.parentApi?.timeRange$ : undefined
);
const absoluteTimeRange = convertToAbsoluteTimeRange(timeRange);
const absoluteParentTimeRange = convertToAbsoluteTimeRange(parentTimeRange);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
import type { IUiSettingsClient } from '@kbn/core/public';
import type { TimefilterContract } from '@kbn/data-plugin/public';
import type { DataViewsContract } from '@kbn/data-views-plugin/public';
import type { Filter, Query } from '@kbn/es-query';
import { isOfAggregateQueryType, type Filter, type Query } from '@kbn/es-query';
import type { DashboardStart } from '@kbn/dashboard-plugin/public';
import type { LensApi } from '@kbn/lens-plugin/public';
import type { JobCreatorType } from '../common/job_creator';
Expand Down Expand Up @@ -199,6 +199,10 @@ export class QuickLensJobCreator extends QuickJobCreatorBase {
bucketSpan: string,
layerIndex?: number
) {
// @TODO: ask ML team to check if ES|QL query here is ok
if (isOfAggregateQueryType(chartInfo.query)) {
throw new Error('Cannot create job, query is of aggregate type');
}
const compatibleLayers = chartInfo.layers.filter(isCompatibleLayer);

const selectedLayer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { obsvReportConfigMap } from '../obsv_exploratory_view';
import { sampleAttributeWithReferenceLines } from './test_data/sample_attribute_with_reference_lines';
import { lensPluginMock } from '@kbn/lens-plugin/public/mocks';
import { FormulaPublicApi, XYState } from '@kbn/lens-plugin/public';
import { Query } from '@kbn/es-query';

describe('Lens Attribute', () => {
mockAppDataView();
Expand Down Expand Up @@ -448,7 +449,9 @@ describe('Lens Attribute', () => {
reportViewConfig.reportType,
formulaHelper
).getJSON();
expect(multiSeriesLensAttr.state.query.query).toEqual('transaction.duration.us < 60000000');
expect((multiSeriesLensAttr.state.query as Query).query).toEqual(
'transaction.duration.us < 60000000'
);
});

describe('Layer breakdowns', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';
import { capitalize } from 'lodash';
import { ExistsFilter, isExistsFilter } from '@kbn/es-query';
import { ExistsFilter, Query, isExistsFilter } from '@kbn/es-query';
import {
AvgIndexPatternColumn,
CardinalityIndexPatternColumn,
Expand Down Expand Up @@ -1271,7 +1271,7 @@ export class LensAttributes {
visualizationType: 'lnsXY' | 'lnsLegacyMetric' | 'lnsHeatmap' = 'lnsXY',
lastRefresh?: number
): TypedLensByValueInput['attributes'] {
const query = this.globalFilter || this.layerConfigs[0].seriesConfig.query;
const query: Query | undefined = this.globalFilter || this.layerConfigs[0].seriesConfig.query;

const { internalReferences, adHocDataViews } = this.getReferences();

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

import { useCallback } from 'react';
import { Filter, Query, TimeRange } from '@kbn/es-query';
import { AggregateQuery, Filter, Query, TimeRange } from '@kbn/es-query';
import type { Action, ActionExecutionContext } from '@kbn/ui-actions-plugin/public';
import { i18n } from '@kbn/i18n';
import useAsync from 'react-use/lib/useAsync';
Expand Down Expand Up @@ -37,7 +37,13 @@ export const useLensAttributes = (params: UseLensAttributesParams) => {
}, [params, dataViews, lens]);

const injectFilters = useCallback(
({ filters, query }: { filters: Filter[]; query: Query }): LensAttributes | null => {
({
filters,
query,
}: {
filters: Filter[];
query: Query | AggregateQuery;
}): LensAttributes | null => {
if (!attributes) {
return null;
}
Expand All @@ -63,7 +69,7 @@ export const useLensAttributes = (params: UseLensAttributesParams) => {
}: {
timeRange: TimeRange;
filters: Filter[];
query: Query;
query: Query | AggregateQuery;
searchSessionId?: string;
}) =>
() => {
Expand Down Expand Up @@ -94,7 +100,7 @@ export const useLensAttributes = (params: UseLensAttributesParams) => {
}: {
timeRange: TimeRange;
filters?: Filter[];
query?: Query;
query?: Query | AggregateQuery;
searchSessionId?: string;
}) => {
const openInLens = getOpenInLensAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ export function VisualizeESQL({
(
isLoading: boolean,
adapters: InlineEditLensEmbeddableContext['lensEvent']['adapters'] | undefined,
lensEmbeddableOutput$?: InlineEditLensEmbeddableContext['lensEvent']['embeddableOutput$']
dataLoading$?: InlineEditLensEmbeddableContext['lensEvent']['dataLoading$']
) => {
const adapterTables = adapters?.tables?.tables;
if (adapterTables && !isLoading) {
setLensLoadEvent({
adapters,
embeddableOutput$: lensEmbeddableOutput$,
dataLoading$,
});
}
},
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/security_solution/public/app/actions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import type { IEmbeddable } from '@kbn/embeddable-plugin/public';
import { LENS_EMBEDDABLE_TYPE, type Embeddable as LensEmbeddable } from '@kbn/lens-plugin/public';
import { isLensApi } from '@kbn/lens-plugin/public';
import type { Serializable } from '@kbn/utility-types';
import { APP_UI_ID } from '../../../common/constants';

Expand All @@ -21,8 +21,10 @@ export const isInSecurityApp = (currentAppId?: string): boolean => {
return !!currentAppId && currentAppId === APP_UI_ID;
};

export const isLensEmbeddable = (embeddable: IEmbeddable): embeddable is LensEmbeddable => {
return embeddable.type === LENS_EMBEDDABLE_TYPE;
// @TODO: this is a temporary fix. It needs a better refactor on the consumer side here to
// adapt to the new Embeddable architecture
export const isLensEmbeddable = (embeddable: IEmbeddable): embeddable is IEmbeddable => {
return isLensApi(embeddable);
};

export const fieldHasCellActions = (field?: string): boolean => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
TypedLensByValueInput,
XYState,
} from '@kbn/lens-plugin/public';
import type { LensBaseEmbeddableInput } from '@kbn/lens-plugin/public/embeddable';
import { setAbsoluteRangeDatePicker } from '../../store/inputs/actions';
import { useKibana } from '../../lib/kibana';
import { useLensAttributes } from './use_lens_attributes';
Expand Down Expand Up @@ -173,7 +172,7 @@ const LensEmbeddableComponent: React.FC<LensEmbeddableComponentProps> = ({
[dispatch, inputsModelId]
);

const onFilterCallback = useCallback<Required<LensBaseEmbeddableInput>['onFilter']>(
const onFilterCallback = useCallback<Required<TypedLensByValueInput>['onFilter']>(
(event) => {
if (disableOnClickFilter) {
event.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* 2.0.
*/

import type { LensBaseEmbeddableInput } from '@kbn/lens-plugin/public/embeddable';
import type { LensEmbeddableInput } from '@kbn/lens-plugin/public';
import { useCallback } from 'react';
import type { OnEmbeddableLoaded, Request } from './types';

import { getRequestsAndResponses } from './utils';

export const useEmbeddableInspect = (onEmbeddableLoad?: OnEmbeddableLoaded) => {
const setInspectData = useCallback<NonNullable<LensBaseEmbeddableInput['onLoad']>>(
const setInspectData = useCallback<NonNullable<LensEmbeddableInput['onLoad']>>(
(isLoading, adapters) => {
if (!adapters) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useSourcererDataView } from '../../../sourcerer/containers';
import { kpiHostMetricLensAttributes } from './lens_attributes/hosts/kpi_host_metric';
import { useRouteSpy } from '../../utils/route/use_route_spy';
import { SecurityPageName } from '../../../app/types';
import type { Query } from '@kbn/es-query';

jest.mock('../../../sourcerer/containers');
jest.mock('../../utils/route/use_route_spy', () => ({
Expand Down Expand Up @@ -145,7 +146,7 @@ describe('useLensAttributes', () => {
{ wrapper }
);

expect(result?.current?.state.query.query).toEqual('');
expect((result?.current?.state.query as Query).query).toEqual('');

expect(result?.current?.state.filters).toEqual([
...getExternalAlertLensAttributes().state.filters,
Expand Down
Loading

0 comments on commit f6e6df0

Please sign in to comment.