Skip to content

Commit

Permalink
[Unified observability] Use the new dataViews service in the observ…
Browse files Browse the repository at this point in the history
…ability plugin (#124952)

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Alejandro Fernández Gómez and kibanamachine authored Feb 10, 2022
1 parent 8ee9765 commit e298474
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 31 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/observability/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"alerting",
"cases",
"data",
"dataViews",
"features",
"inspector",
"ruleRegistry",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function getExploratoryViewEmbeddable(

setLoading(true);
try {
const obsvIndexP = new ObservabilityDataViews(plugins.data);
const obsvIndexP = new ObservabilityDataViews(plugins.dataViews);
const indPattern = await obsvIndexP.getDataView(
dataType,
dataTypesIndexPatterns?.[dataType]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function IndexPatternContextProvider({ children }: ProviderProps) {
const [hasAppData, setHasAppData] = useState<HasAppDataState>({} as HasAppDataState);

const {
services: { data },
services: { dataViews },
} = useKibana<ObservabilityPublicPluginsStart>();

const { indexPatterns: indexPatternsList } = useExploratoryView();
Expand Down Expand Up @@ -83,7 +83,7 @@ export function IndexPatternContextProvider({ children }: ProviderProps) {
setHasAppData((prevState) => ({ ...prevState, [dataType]: hasDataT }));

if (hasDataT && indices) {
const obsvIndexP = new ObservabilityDataViews(data);
const obsvIndexP = new ObservabilityDataViews(dataViews);
const indPattern = await obsvIndexP.getDataView(dataType, indices);

setIndexPatterns((prevState) => ({ ...prevState, [dataType]: indPattern }));
Expand All @@ -100,7 +100,7 @@ export function IndexPatternContextProvider({ children }: ProviderProps) {
}
}
},
[data, hasAppData, indexPatternsList, loading]
[dataViews, hasAppData, indexPatternsList, loading]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { IndexPattern, IndexPatternsContract } from '../../../../../../../src/pl
import { AppDataType, SeriesUrl, UrlFilter } from './types';
import { createStubIndexPattern } from '../../../../../../../src/plugins/data/common/stubs';
import { dataPluginMock } from '../../../../../../../src/plugins/data/public/mocks';
import { dataViewPluginMocks } from '../../../../../../../src/plugins/data_views/public/mocks';
import { ListItem } from '../../../hooks/use_values_list';
import { TRANSACTION_DURATION } from './configurations/constants/elasticsearch_fieldnames';
import { casesPluginMock } from '../../../../../cases/public/mocks';
Expand Down Expand Up @@ -133,6 +134,7 @@ export const mockCore: () => Partial<CoreStart & ObservabilityPublicPluginsStart
},
lens: lensPluginMock.createStartContract(),
data: dataPluginMock.createStartContract(),
dataViews: dataViewPluginMocks.createStartContract(),
cases: casesPluginMock.createStartContract(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function AlertsPage() {
{
id: 'dynamic-observability-alerts-table-index-pattern',
title: indexNames.join(','),
fields: await plugins.data.indexPatterns.getFieldsForWildcard({
fields: await plugins.dataViews.getFieldsForWildcard({
pattern: indexNames.join(','),
allowNoIndex: true,
}),
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/observability/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
DataPublicPluginSetup,
DataPublicPluginStart,
} from '../../../../src/plugins/data/public';
import type { DataViewsPublicPluginStart } from '../../../../src/plugins/data_views/public';
import type { DiscoverStart } from '../../../../src/plugins/discover/public';
import type { EmbeddableStart } from '../../../../src/plugins/embeddable/public';
import type {
Expand Down Expand Up @@ -60,6 +61,7 @@ export interface ObservabilityPublicPluginsStart {
home?: HomePublicPluginStart;
triggersActionsUi: TriggersAndActionsUIPublicPluginStart;
data: DataPublicPluginStart;
dataViews: DataViewsPublicPluginStart;
lens: LensPublicStart;
discover: DiscoverStart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,62 +68,62 @@ const fieldFormats = {
};

describe('ObservabilityIndexPatterns', function () {
const { data } = mockCore();
data!.indexPatterns.get = jest.fn().mockReturnValue({ title: 'index-*' });
data!.indexPatterns.createAndSave = jest.fn().mockReturnValue({ id: dataViewList.ux });
data!.indexPatterns.updateSavedObject = jest.fn();
const { dataViews } = mockCore();
dataViews!.get = jest.fn().mockReturnValue({ title: 'index-*' });
dataViews!.createAndSave = jest.fn().mockReturnValue({ id: dataViewList.ux });
dataViews!.updateSavedObject = jest.fn();

it('should return index pattern for app', async function () {
const obsv = new ObservabilityDataViews(data!);
const obsv = new ObservabilityDataViews(dataViews!);

const indexP = await obsv.getDataView('ux', 'heartbeat-8*,synthetics-*');

expect(indexP).toEqual({ id: 'rum_static_index_pattern_id' });

expect(data?.indexPatterns.get).toHaveBeenCalledWith(
expect(dataViews?.get).toHaveBeenCalledWith(
'rum_static_index_pattern_id_heartbeat_8_synthetics_'
);
expect(data?.indexPatterns.get).toHaveBeenCalledTimes(1);
expect(dataViews?.get).toHaveBeenCalledTimes(1);
});

it('should creates missing index pattern', async function () {
data!.indexPatterns.get = jest.fn().mockImplementation(() => {
dataViews!.get = jest.fn().mockImplementation(() => {
throw new SavedObjectNotFound('index_pattern');
});

data!.indexPatterns.createAndSave = jest.fn().mockReturnValue({ id: dataViewList.ux });
dataViews!.createAndSave = jest.fn().mockReturnValue({ id: dataViewList.ux });

const obsv = new ObservabilityDataViews(data!);
const obsv = new ObservabilityDataViews(dataViews!);

const indexP = await obsv.getDataView('ux', 'trace-*,apm-*');

expect(indexP).toEqual({ id: dataViewList.ux });

expect(data?.indexPatterns.createAndSave).toHaveBeenCalledWith({
expect(dataViews?.createAndSave).toHaveBeenCalledWith({
fieldFormats,
id: 'rum_static_index_pattern_id_trace_apm_',
timeFieldName: '@timestamp',
title: '(rum-data-view)*,trace-*,apm-*',
});

expect(data?.indexPatterns.createAndSave).toHaveBeenCalledTimes(1);
expect(dataViews?.createAndSave).toHaveBeenCalledTimes(1);
});

it('should return getFieldFormats', function () {
const obsv = new ObservabilityDataViews(data!);
const obsv = new ObservabilityDataViews(dataViews!);

expect(obsv.getFieldFormats('ux')).toEqual(fieldFormats);
});

it('should validate field formats', async function () {
mockIndexPattern.getFormatterForField = jest.fn().mockReturnValue({ params: () => {} });

const obsv = new ObservabilityDataViews(data!);
const obsv = new ObservabilityDataViews(dataViews!);

await obsv.validateFieldFormats('ux', mockIndexPattern);

expect(data?.indexPatterns.updateSavedObject).toHaveBeenCalledTimes(1);
expect(data?.indexPatterns.updateSavedObject).toHaveBeenCalledWith(
expect(dataViews?.updateSavedObject).toHaveBeenCalledTimes(1);
expect(dataViews?.updateSavedObject).toHaveBeenCalledWith(
expect.objectContaining({ fieldFormatMap: fieldFormats })
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

import type { FieldFormat as IFieldFormat } from 'src/plugins/field_formats/common';
import { SavedObjectNotFound } from '../../../../../../src/plugins/kibana_utils/public';
import { DataPublicPluginStart } from '../../../../../../src/plugins/data/public';
import type { DataView, DataViewSpec } from '../../../../../../src/plugins/data/common';
import type {
DataViewsPublicPluginStart,
DataView,
DataViewSpec,
} from '../../../../../../src/plugins/data_views/public';
import { rumFieldFormats } from '../../components/shared/exploratory_view/configurations/rum/field_formats';
import { syntheticsFieldFormats } from '../../components/shared/exploratory_view/configurations/synthetics/field_formats';
import {
Expand Down Expand Up @@ -77,19 +80,19 @@ export function isParamsSame(param1: IFieldFormat['_params'], param2: FieldForma
}

export class ObservabilityDataViews {
data?: DataPublicPluginStart;
dataViews?: DataViewsPublicPluginStart;

constructor(data: DataPublicPluginStart) {
this.data = data;
constructor(dataViews: DataViewsPublicPluginStart) {
this.dataViews = dataViews;
}

async createDataView(app: AppDataType, indices: string) {
if (!this.data) {
if (!this.dataViews) {
throw new Error('data is not defined');
}

const appIndicesPattern = getAppIndicesWithPattern(app, indices);
return await this.data.dataViews.createAndSave({
return await this.dataViews.createAndSave({
title: appIndicesPattern,
id: getAppDataViewId(app, indices),
timeFieldName: '@timestamp',
Expand All @@ -113,7 +116,7 @@ export class ObservabilityDataViews {
}
});
if (isParamsDifferent) {
await this.data?.dataViews.updateSavedObject(dataView);
await this.dataViews?.updateSavedObject(dataView);
}
}
}
Expand Down Expand Up @@ -142,7 +145,7 @@ export class ObservabilityDataViews {
}

async getDataView(app: AppDataType, indices?: string): Promise<DataView | undefined> {
if (!this.data) {
if (!this.dataViews) {
throw new Error('data is not defined');
}
let appIndices = indices;
Expand All @@ -155,7 +158,7 @@ export class ObservabilityDataViews {
const dataViewId = getAppDataViewId(app, appIndices);
const dataViewTitle = getAppIndicesWithPattern(app, appIndices);
// we will get the data view by id
const dataView = await this.data?.indexPatterns.get(dataViewId);
const dataView = await this.dataViews?.get(dataViewId);

// and make sure title matches, otherwise, we will need to create it
if (dataView.title !== dataViewTitle) {
Expand Down

0 comments on commit e298474

Please sign in to comment.