Skip to content

Commit

Permalink
Use dataViews service inside ObservabilityDataViews
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández Gómez committed Feb 8, 2022
1 parent 28d72f7 commit eef8302
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
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 @@ -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
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 eef8302

Please sign in to comment.