Skip to content

Commit

Permalink
Add registerProfileProviders and new example profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed Jun 12, 2024
1 parent ad83b20 commit 831f324
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 127 deletions.
1 change: 1 addition & 0 deletions src/plugins/discover/public/context_awareness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

export * from './types';
export * from './profiles';
export { registerProfileProviders } from './profile_providers';
export { getMergedAccessor } from './composable_profile';
export { ProfilesManager } from './profiles_manager';
export { useProfileAccessor, useRootProfile } from './hooks';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { exampleDataSourceProfileProvider } from './profile';
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,20 @@
*/

import { EuiBadge } from '@elastic/eui';
import {
DataTableRecord,
getMessageFieldWithFallbacks,
LogDocumentOverview,
} from '@kbn/discover-utils';
import type { DataTableRecord } from '@kbn/discover-utils';
import { isOfAggregateQueryType } from '@kbn/es-query';
import { getIndexPatternFromESQLQuery } from '@kbn/esql-utils';
import { euiThemeVars } from '@kbn/ui-theme';
import { capitalize } from 'lodash';
import React from 'react';
import { DataSourceType, isDataSourceType } from '../../../common/data_sources';
import { DataSourceCategory, DataSourceProfileProvider } from './data_source_profile';
import { DocumentProfileProvider, DocumentType } from './document_profile';
import { RootProfileProvider, SolutionType } from './root_profile';
import { DataSourceType, isDataSourceType } from '../../../../common/data_sources';
import { DataSourceCategory, DataSourceProfileProvider } from '../../profiles';

export const o11yRootProfileProvider: RootProfileProvider = {
profileId: 'o11y-root-profile',
profile: {},
resolve: (params) => {
if (params.solutionNavId === 'oblt') {
return {
isMatch: true,
context: {
solutionType: SolutionType.Observability,
},
};
}

return { isMatch: false };
},
};

export const logsDataSourceProfileProvider: DataSourceProfileProvider = {
profileId: 'logs-data-source-profile',
export const exampleDataSourceProfileProvider: DataSourceProfileProvider = {
profileId: 'example-data-source-profile',
profile: {
getCellRenderers: (prev) => () => ({
...prev(),
'@timestamp': (props) => {
const timestamp = getFieldValue(props.row, '@timestamp');
return (
<EuiBadge color="hollow" title={timestamp}>
{timestamp}
</EuiBadge>
);
},
'log.level': (props) => {
const level = getFieldValue(props.row, 'log.level');
if (!level) {
Expand All @@ -68,12 +37,6 @@ export const logsDataSourceProfileProvider: DataSourceProfileProvider = {
</EuiBadge>
);
},
message: (props) => {
const { value } = getMessageFieldWithFallbacks(
props.row.flattened as unknown as LogDocumentOverview
);
return value || <span css={{ color: euiThemeVars.euiTextSubduedColor }}>(None)</span>;
},
}),
},
resolve: (params) => {
Expand All @@ -89,31 +52,14 @@ export const logsDataSourceProfileProvider: DataSourceProfileProvider = {
indices = params.dataView.getIndexPattern().split(',');
}

if (indices.every((index) => index.startsWith('logs-'))) {
return {
isMatch: true,
context: { category: DataSourceCategory.Logs },
};
}

return { isMatch: false };
},
};

export const logDocumentProfileProvider: DocumentProfileProvider = {
profileId: 'log-document-profile',
profile: {},
resolve: (params) => {
if (getFieldValue(params.record, 'data_stream.type') === 'logs') {
return {
isMatch: true,
context: {
type: DocumentType.Log,
},
};
if (!indices.every((index) => index.startsWith('logs-'))) {
return { isMatch: false };
}

return { isMatch: false };
return {
isMatch: true,
context: { category: DataSourceCategory.Logs },
};
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { exampleDocumentProfileProvider } from './profile';
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { DataTableRecord } from '@kbn/discover-utils';
import { DocumentProfileProvider, DocumentType } from '../../profiles';

export const exampleDocumentProfileProvider: DocumentProfileProvider = {
profileId: 'example-document-profile',
profile: {},
resolve: (params) => {
if (getFieldValue(params.record, 'data_stream.type') !== 'logs') {
return { isMatch: false };
}

return {
isMatch: true,
context: {
type: DocumentType.Log,
},
};
},
};

const getFieldValue = (record: DataTableRecord, field: string) => {
const value = record.flattened[field];
return Array.isArray(value) ? value[0] : value;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { exampleRootProfileProvider } from './profile';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { EuiBadge } from '@elastic/eui';
import type { DataTableRecord } from '@kbn/discover-utils';
import React from 'react';
import { RootProfileProvider, SolutionType } from '../../profiles';

export const exampleRootProfileProvider: RootProfileProvider = {
profileId: 'example-root-profile',
profile: {
getCellRenderers: (prev) => () => ({
...prev(),
'@timestamp': (props) => {
const timestamp = getFieldValue(props.row, '@timestamp');
return (
<EuiBadge color="hollow" title={timestamp} data-test-subj="exampleRootProfileTimestamp">
{timestamp}
</EuiBadge>
);
},
}),
},
resolve: () => {
return { isMatch: true, context: { solutionType: SolutionType.Default } };
},
};

const getFieldValue = (record: DataTableRecord, field: string) => {
const value = record.flattened[field];
return Array.isArray(value) ? value[0] : value;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { registerProfileProviders } from './register_profile_providers';
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type {
DataSourceProfileService,
DocumentProfileService,
RootProfileService,
} from '../profiles';
import type { BaseProfileProvider, BaseProfileService } from '../profile_service';
import { exampleDataSourceProfileProvider } from './example_data_source_profile';
import { exampleDocumentProfileProvider } from './example_document_profile';
import { exampleRootProfileProvider } from './example_root_pofile';

export const registerProfileProviders = ({
rootProfileService,
dataSourceProfileService,
documentProfileService,
enabledProfileIds,
}: {
rootProfileService: RootProfileService;
dataSourceProfileService: DataSourceProfileService;
documentProfileService: DocumentProfileService;
enabledProfileIds: string[];
}) => {
const rootProfileProviders = [exampleRootProfileProvider];
const dataSourceProfileProviders = [exampleDataSourceProfileProvider];
const documentProfileProviders = [exampleDocumentProfileProvider];

registerEnabledProfileProviders({
profileService: rootProfileService,
availableProviders: rootProfileProviders,
enabledProfileIds,
});

registerEnabledProfileProviders({
profileService: dataSourceProfileService,
availableProviders: dataSourceProfileProviders,
enabledProfileIds,
});

registerEnabledProfileProviders({
profileService: documentProfileService,
availableProviders: documentProfileProviders,
enabledProfileIds,
});
};

const registerEnabledProfileProviders = <
TProvider extends BaseProfileProvider<{}>,
TService extends BaseProfileService<TProvider, {}>
>({
profileService,
availableProviders,
enabledProfileIds,
}: {
profileService: TService;
availableProviders: TProvider[];
enabledProfileIds: string[];
}) => {
for (const profile of availableProviders) {
if (enabledProfileIds.includes(profile.profileId)) {
profileService.registerProvider(profile);
}
}
};

This file was deleted.

Loading

0 comments on commit 831f324

Please sign in to comment.