scratchpad.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {useCallback, useLayoutEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as echarts from 'echarts/core';
  4. import {space} from 'sentry/styles/space';
  5. import {getMetricsCorrelationSpanUrl} from 'sentry/utils/metrics';
  6. import {hasDDMExperimentalFeature} from 'sentry/utils/metrics/features';
  7. import type {MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
  8. import type {MetricsQueryApiQueryParams} from 'sentry/utils/metrics/useMetricsQuery';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import useProjects from 'sentry/utils/useProjects';
  12. import useRouter from 'sentry/utils/useRouter';
  13. import {DDM_CHART_GROUP, MIN_WIDGET_WIDTH} from 'sentry/views/ddm/constants';
  14. import {useDDMContext} from 'sentry/views/ddm/context';
  15. import {useGetCachedChartPalette} from 'sentry/views/ddm/utils/metricsChartPalette';
  16. import type {Sample} from './widget';
  17. import {MetricWidget} from './widget';
  18. function widgetToQuery(widget: MetricWidgetQueryParams): MetricsQueryApiQueryParams {
  19. return {
  20. mri: widget.mri,
  21. op: widget.op,
  22. groupBy: widget.groupBy,
  23. query: widget.query,
  24. };
  25. }
  26. export function MetricScratchpad() {
  27. const {
  28. setSelectedWidgetIndex,
  29. selectedWidgetIndex,
  30. widgets,
  31. updateWidget,
  32. showQuerySymbols,
  33. highlightedSampleId,
  34. focusArea,
  35. isMultiChartMode,
  36. } = useDDMContext();
  37. const {selection} = usePageFilters();
  38. const router = useRouter();
  39. const organization = useOrganization();
  40. const {projects} = useProjects();
  41. const getChartPalette = useGetCachedChartPalette();
  42. // Make sure all charts are connected to the same group whenever the widgets definition changes
  43. useLayoutEffect(() => {
  44. echarts.connect(DDM_CHART_GROUP);
  45. }, [widgets]);
  46. const handleChange = useCallback(
  47. (index: number, widget: Partial<MetricWidgetQueryParams>) => {
  48. updateWidget(index, widget);
  49. },
  50. [updateWidget]
  51. );
  52. const handleSampleClick = useCallback(
  53. (sample: Sample) => {
  54. const project = projects.find(p => parseInt(p.id, 10) === sample.projectId);
  55. router.push(
  56. getMetricsCorrelationSpanUrl(
  57. organization,
  58. project?.slug,
  59. sample.spanId,
  60. sample.transactionId,
  61. sample.transactionSpanId
  62. )
  63. );
  64. },
  65. [projects, router, organization]
  66. );
  67. const firstWidget = widgets[0];
  68. const Wrapper =
  69. !isMultiChartMode || widgets.length === 1
  70. ? StyledSingleWidgetWrapper
  71. : StyledMetricDashboard;
  72. return (
  73. <Wrapper>
  74. {isMultiChartMode ? (
  75. widgets.map((widget, index) => (
  76. <MetricWidget
  77. key={index}
  78. index={index}
  79. getChartPalette={getChartPalette}
  80. onSelect={setSelectedWidgetIndex}
  81. displayType={widget.displayType}
  82. focusedSeries={widget.focusedSeries}
  83. tableSort={widget.sort}
  84. queries={[widgetToQuery(widget)]}
  85. isSelected={selectedWidgetIndex === index}
  86. hasSiblings={widgets.length > 1}
  87. onChange={handleChange}
  88. filters={selection}
  89. focusArea={focusArea}
  90. showQuerySymbols={showQuerySymbols}
  91. onSampleClick={handleSampleClick}
  92. chartHeight={hasDDMExperimentalFeature(organization) ? 200 : 300}
  93. highlightedSampleId={
  94. selectedWidgetIndex === index ? highlightedSampleId : undefined
  95. }
  96. />
  97. ))
  98. ) : (
  99. <MetricWidget
  100. index={0}
  101. getChartPalette={getChartPalette}
  102. onSelect={setSelectedWidgetIndex}
  103. displayType={firstWidget.displayType}
  104. focusedSeries={firstWidget.focusedSeries}
  105. tableSort={firstWidget.sort}
  106. queries={widgets.map(widgetToQuery)}
  107. isSelected
  108. hasSiblings={false}
  109. onChange={handleChange}
  110. filters={selection}
  111. focusArea={focusArea}
  112. showQuerySymbols={false}
  113. onSampleClick={handleSampleClick}
  114. chartHeight={hasDDMExperimentalFeature(organization) ? 200 : 300}
  115. highlightedSampleId={highlightedSampleId}
  116. />
  117. )}
  118. </Wrapper>
  119. );
  120. }
  121. const StyledMetricDashboard = styled('div')`
  122. display: grid;
  123. grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  124. gap: ${space(2)};
  125. @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
  126. grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  127. }
  128. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  129. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  130. }
  131. grid-auto-rows: auto;
  132. `;
  133. const StyledSingleWidgetWrapper = styled('div')`
  134. display: grid;
  135. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  136. `;