anomalyDetectionFeedbackBanner.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {Fragment, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import Alert from 'sentry/components/alert';
  4. import {useDismissable} from 'sentry/components/banner';
  5. import {Button} from 'sentry/components/button';
  6. import {feedbackClient} from 'sentry/components/featureFeedback/feedbackModal';
  7. import {t} from 'sentry/locale';
  8. import ConfigStore from 'sentry/stores/configStore';
  9. import {space} from 'sentry/styles/space';
  10. import type {Organization} from 'sentry/types/organization';
  11. import {trackAnalytics} from 'sentry/utils/analytics';
  12. import {useFeedbackForm} from 'sentry/utils/useFeedbackForm';
  13. import type {Incident} from 'sentry/views/alerts/types';
  14. interface AnomalyDetectionFeedbackProps {
  15. id: string;
  16. organization: Organization;
  17. selectedIncident: Incident;
  18. }
  19. export default function AnomalyDetectionFeedbackBanner({
  20. id,
  21. organization,
  22. selectedIncident,
  23. }: AnomalyDetectionFeedbackProps) {
  24. const [isSubmitted, submit] = useDismissable(id);
  25. const openFeedbackForm = useFeedbackForm();
  26. const handleClick = useCallback(
  27. (anomalyCorrectlyIdentified: boolean) => {
  28. trackAnalytics('anomaly-detection.feedback-submitted', {
  29. choice_selected: anomalyCorrectlyIdentified,
  30. organization,
  31. incident_id: id,
  32. });
  33. feedbackClient.captureEvent({
  34. request: {
  35. url: window.location.href, // gives the full url (origin + pathname)
  36. },
  37. tags: {
  38. featureName: 'anomaly-detection-alerts-feedback',
  39. choice_selected: anomalyCorrectlyIdentified,
  40. incident_id: id,
  41. alert_rule_id: selectedIncident.alertRule.id,
  42. metric: selectedIncident.alertRule.query,
  43. sensitivity: selectedIncident.alertRule.sensitivity,
  44. direction: selectedIncident.alertRule.thresholdType,
  45. time_window: selectedIncident.alertRule.timeWindow,
  46. },
  47. user: ConfigStore.get('user'),
  48. level: 'info',
  49. message: 'Anomaly Detection Alerts Banner Feedback',
  50. });
  51. if (!anomalyCorrectlyIdentified && openFeedbackForm) {
  52. openFeedbackForm({
  53. messagePlaceholder: t('Why was this anomaly incorrect?'),
  54. tags: {
  55. ['feedback.source']: 'anomaly_detection_false_positive',
  56. ['feedback.owner']: 'ml-ai',
  57. },
  58. });
  59. }
  60. submit();
  61. },
  62. [
  63. id,
  64. organization,
  65. selectedIncident.alertRule.id,
  66. selectedIncident.alertRule.query,
  67. selectedIncident.alertRule.sensitivity,
  68. selectedIncident.alertRule.thresholdType,
  69. selectedIncident.alertRule.timeWindow,
  70. submit,
  71. openFeedbackForm,
  72. ]
  73. );
  74. if (isSubmitted) {
  75. return null;
  76. }
  77. return (
  78. <StyledAlert
  79. type="info"
  80. trailingItems={
  81. <Fragment>
  82. <StyledButton borderless onClick={() => handleClick(true)}>
  83. {t('Yes')}
  84. </StyledButton>
  85. <StyledButton borderless onClick={() => handleClick(false)}>
  86. {t('No')}
  87. </StyledButton>
  88. </Fragment>
  89. }
  90. showIcon
  91. >
  92. {t('Was the anomaly correctly identified?')}
  93. </StyledAlert>
  94. );
  95. }
  96. const StyledButton = styled(Button)`
  97. background-color: transparent;
  98. color: ${p => p.theme.alert.info.color};
  99. border: 1px solid ${p => p.theme.alert.info.border};
  100. &:hover,
  101. &:focus,
  102. &:active {
  103. color: ${p => p.theme.alert.info.color};
  104. border-color: ${p => p.theme.alert.info.borderHover};
  105. }
  106. `;
  107. const StyledAlert = styled(Alert)`
  108. margin: 0;
  109. padding-top: ${space(3)};
  110. padding-bottom: ${space(3)};
  111. `;