Skip to content

Commit

Permalink
[ML] Add MlAnnotationUpdatesContext.Provider, rename file
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Nov 17, 2020
1 parent 0cf7ad0 commit 2165da7
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Annotation } from '../../../../../common/types/annotations';
import { AnnotationUpdatesService } from '../../../services/annotations_service';

import { AnnotationFlyout } from './index';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/use_ml_annotation_updates';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/ml_annotation_updates_context';

jest.mock('../../../util/dependency_cache', () => ({
getToastNotifications: () => ({ addSuccess: jest.fn(), addDanger: jest.fn() }),
Expand All @@ -24,7 +24,7 @@ const annotationUpdatesService = new AnnotationUpdatesService();

const MlAnnotationUpdatesContextProvider = ({ children }: { children: React.ReactElement }) => {
return (
<MlAnnotationUpdatesContext.Provider value={{ annotationUpdatesService }}>
<MlAnnotationUpdatesContext.Provider value={annotationUpdatesService}>
<IntlProvider>{children}</IntlProvider>
</MlAnnotationUpdatesContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
} from '../../../../../common/types/annotations';
import { PartitionFieldsType } from '../../../../../common/types/anomalies';
import { PARTITION_FIELDS } from '../../../../../common/constants/anomalies';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/use_ml_annotation_updates';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/ml_annotation_updates_context';

interface ViewableDetector {
index: number;
Expand Down Expand Up @@ -415,7 +415,7 @@ export class AnnotationFlyoutUI extends Component<CommonProps & Props> {
}

export const AnnotationFlyout: FC<any> = (props) => {
const { annotationUpdatesService } = useContext(MlAnnotationUpdatesContext);
const annotationUpdatesService = useContext(MlAnnotationUpdatesContext);
const annotationProp = useObservable(annotationUpdatesService.isAnnotationInitialized$());

const cancelEditingHandler = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { withKibana } from '../../../../../../../../src/plugins/kibana_react/pub
import { ML_APP_URL_GENERATOR, ML_PAGES } from '../../../../../common/constants/ml_url_generator';
import { PLUGIN_ID } from '../../../../../common/constants/app';
import { timeFormatter } from '../../../../../common/util/date_utils';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/use_ml_annotation_updates';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/ml_annotation_updates_context';

const CURRENT_SERIES = 'current_series';
/**
Expand Down Expand Up @@ -695,6 +695,6 @@ class AnnotationsTableUI extends Component {
}

export const AnnotationsTable = withKibana((props) => {
const { annotationUpdatesService } = useContext(MlAnnotationUpdatesContext);
const annotationUpdatesService = useContext(MlAnnotationUpdatesContext);
return <AnnotationsTableUI annotationUpdatesService={annotationUpdatesService} {...props} />;
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import { createContext } from 'react';
import { AnnotationUpdatesService } from '../../services/annotations_service';

export interface MlAnnotationUpdatesContextValue {
annotationUpdatesService: AnnotationUpdatesService;
}
export type MlAnnotationUpdatesContextValue = AnnotationUpdatesService;

export const MlAnnotationUpdatesContext = createContext<MlAnnotationUpdatesContextValue>({
annotationUpdatesService: new AnnotationUpdatesService(),
});
export const MlAnnotationUpdatesContext = createContext<MlAnnotationUpdatesContextValue>(
new AnnotationUpdatesService()
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { FC, useEffect, useState, useCallback } from 'react';
import React, { FC, useEffect, useState, useCallback, useMemo } from 'react';
import useObservable from 'react-use/lib/useObservable';

import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -34,6 +34,8 @@ import { getBreadcrumbWithUrlForApp } from '../breadcrumbs';
import { useTimefilter } from '../../contexts/kibana';
import { isViewBySwimLaneData } from '../../explorer/swimlane_container';
import { JOB_ID } from '../../../../common/constants/anomalies';
import { MlAnnotationUpdatesContext } from '../../contexts/ml/ml_annotation_updates_context';
import { AnnotationUpdatesService } from '../../services/annotations_service';

export const explorerRouteFactory = (
navigateToPath: NavigateToPath,
Expand All @@ -59,10 +61,13 @@ const PageWrapper: FC<PageProps> = ({ deps }) => {
jobs: mlJobService.loadJobsWrapper,
jobsWithTimeRange: () => ml.jobs.jobsWithTimerange(getDateFormatTz()),
});
const annotationUpdatesService = useMemo(() => new AnnotationUpdatesService(), []);

return (
<PageLoader context={context}>
<ExplorerUrlStateManager jobsWithTimeRange={results.jobsWithTimeRange.jobs} />
<MlAnnotationUpdatesContext.Provider value={annotationUpdatesService}>
<ExplorerUrlStateManager jobsWithTimeRange={results.jobsWithTimeRange.jobs} />
</MlAnnotationUpdatesContext.Provider>
</PageLoader>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useEffect, FC } from 'react';
import React, { useEffect, FC, useMemo } from 'react';
import useObservable from 'react-use/lib/useObservable';
import { i18n } from '@kbn/i18n';

Expand All @@ -19,6 +19,8 @@ import { basicResolvers } from '../resolvers';
import { JobsPage } from '../../jobs/jobs_list';
import { useTimefilter } from '../../contexts/kibana';
import { getBreadcrumbWithUrlForApp } from '../breadcrumbs';
import { AnnotationUpdatesService } from '../../services/annotations_service';
import { MlAnnotationUpdatesContext } from '../../contexts/ml/ml_annotation_updates_context';

export const jobListRouteFactory = (navigateToPath: NavigateToPath, basePath: string): MlRoute => ({
path: '/jobs',
Expand Down Expand Up @@ -57,10 +59,13 @@ const PageWrapper: FC<PageProps> = ({ deps }) => {
setGlobalState({ refreshInterval });
timefilter.setRefreshInterval(refreshInterval);
}, []);
const annotationUpdatesService = useMemo(() => new AnnotationUpdatesService(), []);

return (
<PageLoader context={context}>
<JobsPage blockRefresh={blockRefresh} lastRefresh={lastRefresh} />
<MlAnnotationUpdatesContext.Provider value={annotationUpdatesService}>
<JobsPage blockRefresh={blockRefresh} lastRefresh={lastRefresh} />
</MlAnnotationUpdatesContext.Provider>
</PageLoader>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { isEqual } from 'lodash';
import React, { FC, useCallback, useEffect, useState } from 'react';
import React, { FC, useCallback, useEffect, useMemo, useState } from 'react';
import usePrevious from 'react-use/lib/usePrevious';
import moment from 'moment';

Expand Down Expand Up @@ -39,7 +39,8 @@ import { basicResolvers } from '../resolvers';
import { getBreadcrumbWithUrlForApp } from '../breadcrumbs';
import { useTimefilter } from '../../contexts/kibana';
import { useToastNotificationService } from '../../services/toast_notification_service';

import { AnnotationUpdatesService } from '../../services/annotations_service';
import { MlAnnotationUpdatesContext } from '../../contexts/ml/ml_annotation_updates_context';
export const timeSeriesExplorerRouteFactory = (
navigateToPath: NavigateToPath,
basePath: string
Expand All @@ -64,13 +65,16 @@ const PageWrapper: FC<PageProps> = ({ deps }) => {
jobs: mlJobService.loadJobsWrapper,
jobsWithTimeRange: () => ml.jobs.jobsWithTimerange(getDateFormatTz()),
});
const annotationUpdatesService = useMemo(() => new AnnotationUpdatesService(), []);

return (
<PageLoader context={context}>
<TimeSeriesExplorerUrlStateManager
config={deps.config}
jobsWithTimeRange={results.jobsWithTimeRange.jobs}
/>
<MlAnnotationUpdatesContext.Provider value={annotationUpdatesService}>
<TimeSeriesExplorerUrlStateManager
config={deps.config}
jobsWithTimeRange={results.jobsWithTimeRange.jobs}
/>
</MlAnnotationUpdatesContext.Provider>
</PageLoader>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
unhighlightFocusChartAnnotation,
ANNOTATION_MIN_WIDTH,
} from './timeseries_chart_annotations';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/use_ml_annotation_updates';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/ml_annotation_updates_context';

const focusZoomPanelHeight = 25;
const focusChartHeight = 310;
Expand Down Expand Up @@ -1799,7 +1799,7 @@ class TimeseriesChartIntl extends Component {
}

export const TimeseriesChart = (props) => {
const { annotationUpdatesService } = useContext(MlAnnotationUpdatesContext);
const annotationUpdatesService = useContext(MlAnnotationUpdatesContext);
const annotationProp = useObservable(annotationUpdatesService.isAnnotationInitialized$());

if (annotationProp === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useMlKibana, useNotifications } from '../../../contexts/kibana';
import { getBoundsRoundedToInterval } from '../../../util/time_buckets';
import { ANNOTATION_EVENT_USER } from '../../../../../common/constants/annotations';
import { getControlsForDetector } from '../../get_controls_for_detector';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/use_ml_annotation_updates';
import { MlAnnotationUpdatesContext } from '../../../contexts/ml/ml_annotation_updates_context';

interface TimeSeriesChartWithTooltipsProps {
bounds: any;
Expand Down Expand Up @@ -51,7 +51,7 @@ export const TimeSeriesChartWithTooltips: FC<TimeSeriesChartWithTooltipsProps> =
},
} = useMlKibana();

const { annotationUpdatesService } = useContext(MlAnnotationUpdatesContext);
const annotationUpdatesService = useContext(MlAnnotationUpdatesContext);

const [annotationData, setAnnotationData] = useState<Annotation[]>([]);

Expand Down

0 comments on commit 2165da7

Please sign in to comment.