Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [ML] AIOps: Cleanup context/embeddingOrigin (#194442) #195284

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { METRIC_TYPE, UiCounterMetricType } from '@kbn/analytics';
import { type EmbeddablePatternAnalysisInput } from '@kbn/aiops-log-pattern-analysis/embeddable';
import { pick } from 'lodash';
import type { LogCategorizationEmbeddableProps } from '@kbn/aiops-plugin/public/components/log_categorization/log_categorization_for_embeddable/log_categorization_for_discover';
import type { AiopsAppContextValue } from '@kbn/aiops-plugin/public/hooks/use_aiops_app_context';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import type { DiscoverStateContainer } from '../../state_management/discover_state';
import { PATTERN_ANALYSIS_LOADED } from './constants';
Expand Down Expand Up @@ -63,8 +64,11 @@ export const PatternAnalysisTable = (props: PatternAnalysisTableProps) => {
return (
<aiopsService.PatternAnalysisComponent
props={patternAnalysisComponentProps}
deps={services}
embeddingOrigin="discover"
appContextValue={
// TODO We shouldn't cast to `unknown` here, goal is to use feature specific contexts.
// See https://github.com/elastic/kibana/pull/194442
{ embeddingOrigin: 'discover', ...services } as unknown as AiopsAppContextValue
}
/>
);
};
16 changes: 11 additions & 5 deletions x-pack/packages/ml/aiops_common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ export const AIOPS_API_ENDPOINT = {
CATEGORIZATION_FIELD_VALIDATION: '/internal/aiops/categorization_field_validation',
} as const;

export const AIOPS_TELEMETRY_ID = {
AIOPS_DEFAULT_SOURCE: 'ml_aiops_labs',
AIOPS_ANALYSIS_RUN_ORIGIN: 'aiops-analysis-run-origin',
} as const;
/**
* Used for telemetry purposes to track the origin of the analysis run.
*/
export const AIOPS_ANALYSIS_RUN_ORIGIN = 'aiops-analysis-run-origin';

export const EMBEDDABLE_ORIGIN = 'embeddable';
export const AIOPS_EMBEDDABLE_ORIGIN = {
CASES: 'cases',
DASHBOARD: 'dashboard',
DEFAULT: 'embeddable',
DISCOVER: 'discover',
ML_AIOPS_LABS: 'ml_aiops_labs',
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import {
mlTimefilterRefresh$,
} from '@kbn/ml-date-picker';
import { UI_SETTINGS } from '@kbn/data-plugin/common';
import { AIOPS_TELEMETRY_ID } from '@kbn/aiops-common/constants';
import { AIOPS_EMBEDDABLE_ORIGIN } from '@kbn/aiops-common/constants';

import { DataSourceContext } from '../../hooks/use_data_source';
import type { AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import type { AiopsAppContextValue } from '../../hooks/use_aiops_app_context';
import { AiopsAppContext } from '../../hooks/use_aiops_app_context';
import { AIOPS_STORAGE_KEYS } from '../../types/storage';

Expand All @@ -52,20 +52,20 @@ export interface ChangePointDetectionAppStateProps {
dataView: DataView;
/** The saved search to analyze. */
savedSearch: SavedSearch | null;
/** App dependencies */
appDependencies: AiopsAppDependencies;
/** App context value */
appContextValue: AiopsAppContextValue;
/** Optional flag to indicate whether kibana is running in serverless */
showFrozenDataTierChoice?: boolean;
}

export const ChangePointDetectionAppState: FC<ChangePointDetectionAppStateProps> = ({
dataView,
savedSearch,
appDependencies,
appContextValue,
showFrozenDataTierChoice = true,
}) => {
const datePickerDeps: DatePickerDependencies = {
...pick(appDependencies, ['data', 'http', 'notifications', 'theme', 'uiSettings', 'i18n']),
...pick(appContextValue, ['data', 'http', 'notifications', 'theme', 'uiSettings', 'i18n']),
uiSettingsKeys: UI_SETTINGS,
showFrozenDataTierChoice,
};
Expand All @@ -80,19 +80,19 @@ export const ChangePointDetectionAppState: FC<ChangePointDetectionAppStateProps>
return <>{warning}</>;
}

appDependencies.embeddingOrigin = AIOPS_TELEMETRY_ID.AIOPS_DEFAULT_SOURCE;
appContextValue.embeddingOrigin = AIOPS_EMBEDDABLE_ORIGIN.ML_AIOPS_LABS;

const PresentationContextProvider =
appDependencies.presentationUtil?.ContextProvider ?? React.Fragment;
appContextValue.presentationUtil?.ContextProvider ?? React.Fragment;

const CasesContext = appDependencies.cases?.ui.getCasesContext() ?? React.Fragment;
const casesPermissions = appDependencies.cases?.helpers.canUseCases();
const CasesContext = appContextValue.cases?.ui.getCasesContext() ?? React.Fragment;
const casesPermissions = appContextValue.cases?.helpers.canUseCases();

return (
<PresentationContextProvider>
<KibanaThemeProvider theme={appDependencies.theme}>
<KibanaThemeProvider theme={appContextValue.theme}>
<CasesContext owner={[]} permissions={casesPermissions!}>
<AiopsAppContext.Provider value={appDependencies}>
<AiopsAppContext.Provider value={appContextValue}>
<UrlStateProvider>
<DataSourceContext.Provider value={{ dataView, savedSearch }}>
<StorageContextProvider storage={localStorage} storageKeys={AIOPS_STORAGE_KEYS}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import { UrlStateProvider } from '@kbn/ml-url-state';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { DatePickerContextProvider, type DatePickerDependencies } from '@kbn/ml-date-picker';
import { UI_SETTINGS } from '@kbn/data-plugin/common';
import { AIOPS_TELEMETRY_ID } from '@kbn/aiops-common/constants';

import { DataSourceContext } from '../../hooks/use_data_source';
import type { AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import type { AiopsAppContextValue } from '../../hooks/use_aiops_app_context';
import { AIOPS_STORAGE_KEYS } from '../../types/storage';
import { AiopsAppContext } from '../../hooks/use_aiops_app_context';

Expand All @@ -35,16 +34,16 @@ export interface LogCategorizationAppStateProps {
dataView: DataView;
/** The saved search to analyze. */
savedSearch: SavedSearch | null;
/** App dependencies */
appDependencies: AiopsAppDependencies;
/** App context value */
appContextValue: AiopsAppContextValue;
/** Optional flag to indicate whether kibana is running in serverless */
showFrozenDataTierChoice?: boolean;
}

export const LogCategorizationAppState: FC<LogCategorizationAppStateProps> = ({
dataView,
savedSearch,
appDependencies,
appContextValue,
showFrozenDataTierChoice = true,
}) => {
if (!dataView) return null;
Expand All @@ -56,18 +55,18 @@ export const LogCategorizationAppState: FC<LogCategorizationAppStateProps> = ({
}

const datePickerDeps: DatePickerDependencies = {
...pick(appDependencies, ['data', 'http', 'notifications', 'theme', 'uiSettings', 'i18n']),
...pick(appContextValue, ['data', 'http', 'notifications', 'theme', 'uiSettings', 'i18n']),
uiSettingsKeys: UI_SETTINGS,
showFrozenDataTierChoice,
};

return (
<AiopsAppContext.Provider value={appDependencies}>
<AiopsAppContext.Provider value={appContextValue}>
<UrlStateProvider>
<DataSourceContext.Provider value={{ dataView, savedSearch }}>
<StorageContextProvider storage={localStorage} storageKeys={AIOPS_STORAGE_KEYS}>
<DatePickerContextProvider {...datePickerDeps}>
<LogCategorizationPage embeddingOrigin={AIOPS_TELEMETRY_ID.AIOPS_DEFAULT_SOURCE} />
<LogCategorizationPage />
</DatePickerContextProvider>
</StorageContextProvider>
</DataSourceContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { FieldValidationResults } from '@kbn/ml-category-validator';
import type { Category } from '@kbn/aiops-log-pattern-analysis/types';

import type { CategorizationAdditionalFilter } from '@kbn/aiops-log-pattern-analysis/create_category_request';
import { AIOPS_TELEMETRY_ID } from '@kbn/aiops-common/constants';
import { AIOPS_ANALYSIS_RUN_ORIGIN } from '@kbn/aiops-common/constants';
import type { EmbeddablePatternAnalysisInput } from '@kbn/aiops-log-pattern-analysis/embeddable';
import { css } from '@emotion/react';
import { useTableState } from '@kbn/ml-in-memory-table/hooks/use_table_state';
Expand Down Expand Up @@ -270,7 +270,7 @@ export const LogCategorizationDiscover: FC<LogCategorizationEmbeddableProps> = (
searchQuery,
runtimeMappings,
{
[AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
[AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
}
),
runCategorizeRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,40 @@

import type { FC } from 'react';
import React, { Suspense } from 'react';
import type { ThemeServiceStart } from '@kbn/core-theme-browser';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { IUiSettingsClient } from '@kbn/core/public';
import type { CoreStart } from '@kbn/core/public';
import type { LensPublicStart } from '@kbn/lens-plugin/public';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { ChartsPluginStart } from '@kbn/charts-plugin/public';
import type { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import { pick } from 'lodash';

import { DatePickerContextProvider } from '@kbn/ml-date-picker';
import { StorageContextProvider } from '@kbn/ml-local-storage';

import { pick } from 'lodash';
import { UI_SETTINGS } from '@kbn/data-plugin/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';

import { AIOPS_STORAGE_KEYS } from '../../../types/storage';
import type { AiopsAppDependencies } from '../../../hooks/use_aiops_app_context';
import { AiopsAppContext } from '../../../hooks/use_aiops_app_context';
import { AiopsAppContext, type AiopsAppContextValue } from '../../../hooks/use_aiops_app_context';

import type { LogCategorizationEmbeddableProps } from './log_categorization_for_discover';
import { LogCategorizationDiscover } from './log_categorization_for_discover';

export interface EmbeddableLogCategorizationDeps {
theme: ThemeServiceStart;
data: DataPublicPluginStart;
uiSettings: IUiSettingsClient;
http: CoreStart['http'];
notifications: CoreStart['notifications'];
i18n: CoreStart['i18n'];
lens: LensPublicStart;
fieldFormats: FieldFormatsStart;
application: CoreStart['application'];
charts: ChartsPluginStart;
uiActions: UiActionsStart;
}

export interface LogCategorizationEmbeddableWrapperProps {
deps: EmbeddableLogCategorizationDeps;
appContextValue: AiopsAppContextValue;
props: LogCategorizationEmbeddableProps;
embeddingOrigin?: string;
}

const localStorage = new Storage(window.localStorage);

export const LogCategorizationDiscoverWrapper: FC<LogCategorizationEmbeddableWrapperProps> = ({
deps,
appContextValue,
props,
embeddingOrigin,
}) => {
const I18nContext = deps.i18n.Context;
const aiopsAppContextValue = {
embeddingOrigin,
...deps,
} as unknown as AiopsAppDependencies;
const I18nContext = appContextValue.i18n.Context;

const datePickerDeps = {
...pick(deps, ['data', 'http', 'notifications', 'theme', 'uiSettings', 'i18n']),
...pick(appContextValue, ['data', 'http', 'notifications', 'theme', 'uiSettings', 'i18n']),
uiSettingsKeys: UI_SETTINGS,
};

return (
<I18nContext>
<AiopsAppContext.Provider value={aiopsAppContextValue}>
<AiopsAppContext.Provider value={appContextValue}>
<DatePickerContextProvider {...datePickerDeps}>
<StorageContextProvider storage={localStorage} storageKeys={AIOPS_STORAGE_KEYS}>
<Suspense fallback={null}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Category } from '@kbn/aiops-log-pattern-analysis/types';
import type { CategorizationAdditionalFilter } from '@kbn/aiops-log-pattern-analysis/create_category_request';
import type { EmbeddablePatternAnalysisInput } from '@kbn/aiops-log-pattern-analysis/embeddable';
import { useTableState } from '@kbn/ml-in-memory-table/hooks/use_table_state';
import { AIOPS_TELEMETRY_ID } from '@kbn/aiops-common/constants';
import { AIOPS_ANALYSIS_RUN_ORIGIN } from '@kbn/aiops-common/constants';
import datemath from '@elastic/datemath';
import useMountedState from 'react-use/lib/useMountedState';
import { useFilterQueryUpdates } from '../../../hooks/use_filters_query';
Expand Down Expand Up @@ -224,7 +224,7 @@ export const LogCategorizationEmbeddable: FC<LogCategorizationEmbeddableProps> =
searchQuery,
runtimeMappings,
{
[AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
[AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
}
),
runCategorizeRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { usePageUrlState } from '@kbn/ml-url-state';
import type { FieldValidationResults } from '@kbn/ml-category-validator';
import { AIOPS_TELEMETRY_ID } from '@kbn/aiops-common/constants';
import { AIOPS_ANALYSIS_RUN_ORIGIN } from '@kbn/aiops-common/constants';
import type { CategorizationAdditionalFilter } from '@kbn/aiops-log-pattern-analysis/create_category_request';
import type { Category } from '@kbn/aiops-log-pattern-analysis/types';

Expand Down Expand Up @@ -64,8 +64,6 @@ export interface LogCategorizationPageProps {
savedSearch: SavedSearch | null;
selectedField: DataViewField;
onClose: () => void;
/** Identifier to indicate the plugin utilizing the component */
embeddingOrigin: string;
additionalFilter?: CategorizationAdditionalFilter;
}

Expand All @@ -76,7 +74,6 @@ export const LogCategorizationFlyout: FC<LogCategorizationPageProps> = ({
savedSearch,
selectedField,
onClose,
embeddingOrigin,
additionalFilter,
}) => {
const {
Expand All @@ -85,6 +82,7 @@ export const LogCategorizationFlyout: FC<LogCategorizationPageProps> = ({
query: { getState },
},
uiSettings,
embeddingOrigin,
} = useAiopsAppContext();

const { runValidateFieldRequest, cancelRequest: cancelValidationRequest } =
Expand Down Expand Up @@ -198,7 +196,7 @@ export const LogCategorizationFlyout: FC<LogCategorizationPageProps> = ({
searchQuery,
runtimeMappings,
{
[AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
[AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
}
),
runCategorizeRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import { FormattedMessage } from '@kbn/i18n-react';
import { usePageUrlState, useUrlState } from '@kbn/ml-url-state';
import type { FieldValidationResults } from '@kbn/ml-category-validator';
import type { SearchQueryLanguage } from '@kbn/ml-query-utils';
import { AIOPS_TELEMETRY_ID } from '@kbn/aiops-common/constants';
import { AIOPS_ANALYSIS_RUN_ORIGIN } from '@kbn/aiops-common/constants';
import type { Category } from '@kbn/aiops-log-pattern-analysis/types';

import { useTableState } from '@kbn/ml-in-memory-table/hooks/use_table_state';

import { useDataSource } from '../../hooks/use_data_source';
import { useData } from '../../hooks/use_data';
import { useSearch } from '../../hooks/use_search';
Expand Down Expand Up @@ -59,14 +59,10 @@ import { useActions } from './category_table/use_actions';
const BAR_TARGET = 20;
const DEFAULT_SELECTED_FIELD = 'message';

interface LogCategorizationPageProps {
/** Identifier to indicate the plugin utilizing the component */
embeddingOrigin: string;
}

export const LogCategorizationPage: FC<LogCategorizationPageProps> = ({ embeddingOrigin }) => {
export const LogCategorizationPage: FC = () => {
const {
notifications: { toasts },
embeddingOrigin,
} = useAiopsAppContext();
const { dataView, savedSearch } = useDataSource();

Expand Down Expand Up @@ -229,7 +225,7 @@ export const LogCategorizationPage: FC<LogCategorizationPageProps> = ({ embeddin
searchQuery,
runtimeMappings,
{
[AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
[AIOPS_ANALYSIS_RUN_ORIGIN]: embeddingOrigin,
}
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { StorageContextProvider } from '@kbn/ml-local-storage';
import type { CategorizationAdditionalFilter } from '@kbn/aiops-log-pattern-analysis/create_category_request';
import type { AiopsPluginStartDeps } from '../../types';
import { LogCategorizationFlyout } from './log_categorization_for_flyout';
import { AiopsAppContext, type AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import { AiopsAppContext, type AiopsAppContextValue } from '../../hooks/use_aiops_app_context';
import { AIOPS_STORAGE_KEYS } from '../../types/storage';

const localStorage = new Storage(window.localStorage);
Expand All @@ -43,13 +43,14 @@ export async function showCategorizeFlyout(
resolve();
};

const appDependencies: AiopsAppDependencies = {
const appContextValue: AiopsAppContextValue = {
embeddingOrigin: originatingApp,
...coreStart,
...plugins,
};
const startServices = pick(coreStart, 'analytics', 'i18n', 'theme');
const datePickerDeps: DatePickerDependencies = {
...pick(appDependencies, ['data', 'http', 'notifications', 'theme', 'uiSettings']),
...pick(appContextValue, ['data', 'http', 'notifications', 'theme', 'uiSettings']),
i18n,
uiSettingsKeys: UI_SETTINGS,
};
Expand All @@ -61,15 +62,14 @@ export async function showCategorizeFlyout(
...coreStart,
}}
>
<AiopsAppContext.Provider value={appDependencies}>
<AiopsAppContext.Provider value={appContextValue}>
<DatePickerContextProvider {...datePickerDeps}>
<StorageContextProvider storage={localStorage} storageKeys={AIOPS_STORAGE_KEYS}>
<LogCategorizationFlyout
dataView={dataView}
savedSearch={null}
selectedField={field}
onClose={onFlyoutClose}
embeddingOrigin={originatingApp}
additionalFilter={additionalFilter}
/>
</StorageContextProvider>
Expand Down
Loading