index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {getInterval} from 'sentry/components/charts/utils';
  4. import {CompactSelect} from 'sentry/components/compactSelect';
  5. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  9. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import {useChartInterval} from 'sentry/views/explore/hooks/useChartInterval';
  12. import {useChartType} from 'sentry/views/explore/hooks/useChartType';
  13. import {useVisualizes} from 'sentry/views/explore/hooks/useVisualizes';
  14. import Chart, {ChartType} from 'sentry/views/insights/common/components/chart';
  15. import ChartPanel from 'sentry/views/insights/common/components/chartPanel';
  16. import {useSpanIndexedSeries} from 'sentry/views/insights/common/queries/useDiscoverSeries';
  17. import {CHART_HEIGHT} from 'sentry/views/insights/database/settings';
  18. interface ExploreChartsProps {
  19. query: string;
  20. }
  21. const exploreChartTypeOptions = [
  22. {
  23. value: ChartType.LINE,
  24. label: t('Line'),
  25. },
  26. {
  27. value: ChartType.AREA,
  28. label: t('Area'),
  29. },
  30. {
  31. value: ChartType.BAR,
  32. label: t('Bar'),
  33. },
  34. ];
  35. // TODO: Update to support aggregate mode and multiple queries / visualizations
  36. export function ExploreCharts({query}: ExploreChartsProps) {
  37. const pageFilters = usePageFilters();
  38. const [visualizes] = useVisualizes();
  39. const [chartType, setChartType] = useChartType();
  40. const [interval, setInterval, intervalOptions] = useChartInterval();
  41. const series = useSpanIndexedSeries(
  42. {
  43. search: new MutableSearch(query ?? ''),
  44. yAxis: visualizes,
  45. interval: interval ?? getInterval(pageFilters.selection.datetime, 'metrics'),
  46. enabled: true,
  47. },
  48. 'api.explorer.stats'
  49. );
  50. return (
  51. <Fragment>
  52. {visualizes.map((visualize, index) => (
  53. <ChartContainer key={index}>
  54. <ChartPanel>
  55. <ChartHeader>
  56. <ChartTitle>{visualize}</ChartTitle>
  57. <ChartSettingsContainer>
  58. <CompactSelect
  59. size="xs"
  60. triggerProps={{prefix: t('Display')}}
  61. value={chartType}
  62. options={exploreChartTypeOptions}
  63. onChange={newChartType => setChartType(newChartType.value)}
  64. />
  65. <CompactSelect
  66. size="xs"
  67. value={interval}
  68. onChange={({value}) => setInterval(value)}
  69. triggerProps={{
  70. prefix: t('Interval'),
  71. }}
  72. options={intervalOptions}
  73. />
  74. </ChartSettingsContainer>
  75. </ChartHeader>
  76. <Chart
  77. height={CHART_HEIGHT}
  78. grid={{
  79. left: '0',
  80. right: '0',
  81. top: '8px',
  82. bottom: '0',
  83. }}
  84. data={[series.data[visualize]]}
  85. error={series.error}
  86. loading={series.isPending}
  87. chartColors={CHART_PALETTE[2]}
  88. type={chartType}
  89. aggregateOutputFormat={aggregateOutputType(visualize)}
  90. showLegend
  91. />
  92. </ChartPanel>
  93. </ChartContainer>
  94. ))}
  95. </Fragment>
  96. );
  97. }
  98. const ChartContainer = styled('div')`
  99. display: grid;
  100. gap: 0;
  101. grid-template-columns: 1fr;
  102. margin-bottom: ${space(3)};
  103. `;
  104. const ChartHeader = styled('div')`
  105. display: flex;
  106. align-items: flex-start;
  107. justify-content: space-between;
  108. margin-bottom: ${space(1)};
  109. `;
  110. const ChartTitle = styled('div')`
  111. ${p => p.theme.text.cardTitle}
  112. `;
  113. const ChartSettingsContainer = styled('div')`
  114. display: flex;
  115. align-items: center;
  116. gap: ${space(0.5)};
  117. `;