metricHistory.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {useMemo} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import CollapsePanel from 'sentry/components/collapsePanel';
  5. import {PanelTable} from 'sentry/components/panels/panelTable';
  6. import {t, tn} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import type {ActivationTriggerActivity, AlertRuleActivation} from 'sentry/types/alerts';
  9. // import {ActivationTrigger} from 'sentry/types/alerts';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import MetricAlertActivity from 'sentry/views/alerts/rules/metric/details/metricActivity';
  12. import MetricHistoryActivation from 'sentry/views/alerts/rules/metric/details/metricHistoryActivation';
  13. import type {Incident} from 'sentry/views/alerts/types';
  14. const COLLAPSE_COUNT = 3;
  15. type Props = {
  16. activations?: AlertRuleActivation[];
  17. incidents?: Incident[];
  18. };
  19. function MetricHistory({incidents}: Props) {
  20. const organization = useOrganization();
  21. const sortedActivity = useMemo(() => {
  22. const filteredIncidents = (incidents ?? []).filter(
  23. incident => incident.activities?.length
  24. );
  25. const activationTriggers: ActivationTriggerActivity[] = [];
  26. // NOTE: disabling start/finish trigger rows for now until we've determined whether its
  27. // valuable during EA
  28. // activations?.forEach(activation => {
  29. // activationTriggers.push({
  30. // type: ActivationTrigger.ACTIVATED,
  31. // activator: activation.activator,
  32. // conditionType: activation.conditionType,
  33. // dateCreated: activation.dateCreated,
  34. // });
  35. // if (activation.isComplete) {
  36. // activationTriggers.push({
  37. // type: ActivationTrigger.FINISHED,
  38. // activator: activation.activator,
  39. // conditionType: activation.conditionType,
  40. // dateCreated: activation.finishedAt,
  41. // });
  42. // }
  43. // });
  44. return [...filteredIncidents, ...activationTriggers].sort((a, b) =>
  45. a.dateCreated > b.dateCreated ? -1 : 1
  46. );
  47. }, [incidents]);
  48. const numOfActivities = sortedActivity.length;
  49. return (
  50. <CollapsePanel
  51. items={numOfActivities}
  52. collapseCount={COLLAPSE_COUNT}
  53. disableBorder={false}
  54. buttonTitle={tn('Hidden Alert', 'Hidden Alerts', numOfActivities - COLLAPSE_COUNT)}
  55. >
  56. {({isExpanded, showMoreButton}) => (
  57. <div>
  58. <StyledPanelTable
  59. headers={[t('Alert'), t('Reason'), t('Duration'), t('Date Triggered')]}
  60. isEmpty={!numOfActivities}
  61. emptyMessage={t('No alerts triggered during this time.')}
  62. expanded={numOfActivities <= COLLAPSE_COUNT || isExpanded}
  63. data-test-id={'history-table'}
  64. >
  65. {sortedActivity.map((item, idx) => {
  66. if (idx >= COLLAPSE_COUNT && !isExpanded) {
  67. return null;
  68. }
  69. if ('activator' in item) {
  70. return (
  71. <MetricHistoryActivation
  72. key={`${item.type}-${item.activator}`}
  73. activationActivity={item}
  74. organization={organization}
  75. />
  76. );
  77. }
  78. return (
  79. <MetricAlertActivity
  80. key={idx}
  81. incident={item}
  82. organization={organization}
  83. />
  84. );
  85. })}
  86. </StyledPanelTable>
  87. {showMoreButton}
  88. </div>
  89. )}
  90. </CollapsePanel>
  91. );
  92. }
  93. export default MetricHistory;
  94. const StyledPanelTable = styled(PanelTable)<{expanded: boolean; isEmpty: boolean}>`
  95. grid-template-columns: max-content 1fr repeat(2, max-content);
  96. & > div {
  97. padding: ${space(1)} ${space(2)};
  98. }
  99. div:last-of-type {
  100. padding: ${p => p.isEmpty && `48px ${space(1)}`};
  101. }
  102. ${p =>
  103. !p.expanded &&
  104. css`
  105. margin-bottom: 0px;
  106. border-bottom-left-radius: 0px;
  107. border-bottom-right-radius: 0px;
  108. border-bottom: none;
  109. `}
  110. `;