import { Serie } from '@nivo/line' ; import { IPieDatum , IQueryResult } from '../components/costmanagement/interfaces' ; import { IMetric } from './interfaces' ; import { formatTime as formatTimeCore } from '@kobsio/plugin-core' ; export const getResourceGroupFromID = ( id ) : string => { const parts = id . split ( '/' ) ; let index = parts . indexOf ( 'resourceGroups' ) ; if ( index === - 1 ) { index = parts . indexOf ( 'resourcegroups' ) ; } if ( index > - 1 && parts . length >= index + 1 ) { return parts [ index + 1 ] ; } return '' ; } ; export const formatTime = ( time ) : string => { return formatTimeCore ( Math . floor ( new Date ( time ) . getTime ( ) / 1000 ) ) ; } ; export const formatAxisBottom = ( timeStart , timeEnd ) : string => { timeStart = Math . floor ( timeStart ) ; timeEnd = Math . floor ( timeEnd ) ; if ( timeEnd - timeStart < 3600 ) { return '%H:%M:%S' ; } else if ( timeEnd - timeStart < 86400 ) { return '%H:%M' ; } else if ( timeEnd - timeStart < 604800 ) { return '%m-%d %H:%M' ; } return '%m-%d' ; } ; export const convertMetrics = ( metrics , aggregationType ) : Serie [ ] => { const series = [ ] ; for ( let i = 0 ; i < metrics . length ; i ++ ) { for ( let j = 0 ; j < metrics [ i ] . timeseries . length ; j ++ ) { series . push ( { data : metrics [ i ] . timeseries [ j ] . data . map ( ( datum ) => { return { unit : metrics [ i ] . unit , x : new Date ( datum . timeStamp ) , y : datum [ aggregationType . toLowerCase ( ) ] === undefined ? null : datum [ aggregationType . toLowerCase ( ) ] , } ; } ) , id : metrics [ i ] . name . localizedValue , } ) ; } } return series ; } ; export const convertQueryResult = ( data ) : IPieDatum [ ] => { const pieData = [ ] ; for ( let i = 0 ; i < data . properties . rows . length ; i ++ ) { pieData . push ( { id : data . properties . rows [ i ] [ 1 ] , label : data . properties . rows [ i ] [ 1 ] , value : data . properties . rows [ i ] [ 0 ] , } ) ; } return pieData ; } ; export const formatMetrics = ( metrics , aggregationType ) : IMetric [ ] => { const formattedMetrics = [ ] ; let min = 0 ; let max = 0 ; for ( const metric of metrics ) { if ( metric . unit !== 'Bytes' ) { return metrics ; } for ( let i = 0 ; i < metric . timeseries . length ; i ++ ) { for ( let j = 0 ; j < metric . timeseries [ i ] . data . length ; j ++ ) { const value = metric . timeseries [ i ] . data [ j ] [ aggregationType . toLowerCase ( ) ] ; if ( i === 0 && j === 0 && value !== undefined ) { min = value ; max = value ; } if ( value !== undefined && value < min ) { min = value ; } if ( value !== undefined && value > max ) { max = value ; } } } } let exponent = Math . floor ( Math . log ( max ) / Math . log ( 1024 ) ) ; if ( exponent === 0 ) { return metrics ; } if ( exponent > 8 ) { exponent = 8 ; } for ( const metric of metrics ) { for ( let i = 0 ; i < metric . timeseries . length ; i ++ ) { for ( let j = 0 ; j < metric . timeseries [ i ] . data . length ; j ++ ) { const value = metric . timeseries [ i ] . data [ j ] [ aggregationType . toLowerCase ( ) ] ; if ( value !== undefined ) { metric . timeseries [ i ] . data [ j ] [ aggregationType . toLowerCase ( ) ] = value / Math . pow ( 1024 , exponent ) ; } } } const sizes = [ 'Bytes' , 'KiB' , 'MiB' , 'GiB' , 'TiB' , 'PiB' , 'EiB' , 'ZiB' , 'YiB' ] ; metric . unit = sizes [ exponent ] ; formattedMetrics . push ( metric ) ; } return formattedMetrics ; } ;