Skip to content

Commit

Permalink
[ML] AIOps: Additional props for Change Point embeddable (#167606)
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Sep 29, 2023
1 parent ee1f448 commit 772739a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { EuiLoadingChart } from '@elastic/eui';
import { EMBEDDABLE_CHANGE_POINT_CHART_TYPE } from '../../common/constants';
import type { AiopsPluginStartDeps } from '../types';
import type { EmbeddableChangePointChartInput } from './embeddable_change_point_chart';
import type { ChangePointAnnotation } from '../components/change_point_detection/change_point_detection_context';

export interface EmbeddableChangePointChartProps {
dataViewId: string;
Expand All @@ -27,6 +28,18 @@ export interface EmbeddableChangePointChartProps {
splitField?: string;
partitions?: string[];
maxSeriesToPlot?: number;
/**
* Component to render if there are no change points found
*/
emptyState?: React.ReactElement;
/**
* Outputs the most recent change point data
*/
onChange?: (changePointData: ChangePointAnnotation[]) => void;
/**
* Last reload request time, can be used for manual reload
*/
lastReloadRequestTime?: number;
}

export function getEmbeddableChangePointChart(core: CoreStart, plugins: AiopsPluginStartDeps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* 2.0.
*/

import { type Observable } from 'rxjs';
import React, { FC, useEffect, useMemo } from 'react';
import { BehaviorSubject, type Observable, combineLatest } from 'rxjs';
import { map, distinctUntilChanged } from 'rxjs/operators';
import React, { FC, useEffect, useMemo, useState } from 'react';
import { useTimefilter } from '@kbn/ml-date-picker';
import { css } from '@emotion/react';
import useObservable from 'react-use/lib/useObservable';
Expand Down Expand Up @@ -55,8 +56,31 @@ export const EmbeddableInputTracker: FC<EmbeddableInputTrackerProps> = ({
}) => {
const input = useObservable(input$, initialInput);

const [manualReload$] = useState<BehaviorSubject<number>>(
new BehaviorSubject<number>(initialInput.lastReloadRequestTime ?? Date.now())
);

useEffect(
function updateManualReloadSubject() {
if (
input.lastReloadRequestTime === initialInput.lastReloadRequestTime ||
!input.lastReloadRequestTime
)
return;
manualReload$.next(input.lastReloadRequestTime);
},
[input.lastReloadRequestTime, initialInput.lastReloadRequestTime, manualReload$]
);

const resultObservable$ = useMemo<Observable<number>>(() => {
return combineLatest([reload$, manualReload$]).pipe(
map(([reload, manualReload]) => Math.max(reload, manualReload)),
distinctUntilChanged()
);
}, [manualReload$, reload$]);

return (
<ReloadContextProvider reload$={reload$}>
<ReloadContextProvider reload$={resultObservable$}>
<DataSourceContextProvider dataViewId={input.dataViewId}>
<FilterQueryContextProvider timeRange={input.timeRange}>
<ChartGridEmbeddableWrapper
Expand All @@ -70,6 +94,8 @@ export const EmbeddableInputTracker: FC<EmbeddableInputTrackerProps> = ({
onLoading={onLoading}
onRenderComplete={onRenderComplete}
onError={onError}
onChange={input.onChange}
emptyState={input.emptyState}
/>
</FilterQueryContextProvider>
</DataSourceContextProvider>
Expand Down Expand Up @@ -103,6 +129,8 @@ export const ChartGridEmbeddableWrapper: FC<
onError,
onLoading,
onRenderComplete,
onChange,
emptyState,
}) => {
const { filters, query, timeRange } = useFilerQueryUpdates();

Expand Down Expand Up @@ -189,8 +217,12 @@ export const ChartGridEmbeddableWrapper: FC<
resultChangePoints = resultChangePoints.slice(0, maxSeriesToPlot);
}

if (onChange) {
onChange(resultChangePoints);
}

return resultChangePoints;
}, [results, maxSeriesToPlot]);
}, [results, maxSeriesToPlot, onChange]);

return (
<div
Expand All @@ -205,6 +237,8 @@ export const ChartGridEmbeddableWrapper: FC<
interval={requestParams.interval}
onRenderComplete={onRenderComplete}
/>
) : emptyState ? (
emptyState
) : (
<NoChangePointsWarning />
)}
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/aiops/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import type { UiActionsStart, UiActionsSetup } from '@kbn/ui-actions-plugin/publ
import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public';
import type { EmbeddableSetup, EmbeddableStart } from '@kbn/embeddable-plugin/public';
import type { CasesUiSetup } from '@kbn/cases-plugin/public';
import { LicensingPluginSetup } from '@kbn/licensing-plugin/public';
import type { EmbeddableChangePointChartProps } from './embeddable';
import type { LicensingPluginSetup } from '@kbn/licensing-plugin/public';
import type { EmbeddableChangePointChartInput } from './embeddable/embeddable_change_point_chart';

export interface AiopsPluginSetupDeps {
embeddable: EmbeddableSetup;
Expand All @@ -44,5 +44,5 @@ export interface AiopsPluginStartDeps {

export type AiopsPluginSetup = void;
export interface AiopsPluginStart {
EmbeddableChangePointChart: React.ComponentType<EmbeddableChangePointChartProps>;
EmbeddableChangePointChart: React.ComponentType<EmbeddableChangePointChartInput>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,19 @@ export default function AlertDetailsAppSection({
<EuiSpacer size="l" />
<EuiFlexGroup direction="column" data-test-subj="thresholdAlertRelatedEventsSection">
{ruleParams.criteria.map((criterion, criterionIndex) =>
criterion.metrics?.map(
(metric, metricIndex) =>
dataView &&
dataView.id && (
<EmbeddableChangePointChart
key={`embeddableChart-criterion${criterionIndex}-metric${metricIndex}`}
dataViewId={dataView.id}
timeRange={relatedEventsTimeRange(criterion)}
fn={metric.aggType ?? ''}
metricField={metric.field ?? ''}
/>
)
)
criterion.metrics?.map((metric, metricIndex) => {
const id = `embeddableChart-criterion${criterionIndex}-metric${metricIndex}`;
return dataView?.id ? (
<EmbeddableChangePointChart
id={id}
key={id}
dataViewId={dataView.id}
timeRange={relatedEventsTimeRange(criterion)}
fn={metric.aggType ?? ''}
metricField={metric.field ?? ''}
/>
) : null;
})
)}
</EuiFlexGroup>
</>
Expand Down

0 comments on commit 772739a

Please sign in to comment.