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

[ML] AIOps: Fix to not run log rate analysis twice when no spike/dip detected. #180980

Merged
merged 15 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions x-pack/packages/ml/aiops_components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { DualBrush, DualBrushAnnotation } from './src/dual_brush';
export { ProgressControls } from './src/progress_controls';
export {
DocumentCountChart,
DocumentCountChartWithAutoAnalysisStart,
type BrushSettings,
type BrushSelectionUpdateHandler,
} from './src/document_count_chart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';

import { DualBrush, DualBrushAnnotation } from '../..';

import { useLogRateAnalysisStateContext } from '../log_rate_analysis_state_provider';

import { BrushBadge } from './brush_badge';

declare global {
Expand Down Expand Up @@ -118,9 +120,11 @@ export interface DocumentCountChartProps {
chartPointsSplitLabel: string;
/** Whether or not brush has been reset */
isBrushCleared: boolean;
/** Callback to set the autoRunAnalysis flag */
setAutoRunAnalysis?: (d: boolean) => void;
darnautov marked this conversation as resolved.
Show resolved Hide resolved
/** Timestamp for start of initial analysis */
autoAnalysisStart?: number | WindowParameters;
/** Optional style to override bar chart */
/** Optional style to override bar chart */
barStyleAccessor?: BarStyleAccessor;
/** Optional color override for the default bar color for charts */
barColorOverride?: string;
Expand Down Expand Up @@ -181,6 +185,7 @@ export const DocumentCountChart: FC<DocumentCountChartProps> = (props) => {
interval,
chartPointsSplitLabel,
isBrushCleared,
setAutoRunAnalysis,
autoAnalysisStart,
barColorOverride,
barStyleAccessor,
Expand Down Expand Up @@ -305,6 +310,15 @@ export const DocumentCountChart: FC<DocumentCountChartProps> = (props) => {
windowParameters === undefined &&
adjustedChartPoints !== undefined
) {
if (setAutoRunAnalysis) {
setAutoRunAnalysis(
typeof startRange === 'number' &&
changePoint !== undefined &&
startRange >= changePoint.startTs &&
startRange <= changePoint.endTs
);
}

const wp = getWindowParametersForTrigger(
startRange,
interval,
Expand Down Expand Up @@ -333,6 +347,7 @@ export const DocumentCountChart: FC<DocumentCountChartProps> = (props) => {
timeRangeLatest,
snapTimestamps,
originalWindowParameters,
setAutoRunAnalysis,
setWindowParameters,
brushSelectionUpdateHandler,
adjustedChartPoints,
Expand Down Expand Up @@ -535,3 +550,15 @@ export const DocumentCountChart: FC<DocumentCountChartProps> = (props) => {
</>
);
};

export const DocumentCountChartWithAutoAnalysisStart: FC<DocumentCountChartProps> = (props) => {
const { initialAnalysisStart, setAutoRunAnalysis } = useLogRateAnalysisStateContext();

return (
<DocumentCountChart
{...props}
autoAnalysisStart={initialAnalysisStart}
setAutoRunAnalysis={setAutoRunAnalysis}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 2.0.
*/

export { DocumentCountChart } from './document_count_chart';
export {
DocumentCountChart,
DocumentCountChartWithAutoAnalysisStart,
} from './document_count_chart';
export type {
BrushSelectionUpdateHandler,
BrushSettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export {
useLogRateAnalysisStateContext,
LogRateAnalysisStateProvider,
} from './log_rate_analysis_state_provider';
export type { GroupTableItem } from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ import React, {
} from 'react';

import type { SignificantItem } from '@kbn/ml-agg-utils';
import type { WindowParameters } from '@kbn/aiops-log-rate-analysis';

import type { GroupTableItem } from './types';

type InitialAnalysisStart = number | WindowParameters | undefined;
type SignificantItemOrNull = SignificantItem | null;
type GroupOrNull = GroupTableItem | null;

interface LogRateAnalysisResultsTableRow {
interface LogRateAnalysisState {
autoRunAnalysis: boolean;
setAutoRunAnalysis: Dispatch<SetStateAction<boolean>>;
initialAnalysisStart: InitialAnalysisStart;
setInitialAnalysisStart: Dispatch<SetStateAction<InitialAnalysisStart>>;
pinnedSignificantItem: SignificantItemOrNull;
setPinnedSignificantItem: Dispatch<SetStateAction<SignificantItemOrNull>>;
pinnedGroup: GroupOrNull;
Expand All @@ -36,12 +42,24 @@ interface LogRateAnalysisResultsTableRow {
clearAllRowState: () => void;
}

export const logRateAnalysisResultsTableRowContext = createContext<
LogRateAnalysisResultsTableRow | undefined
>(undefined);
export const logRateAnalysisStateContext = createContext<LogRateAnalysisState | undefined>(
darnautov marked this conversation as resolved.
Show resolved Hide resolved
undefined
);

export const LogRateAnalysisResultsTableRowStateProvider: FC = ({ children }) => {
// State that will be shared with all components
interface LogRateAnalysisStateProviderProps {
initialAnalysisStart?: InitialAnalysisStart;
}

export const LogRateAnalysisStateProvider: FC<LogRateAnalysisStateProviderProps> = ({
children,
initialAnalysisStart: incomingInitialAnalysisStart,
}) => {
const [autoRunAnalysis, setAutoRunAnalysis] = useState(true);
const [initialAnalysisStart, setInitialAnalysisStart] = useState<
number | WindowParameters | undefined
>(incomingInitialAnalysisStart);

// Row state that will be shared with all components
const [pinnedSignificantItem, setPinnedSignificantItem] = useState<SignificantItemOrNull>(null);
const [pinnedGroup, setPinnedGroup] = useState<GroupOrNull>(null);
const [selectedSignificantItem, setSelectedSignificantItem] =
Expand All @@ -66,8 +84,12 @@ export const LogRateAnalysisResultsTableRowStateProvider: FC = ({ children }) =>
}
}, [selectedGroup, pinnedGroup]);

const contextValue: LogRateAnalysisResultsTableRow = useMemo(
const contextValue: LogRateAnalysisState = useMemo(
() => ({
autoRunAnalysis,
setAutoRunAnalysis,
initialAnalysisStart,
setInitialAnalysisStart,
pinnedSignificantItem,
setPinnedSignificantItem,
pinnedGroup,
Expand All @@ -86,6 +108,10 @@ export const LogRateAnalysisResultsTableRowStateProvider: FC = ({ children }) =>
},
}),
[
autoRunAnalysis,
setAutoRunAnalysis,
initialAnalysisStart,
setInitialAnalysisStart,
pinnedSignificantItem,
setPinnedSignificantItem,
pinnedGroup,
Expand All @@ -101,19 +127,19 @@ export const LogRateAnalysisResultsTableRowStateProvider: FC = ({ children }) =>

return (
// Provider managing the state
<logRateAnalysisResultsTableRowContext.Provider value={contextValue}>
<logRateAnalysisStateContext.Provider value={contextValue}>
{children}
</logRateAnalysisResultsTableRowContext.Provider>
</logRateAnalysisStateContext.Provider>
);
};

export const useLogRateAnalysisResultsTableRowContext = () => {
const logRateAnalysisResultsTableRow = useContext(logRateAnalysisResultsTableRowContext);
export const useLogRateAnalysisStateContext = () => {
const logRateAnalysisState = useContext(logRateAnalysisStateContext);

// If `undefined`, throw an error.
if (logRateAnalysisResultsTableRow === undefined) {
throw new Error('useLogRateAnalysisResultsTableRowContext was used outside of its Provider');
if (logRateAnalysisState === undefined) {
throw new Error('useLogRateAnalysisStateContext was used outside of its Provider');
}

return logRateAnalysisResultsTableRow;
return logRateAnalysisState;
};
1 change: 1 addition & 0 deletions x-pack/packages/ml/aiops_components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@kbn/field-formats-plugin",
"@kbn/visualization-utils",
"@kbn/aiops-log-rate-analysis",
"@kbn/ml-agg-utils",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

import type { SignificantItem } from '@kbn/ml-agg-utils';

import type { GroupTableItem } from '../../components/log_rate_analysis_results_table/types';
import type { GroupTableItem } from '@kbn/aiops-components/src/log_rate_analysis_state_provider/types';

import { buildExtendedBaseFilterCriteria } from './build_extended_base_filter_criteria';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import type { Query } from '@kbn/es-query';
import { type SignificantItem, SIGNIFICANT_ITEM_TYPE } from '@kbn/ml-agg-utils';
import { buildBaseFilterCriteria } from '@kbn/ml-query-utils';
import { getCategoryQuery } from '@kbn/aiops-log-pattern-analysis/get_category_query';

import type { GroupTableItem } from '../../components/log_rate_analysis_results_table/types';
import type { GroupTableItem } from '@kbn/aiops-components/src/log_rate_analysis_state_provider/types';

/*
* Contains utility functions for building and processing queries.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import type {
} from '@elastic/charts/dist/chart_types/xy_chart/utils/specs';

import type { LogRateHistogramItem, WindowParameters } from '@kbn/aiops-log-rate-analysis';
import { DocumentCountChart, type BrushSelectionUpdateHandler } from '@kbn/aiops-components';
import {
DocumentCountChartWithAutoAnalysisStart,
type BrushSelectionUpdateHandler,
} from '@kbn/aiops-components';

import { useAiopsAppContext } from '../../../hooks/use_aiops_app_context';
import type { DocumentCountStats } from '../../../get_document_stats';
Expand All @@ -29,13 +32,11 @@ export interface DocumentCountContentProps {
isBrushCleared: boolean;
totalCount: number;
sampleProbability: number;
initialAnalysisStart?: number | WindowParameters;
/** Optional color override for the default bar color for charts */
barColorOverride?: string;
/** Optional color override for the highlighted bar color for charts */
barHighlightColorOverride?: string;
windowParameters?: WindowParameters;
incomingInitialAnalysisStart?: number | WindowParameters;
baselineLabel?: string;
deviationLabel?: string;
barStyleAccessor?: BarStyleAccessor;
Expand All @@ -51,11 +52,9 @@ export const DocumentCountContent: FC<DocumentCountContentProps> = ({
isBrushCleared,
totalCount,
sampleProbability,
initialAnalysisStart,
barColorOverride,
barHighlightColorOverride,
windowParameters,
incomingInitialAnalysisStart,
...docCountChartProps
}) => {
const { data, uiSettings, fieldFormats, charts } = useAiopsAppContext();
Expand Down Expand Up @@ -100,7 +99,7 @@ export const DocumentCountContent: FC<DocumentCountContentProps> = ({
</EuiFlexItem>
{documentCountStats.interval !== undefined && (
<EuiFlexItem>
<DocumentCountChart
<DocumentCountChartWithAutoAnalysisStart
dependencies={{ data, uiSettings, fieldFormats, charts }}
brushSelectionUpdateHandler={brushSelectionUpdateHandler}
chartPoints={chartPoints}
Expand All @@ -110,7 +109,6 @@ export const DocumentCountContent: FC<DocumentCountContentProps> = ({
interval={documentCountStats.interval}
chartPointsSplitLabel={documentCountStatsSplitLabel}
isBrushCleared={isBrushCleared}
autoAnalysisStart={initialAnalysisStart}
barColorOverride={barColorOverride}
barHighlightColorOverride={barHighlightColorOverride}
changePoint={documentCountStats.changePoint}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ 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 { LogRateAnalysisStateProvider } from '@kbn/aiops-components/src/log_rate_analysis_state_provider';
import type { AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import { AiopsAppContext } from '../../hooks/use_aiops_app_context';
import { DataSourceContext } from '../../hooks/use_data_source';
import { AIOPS_STORAGE_KEYS } from '../../types/storage';

import { LogRateAnalysisResultsTableRowStateProvider } from '../log_rate_analysis_results_table/log_rate_analysis_results_table_row_provider';

import { LogRateAnalysisPage } from './log_rate_analysis_page';
import { timeSeriesDataViewWarning } from '../../application/utils/time_series_dataview_check';

Expand Down Expand Up @@ -70,13 +69,13 @@ export const LogRateAnalysisAppState: FC<LogRateAnalysisAppStateProps> = ({
<AiopsAppContext.Provider value={appDependencies}>
<UrlStateProvider>
<DataSourceContext.Provider value={{ dataView, savedSearch }}>
<LogRateAnalysisResultsTableRowStateProvider>
<LogRateAnalysisStateProvider>
<StorageContextProvider storage={localStorage} storageKeys={AIOPS_STORAGE_KEYS}>
<DatePickerContextProvider {...datePickerDeps}>
<LogRateAnalysisPage stickyHistogram={stickyHistogram} />
</DatePickerContextProvider>
</StorageContextProvider>
</LogRateAnalysisResultsTableRowStateProvider>
</LogRateAnalysisStateProvider>
</DataSourceContext.Provider>
</UrlStateProvider>
</AiopsAppContext.Provider>
Expand Down
Loading