import normalizeUrl from "normalize-url" ; import { format } from "url" ; import { flatten } from "lodash" ; import { ModuleConfig } from "@garden-io/core/build/src/config/module" ; import { ServiceIngress } from "@garden-io/core/build/src/types/service" ; export function getServiceNames ( moduleConfigs ) { return flatten ( moduleConfigs . map ( ( m ) => m . serviceConfigs . map ( ( s ) => s . name ) ) ) ; } export function timeConversion ( millisec ) { const seconds = + ( millisec / 1000 ) . toFixed ( 1 ) ; const minutes = + ( millisec / ( 1000 * 60 ) ) . toFixed ( 1 ) ; const hours = + ( millisec / ( 1000 * 60 * 60 ) ) . toFixed ( 1 ) ; const days = + ( millisec / ( 1000 * 60 * 60 * 24 ) ) . toFixed ( 1 ) ; let formatTime = ( num , prefix ) => `${ num } ${ prefix }` ; let timeFormatted = null ; if ( seconds < 60 ) { timeFormatted = formatTime ( seconds , "Sec" ) ; } else if ( minutes < 60 ) { timeFormatted = formatTime ( minutes , "Min" ) ; } else if ( hours < 24 ) { timeFormatted = formatTime ( hours , "Hrs" ) ; } else { timeFormatted = formatTime ( days , "Days" ) ; } return timeFormatted ; } export function getDuration ( start , end ) { const startValue = new Date ( start ) . valueOf ( ) ; const endValue = new Date ( end ) . valueOf ( ) ; const duration = timeConversion ( endValue - startValue ) ; return duration ; } export const truncateMiddle = ( str , resLength = 35 ) => { if ( str . length > resLength ) { const middle = Math . ceil ( resLength / 2 ) ; return str . substr ( 0 , middle ) + "..." + str . substr ( str . length - middle , str . length ) ; } return str ; } ; /**
 * Returns the link URL or falls back to constructing the URL from the ingress spec
 */ export function getLinkUrl ( ingress ) { if ( ingress . linkUrl ) { return ingress . linkUrl ; } return normalizeUrl ( format ( { protocol : ingress . protocol , hostname : ingress . hostname , port : ingress . port , pathname : ingress . path , } ) ) ; } /**
 * Test names are not unique so we construct a unique key from the module name and the test name.
 */ export function getTestKey ( { testName , moduleName } ) { return `${ moduleName }.${ testName }` ; } let _canvas ; param { String } text param { String } font see https export function getTextWidth ( text , font ) { const canvas = _canvas || ( _canvas = document . createElement ( "canvas" ) ) ; const context = canvas . getContext ( "2d" ) ! ; context . font = font ; const metrics = context . measureText ( text ) ; return metrics . width ; }