dashboard.spec.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import type {MRI} from 'sentry/types/metrics';
  2. import {
  3. convertToDashboardWidget,
  4. getWidgetQuery,
  5. toDisplayType,
  6. } from 'sentry/utils/metrics/dashboard';
  7. import {MetricDisplayType} from 'sentry/utils/metrics/types';
  8. import {DisplayType} from 'sentry/views/dashboards/types';
  9. describe('convertToDashboardWidget', () => {
  10. it('should convert a metrics query to a dashboard widget (metrics mri, with grouping)', () => {
  11. expect(
  12. convertToDashboardWidget(
  13. [
  14. {
  15. groupBy: ['project'],
  16. query: 'event.type:transaction',
  17. mri: 'c:custom/login@second',
  18. op: 'p95',
  19. id: 1,
  20. },
  21. ],
  22. MetricDisplayType.AREA
  23. )
  24. ).toEqual({
  25. title: '',
  26. displayType: DisplayType.AREA,
  27. widgetType: 'custom-metrics',
  28. limit: 10,
  29. queries: [
  30. {
  31. name: '1',
  32. aggregates: ['p95(c:custom/login@second)'],
  33. columns: ['project'],
  34. fields: ['p95(c:custom/login@second)'],
  35. conditions: 'event.type:transaction',
  36. orderby: undefined,
  37. },
  38. ],
  39. });
  40. });
  41. it('should convert a metrics query to a dashboard widget (transaction mri, with grouping)', () => {
  42. expect(
  43. convertToDashboardWidget(
  44. [
  45. {
  46. groupBy: [],
  47. query: '',
  48. mri: 'd:transactions/measurements.duration@second',
  49. op: 'p95',
  50. },
  51. ],
  52. MetricDisplayType.BAR
  53. )
  54. ).toEqual({
  55. title: '',
  56. displayType: DisplayType.BAR,
  57. widgetType: 'custom-metrics',
  58. limit: 10,
  59. queries: [
  60. {
  61. name: '',
  62. aggregates: ['p95(d:transactions/measurements.duration@second)'],
  63. columns: [],
  64. fields: ['p95(d:transactions/measurements.duration@second)'],
  65. conditions: '',
  66. orderby: undefined,
  67. },
  68. ],
  69. });
  70. });
  71. it('should convert a metrics formula to a dashboard widget (transaction mri, with grouping)', () => {
  72. expect(
  73. convertToDashboardWidget(
  74. [
  75. {
  76. id: 0,
  77. groupBy: [],
  78. query: '',
  79. mri: 'd:transactions/measurements.duration@second',
  80. op: 'p95',
  81. isHidden: true,
  82. },
  83. {
  84. formula: '$b / 2',
  85. isHidden: false,
  86. },
  87. ],
  88. MetricDisplayType.BAR
  89. )
  90. ).toEqual({
  91. title: '',
  92. displayType: DisplayType.BAR,
  93. widgetType: 'custom-metrics',
  94. limit: 10,
  95. queries: [
  96. {
  97. name: '0',
  98. aggregates: ['p95(d:transactions/measurements.duration@second)'],
  99. columns: [],
  100. fields: ['p95(d:transactions/measurements.duration@second)'],
  101. conditions: '',
  102. orderby: undefined,
  103. isHidden: true,
  104. },
  105. {
  106. name: '',
  107. aggregates: ['equation|$b / 2'],
  108. columns: [],
  109. fields: ['equation|$b / 2'],
  110. conditions: '',
  111. orderby: undefined,
  112. isHidden: false,
  113. },
  114. ],
  115. });
  116. });
  117. });
  118. describe('getWidgetQuery', () => {
  119. const metricsQueryBase = {
  120. datetime: {start: '2022-01-01', end: '2022-01-31', period: null, utc: false},
  121. environments: ['production'],
  122. projects: [1],
  123. groupBy: [],
  124. query: '',
  125. };
  126. it('should return the correct widget query object', () => {
  127. // Arrange
  128. const metricsQuery = {
  129. ...metricsQueryBase,
  130. mri: 'd:custom/sentry.events.symbolicator.query_task@second' as MRI,
  131. op: 'sum',
  132. query: 'status = "success"',
  133. title: 'Example Widget',
  134. };
  135. const expectedWidgetQuery = {
  136. name: '',
  137. aggregates: ['sum(d:custom/sentry.events.symbolicator.query_task@second)'],
  138. columns: [],
  139. fields: ['sum(d:custom/sentry.events.symbolicator.query_task@second)'],
  140. conditions: 'status = "success"',
  141. orderby: undefined,
  142. };
  143. expect(getWidgetQuery(metricsQuery)).toEqual(expectedWidgetQuery);
  144. });
  145. });
  146. describe('toDisplayType', () => {
  147. it('should return the displayType if it is a valid MetricDisplayType', () => {
  148. expect(DisplayType.BAR).toEqual(toDisplayType(DisplayType.BAR));
  149. expect(DisplayType.LINE).toEqual(toDisplayType(DisplayType.LINE));
  150. expect(DisplayType.AREA).toEqual(toDisplayType(DisplayType.AREA));
  151. expect(DisplayType.BIG_NUMBER).toEqual(toDisplayType(DisplayType.BIG_NUMBER));
  152. expect(DisplayType.TABLE).toEqual(toDisplayType(DisplayType.TABLE));
  153. expect(DisplayType.TOP_N).toEqual(toDisplayType(DisplayType.TOP_N));
  154. });
  155. it('should return DisplayType.LINE if the displayType is invalid or unsupported', () => {
  156. expect(DisplayType.LINE).toEqual(toDisplayType(undefined));
  157. expect(DisplayType.LINE).toEqual(toDisplayType(''));
  158. });
  159. });