Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Aug 2, 2024
1 parent e8d680d commit ebcb88c
Show file tree
Hide file tree
Showing 25 changed files with 220 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Mock the services required for reading and writing job data.
jest.mock('../../services/job_service', () => ({
mlJobService: {
mlJobServiceFactory: () => ({
getJob: () => {
return {
job_id: 'farequote_no_by',
Expand Down Expand Up @@ -43,9 +43,8 @@ jest.mock('../../services/job_service', () => ({
},
};
},
},
}),
}));
jest.mock('../../services/ml_api_service', () => 'ml');
jest.mock('../../capabilities/check_capabilities', () => ({
checkPermission: () => true,
}));
Expand Down Expand Up @@ -93,6 +92,7 @@ function prepareTest() {
},
},
},
mlServices: { mlApiServices: {} },
notifications: {
toasts: {
addDanger: () => {},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
EuiButton,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useMlKibana } from '../../../../contexts/kibana';
import type { MlSummaryJob } from '../../../../../../common/types/anomaly_detection_jobs';
import { isManagedJob } from '../../../jobs_utils';
import { useMlJobService } from '../../../../services/job_service';
Expand All @@ -38,6 +39,11 @@ export const CloseJobsConfirmModal: FC<Props> = ({
unsetShowFunction,
refreshJobs,
}) => {
const {
services: {
notifications: { toasts },
},
} = useMlKibana();
const mlJobService = useMlJobService();
const [modalVisible, setModalVisible] = useState(false);
const [hasManagedJob, setHasManaged] = useState(true);
Expand Down Expand Up @@ -115,7 +121,7 @@ export const CloseJobsConfirmModal: FC<Props> = ({

<EuiButton
onClick={() => {
closeJobs(mlJobService, jobsToReset, refreshJobs);
closeJobs(toasts, mlJobService, jobsToReset, refreshJobs);
closeModal();
}}
fill
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
EuiButton,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useMlKibana } from '../../../../contexts/kibana';
import type { MlSummaryJob } from '../../../../../../common/types/anomaly_detection_jobs';
import { isManagedJob } from '../../../jobs_utils';
import { useMlJobService } from '../../../../services/job_service';
Expand All @@ -39,6 +40,11 @@ export const StopDatafeedsConfirmModal: FC<Props> = ({
unsetShowFunction,
refreshJobs,
}) => {
const {
services: {
notifications: { toasts },
},
} = useMlKibana();
const mlJobService = useMlJobService();
const [modalVisible, setModalVisible] = useState(false);
const [hasManagedJob, setHasManaged] = useState(true);
Expand Down Expand Up @@ -116,7 +122,7 @@ export const StopDatafeedsConfirmModal: FC<Props> = ({

<EuiButton
onClick={() => {
stopDatafeeds(mlJobService, jobsToStop, refreshJobs);
stopDatafeeds(toasts, mlJobService, jobsToStop, refreshJobs);
closeModal();
}}
fill
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import { EditQueryDelay } from './edit_query_delay';
import type { ChartDirectionType } from './constants';
import { CHART_DIRECTION, CHART_SIZE } from './constants';
import { loadFullJob } from '../utils';
import { useMlJobService } from '../../../../services/job_service';
import { checkPermission } from '../../../../capabilities/check_capabilities';
import { type ChartDataWithNullValues, fillMissingChartData } from './fill_missing_chart_data';

Expand Down Expand Up @@ -111,7 +110,7 @@ export const DatafeedChartFlyout: FC<DatafeedChartFlyoutProps> = ({
onClose,
onModelSnapshotAnnotationClick,
}) => {
const mlJobService = useMlJobService();
const mlApiServices = useMlApiContext();
const [data, setData] = useState<{
datafeedConfig: CombinedJobWithStats['datafeed_config'] | undefined;
bucketSpan: string | undefined;
Expand Down Expand Up @@ -214,7 +213,7 @@ export const DatafeedChartFlyout: FC<DatafeedChartFlyoutProps> = ({

const getJobAndSnapshotData = useCallback(async () => {
try {
const job: CombinedJobWithStats = await loadFullJob(mlJobService, jobId);
const job: CombinedJobWithStats = await loadFullJob(mlApiServices, jobId);
const modelSnapshotResultsLine: LineAnnotationDatumWithModelSnapshot[] = [];
const modelSnapshotsResp = await getModelSnapshots(jobId);
const modelSnapshots = modelSnapshotsResp.model_snapshots ?? [];
Expand Down Expand Up @@ -661,7 +660,7 @@ export const JobListDatafeedChartFlyout: FC<JobListDatafeedChartFlyoutProps> = (
unsetShowFunction,
refreshJobs,
}) => {
const mlJobService = useMlJobService();
const mlApiServices = useMlApiContext();
const [isVisible, setIsVisible] = useState(false);
const [job, setJob] = useState<MlSummaryJob | undefined>();
const [jobWithStats, setJobWithStats] = useState<CombinedJobWithStats | undefined>();
Expand All @@ -678,10 +677,10 @@ export const JobListDatafeedChartFlyout: FC<JobListDatafeedChartFlyoutProps> = (
const showRevertModelSnapshot = useCallback(async () => {
// Need to load the full job with stats, as the model snapshot
// flyout needs the timestamp of the last result.
const fullJob: CombinedJobWithStats = await loadFullJob(mlJobService, job!.id);
const fullJob: CombinedJobWithStats = await loadFullJob(mlApiServices, job!.id);
setJobWithStats(fullJob);
setIsRevertModelSnapshotFlyoutVisible(true);
// exclude mlJobservice from deps
// exclude mlApiServices from deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [job]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import { useMlKibana } from '../../../../contexts/kibana';
import { deleteJobs } from '../utils';
import { BLOCKED_JOBS_REFRESH_INTERVAL_MS } from '../../../../../../common/constants/jobs_list';
import { DeleteSpaceAwareItemCheckModal } from '../../../../components/delete_space_aware_item_check_modal';
Expand All @@ -40,6 +41,11 @@ interface Props {
}

export const DeleteJobModal: FC<Props> = ({ setShowFunction, unsetShowFunction, refreshJobs }) => {
const {
services: {
notifications: { toasts },
},
} = useMlKibana();
const mlJobService = useMlJobService();
const [deleting, setDeleting] = useState(false);
const [modalVisible, setModalVisible] = useState(false);
Expand Down Expand Up @@ -85,6 +91,7 @@ export const DeleteJobModal: FC<Props> = ({ setShowFunction, unsetShowFunction,
const deleteJob = useCallback(() => {
setDeleting(true);
deleteJobs(
toasts,
mlJobService,
jobIds.map((id) => ({ id })),
deleteUserAnnotations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React, { Component } from 'react';
import { cloneDeep, isEqual, pick } from 'lodash';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { withKibana } from '@kbn/kibana-react-plugin/public';
import {
EuiButton,
EuiButtonEmpty,
Expand All @@ -30,7 +31,6 @@ import { saveJob } from './edit_utils';
import { loadFullJob } from '../utils';
import { validateModelMemoryLimit, validateGroupNames } from '../validate_job';
import { toastNotificationServiceProvider } from '../../../../services/toast_notification_service';
import { withKibana } from '@kbn/kibana-react-plugin/public';
import { XJson } from '@kbn/es-ui-shared-plugin/public';
import { DATAFEED_STATE, JOB_STATE } from '../../../../../../common/constants/states';
import { CustomUrlsWrapper, isValidCustomUrls } from '../../../../components/custom_urls';
Expand All @@ -42,8 +42,8 @@ const { collapseLiteralStrings } = XJson;
export class EditJobFlyoutUI extends Component {
_initialJobFormState = null;

constructor(props) {
super(props);
constructor(props, constructorContext) {
super(props, constructorContext);

this.state = {
job: {},
Expand Down Expand Up @@ -120,7 +120,7 @@ export class EditJobFlyoutUI extends Component {

showFlyout = (jobLite) => {
const hasDatafeed = jobLite.hasDatafeed;
loadFullJob(jobLite.id)
loadFullJob(this.props.kibana.services.mlServices.mlApiServices, jobLite.id)
.then((job) => {
this.extractJob(job, hasDatafeed);
this.setState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ import { FormattedMessage } from '@kbn/i18n-react';

import { EuiFieldText, EuiForm, EuiFormRow, EuiSpacer, EuiTitle } from '@elastic/eui';

import { mlJobService } from '../../../../../services/job_service';
import { mlJobServiceFactory } from '../../../../../services/job_service';
import { toastNotificationServiceProvider } from '../../../../../services/toast_notification_service';
import { detectorToString } from '../../../../../util/string_utils';

export class Detectors extends Component {
constructor(props) {
super(props);
static contextType = context;

constructor(props, constructorContext) {
super(props, constructorContext);

const mlJobService = mlJobServiceFactory(
toastNotificationServiceProvider(constructorContext.services.notifications.toasts),
constructorContext.services.mlServices.mlApiServices
);

this.detectors = mlJobService.getJobGroups().map((g) => ({ label: g.id }));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { i18n } from '@kbn/i18n';
import { isManagedJob } from '../../../jobs_utils';

export function actionsMenuContent(
toastNotifications,
application,
mlJobService,
showEditJobFlyout,
Expand Down Expand Up @@ -112,7 +113,7 @@ export function actionsMenuContent(
if (isManagedJob(item)) {
showCloseJobsConfirmModal([item]);
} else {
closeJobs([item], refreshJobs);
closeJobs(toastNotifications, mlJobService, [item], refreshJobs);
}

closeMenu(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class JobsListViewUI extends Component {
}

this.setState({ itemIdToExpandedRowMap }, () => {
loadFullJob(jobId)
loadFullJob(this.props.kibana.services.mlServices.mlApiServices, jobId)
.then((job) => {
const fullJobsList = { ...this.state.fullJobsList };
if (this.props.showNodeInfo === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import React, { Component } from 'react';

import { EuiButtonIcon, EuiContextMenuPanel, EuiContextMenuItem, EuiPopover } from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';

import { isManagedJob } from '../../../jobs_utils';

import { mlJobServiceFactory } from '../../../../services/job_service';
import { toastNotificationServiceProvider } from '../../../../services/toast_notification_service';

import {
closeJobs,
stopDatafeeds,
Expand All @@ -20,13 +28,12 @@ import {
isClosable,
isResettable,
} from '../utils';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { isManagedJob } from '../../../jobs_utils';

class MultiJobActionsMenuUI extends Component {
constructor(props) {
super(props);
static contextType = context;

constructor(props, constructorContext) {
super(props, constructorContext);

this.state = {
isOpen: false,
Expand All @@ -37,6 +44,13 @@ class MultiJobActionsMenuUI extends Component {
this.canCloseJob = checkPermission('canCloseJob') && mlNodesAvailable();
this.canResetJob = checkPermission('canResetJob') && mlNodesAvailable();
this.canCreateMlAlerts = checkPermission('canCreateMlAlerts');

this.toastNoticiations = constructorContext.services.notifications.toasts;
const mlApiServices = constructorContext.services.mlServices.mlApiServices;
const toastNotificationService = toastNotificationServiceProvider(
constructorContext.services.notifications.toasts
);
this.mlJobService = mlJobServiceFactory(toastNotificationService, mlApiServices);
}

onButtonClick = () => {
Expand Down Expand Up @@ -101,7 +115,7 @@ class MultiJobActionsMenuUI extends Component {
if (this.props.jobs.some((j) => isManagedJob(j))) {
this.props.showCloseJobsConfirmModal(this.props.jobs);
} else {
closeJobs(this.props.jobs);
closeJobs(this.toastNotifications, this.mlJobService, this.props.jobs);
}

this.closePopover();
Expand Down
Loading

0 comments on commit ebcb88c

Please sign in to comment.