123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import {DateTimeObject, getSeriesApiInterval} from 'sentry/components/charts/utils';
- import {DATA_CATEGORY_INFO} from 'sentry/constants';
- import {DataCategoryInfo} from 'sentry/types';
- import {formatBytesBase10} from 'sentry/utils';
- import {parsePeriodToHours} from 'sentry/utils/dates';
- export const MILLION = 10 ** 6;
- export const BILLION = 10 ** 9;
- export const GIGABYTE = 10 ** 9;
- type FormatOptions = {
-
- isAbbreviated?: boolean;
-
- useUnitScaling?: boolean;
- };
- export function formatUsageWithUnits(
- usageQuantity: number = 0,
- dataCategory: DataCategoryInfo['plural'],
- options: FormatOptions = {isAbbreviated: false, useUnitScaling: false}
- ) {
- if (dataCategory !== DATA_CATEGORY_INFO.attachment.plural) {
- return options.isAbbreviated
- ? abbreviateUsageNumber(usageQuantity)
- : usageQuantity.toLocaleString();
- }
- if (options.useUnitScaling) {
- return formatBytesBase10(usageQuantity);
- }
- const usageGb = usageQuantity / GIGABYTE;
- return options.isAbbreviated
- ? `${abbreviateUsageNumber(usageGb)} GB`
- : `${usageGb.toLocaleString(undefined, {maximumFractionDigits: 2})} GB`;
- }
- export function getFormatUsageOptions(
- dataCategory: DataCategoryInfo['plural']
- ): FormatOptions {
- return {
- isAbbreviated: dataCategory !== DATA_CATEGORY_INFO.attachment.plural,
- useUnitScaling: dataCategory === DATA_CATEGORY_INFO.attachment.plural,
- };
- }
- export function abbreviateUsageNumber(n: number) {
- if (n >= BILLION) {
- return (n / BILLION).toLocaleString(undefined, {maximumFractionDigits: 2}) + 'B';
- }
- if (n >= MILLION) {
- return (n / MILLION).toLocaleString(undefined, {maximumFractionDigits: 1}) + 'M';
- }
- if (n >= 1000) {
- return (n / 1000).toFixed().toLocaleString() + 'K';
- }
-
- return n.toFixed().toLocaleString();
- }
- export function isDisplayUtc(datetime: DateTimeObject): boolean {
- if (datetime.utc) {
- return true;
- }
- const interval = getSeriesApiInterval(datetime);
- const hours = parsePeriodToHours(interval);
- return hours >= 24;
- }
- export function getOffsetFromCursor(cursor?: string) {
- const offset = Number(cursor?.split(':')[1]);
- return isNaN(offset) ? 0 : offset;
- }
- export function getPaginationPageLink({
- numRows,
- pageSize,
- offset,
- }: {
- numRows: number;
- offset: number;
- pageSize: number;
- }) {
- const prevOffset = offset - pageSize;
- const nextOffset = offset + pageSize;
- return `<link>; rel="previous"; results="${prevOffset >= 0}"; cursor="0:${Math.max(
- 0,
- prevOffset
- )}:1", <link>; rel="next"; results="${
- nextOffset < numRows
- }"; cursor="0:${nextOffset}:0"`;
- }
|