metricReadout.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import type {ReactText} from 'react';
  2. import {Fragment} from 'react';
  3. import styled from '@emotion/styled';
  4. import Duration from 'sentry/components/duration';
  5. import FileSize from 'sentry/components/fileSize';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import {Tooltip} from 'sentry/components/tooltip';
  8. import {defined} from 'sentry/utils';
  9. import type {CountUnit, PercentageUnit} from 'sentry/utils/discover/fields';
  10. import {DurationUnit, RateUnit, SizeUnit} from 'sentry/utils/discover/fields';
  11. import {
  12. formatAbbreviatedNumber,
  13. formatPercentage,
  14. formatRate,
  15. } from 'sentry/utils/formatters';
  16. import {Block} from 'sentry/views/starfish/views/spanSummaryPage/block';
  17. type Unit =
  18. | DurationUnit.MILLISECOND
  19. | SizeUnit.BYTE
  20. | RateUnit
  21. | CountUnit
  22. | PercentageUnit;
  23. interface Props {
  24. title: string;
  25. unit: Unit;
  26. value: ReactText | undefined;
  27. align?: 'left' | 'right';
  28. isLoading?: boolean;
  29. tooltip?: React.ReactNode;
  30. }
  31. export function MetricReadout(props: Props) {
  32. return (
  33. <Block title={props.title} alignment={props.align}>
  34. <ReadoutContent {...props} />
  35. </Block>
  36. );
  37. }
  38. function ReadoutContent({unit, value, tooltip, align = 'right', isLoading}: Props) {
  39. if (isLoading) {
  40. return (
  41. <LoadingContainer align={align}>
  42. <LoadingIndicator mini />
  43. </LoadingContainer>
  44. );
  45. }
  46. if (!defined(value)) {
  47. return <Fragment>--</Fragment>;
  48. }
  49. let renderedValue: React.ReactNode;
  50. if (isARateUnit(unit)) {
  51. renderedValue = (
  52. <NumberContainer align={align}>
  53. {formatRate(typeof value === 'string' ? parseFloat(value) : value, unit)}
  54. </NumberContainer>
  55. );
  56. }
  57. if (unit === DurationUnit.MILLISECOND) {
  58. // TODO: Implement other durations
  59. renderedValue = (
  60. <NumberContainer align={align}>
  61. <Duration
  62. seconds={typeof value === 'string' ? parseFloat(value) : value / 1000}
  63. fixedDigits={2}
  64. abbreviation
  65. />
  66. </NumberContainer>
  67. );
  68. }
  69. if (unit === SizeUnit.BYTE) {
  70. // TODO: Implement other sizes
  71. renderedValue = (
  72. <NumberContainer align={align}>
  73. <FileSize bytes={typeof value === 'string' ? parseInt(value, 10) : value} />
  74. </NumberContainer>
  75. );
  76. }
  77. if (unit === 'count') {
  78. renderedValue = (
  79. <NumberContainer align={align}>
  80. {formatAbbreviatedNumber(typeof value === 'string' ? parseInt(value, 10) : value)}
  81. </NumberContainer>
  82. );
  83. }
  84. if (unit === 'percentage') {
  85. renderedValue = (
  86. <NumberContainer align={align}>
  87. {formatPercentage(typeof value === 'string' ? parseFloat(value) : value)}
  88. </NumberContainer>
  89. );
  90. }
  91. if (tooltip) {
  92. return (
  93. <NumberContainer align={align}>
  94. <Tooltip title={tooltip} isHoverable showUnderline>
  95. {renderedValue}
  96. </Tooltip>
  97. </NumberContainer>
  98. );
  99. }
  100. return <NumberContainer align={align}>{renderedValue}</NumberContainer>;
  101. }
  102. const NumberContainer = styled('div')<{align: 'left' | 'right'}>`
  103. text-align: ${p => p.align};
  104. font-variant-numeric: tabular-nums;
  105. `;
  106. const LoadingContainer = styled('div')<{align: 'left' | 'right'}>`
  107. display: flex;
  108. justify-content: ${p => (p.align === 'right' ? 'flex-end' : 'flex-start')};
  109. align-items: center;
  110. `;
  111. function isARateUnit(unit: string): unit is RateUnit {
  112. return (Object.values(RateUnit) as string[]).includes(unit);
  113. }