bigNumber.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {space} from 'sentry/styles/space';
  4. import type {MetricsQueryApiResponse} from 'sentry/types';
  5. import {formatMetricsUsingUnitAndOp} from 'sentry/utils/metrics/formatters';
  6. import {parseMRI} from 'sentry/utils/metrics/mri';
  7. import {
  8. isMetricFormula,
  9. type MetricsQueryApiQueryParams,
  10. type MetricsQueryApiRequestQuery,
  11. } from 'sentry/utils/metrics/useMetricsQuery';
  12. import {LoadingScreen} from 'sentry/views/dashboards/widgetCard/widgetCardChartContainer';
  13. interface MetricBigNumberContainerProps {
  14. isLoading: boolean;
  15. metricQueries: MetricsQueryApiQueryParams[];
  16. timeseriesData?: MetricsQueryApiResponse;
  17. }
  18. export function MetricBigNumberContainer({
  19. timeseriesData,
  20. metricQueries,
  21. isLoading,
  22. }: MetricBigNumberContainerProps) {
  23. const bigNumberData = useMemo(() => {
  24. return timeseriesData ? getBigNumberData(timeseriesData, metricQueries) : undefined;
  25. }, [timeseriesData, metricQueries]);
  26. return (
  27. <BigNumberWrapper>
  28. <LoadingScreen loading={isLoading} />
  29. <BigNumber>{bigNumberData}</BigNumber>
  30. </BigNumberWrapper>
  31. );
  32. }
  33. export function getBigNumberData(
  34. data: MetricsQueryApiResponse,
  35. queries: MetricsQueryApiQueryParams[]
  36. ): string {
  37. const filteredQueries = queries.filter(
  38. query => !isMetricFormula(query)
  39. ) as MetricsQueryApiRequestQuery[];
  40. const firstQuery = filteredQueries[0];
  41. const value = data.data[0][0].totals;
  42. return formatMetricsUsingUnitAndOp(
  43. value,
  44. parseMRI(firstQuery.mri)?.unit!,
  45. firstQuery.op
  46. );
  47. }
  48. const BigNumberWrapper = styled('div')`
  49. height: 100%;
  50. width: 100%;
  51. overflow: hidden;
  52. `;
  53. export const BigNumber = styled('div')`
  54. line-height: 1;
  55. display: inline-flex;
  56. flex: 1;
  57. width: 100%;
  58. min-height: 0;
  59. font-size: 32px;
  60. color: ${p => p.theme.headingColor};
  61. padding: ${space(1)} ${space(3)} ${space(3)} ${space(3)};
  62. * {
  63. text-align: left !important;
  64. }
  65. `;