utils.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {DateTimeObject, getSeriesApiInterval} from 'sentry/components/charts/utils';
  2. import {DATA_CATEGORY_INFO} from 'sentry/constants';
  3. import {DataCategoryInfo} from 'sentry/types';
  4. import {formatBytesBase10} from 'sentry/utils';
  5. import {parsePeriodToHours} from 'sentry/utils/dates';
  6. export const MILLION = 10 ** 6;
  7. export const BILLION = 10 ** 9;
  8. export const GIGABYTE = 10 ** 9;
  9. type FormatOptions = {
  10. /**
  11. * Truncate 1234 => 1.2k or 1,234,000 to 1.23M
  12. */
  13. isAbbreviated?: boolean;
  14. /**
  15. * Convert attachments to use the most appropriate unit KB/MB/GB/TB/etc.
  16. * Otherwise, it will default to GB
  17. */
  18. useUnitScaling?: boolean;
  19. };
  20. /**
  21. * This expects usage values/quantities for the data categories that we sell.
  22. *
  23. * Note: usageQuantity for Attachments should be in BYTES
  24. */
  25. export function formatUsageWithUnits(
  26. usageQuantity: number = 0,
  27. dataCategory: DataCategoryInfo['plural'],
  28. options: FormatOptions = {isAbbreviated: false, useUnitScaling: false}
  29. ) {
  30. if (dataCategory !== DATA_CATEGORY_INFO.attachment.plural) {
  31. return options.isAbbreviated
  32. ? abbreviateUsageNumber(usageQuantity)
  33. : usageQuantity.toLocaleString();
  34. }
  35. if (options.useUnitScaling) {
  36. return formatBytesBase10(usageQuantity);
  37. }
  38. const usageGb = usageQuantity / GIGABYTE;
  39. return options.isAbbreviated
  40. ? `${abbreviateUsageNumber(usageGb)} GB`
  41. : `${usageGb.toLocaleString(undefined, {maximumFractionDigits: 2})} GB`;
  42. }
  43. /**
  44. * Good default for "formatUsageWithUnits"
  45. */
  46. export function getFormatUsageOptions(
  47. dataCategory: DataCategoryInfo['plural']
  48. ): FormatOptions {
  49. return {
  50. isAbbreviated: dataCategory !== DATA_CATEGORY_INFO.attachment.plural,
  51. useUnitScaling: dataCategory === DATA_CATEGORY_INFO.attachment.plural,
  52. };
  53. }
  54. /**
  55. * Instead of using this function directly, use formatReservedWithUnits or
  56. * formatUsageWithUnits with options.isAbbreviated to true instead.
  57. *
  58. * This function display different precision for billion/million/thousand to
  59. * provide clarity on usage of errors/transactions/attachments to the user.
  60. *
  61. * If you are not displaying usage numbers, it might be better to use
  62. * `formatAbbreviatedNumber` in 'sentry/utils/formatters'
  63. */
  64. export function abbreviateUsageNumber(n: number) {
  65. if (n >= BILLION) {
  66. return (n / BILLION).toLocaleString(undefined, {maximumFractionDigits: 2}) + 'B';
  67. }
  68. if (n >= MILLION) {
  69. return (n / MILLION).toLocaleString(undefined, {maximumFractionDigits: 1}) + 'M';
  70. }
  71. if (n >= 1000) {
  72. return (n / 1000).toFixed().toLocaleString() + 'K';
  73. }
  74. // Do not show decimals
  75. return n.toFixed().toLocaleString();
  76. }
  77. /**
  78. * We want to display datetime in UTC in the following situations:
  79. *
  80. * 1) The user selected an absolute date range with UTC
  81. * 2) The user selected a wide date range with 1d interval
  82. *
  83. * When the interval is 1d, we need to use UTC because the 24 hour range might
  84. * shift forward/backward depending on the user's timezone, or it might be
  85. * displayed as a day earlier/later
  86. */
  87. export function isDisplayUtc(datetime: DateTimeObject): boolean {
  88. if (datetime.utc) {
  89. return true;
  90. }
  91. const interval = getSeriesApiInterval(datetime);
  92. const hours = parsePeriodToHours(interval);
  93. return hours >= 24;
  94. }