[data views] Consolidate code that gets ui settings (#178033)

## Summary

Simple code cleanup. Consolidate UI Setting access and make available
with method in Data Views service.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Matthew Kime 2024-03-06 06:56:05 -06:00 committed by GitHub
parent c983a15784
commit c8604ecaa4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 13 deletions

View file

@ -71,3 +71,5 @@ export const FIELDS_PATH = '/internal/data_views/fields';
export const EXISTING_INDICES_PATH = '/internal/data_views/_existing_indices';
export const DATA_VIEWS_FIELDS_EXCLUDED_TIERS = 'data_views:fields_excluded_data_tiers';
export const DEFAULT_DATA_VIEW_ID = 'defaultIndex';

View file

@ -12,6 +12,7 @@ import { castEsToKbnFieldTypeName } from '@kbn/field-types';
import { FieldFormatsStartCommon, FORMATS_UI_SETTINGS } from '@kbn/field-formats-plugin/common';
import { v4 as uuidv4 } from 'uuid';
import { PersistenceAPI } from '../types';
import { DEFAULT_DATA_VIEW_ID } from '../constants';
import type { RuntimeField, RuntimeFieldSpec, RuntimeType } from '../types';
import { DataView } from './data_view';
@ -294,6 +295,9 @@ export interface DataViewsServicePublicMethods {
* Returns whether a default data view exists.
*/
defaultDataViewExists: () => Promise<boolean>;
getMetaFields: () => Promise<string[] | undefined>;
getShortDotsEnable: () => Promise<boolean | undefined>;
}
/**
@ -483,7 +487,7 @@ export class DataViewsService {
* Get default index pattern id
*/
getDefaultId = async (): Promise<string | null> => {
const defaultIndexPatternId = await this.config.get<string | null>('defaultIndex');
const defaultIndexPatternId = await this.config.get<string | null>(DEFAULT_DATA_VIEW_ID);
return defaultIndexPatternId ?? null;
};
@ -493,8 +497,8 @@ export class DataViewsService {
* @param force set default data view even if there's an existing default
*/
setDefault = async (id: string | null, force = false) => {
if (force || !(await this.config.get('defaultIndex'))) {
await this.config.set('defaultIndex', id);
if (force || !(await this.getDefaultId())) {
await this.config.set(DEFAULT_DATA_VIEW_ID, id);
}
};
@ -505,13 +509,18 @@ export class DataViewsService {
return this.apiClient.hasUserDataView();
}
getMetaFields = async () => await this.config.get<string[]>(META_FIELDS);
getShortDotsEnable = async () =>
await this.config.get<boolean>(FORMATS_UI_SETTINGS.SHORT_DOTS_ENABLE);
/**
* Get field list by providing { pattern }.
* @param options options for getting field list
* @returns FieldSpec[]
*/
getFieldsForWildcard = async (options: GetFieldsOptions): Promise<FieldSpec[]> => {
const metaFields = await this.config.get<string[]>(META_FIELDS);
const metaFields = await this.getMetaFields();
const { fields } = await this.apiClient.getFieldsForWildcard({
...options,
metaFields,
@ -542,7 +551,7 @@ export class DataViewsService {
dataView: DataView,
forceRefresh: boolean = false
) => {
const metaFields = await this.config.get<string[]>(META_FIELDS);
const metaFields = await this.getMetaFields();
return this.apiClient.getFieldsForWildcard({
type: dataView.type,
rollupIndex: dataView?.typeMeta?.params?.rollup_index,
@ -555,7 +564,7 @@ export class DataViewsService {
};
private getFieldsAndIndicesForWildcard = async (options: GetFieldsOptions) => {
const metaFields = await this.config.get<string[]>(META_FIELDS);
const metaFields = await this.getMetaFields();
return this.apiClient.getFieldsForWildcard({
pattern: options.pattern,
metaFields,
@ -790,7 +799,7 @@ export class DataViewsService {
spec.title as string,
{
pattern: title as string,
metaFields: await this.config.get(META_FIELDS),
metaFields: await this.getMetaFields(),
type,
rollupIndex: typeMeta?.params?.rollup_index,
allowNoIndex: spec.allowNoIndex,
@ -957,8 +966,8 @@ export class DataViewsService {
skipFetchFields = false,
displayErrors = true
): Promise<DataView> {
const shortDotsEnable = await this.config.get<boolean>(FORMATS_UI_SETTINGS.SHORT_DOTS_ENABLE);
const metaFields = await this.config.get<string[] | undefined>(META_FIELDS);
const shortDotsEnable = await this.getShortDotsEnable();
const metaFields = await this.getMetaFields();
const spec = {
id: id ?? uuidv4(),
@ -1193,21 +1202,21 @@ export class DataViewsService {
private async getDefaultDataViewId() {
const patterns = await this.getIdsWithTitle();
let defaultId: string | undefined = await this.config.get('defaultIndex');
let defaultId: string | null = await this.getDefaultId();
const exists = defaultId ? patterns.some((pattern) => pattern.id === defaultId) : false;
if (defaultId && !exists) {
if (await this.getCanSaveAdvancedSettings()) {
await this.config.remove('defaultIndex');
await this.config.remove(DEFAULT_DATA_VIEW_ID);
}
defaultId = undefined;
defaultId = null;
}
if (!defaultId && patterns.length >= 1 && (await this.hasUserDataView().catch(() => true))) {
defaultId = patterns[0].id;
if (await this.getCanSaveAdvancedSettings()) {
await this.config.set('defaultIndex', defaultId);
await this.config.set(DEFAULT_DATA_VIEW_ID, defaultId);
}
}