Skip to content

Commit

Permalink
[data views] Rename a bunch of index pattern code references to data …
Browse files Browse the repository at this point in the history
…views (#112770)

* rename a bunch of index pattern references to data views
  • Loading branch information
mattkime authored Sep 22, 2021
1 parent 1adb492 commit 4b71c43
Show file tree
Hide file tree
Showing 42 changed files with 100 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ const createDashboardAppStateServices = () => {
const defaults = makeDefaultServices();
const indexPatterns = {} as IndexPatternsContract;
const defaultIndexPattern = { id: 'foo', fields: [{ name: 'bar' }] } as IIndexPattern;
indexPatterns.ensureDefaultIndexPattern = jest
.fn()
.mockImplementation(() => Promise.resolve(true));
indexPatterns.ensureDefaultDataView = jest.fn().mockImplementation(() => Promise.resolve(true));
indexPatterns.getDefault = jest
.fn()
.mockImplementation(() => Promise.resolve(defaultIndexPattern));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const loadSavedDashboardState = async ({
notifications.toasts.addWarning(getDashboard60Warning());
return;
}
await indexPatterns.ensureDefaultIndexPattern();
await indexPatterns.ensureDefaultDataView();
let savedDashboard: DashboardSavedObject | undefined;
try {
savedDashboard = (await savedDashboards.get(savedDashboardId)) as DashboardSavedObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
*/

import { stubFieldSpecMap, stubLogstashFieldSpecMap } from './field.stub';
import { createStubIndexPattern } from './data_views/index_pattern.stub';
export { createStubIndexPattern } from './data_views/index_pattern.stub';
import { createStubDataView } from './data_views/data_view.stub';
export {
createStubDataView,
createStubDataView as createStubIndexPattern,
} from './data_views/data_view.stub';
import { SavedObject } from '../../../../core/types';
import { IndexPatternAttributes } from '../types';
import { DataViewAttributes } from '../types';

export const stubIndexPattern = createStubIndexPattern({
export const stubDataView = createStubDataView({
spec: {
id: 'logstash-*',
fields: stubFieldSpecMap,
Expand All @@ -21,15 +24,19 @@ export const stubIndexPattern = createStubIndexPattern({
},
});

export const stubIndexPatternWithoutTimeField = createStubIndexPattern({
export const stubIndexPattern = stubDataView;

export const stubDataViewWithoutTimeField = createStubDataView({
spec: {
id: 'logstash-*',
fields: stubFieldSpecMap,
title: 'logstash-*',
},
});

export const stubLogstashIndexPattern = createStubIndexPattern({
export const stubIndexPatternWithoutTimeField = stubDataViewWithoutTimeField;

export const stubLogstashDataView = createStubDataView({
spec: {
id: 'logstash-*',
title: 'logstash-*',
Expand All @@ -38,9 +45,11 @@ export const stubLogstashIndexPattern = createStubIndexPattern({
},
});

export function stubbedSavedObjectIndexPattern(
export const stubLogstashIndexPattern = stubLogstashDataView;

export function stubbedSavedObjectDataView(
id: string | null = null
): SavedObject<IndexPatternAttributes> {
): SavedObject<DataViewAttributes> {
return {
id: id ?? '',
type: 'index-pattern',
Expand All @@ -53,3 +62,5 @@ export function stubbedSavedObjectIndexPattern(
references: [],
};
}

export const stubbedSavedObjectIndexPattern = stubbedSavedObjectDataView;
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
* Side Public License, v 1.
*/

import { IndexPattern } from './data_view';
import { DataView } from './data_view';
import { DataViewSpec } from '../types';
import { FieldFormatsStartCommon } from '../../../../field_formats/common';
import { fieldFormatsMock } from '../../../../field_formats/common/mocks';

/**
* Create a custom stub index pattern. Use it in your unit tests where an {@link IndexPattern} expected.
* Create a custom stub index pattern. Use it in your unit tests where an {@link DataView} expected.
* @param spec - Serialized index pattern object
* @param opts - Specify index pattern options
* @param deps - Optionally provide dependencies, you can provide a custom field formats implementation, by default a dummy mock is used
*
* @returns - an {@link IndexPattern} instance
* @returns - an {@link DataView} instance
*
*
* @example
Expand All @@ -32,7 +32,7 @@ import { fieldFormatsMock } from '../../../../field_formats/common/mocks';
*
* ```
*/
export const createStubIndexPattern = ({
export const createStubDataView = ({
spec,
opts,
deps,
Expand All @@ -45,12 +45,10 @@ export const createStubIndexPattern = ({
deps?: {
fieldFormats?: FieldFormatsStartCommon;
};
}): IndexPattern => {
const indexPattern = new IndexPattern({
}): DataView =>
new DataView({
spec,
metaFields: opts?.metaFields ?? ['_id', '_type', '_source'],
shortDotsEnable: opts?.shortDotsEnable,
fieldFormats: deps?.fieldFormats ?? fieldFormatsMock,
});
return indexPattern;
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { fieldFormatsMock } from '../../../../field_formats/common/mocks';
import { FieldFormat } from '../../../../field_formats/common';
import { RuntimeField } from '../types';
import { stubLogstashFields } from '../field.stub';
import { stubbedSavedObjectIndexPattern } from '../index_pattern.stub';
import { stubbedSavedObjectIndexPattern } from '../data_view.stub';

class MockFieldFormatter {}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/data_views/data_views/data_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class DataView implements IIndexPattern {

// Date value returned in "_source" could be in any number of formats
// Use a docvalue for each date field to ensure standardized formats when working with date fields
// indexPattern.flattenHit will override "_source" values when the same field is also defined in "fields"
// dataView.flattenHit will override "_source" values when the same field is also defined in "fields"
const docvalueFields = reject(this.fields.getByType('date'), 'scripted').map((dateField) => {
return {
field: dateField.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DataViewsService, DataView } from '.';
import { fieldFormatsMock } from '../../../../field_formats/common/mocks';

import { UiSettingsCommon, SavedObjectsClientCommon, SavedObject } from '../types';
import { stubbedSavedObjectIndexPattern } from '../index_pattern.stub';
import { stubbedSavedObjectIndexPattern } from '../data_view.stub';

const createFieldsFetcher = jest.fn().mockImplementation(() => ({
getFieldsForWildcard: jest.fn().mockImplementation(() => {
Expand Down
26 changes: 11 additions & 15 deletions src/plugins/data/common/data_views/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ export class DataViewsService {
private fieldFormats: FieldFormatsStartCommon;
private onNotification: OnNotification;
private onError: OnError;
private indexPatternCache: ReturnType<typeof createDataViewCache>;
private dataViewCache: ReturnType<typeof createDataViewCache>;

ensureDefaultIndexPattern: EnsureDefaultDataView;
ensureDefaultDataView: EnsureDefaultDataView;

constructor({
uiSettings,
Expand All @@ -96,12 +96,9 @@ export class DataViewsService {
this.fieldFormats = fieldFormats;
this.onNotification = onNotification;
this.onError = onError;
this.ensureDefaultIndexPattern = createEnsureDefaultDataView(
uiSettings,
onRedirectNoIndexPattern
);
this.ensureDefaultDataView = createEnsureDefaultDataView(uiSettings, onRedirectNoIndexPattern);

this.indexPatternCache = createDataViewCache();
this.dataViewCache = createDataViewCache();
}

/**
Expand Down Expand Up @@ -190,9 +187,9 @@ export class DataViewsService {
clearCache = (id?: string) => {
this.savedObjectsCache = null;
if (id) {
this.indexPatternCache.clear(id);
this.dataViewCache.clear(id);
} else {
this.indexPatternCache.clearAll();
this.dataViewCache.clearAll();
}
};

Expand Down Expand Up @@ -505,12 +502,11 @@ export class DataViewsService {

get = async (id: string): Promise<DataView> => {
const indexPatternPromise =
this.indexPatternCache.get(id) ||
this.indexPatternCache.set(id, this.getSavedObjectAndInit(id));
this.dataViewCache.get(id) || this.dataViewCache.set(id, this.getSavedObjectAndInit(id));

// don't cache failed requests
indexPatternPromise.catch(() => {
this.indexPatternCache.clear(id);
this.dataViewCache.clear(id);
});

return indexPatternPromise;
Expand Down Expand Up @@ -580,7 +576,7 @@ export class DataViewsService {
)) as SavedObject<DataViewAttributes>;

const createdIndexPattern = await this.initFromSavedObject(response);
this.indexPatternCache.set(createdIndexPattern.id!, Promise.resolve(createdIndexPattern));
this.dataViewCache.set(createdIndexPattern.id!, Promise.resolve(createdIndexPattern));
if (this.savedObjectsCache) {
this.savedObjectsCache.push(response as SavedObject<IndexPatternListSavedObjectAttrs>);
}
Expand Down Expand Up @@ -668,7 +664,7 @@ export class DataViewsService {
indexPattern.version = samePattern.version;

// Clear cache
this.indexPatternCache.clear(indexPattern.id!);
this.dataViewCache.clear(indexPattern.id!);

// Try the save again
return this.updateSavedObject(indexPattern, saveAttempts, ignoreErrors);
Expand All @@ -682,7 +678,7 @@ export class DataViewsService {
* @param indexPatternId: Id of kibana Index Pattern to delete
*/
async delete(indexPatternId: string) {
this.indexPatternCache.clear(indexPatternId);
this.dataViewCache.clear(indexPatternId);
return this.savedObjectsClient.delete(DATA_VIEW_SAVED_OBJECT_TYPE, indexPatternId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Side Public License, v 1.
*/

import { IndexPattern } from './data_view';
import { DataView } from './data_view';

import { fieldFormatsMock } from '../../../../field_formats/common/mocks';
import { flattenHitWrapper } from './flatten_hit';
import { stubbedSavedObjectIndexPattern } from '../index_pattern.stub';
import { stubbedSavedObjectIndexPattern } from '../data_view.stub';

class MockFieldFormatter {}

Expand All @@ -24,7 +24,7 @@ function create(id: string) {
attributes: { timeFieldName, fields, title },
} = stubbedSavedObjectIndexPattern(id);

return new IndexPattern({
return new DataView({
spec: {
id,
type,
Expand All @@ -41,7 +41,7 @@ function create(id: string) {
}

describe('flattenHit', () => {
let indexPattern: IndexPattern;
let indexPattern: DataView;

// create an indexPattern instance for each test
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/data_views/data_views/flatten_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ function decorateFlattenedWrapper(hit: Record<string, any>, metaFields: Record<s
*
* @internal
*/
export function flattenHitWrapper(indexPattern: DataView, metaFields = {}, cache = new WeakMap()) {
export function flattenHitWrapper(dataView: DataView, metaFields = {}, cache = new WeakMap()) {
return function cachedFlatten(hit: Record<string, any>, deep = false) {
const decorateFlattened = decorateFlattenedWrapper(hit, metaFields);
const cached = cache.get(hit);
const flattened = cached || flattenHit(indexPattern, hit, deep);
const flattened = cached || flattenHit(dataView, hit, deep);
if (!cached) {
cache.set(hit, { ...flattened });
}
Expand Down
14 changes: 7 additions & 7 deletions src/plugins/data/common/data_views/data_views/format_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ const partialFormattedCache = new WeakMap();

// Takes a hit, merges it with any stored/scripted fields, and with the metaFields
// returns a formatted version
export function formatHitProvider(indexPattern: DataView, defaultFormat: any) {
export function formatHitProvider(dataView: DataView, defaultFormat: any) {
function convert(
hit: Record<string, any>,
val: any,
fieldName: string,
type: FieldFormatsContentType = 'html'
) {
const field = indexPattern.fields.getByName(fieldName);
const format = field ? indexPattern.getFormatterForField(field) : defaultFormat;
const field = dataView.fields.getByName(fieldName);
const format = field ? dataView.getFormatterForField(field) : defaultFormat;

return format.convert(val, type, { field, hit, indexPattern });
return format.convert(val, type, { field, hit, indexPattern: dataView });
}

function formatHit(hit: Record<string, any>, type: string = 'html') {
if (type === 'text') {
// formatHit of type text is for react components to get rid of <span ng-non-bindable>
// since it's currently just used at the discover's doc view table, caching is not necessary
const flattened = indexPattern.flattenHit(hit);
const flattened = dataView.flattenHit(hit);
const result: Record<string, any> = {};
for (const [key, value] of Object.entries(flattened)) {
result[key] = convert(hit, value, key, type);
Expand All @@ -53,7 +53,7 @@ export function formatHitProvider(indexPattern: DataView, defaultFormat: any) {
const cache: Record<string, any> = {};
formattedCache.set(hit, cache);

_.forOwn(indexPattern.flattenHit(hit), function (val: any, fieldName?: string) {
_.forOwn(dataView.flattenHit(hit), function (val: any, fieldName?: string) {
// sync the formatted and partial cache
if (!fieldName) {
return;
Expand All @@ -77,7 +77,7 @@ export function formatHitProvider(indexPattern: DataView, defaultFormat: any) {
partialFormattedCache.set(hit, partials);
}

const val = fieldName === '_source' ? hit._source : indexPattern.flattenHit(hit)[fieldName];
const val = fieldName === '_source' ? hit._source : dataView.flattenHit(hit)[fieldName];
return convert(hit, val, fieldName);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
export class DuplicateDataViewError extends Error {
constructor(message: string) {
super(message);
this.name = 'DuplicateIndexPatternError';
this.name = 'DuplicateDataViewError';
}
}
10 changes: 5 additions & 5 deletions src/plugins/data/common/data_views/field.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Side Public License, v 1.
*/

import { FieldSpec, IndexPatternField } from '.';
import { FieldSpec, DataViewField } from '.';

export const createIndexPatternFieldStub = ({ spec }: { spec: FieldSpec }): IndexPatternField => {
return new IndexPatternField(spec);
export const createIndexPatternFieldStub = ({ spec }: { spec: FieldSpec }): DataViewField => {
return new DataViewField(spec);
};

export const stubFieldSpecMap: Record<string, FieldSpec> = {
Expand Down Expand Up @@ -71,7 +71,7 @@ export const stubFieldSpecMap: Record<string, FieldSpec> = {
},
};

export const stubFields: IndexPatternField[] = Object.values(stubFieldSpecMap).map((spec) =>
export const stubFields: DataViewField[] = Object.values(stubFieldSpecMap).map((spec) =>
createIndexPatternFieldStub({ spec })
);

Expand Down Expand Up @@ -404,6 +404,6 @@ export const stubLogstashFieldSpecMap: Record<string, FieldSpec> = {
},
};

export const stubLogstashFields: IndexPatternField[] = Object.values(stubLogstashFieldSpecMap).map(
export const stubLogstashFields: DataViewField[] = Object.values(stubLogstashFieldSpecMap).map(
(spec) => createIndexPatternFieldStub({ spec })
);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { IndexPatternField } from './index_pattern_field';
import { IndexPatternField } from './data_view_field';
import { IndexPattern } from '..';
import { KBN_FIELD_TYPES } from '../../../common';
import { FieldSpec, RuntimeField } from '../types';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/data_views/fields/field_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { findIndex } from 'lodash';
import { IFieldType } from './types';
import { DataViewField } from './index_pattern_field';
import { DataViewField } from './data_view_field';
import { FieldSpec, DataViewFieldMap } from '../types';
import { DataView } from '../data_views';

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/data_views/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
export * from './types';
export { isFilterable, isNestedField } from './utils';
export * from './field_list';
export * from './index_pattern_field';
export * from './data_view_field';
Loading

0 comments on commit 4b71c43

Please sign in to comment.