useFlagSeries.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {useTheme} from '@emotion/react';
  2. import moment from 'moment-timezone';
  3. import MarkLine from 'sentry/components/charts/components/markLine';
  4. import {t} from 'sentry/locale';
  5. import type {Event} from 'sentry/types/event';
  6. import {getFormattedDate} from 'sentry/utils/dates';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {hydrateToFlagSeries} from 'sentry/views/issueDetails/streamline/featureFlagUtils';
  10. import {useOrganizationFlagLog} from 'sentry/views/issueDetails/streamline/useOrganizationFlagLog';
  11. interface FlagSeriesProps {
  12. event: Event | undefined;
  13. query: Record<string, any>;
  14. }
  15. export default function useFlagSeries({query = {}, event}: FlagSeriesProps) {
  16. const theme = useTheme();
  17. const organization = useOrganization();
  18. const {
  19. data: rawFlagData,
  20. isError,
  21. isPending,
  22. } = useOrganizationFlagLog({organization, query});
  23. const {selection} = usePageFilters();
  24. if (!rawFlagData || isError || isPending) {
  25. return {
  26. seriesName: t('Feature Flags'),
  27. markLine: {},
  28. data: [],
  29. };
  30. }
  31. const hydratedFlagData = hydrateToFlagSeries(rawFlagData);
  32. const evaluatedFlagNames = event?.contexts.flags?.values.map(f => f.flag);
  33. const intersectionFlags = hydratedFlagData.filter(f =>
  34. evaluatedFlagNames?.includes(f.name)
  35. );
  36. // create a markline series using hydrated flag data
  37. const markLine = MarkLine({
  38. animation: false,
  39. lineStyle: {
  40. color: theme.blue300,
  41. opacity: 0.3,
  42. type: 'solid',
  43. },
  44. label: {
  45. show: false,
  46. },
  47. data: intersectionFlags,
  48. tooltip: {
  49. trigger: 'item',
  50. formatter: ({data}: any) => {
  51. const time = getFormattedDate(data.xAxis, 'MMM D, YYYY LT z', {
  52. local: !selection.datetime.utc,
  53. });
  54. return [
  55. '<div class="tooltip-series">',
  56. `<div><span class="tooltip-label"><strong>${t(
  57. 'Feature Flag'
  58. )}</strong></span></div>`,
  59. `<span class="tooltip-label-align-start"><code class="tooltip-code-no-margin">${data.name}</code>${data.label.formatter()}</span>`,
  60. '</div>',
  61. '<div class="tooltip-footer">',
  62. time,
  63. event?.dateCreated &&
  64. ` (${moment(time).from(event.dateCreated, true)} ${t('before this event')})`,
  65. '</div>',
  66. '<div class="tooltip-arrow"></div>',
  67. ].join('');
  68. },
  69. },
  70. });
  71. return {
  72. seriesName: t('Feature Flags'),
  73. data: [],
  74. color: theme.blue200,
  75. markLine,
  76. type: 'line', // use this type so the bar chart doesn't shrink/grow
  77. };
  78. }