formatChartValue.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import {formatBytesBase2} from 'sentry/utils/bytes/formatBytesBase2';
  2. import {formatBytesBase10} from 'sentry/utils/bytes/formatBytesBase10';
  3. import {
  4. ABYTE_UNITS,
  5. DURATION_UNITS,
  6. SIZE_UNITS,
  7. } from 'sentry/utils/discover/fieldRenderers';
  8. import type {RateUnit} from 'sentry/utils/discover/fields';
  9. import getDuration from 'sentry/utils/duration/getDuration';
  10. import {formatRate} from 'sentry/utils/formatters';
  11. import {formatPercentage} from 'sentry/utils/number/formatPercentage';
  12. export function formatChartValue(value: number, type: string, unit?: string): string {
  13. switch (type) {
  14. case 'integer':
  15. case 'number':
  16. return value.toLocaleString();
  17. case 'percentage':
  18. return formatPercentage(value, 2);
  19. case 'duration':
  20. return getDuration((value * (unit ? DURATION_UNITS[unit] : 1)) / 1000, 2, true);
  21. case 'size':
  22. const bytes = value * SIZE_UNITS[unit ?? 'byte'];
  23. const formatter = ABYTE_UNITS.includes(unit ?? 'byte')
  24. ? formatBytesBase10
  25. : formatBytesBase2;
  26. return formatter(bytes);
  27. case 'rate':
  28. return formatRate(value, unit as RateUnit);
  29. default:
  30. return value.toString();
  31. }
  32. }