|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import React, { FC, useContext, useEffect, useState, useMemo } from 'react'; |
| 8 | +import { EuiBasicTable, EuiCallOut, EuiSpacer, EuiText } from '@elastic/eui'; |
| 9 | +import { FormattedMessage } from '@kbn/i18n/react'; |
| 10 | +import { i18n } from '@kbn/i18n'; |
| 11 | +import { JobCreatorContext } from '../../../job_creator_context'; |
| 12 | +import { CategorizationJobCreator } from '../../../../../common/job_creator'; |
| 13 | +import { Results } from '../../../../../common/results_loader'; |
| 14 | +import { ml } from '../../../../../../../services/ml_api_service'; |
| 15 | + |
| 16 | +const NUMBER_OF_PREVIEW = 5; |
| 17 | +export const CategoryStoppedPartitions: FC = () => { |
| 18 | + const { jobCreator: jc, resultsLoader } = useContext(JobCreatorContext); |
| 19 | + const jobCreator = jc as CategorizationJobCreator; |
| 20 | + const [tableRow, setTableRow] = useState<Array<{ count?: number; example: string }>>([]); |
| 21 | + const [hasStoppedPartitions, setHasStoppedPartitions] = useState(false); |
| 22 | + |
| 23 | + let _resultsSubscription; |
| 24 | + |
| 25 | + const columns = useMemo( |
| 26 | + () => [ |
| 27 | + { |
| 28 | + field: 'partitionName', |
| 29 | + name: i18n.translate( |
| 30 | + 'xpack.ml.newJob.wizard.pickFieldsStep.stoppedPartitionsPreviewColumnName', |
| 31 | + { |
| 32 | + defaultMessage: 'Stopped partition name', |
| 33 | + } |
| 34 | + ), |
| 35 | + render: (partition: any) => ( |
| 36 | + <EuiText size="s"> |
| 37 | + <code>{partition}</code> |
| 38 | + </EuiText> |
| 39 | + ), |
| 40 | + }, |
| 41 | + ], |
| 42 | + [] |
| 43 | + ); |
| 44 | + function setResultsWrapper(results: Results) { |
| 45 | + loadCategoryStoppedPartitions(); |
| 46 | + } |
| 47 | + |
| 48 | + async function loadCategoryStoppedPartitions() { |
| 49 | + const results = await ml.results.getCategoryStoppedPartitions([jobCreator.jobId]); |
| 50 | + |
| 51 | + if ( |
| 52 | + results?.jobs !== undefined && |
| 53 | + Array.isArray(results?.jobs[jobCreator.jobId]) && |
| 54 | + results.jobs[jobCreator.jobId].length > 0 |
| 55 | + ) { |
| 56 | + const stoppedPartitionsPreview = results.jobs[jobCreator.jobId]; |
| 57 | + // once we have reached number of stopped partitions we wanted to show as preview |
| 58 | + // no need to keep fetching anymore |
| 59 | + if ( |
| 60 | + stoppedPartitionsPreview.length >= NUMBER_OF_PREVIEW && |
| 61 | + _resultsSubscription !== undefined |
| 62 | + ) { |
| 63 | + _resultsSubscription.unsubscribe(); |
| 64 | + _resultsSubscription = undefined; |
| 65 | + } |
| 66 | + setHasStoppedPartitions(true); |
| 67 | + setTableRow( |
| 68 | + stoppedPartitionsPreview.slice(0, NUMBER_OF_PREVIEW).map((partitionName) => ({ |
| 69 | + partitionName, |
| 70 | + })) |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + useEffect(() => { |
| 76 | + // only need to run this check if jobCreator.perPartitionStopOnWarn is turned on |
| 77 | + if (jobCreator.perPartitionCategorization && jobCreator.perPartitionStopOnWarn) { |
| 78 | + // subscribe to result updates |
| 79 | + _resultsSubscription = resultsLoader.subscribeToResults(setResultsWrapper); |
| 80 | + return () => { |
| 81 | + if (_resultsSubscription) { |
| 82 | + _resultsSubscription.unsubscribe(); |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + }, []); |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + {hasStoppedPartitions && ( |
| 91 | + <> |
| 92 | + <EuiSpacer /> |
| 93 | + <div> |
| 94 | + <FormattedMessage |
| 95 | + id="xpack.ml.newJob.wizard.pickFieldsStep.categorizationStoppedPartitionsTitle" |
| 96 | + defaultMessage="Stopped partitions" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + |
| 100 | + <EuiCallOut |
| 101 | + color={'warning'} |
| 102 | + size={'s'} |
| 103 | + title={ |
| 104 | + <FormattedMessage |
| 105 | + id="xpack.ml.newJob.wizard.pickFieldsStep.stoppedPartitionsExistCallout" |
| 106 | + defaultMessage="There may be fewer results than there could have been because stop_on_warn is turned on. Both categorization and subsequent anomaly detection have stopped for some partitions for job '{jobId}' where the categorization status has changed to 'warn'." |
| 107 | + values={{ |
| 108 | + jobId: jobCreator.jobId, |
| 109 | + }} |
| 110 | + /> |
| 111 | + } |
| 112 | + /> |
| 113 | + <EuiBasicTable columns={columns} items={tableRow} /> |
| 114 | + </> |
| 115 | + )} |
| 116 | + </> |
| 117 | + ); |
| 118 | +}; |
0 commit comments