import { EVENT_OUTCOME } from '../../../common/elasticsearch_fieldnames' ; import { EventOutcome } from '../../../common/event_outcome' ; import { AggregationOptionsByType , AggregationResultOf , } from '../../../../../../src/core/types/elasticsearch' ; export const getOutcomeAggregation = ( ) => ( { terms : { field : EVENT_OUTCOME , include : [ EventOutcome . failure , EventOutcome . success ] , } , } ) ; type OutcomeAggregation = ReturnType < typeof getOutcomeAggregation > ; export const getTimeseriesAggregation = ( start , end , intervalString ) => ( { date_histogram : { field : '@timestamp' , fixed_interval : intervalString , min_doc_count : 0 , extended_bounds : { min : start , max : end } , } , aggs : { outcomes : getOutcomeAggregation ( ) } , } ) ; export function calculateFailedTransactionRate ( outcomeResponse ) { const outcomes = Object . fromEntries ( outcomeResponse . buckets . map ( ( { key , doc_count : count } ) => [ key , count ] ) ) ; const failedTransactions = outcomes [ EventOutcome . failure ] ?? 0 ; const successfulTransactions = outcomes [ EventOutcome . success ] ?? 0 ; return failedTransactions / ( successfulTransactions + failedTransactions ) ; } export function getFailedTransactionRateTimeSeries ( buckets ) { return buckets . map ( ( dateBucket ) => { return { x : dateBucket . key , y : calculateFailedTransactionRate ( dateBucket . outcomes ) , } ; } ) ; }