import { toArray } from 'lodash' ; import { getFormatService } from '../../../services' ; import { Table } from '../../types' ; import type { Dimensions } from '../../../../../pie/public' ; interface Slice { name : string ; size : number ; parent ? : Slice ; children ? : [ ] ; rawData ? : { table : Table ; row : number ; column : number ; value : string | number | object ; } ; } export const buildHierarchicalData = ( table , { metric , buckets = [ ] } ) => { let slices ; const names = { } ; const metricColumn = table . columns [ metric . accessor ] ; const metricFieldFormatter = metric . format ; if ( ! buckets . length ) { slices = [ { name : metricColumn . name , size : table . rows [ 0 ] [ metricColumn . id ] as number , } , ] ; names [ metricColumn . name ] = metricColumn . name ; } else { slices = [ ] ; table . rows . forEach ( ( row , rowIndex ) => { let parent ; let dataLevel = slices ; buckets . forEach ( ( bucket ) => { const bucketColumn = table . columns [ bucket . accessor ] ; const bucketValueColumn = table . columns [ bucket . accessor + 1 ] ; const bucketFormatter = getFormatService ( ) . deserialize ( bucket . format ) ; const name = bucketFormatter . convert ( row [ bucketColumn . id ] ) ; const size = row [ bucketValueColumn . id ] as number ; names [ name ] = name ; let slice = dataLevel . find ( ( dataLevelSlice ) => dataLevelSlice . name === name ) ; if ( ! slice ) { slice = { name , size , parent , children : [ ] , rawData : { table , row : rowIndex , column : bucket . accessor , value : row [ bucketColumn . id ] , } , } ; dataLevel . push ( slice ) ; } parent = slice ; dataLevel = slice . children as [ ] ; } ) ; } ) ; } return { hits : table . rows . length , raw : table , names : toArray ( names ) , tooltipFormatter : metricFieldFormatter , slices : { children : [ ... slices ] , } , } ; } ;