github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/pages/formatTableData.ts (about) 1 import { Profile } from '@pyroscope/models/src'; 2 import Color from 'color'; 3 import { getFormatter } from '@pyroscope/flamegraph/src/format/format'; 4 5 export interface TableValuesData { 6 color?: Color; 7 mean: number; 8 stdDeviation: number; 9 total: number; 10 tagName: string; 11 totalLabel: string; 12 stdDeviationLabel: string; 13 meanLabel: string; 14 } 15 16 export function addSpaces(maxLen: number, len: number, label: string) { 17 if (!label.includes('.') || !maxLen || !len || len > maxLen) { 18 return label; 19 } 20 21 return ( 22 '\xa0'.repeat(maxLen - len).concat(label.split('.')[0]) + 23 '.'.concat(label.split('.')[1]) 24 ); 25 } 26 27 export function getIntegerSpaceLengthForString(value?: string) { 28 if (!value || !value.includes('.')) { 29 return 1; 30 } 31 32 return value.split('.')[0].length; 33 } 34 35 export function getTableIntegerSpaceLengthByColumn(data: TableValuesData[]) { 36 return data.reduce( 37 (acc, current) => { 38 const meanIntegerSpaceLength = getIntegerSpaceLengthForString( 39 current.meanLabel 40 ); 41 const stdDeviationIntegerSpaceLength = getIntegerSpaceLengthForString( 42 current.stdDeviationLabel 43 ); 44 const totalIntegerSpaceLength = getIntegerSpaceLengthForString( 45 current.totalLabel 46 ); 47 48 return { 49 ...acc, 50 mean: 51 meanIntegerSpaceLength > acc.mean ? meanIntegerSpaceLength : acc.mean, 52 stdDeviation: 53 stdDeviationIntegerSpaceLength > acc.stdDeviation 54 ? stdDeviationIntegerSpaceLength 55 : acc.stdDeviation, 56 total: 57 totalIntegerSpaceLength > acc.total 58 ? totalIntegerSpaceLength 59 : acc.total, 60 }; 61 }, 62 { mean: 1, stdDeviation: 1, total: 1 } 63 ); 64 } 65 66 export const formatValue = ({ 67 value, 68 formatter, 69 profile, 70 }: { 71 value?: number; 72 formatter?: ReturnType<typeof getFormatter>; 73 profile?: Profile; 74 }) => { 75 if (!formatter || !profile || typeof value !== 'number') { 76 return '0'; 77 } 78 79 const formatterResult = `${formatter.format( 80 value, 81 profile.metadata.sampleRate 82 )}`; 83 84 if (String(formatterResult).includes('< 0.01')) { 85 return formatter.formatPrecise(value, profile.metadata.sampleRate); 86 } 87 88 return formatterResult; 89 };