import { encode } from 'rison-node' ; import React , { FunctionComponent , useState , useEffect } from 'react' ; import { buildPhrasesFilter , PhraseFilter } from '@kbn/es-query' ; import { FormattedMessage } from '@kbn/i18n-react' ; import { METRIC_TYPE } from '@kbn/analytics' ; import { EuiLink , EuiFlexGroup , EuiFlexItem , EuiSpacer , EuiPanel , EuiText } from '@elastic/eui' ; import { APPS_WITH_DEPRECATION_LOGS , DEPRECATION_LOGS_ORIGIN_FIELD , } from '../../../../../common/constants' ; import { DataPublicPluginStart } from '../../../../shared_imports' ; import { useAppContext } from '../../../app_context' ; import { uiMetricService , UIM_OBSERVABILITY_CLICK , UIM_DISCOVER_CLICK , } from '../../../lib/ui_metric' ; import { DEPRECATION_LOGS_INDEX_PATTERN , DEPRECATION_LOGS_SOURCE_ID , } from '../../../../../common/constants' ; interface Props { checkpoint : string ; } export const getDeprecationDataView = async ( dataService ) => { const results = await dataService . dataViews . find ( DEPRECATION_LOGS_INDEX_PATTERN ) ; const deprecationDataView = results . find ( ( result ) => result . title === DEPRECATION_LOGS_INDEX_PATTERN ) ; if ( deprecationDataView ) { return deprecationDataView ; } else { const override = false ; const skipFetchFields = true ; const newDataView = await dataService . dataViews . createAndSave ( { title : DEPRECATION_LOGS_INDEX_PATTERN , allowNoIndex : true , } , override , skipFetchFields ) ; return newDataView ; } } ; const DiscoverAppLink = ( { checkpoint } ) => { const { services : { data : dataService } , plugins : { share } , } = useAppContext ( ) ; const [ discoveryUrl , setDiscoveryUrl ] = useState < string | undefined > ( ) ; useEffect ( ( ) => { const getDiscoveryUrl = async ( ) => { const locator = share . url . locators . get ( 'DISCOVER_APP_LOCATOR' ) ; if ( ! locator ) { return ; } const dataView = await getDeprecationDataView ( dataService ) ; const field = dataView . getFieldByName ( DEPRECATION_LOGS_ORIGIN_FIELD ) ; let filters = [ ] ; if ( field !== undefined ) { const filter = buildPhrasesFilter ( field ! , [ ... APPS_WITH_DEPRECATION_LOGS ] , dataView ) ; filter . meta . negate = true ; filters = [ filter ] ; } const url = await locator ?. getUrl ( { indexPatternId : dataView . id , query : { language : 'kuery' , query : `@timestamp > "${ checkpoint }"` , } , filters , } ) ; setDiscoveryUrl ( url ) ; } ; getDiscoveryUrl ( ) ; } , [ dataService , checkpoint , share . url . locators ] ) ; if ( discoveryUrl === undefined ) { return null ; } return ( < EuiLink href = { discoveryUrl } onClick = { ( ) => { uiMetricService . trackUiMetric ( METRIC_TYPE . CLICK , UIM_DISCOVER_CLICK ) ; } } data - test - subj = "viewDiscoverLogs" > < FormattedMessage id = "xpack.upgradeAssistant.overview.viewDiscoverResultsAction" defaultMessage = "Analyze logs in Discover" / > < /EuiLink>); } ; const ObservabilityAppLink = ( { checkpoint } ) => { const { services : { core : { http } , } , } = useAppContext ( ) ; const streamAppPath = '/app/logs/stream' ; const sourceId = DEPRECATION_LOGS_SOURCE_ID ; const logPosition = `(end:now,start:${ encode ( checkpoint ) })` ; const logFilter = encodeURI ( `(language:kuery,query:'not ${ DEPRECATION_LOGS_ORIGIN_FIELD } : (${ APPS_WITH_DEPRECATION_LOGS . join ( ' or ' ) })')` ) ; const queryParams = `sourceId=${ sourceId }&logPosition=${ logPosition }&logFilter=${ logFilter }` ; const logStreamUrl = http ?. basePath ?. prepend ( `${ streamAppPath }?${ queryParams }` ) ; return ( < EuiLink href = { logStreamUrl } onClick = { ( ) => { uiMetricService . trackUiMetric ( METRIC_TYPE . CLICK , UIM_OBSERVABILITY_CLICK ) ; } } data - test - subj = "viewObserveLogs" > < FormattedMessage id = "xpack.upgradeAssistant.overview.viewObservabilityResultsAction" defaultMessage = "View deprecation logs in Observability" / > < /EuiLink>); } ; export const ExternalLinks = ( { checkpoint } ) => { const { infra : hasInfraPlugin } = useAppContext ( ) . plugins ; return ( < EuiFlexGroup > { hasInfraPlugin && ( < EuiFlexItem > < EuiPanel > < EuiText size = "s" > < p > < FormattedMessage id = "xpack.upgradeAssistant.overview.observe.observabilityDescription" defaultMessage = "Get insight into which deprecated APIs are being used and what applications you need to update." / > < /p> < /EuiText> < EuiSpacer size = "m" / > < ObservabilityAppLink checkpoint = { checkpoint } /> < /EuiPanel> < /EuiFlexItem>)} < EuiFlexItem > < EuiPanel > < EuiText size = "s" > < p > < FormattedMessage id = "xpack.upgradeAssistant.overview.observe.discoveryDescription" defaultMessage = "Search and filter the deprecation logs to understand the types of changes you need to make." / > < /p> < /EuiText> < EuiSpacer size = "m" / > < DiscoverAppLink checkpoint = { checkpoint } /> < /EuiPanel> < /EuiFlexItem> < /EuiFlexGroup>); } ;