bigNumber.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if (!bigNumberData) {
  27. return null;
  28. }
  29. return (
  30. <BigNumberWrapper>
  31. <LoadingScreen loading={isLoading} />
  32. <BigNumber>{bigNumberData}</BigNumber>
  33. </BigNumberWrapper>
  34. );
  35. }
  36. export function getBigNumberData(
  37. data: MetricsQueryApiResponse,
  38. queries: MetricsQueryApiQueryParams[]
  39. ): string {
  40. const filteredQueries = queries.filter(
  41. query => !isMetricFormula(query)
  42. ) as MetricsQueryApiRequestQuery[];
  43. const firstQuery = filteredQueries[0];
  44. const value = data.data[0][0].totals;
  45. return formatMetricsUsingUnitAndOp(
  46. value,
  47. parseMRI(firstQuery.mri)?.unit!,
  48. firstQuery.op
  49. );
  50. }
  51. const BigNumberWrapper = styled('div')`
  52. height: 100%;
  53. width: 100%;
  54. overflow: hidden;
  55. `;
  56. export const BigNumber = styled('div')`
  57. line-height: 1;
  58. display: inline-flex;
  59. flex: 1;
  60. width: 100%;
  61. min-height: 0;
  62. font-size: 32px;
  63. color: ${p => p.theme.headingColor};
  64. padding: ${space(1)} ${space(3)} ${space(3)} ${space(3)};
  65. * {
  66. text-align: left !important;
  67. }
  68. `;