metricReadout.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 {PercentChange} from 'sentry/components/percentChange';
  8. import {Tooltip} from 'sentry/components/tooltip';
  9. import {defined} from 'sentry/utils';
  10. import {
  11. type CountUnit,
  12. CurrencyUnit,
  13. DurationUnit,
  14. type PercentageUnit,
  15. type PercentChangeUnit,
  16. RateUnit,
  17. SizeUnit,
  18. } from 'sentry/utils/discover/fields';
  19. import {
  20. formatAbbreviatedNumber,
  21. formatPercentage,
  22. formatRate,
  23. } from 'sentry/utils/formatters';
  24. import {Block} from 'sentry/views/starfish/views/spanSummaryPage/block';
  25. type Unit =
  26. | DurationUnit.MILLISECOND
  27. | SizeUnit.BYTE
  28. | RateUnit
  29. | CountUnit
  30. | PercentageUnit
  31. | PercentChangeUnit
  32. | CurrencyUnit;
  33. interface Props {
  34. title: string;
  35. unit: Unit;
  36. value: ReactText | undefined;
  37. align?: 'left' | 'right';
  38. isLoading?: boolean;
  39. tooltip?: React.ReactNode;
  40. }
  41. export function MetricReadout(props: Props) {
  42. return (
  43. <Block title={props.title} alignment={props.align}>
  44. <ReadoutContent {...props} />
  45. </Block>
  46. );
  47. }
  48. function ReadoutContent({unit, value, tooltip, align = 'right', isLoading}: Props) {
  49. if (isLoading) {
  50. return (
  51. <LoadingContainer align={align}>
  52. <LoadingIndicator mini />
  53. </LoadingContainer>
  54. );
  55. }
  56. if (!defined(value)) {
  57. return <Fragment>--</Fragment>;
  58. }
  59. let renderedValue: React.ReactNode;
  60. if (isARateUnit(unit)) {
  61. renderedValue = (
  62. <NumberContainer align={align}>
  63. {formatRate(typeof value === 'string' ? parseFloat(value) : value, unit, {
  64. minimumValue: MINIMUM_RATE_VALUE,
  65. })}
  66. </NumberContainer>
  67. );
  68. }
  69. if (unit === DurationUnit.MILLISECOND) {
  70. // TODO: Implement other durations
  71. renderedValue = (
  72. <NumberContainer align={align}>
  73. <Duration
  74. seconds={typeof value === 'string' ? parseFloat(value) : value / 1000}
  75. fixedDigits={2}
  76. abbreviation
  77. />
  78. </NumberContainer>
  79. );
  80. }
  81. if (unit === SizeUnit.BYTE) {
  82. // TODO: Implement other sizes
  83. renderedValue = (
  84. <NumberContainer align={align}>
  85. <FileSize bytes={typeof value === 'string' ? parseInt(value, 10) : value} />
  86. </NumberContainer>
  87. );
  88. }
  89. if (unit === 'count') {
  90. renderedValue = (
  91. <NumberContainer align={align}>
  92. {formatAbbreviatedNumber(typeof value === 'string' ? parseInt(value, 10) : value)}
  93. </NumberContainer>
  94. );
  95. }
  96. if (unit === CurrencyUnit.USD) {
  97. const numericValue = typeof value === 'string' ? parseFloat(value) : value;
  98. if (numericValue <= 1) {
  99. renderedValue = (
  100. <NumberContainer align={align}>US ${numericValue.toFixed(3)}</NumberContainer>
  101. );
  102. } else {
  103. renderedValue = (
  104. <NumberContainer align={align}>
  105. US ${formatAbbreviatedNumber(numericValue)}
  106. </NumberContainer>
  107. );
  108. }
  109. }
  110. if (unit === 'percentage') {
  111. renderedValue = (
  112. <NumberContainer align={align}>
  113. {formatPercentage(
  114. typeof value === 'string' ? parseFloat(value) : value,
  115. undefined,
  116. {minimumValue: MINIMUM_PERCENTAGE_VALUE}
  117. )}
  118. </NumberContainer>
  119. );
  120. }
  121. if (unit === 'percent_change') {
  122. renderedValue = (
  123. <NumberContainer align={align}>
  124. <PercentChange
  125. value={typeof value === 'string' ? parseFloat(value) : value}
  126. minimumValue={MINIMUM_PERCENTAGE_VALUE}
  127. />
  128. </NumberContainer>
  129. );
  130. }
  131. if (tooltip) {
  132. return (
  133. <NumberContainer align={align}>
  134. <Tooltip title={tooltip} isHoverable showUnderline>
  135. {renderedValue}
  136. </Tooltip>
  137. </NumberContainer>
  138. );
  139. }
  140. return <NumberContainer align={align}>{renderedValue}</NumberContainer>;
  141. }
  142. const MINIMUM_RATE_VALUE = 0.01;
  143. const MINIMUM_PERCENTAGE_VALUE = 0.0001; // 0.01%
  144. const NumberContainer = styled('div')<{align: 'left' | 'right'}>`
  145. text-align: ${p => p.align};
  146. font-variant-numeric: tabular-nums;
  147. `;
  148. const LoadingContainer = styled('div')<{align: 'left' | 'right'}>`
  149. display: flex;
  150. justify-content: ${p => (p.align === 'right' ? 'flex-end' : 'flex-start')};
  151. align-items: center;
  152. `;
  153. function isARateUnit(unit: string): unit is RateUnit {
  154. return (Object.values(RateUnit) as string[]).includes(unit);
  155. }