import Color from 'color' ; import { get , isPlainObject } from 'lodash' ; import { overwrite } from '../helpers' ; import { calculateLabel } from '../../../../common/calculate_label' ; import { getLastMetric } from './get_last_metric' ; import { formatKey } from './format_key' ; import type { Panel , Series } from '../../../../common/types' ; import type { BaseMeta } from '../request_processors/types' ; const getTimeSeries = < TRawResponse = unknown > ( resp , series ) => get ( resp , `aggregations.timeseries` ) || get ( resp , `aggregations.${ series . id }.timeseries` ) ; interface SplittedData < TMeta extends BaseMeta = BaseMeta > { id : string ; splitByLabel : string ; label : string ; color : string ; meta : TMeta ; timeseries : { buckets : [ { [ s ] : { values : Record < string , unknown > ; } ; } & { key : string | number ; } ] ; } ; } export async function getSplits < TRawResponse = unknown , TMeta extends BaseMeta = BaseMeta > ( resp , panel , series , meta , extractFields ) { if ( ! meta ) { meta = get ( resp , `aggregations.${ series . id }.meta` ) ; } const color = new Color ( series . color ) ; const metric = getLastMetric ( series ) ; const buckets = get ( resp , `aggregations.${ series . id }.buckets` ) ; const fieldsForSeries = meta ?. index ? await extractFields ( { id : meta . index } ) : [ ] ; const splitByLabel = calculateLabel ( metric , series . metrics , fieldsForSeries ) ; if ( buckets ) { if ( Array . isArray ( buckets ) ) { return buckets . map ( ( bucket ) => { if ( bucket . column_filter ) { bucket = { ... bucket , ... bucket . column_filter , } ; } bucket . id = `${ series . id }:${ bucket . key }` ; bucket . splitByLabel = splitByLabel ; bucket . label = formatKey ( bucket . key , series ) ; bucket . labelFormatted = bucket . key_as_string ? formatKey ( bucket . key_as_string , series ) : '' ; bucket . color = color . string ( ) ; bucket . meta = meta ; return bucket ; } ) ; } if ( series . split_mode === 'filters' && isPlainObject ( buckets ) ) { return ( series . split_filters || [ ] ) . map ( ( filter ) => { const bucket = get ( resp , `aggregations.${ series . id }.buckets.${ filter . id }` ) ; bucket . id = `${ series . id }:${ filter . id }` ; bucket . key = filter . id ; bucket . splitByLabel = splitByLabel ; bucket . color = filter . color ; bucket . label = ( filter . label || filter . filter ?. query ) ?? '*' ; bucket . meta = meta ; return bucket ; } ) ; } } const timeseries = getTimeSeries < TRawResponse > ( resp , series ) ; const mergeObj = { timeseries , } ; series . metrics . filter ( ( m ) => /_bucket/ . test ( m . type ) ) . forEach ( ( m ) => { overwrite ( mergeObj , m . id , get ( resp , `aggregations.${ series . id }.${ m . id }` ) ) ; } ) ; return [ { id : series . id , splitByLabel , label : series . label || splitByLabel , color : color . string ( ) , ... mergeObj , meta : meta ! , } , ] ; }