Skip to content

Commit

Permalink
Switch to new 'as const' + typeof + ValueOf approach
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Nov 2, 2020
1 parent d6e3148 commit 19a4d21
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/ingest_manager/common/constants/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export const requiredPackages = {
System: 'system',
Endpoint: 'endpoint',
} as const;

export const dataTypes = {
Logs: 'logs',
Metrics: 'metrics',
} as const;
5 changes: 2 additions & 3 deletions x-pack/plugins/ingest_manager/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Follow pattern from https://github.com/elastic/kibana/pull/52447
// TODO: Update when https://github.com/elastic/kibana/issues/53021 is closed
import { SavedObject, SavedObjectAttributes, SavedObjectReference } from 'src/core/public';
import { requiredPackages } from '../../constants';
import { dataTypes, requiredPackages } from '../../constants';

export enum InstallationStatus {
installed = 'installed',
Expand Down Expand Up @@ -45,8 +45,7 @@ export enum ElasticsearchAssetType {
transform = 'transform',
}

export const dataTypes = ['logs', 'metrics'] as const;
export type DataType = typeof dataTypes[number];
export type DataType = typeof dataTypes;

export enum AgentAssetType {
input = 'input',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import {
import * as Registry from '../../registry';
import { loadFieldsFromYaml, Fields, Field } from '../../fields/field';
import { getPackageKeysByStatus } from '../../packages/get';
import { dataTypes } from '../../../../../common/constants';
import { ValueOf } from '../../../../../common/types';
import {
InstallationStatus,
RegistryPackage,
CallESAsCurrentUser,
DataType,
dataTypes,
} from '../../../../types';
import { appContextService } from '../../../../services';

Expand Down Expand Up @@ -120,7 +121,7 @@ export async function installIndexPatterns(
const packageVersionsInfo = await Promise.all(packageVersionsFetchInfoPromise);

// for each index pattern type, create an index pattern
const indexPatternTypes = dataTypes;
const indexPatternTypes = Object.values(dataTypes);
indexPatternTypes.forEach(async (indexPatternType) => {
// if this is an update because a package is being uninstalled (no pkgkey argument passed) and no other packages are installed, remove the index pattern
if (!pkgName && installedPackages.length === 0) {
Expand All @@ -147,7 +148,7 @@ export async function installIndexPatterns(
// of all fields from all data streams matching data stream type
export const getAllDataStreamFieldsByType = async (
packages: RegistryPackage[],
dataStreamType: DataType
dataStreamType: ValueOf<DataType>
): Promise<Fields> => {
const dataStreamsPromises = packages.reduce<Array<Promise<Field[]>>>((acc, pkg) => {
if (pkg.data_streams) {
Expand Down Expand Up @@ -392,7 +393,7 @@ export const ensureDefaultIndices = async (callCluster: CallESAsCurrentUser) =>
// that no matching indices exist https://github.com/elastic/kibana/issues/62343
const logger = appContextService.getLogger();
return Promise.all(
dataTypes.map(async (indexPattern) => {
Object.values(dataTypes).map(async (indexPattern) => {
const defaultIndexPatternName = indexPattern + INDEX_PATTERN_PLACEHOLDER_SUFFIX;
const indexExists = await callCluster('indices.exists', { index: defaultIndexPatternName });
if (!indexExists) {
Expand Down

0 comments on commit 19a4d21

Please sign in to comment.