toolbarVisualize.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import type {SelectKey, SelectOption} from 'sentry/components/compactSelect';
  5. import {CompactSelect} from 'sentry/components/compactSelect';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {IconAdd} from 'sentry/icons';
  8. import {IconDelete} from 'sentry/icons/iconDelete';
  9. import {t} from 'sentry/locale';
  10. import {defined} from 'sentry/utils';
  11. import type {ParsedFunction} from 'sentry/utils/discover/fields';
  12. import {parseFunction, prettifyTagKey} from 'sentry/utils/discover/fields';
  13. import {ALLOWED_EXPLORE_VISUALIZE_AGGREGATES} from 'sentry/utils/fields';
  14. import {useSpanTags} from 'sentry/views/explore/contexts/spanTagsContext';
  15. import type {Visualize} from 'sentry/views/explore/hooks/useVisualizes';
  16. import {
  17. DEFAULT_VISUALIZATION,
  18. MAX_VISUALIZES,
  19. useVisualizes,
  20. } from 'sentry/views/explore/hooks/useVisualizes';
  21. import {ChartType} from 'sentry/views/insights/common/components/chart';
  22. import {
  23. ToolbarFooter,
  24. ToolbarFooterButton,
  25. ToolbarHeader,
  26. ToolbarHeaderButton,
  27. ToolbarLabel,
  28. ToolbarRow,
  29. ToolbarSection,
  30. } from './styles';
  31. type ParsedVisualize = {
  32. func: ParsedFunction;
  33. label: string;
  34. };
  35. interface ToolbarVisualizeProps {}
  36. export function ToolbarVisualize({}: ToolbarVisualizeProps) {
  37. const [visualizes, setVisualizes] = useVisualizes();
  38. const numberTags = useSpanTags('number');
  39. const parsedVisualizeGroups: ParsedVisualize[][] = useMemo(() => {
  40. return visualizes.map(visualize =>
  41. visualize.yAxes
  42. .map(parseFunction)
  43. .filter(defined)
  44. .map(func => {
  45. return {
  46. func,
  47. label: visualize.label,
  48. };
  49. })
  50. );
  51. }, [visualizes]);
  52. const fieldOptions: SelectOption<string>[] = useMemo(() => {
  53. const unknownOptions = parsedVisualizeGroups
  54. .flatMap(group =>
  55. group.flatMap(entry => {
  56. return entry.func.arguments;
  57. })
  58. )
  59. .filter(option => {
  60. return !numberTags.hasOwnProperty(option);
  61. });
  62. const options = [
  63. ...unknownOptions.map(option => ({
  64. label: prettifyTagKey(option),
  65. value: option,
  66. textValue: option,
  67. })),
  68. ...Object.values(numberTags).map(tag => {
  69. return {
  70. label: tag.name,
  71. value: tag.key,
  72. textValue: tag.name,
  73. };
  74. }),
  75. ];
  76. options.sort((a, b) => {
  77. if (a.label < b.label) {
  78. return -1;
  79. }
  80. if (a.label > b.label) {
  81. return 1;
  82. }
  83. return 0;
  84. });
  85. return options;
  86. }, [numberTags, parsedVisualizeGroups]);
  87. const aggregateOptions: SelectOption<string>[] = useMemo(() => {
  88. return ALLOWED_EXPLORE_VISUALIZE_AGGREGATES.map(aggregate => {
  89. return {
  90. label: aggregate,
  91. value: aggregate,
  92. textValue: aggregate,
  93. };
  94. });
  95. }, []);
  96. const addChart = useCallback(() => {
  97. setVisualizes([
  98. ...visualizes,
  99. {yAxes: [DEFAULT_VISUALIZATION], chartType: ChartType.LINE},
  100. ]);
  101. }, [setVisualizes, visualizes]);
  102. const addOverlay = useCallback(
  103. (group: number) => {
  104. const newVisualizes = visualizes.slice();
  105. newVisualizes[group].yAxes.push(DEFAULT_VISUALIZATION);
  106. setVisualizes(newVisualizes);
  107. },
  108. [setVisualizes, visualizes]
  109. );
  110. const setChartField = useCallback(
  111. (group: number, index: number, {value}: SelectOption<SelectKey>) => {
  112. const newVisualizes = visualizes.slice();
  113. newVisualizes[group].yAxes[index] =
  114. `${parsedVisualizeGroups[group][index].func.name}(${value})`;
  115. setVisualizes(newVisualizes);
  116. },
  117. [parsedVisualizeGroups, setVisualizes, visualizes]
  118. );
  119. const setChartAggregate = useCallback(
  120. (group: number, index: number, {value}: SelectOption<SelectKey>) => {
  121. const newVisualizes = visualizes.slice();
  122. newVisualizes[group].yAxes[index] =
  123. `${value}(${parsedVisualizeGroups[group][index].func.arguments[0]})`;
  124. setVisualizes(newVisualizes);
  125. },
  126. [parsedVisualizeGroups, setVisualizes, visualizes]
  127. );
  128. const deleteOverlay = useCallback(
  129. (group: number, index: number) => {
  130. const newVisualizes: Visualize[] = visualizes
  131. .map((visualize, orgGroup) => {
  132. if (group !== orgGroup) {
  133. return visualize;
  134. }
  135. return {
  136. ...visualize,
  137. yAxes: visualize.yAxes.filter((_, orgIndex) => index !== orgIndex),
  138. };
  139. })
  140. .filter(visualize => visualize.yAxes.length > 0);
  141. setVisualizes(newVisualizes);
  142. },
  143. [setVisualizes, visualizes]
  144. );
  145. const lastVisualization =
  146. parsedVisualizeGroups
  147. .map(parsedVisualizeGroup => parsedVisualizeGroup.length)
  148. .reduce((a, b) => a + b, 0) <= 1;
  149. const shouldRenderLabel = visualizes.length > 1;
  150. return (
  151. <ToolbarSection data-test-id="section-visualizes">
  152. <ToolbarHeader>
  153. <Tooltip
  154. position="right"
  155. title={t(
  156. 'Primary metric that appears in your chart. You can also overlay a series onto an existing chart or add an equation.'
  157. )}
  158. >
  159. <ToolbarLabel>{t('Visualize')}</ToolbarLabel>
  160. </Tooltip>
  161. <Tooltip title={t('Add a new chart')}>
  162. <ToolbarHeaderButton
  163. size="zero"
  164. icon={<IconAdd />}
  165. onClick={addChart}
  166. aria-label={t('Add Chart')}
  167. borderless
  168. disabled={visualizes.length >= MAX_VISUALIZES}
  169. />
  170. </Tooltip>
  171. </ToolbarHeader>
  172. <div>
  173. {parsedVisualizeGroups.map((parsedVisualizeGroup, group) => {
  174. return (
  175. <Fragment key={group}>
  176. {parsedVisualizeGroup.map((parsedVisualize, index) => (
  177. <ToolbarRow key={index}>
  178. {shouldRenderLabel && <ChartLabel>{parsedVisualize.label}</ChartLabel>}
  179. <ColumnCompactSelect
  180. searchable
  181. options={fieldOptions}
  182. value={parsedVisualize.func.arguments[0]}
  183. onChange={newField => setChartField(group, index, newField)}
  184. />
  185. <AggregateCompactSelect
  186. options={aggregateOptions}
  187. value={parsedVisualize.func.name}
  188. onChange={newAggregate =>
  189. setChartAggregate(group, index, newAggregate)
  190. }
  191. />
  192. <Button
  193. borderless
  194. icon={<IconDelete />}
  195. size="zero"
  196. disabled={lastVisualization}
  197. onClick={() => deleteOverlay(group, index)}
  198. aria-label={t('Remove Overlay')}
  199. />
  200. </ToolbarRow>
  201. ))}
  202. <ToolbarFooter>
  203. <ToolbarFooterButton
  204. borderless
  205. size="zero"
  206. icon={<IconAdd />}
  207. onClick={() => addOverlay(group)}
  208. priority="link"
  209. aria-label={t('Add Series')}
  210. >
  211. {t('Add Series')}
  212. </ToolbarFooterButton>
  213. </ToolbarFooter>
  214. </Fragment>
  215. );
  216. })}
  217. </div>
  218. </ToolbarSection>
  219. );
  220. }
  221. const ChartLabel = styled('div')`
  222. background-color: ${p => p.theme.purple100};
  223. border-radius: ${p => p.theme.borderRadius};
  224. text-align: center;
  225. width: 32px;
  226. color: ${p => p.theme.purple400};
  227. white-space: nowrap;
  228. font-weight: ${p => p.theme.fontWeightBold};
  229. align-content: center;
  230. `;
  231. const ColumnCompactSelect = styled(CompactSelect)`
  232. flex: 1 1;
  233. min-width: 0;
  234. > button {
  235. width: 100%;
  236. }
  237. `;
  238. const AggregateCompactSelect = styled(CompactSelect)`
  239. width: 100px;
  240. > button {
  241. width: 100%;
  242. }
  243. `;