groupingVariant.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import capitalize from 'lodash/capitalize';
  4. import Button from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import KeyValueList from 'sentry/components/events/interfaces/keyValueList';
  7. import QuestionTooltip from 'sentry/components/questionTooltip';
  8. import Tooltip from 'sentry/components/tooltip';
  9. import {IconCheckmark, IconClose} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {
  13. EventGroupComponent,
  14. EventGroupVariant,
  15. EventGroupVariantType,
  16. } from 'sentry/types';
  17. import GroupingComponent from './groupingComponent';
  18. import {hasNonContributingComponent} from './utils';
  19. type Props = {
  20. showGroupingConfig: boolean;
  21. variant: EventGroupVariant;
  22. };
  23. type State = {
  24. showNonContributing: boolean;
  25. };
  26. type VariantData = [string, React.ReactNode][];
  27. function addFingerprintInfo(data: VariantData, variant: EventGroupVariant) {
  28. if (variant.matched_rule) {
  29. data.push([
  30. t('Fingerprint rule'),
  31. <TextWithQuestionTooltip key="type">
  32. {variant.matched_rule}
  33. <QuestionTooltip
  34. size="xs"
  35. position="top"
  36. title={t('The server-side fingerprinting rule that produced the fingerprint.')}
  37. />
  38. </TextWithQuestionTooltip>,
  39. ]);
  40. }
  41. if (variant.values) {
  42. data.push([t('Fingerprint values'), variant.values]);
  43. }
  44. if (variant.client_values) {
  45. data.push([
  46. t('Client fingerprint values'),
  47. <TextWithQuestionTooltip key="type">
  48. {variant.client_values}
  49. <QuestionTooltip
  50. size="xs"
  51. position="top"
  52. title={t(
  53. 'The client sent a fingerprint that was overridden by a server-side fingerprinting rule.'
  54. )}
  55. />
  56. </TextWithQuestionTooltip>,
  57. ]);
  58. }
  59. }
  60. class GroupVariant extends Component<Props, State> {
  61. state: State = {
  62. showNonContributing: false,
  63. };
  64. handleShowNonContributing = () => {
  65. this.setState({showNonContributing: true});
  66. };
  67. handleHideNonContributing = () => {
  68. this.setState({showNonContributing: false});
  69. };
  70. getVariantData(): [VariantData, EventGroupComponent | undefined] {
  71. const {variant, showGroupingConfig} = this.props;
  72. const data: VariantData = [];
  73. let component: EventGroupComponent | undefined;
  74. if (!this.state.showNonContributing && variant.hash === null) {
  75. return [data, component];
  76. }
  77. if (variant.hash !== null) {
  78. data.push([
  79. t('Hash'),
  80. <TextWithQuestionTooltip key="hash">
  81. <Hash>{variant.hash}</Hash>
  82. <QuestionTooltip
  83. size="xs"
  84. position="top"
  85. title={t('Events with the same hash are grouped together')}
  86. />
  87. </TextWithQuestionTooltip>,
  88. ]);
  89. }
  90. if (variant.hashMismatch) {
  91. data.push([
  92. t('Hash mismatch'),
  93. t('hashing algorithm produced a hash that does not match the event'),
  94. ]);
  95. }
  96. switch (variant.type) {
  97. case EventGroupVariantType.COMPONENT:
  98. component = variant.component;
  99. data.push([
  100. t('Type'),
  101. <TextWithQuestionTooltip key="type">
  102. {variant.type}
  103. <QuestionTooltip
  104. size="xs"
  105. position="top"
  106. title={t(
  107. 'Uses a complex grouping algorithm taking event data into account'
  108. )}
  109. />
  110. </TextWithQuestionTooltip>,
  111. ]);
  112. if (showGroupingConfig && variant.config?.id) {
  113. data.push([t('Grouping Config'), variant.config.id]);
  114. }
  115. break;
  116. case EventGroupVariantType.CUSTOM_FINGERPRINT:
  117. data.push([
  118. t('Type'),
  119. <TextWithQuestionTooltip key="type">
  120. {variant.type}
  121. <QuestionTooltip
  122. size="xs"
  123. position="top"
  124. title={t('Overrides the default grouping by a custom fingerprinting rule')}
  125. />
  126. </TextWithQuestionTooltip>,
  127. ]);
  128. addFingerprintInfo(data, variant);
  129. break;
  130. case EventGroupVariantType.SALTED_COMPONENT:
  131. component = variant.component;
  132. data.push([
  133. t('Type'),
  134. <TextWithQuestionTooltip key="type">
  135. {variant.type}
  136. <QuestionTooltip
  137. size="xs"
  138. position="top"
  139. title={t(
  140. 'Uses a complex grouping algorithm taking event data and a fingerprint into account'
  141. )}
  142. />
  143. </TextWithQuestionTooltip>,
  144. ]);
  145. addFingerprintInfo(data, variant);
  146. if (showGroupingConfig && variant.config?.id) {
  147. data.push([t('Grouping Config'), variant.config.id]);
  148. }
  149. break;
  150. default:
  151. break;
  152. }
  153. if (component) {
  154. data.push([
  155. t('Grouping'),
  156. <GroupingTree key={component.id}>
  157. <GroupingComponent
  158. component={component}
  159. showNonContributing={this.state.showNonContributing}
  160. />
  161. </GroupingTree>,
  162. ]);
  163. }
  164. return [data, component];
  165. }
  166. renderTitle() {
  167. const {variant} = this.props;
  168. const isContributing = variant.hash !== null;
  169. let title: string;
  170. if (isContributing) {
  171. title = t('Contributing variant');
  172. } else {
  173. const hint = variant.component?.hint;
  174. if (hint) {
  175. title = t('Non-contributing variant: %s', hint);
  176. } else {
  177. title = t('Non-contributing variant');
  178. }
  179. }
  180. return (
  181. <Tooltip title={title}>
  182. <VariantTitle>
  183. <ContributionIcon isContributing={isContributing} />
  184. {t('By')}{' '}
  185. {variant.description
  186. ?.split(' ')
  187. .map(i => capitalize(i))
  188. .join(' ') ?? t('Nothing')}
  189. </VariantTitle>
  190. </Tooltip>
  191. );
  192. }
  193. renderContributionToggle() {
  194. const {showNonContributing} = this.state;
  195. return (
  196. <ContributingToggle merged active={showNonContributing ? 'all' : 'relevant'}>
  197. <Button barId="relevant" size="xs" onClick={this.handleHideNonContributing}>
  198. {t('Contributing values')}
  199. </Button>
  200. <Button barId="all" size="xs" onClick={this.handleShowNonContributing}>
  201. {t('All values')}
  202. </Button>
  203. </ContributingToggle>
  204. );
  205. }
  206. render() {
  207. const [data, component] = this.getVariantData();
  208. return (
  209. <VariantWrapper>
  210. <Header>
  211. {this.renderTitle()}
  212. {hasNonContributingComponent(component) && this.renderContributionToggle()}
  213. </Header>
  214. <KeyValueList
  215. data={data.map(d => ({
  216. key: d[0],
  217. subject: d[0],
  218. value: d[1],
  219. }))}
  220. isContextData
  221. isSorted={false}
  222. />
  223. </VariantWrapper>
  224. );
  225. }
  226. }
  227. const VariantWrapper = styled('div')`
  228. margin-bottom: ${space(4)};
  229. `;
  230. const Header = styled('div')`
  231. display: flex;
  232. align-items: center;
  233. justify-content: space-between;
  234. margin-bottom: ${space(2)};
  235. @media (max-width: ${p => p.theme.breakpoints.small}) {
  236. display: block;
  237. }
  238. `;
  239. const VariantTitle = styled('h5')`
  240. font-size: ${p => p.theme.fontSizeMedium};
  241. margin: 0;
  242. display: flex;
  243. align-items: center;
  244. `;
  245. const ContributionIcon = styled(({isContributing, ...p}) =>
  246. isContributing ? (
  247. <IconCheckmark size="sm" isCircled color="green300" {...p} />
  248. ) : (
  249. <IconClose size="sm" isCircled color="red300" {...p} />
  250. )
  251. )`
  252. margin-right: ${space(1)};
  253. `;
  254. const ContributingToggle = styled(ButtonBar)`
  255. justify-content: flex-end;
  256. @media (max-width: ${p => p.theme.breakpoints.small}) {
  257. margin-top: ${space(0.5)};
  258. }
  259. `;
  260. const GroupingTree = styled('div')`
  261. color: ${p => p.theme.textColor};
  262. `;
  263. const TextWithQuestionTooltip = styled('div')`
  264. display: grid;
  265. align-items: center;
  266. grid-template-columns: max-content min-content;
  267. gap: ${space(0.5)};
  268. `;
  269. const Hash = styled('span')`
  270. @media (max-width: ${p => p.theme.breakpoints.small}) {
  271. ${p => p.theme.overflowEllipsis};
  272. width: 210px;
  273. }
  274. `;
  275. export default GroupVariant;