import axios from 'axios' ; import log from '../util/LoggerFactory' ; import { CanaryAdhocExecutionRequest , CanaryAnalysisExecutionStatusResponse , CanaryConfig , CanaryExecutionResponse , CanaryExecutionResult , CanaryExecutionStatusResponse , KayentaCredential , MetricSetPair } from '../domain/Kayenta' ; export const kayentaClient = axios . create ( { baseURL : `${ window . location . origin }/kayenta/` , timeout : 10000 } ) ; export default class KayentaApiService { async fetchCredentials ( ) { try { const response = await kayentaClient . get ( '/credentials' ) ; return response . data ; } catch ( e ) { log . error ( 'Failed to fetch account metadata from Kayenta' , e ) ; alert ( 'Failed to fetch account metadata from Kayenta' ) ; throw e ; } } async initiateCanaryWithConfig ( canaryAdhocExecutionRequest , application , metricsAccountName , storageAccountName ) { try { const response = await kayentaClient . post ( '/canary' , canaryAdhocExecutionRequest , { params : { application : application . trim ( ) === '' ? undefined : application . trim ( ) , metricsAccountName : metricsAccountName , storageAccountName : storageAccountName } , headers : { 'Content-Type' : 'application/json' } } ) ; log . info ( response ) ; return response . data ; } catch ( error ) { log . info ( error . response ) ; throw error ; } } async fetchCanaryExecutionStatusResponse ( executionId ) { try { const response = await kayentaClient . get ( '/canary/' + executionId ) ; return response . data ; } catch ( e ) { log . error ( 'Failed to fetch canary run status from Kayenta' , e ) ; throw e ; } } async fetchCanaryAnalysisExecutionStatusResponse ( executionId ) { try { const response = await kayentaClient . get ( '/standalone_canary_analysis/' + executionId ) ; return response . data ; } catch ( e ) { log . error ( 'Failed to fetch standalone canary analysis pipeline execution status from Kayenta' , e ) ; throw e ; } } async fetchCanaryConfig ( configId ) { try { const response = await kayentaClient . get ( `/canaryConfig/${ configId }` ) ; return response . data ; } catch ( e ) { log . error ( `Failed to fetch canary config with id: ${ configId } from Kayenta` , e ) ; throw e ; } } async fetchMetricSetPairList ( metricSetPairListId ) { try { const response = await kayentaClient . get ( `/metricSetPairList/${ metricSetPairListId }` ) ; return response . data ; } catch ( e ) { log . error ( `Failed to fetch metric set pair list of id: ${ metricSetPairListId } from Kayenta` , e ) ; throw e ; } } async createMetricSetPairListMap ( canaryExecutionResults ) { const metricSetPairListMap = { } ; for ( let result of canaryExecutionResults ) { const id = result . metricSetPairListId ; metricSetPairListMap [ id ] = await this . fetchMetricSetPairList ( id ) ; } return metricSetPairListMap ; } }