utils.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import {MetricDisplayType, MetricExpressionType} from 'sentry/utils/metrics/types';
  2. import type {DashboardMetricsExpression} from 'sentry/views/dashboards/metrics/types';
  3. import type {Widget} from 'sentry/views/dashboards/types';
  4. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  5. import {expressionsToWidget, getMetricExpressions, toMetricDisplayType} from './utils';
  6. describe('getMetricExpressions function', () => {
  7. it('should return a query', () => {
  8. const widget = {
  9. queries: [
  10. {
  11. aggregates: ['avg(d:transactions/duration@milisecond)'],
  12. conditions: 'foo:bar',
  13. columns: ['release'],
  14. name: 'query_1',
  15. orderby: 'asc',
  16. },
  17. ],
  18. } as Widget;
  19. const metricQueries = getMetricExpressions(widget);
  20. expect(metricQueries).toEqual([
  21. {
  22. groupBy: ['release'],
  23. id: 0,
  24. mri: 'd:transactions/duration@milisecond',
  25. op: 'avg',
  26. query: 'foo:bar',
  27. type: MetricExpressionType.QUERY,
  28. orderBy: 'asc',
  29. isHidden: false,
  30. } satisfies DashboardMetricsExpression,
  31. ]);
  32. });
  33. it('should return an equation', () => {
  34. const widget = {
  35. queries: [
  36. {
  37. aggregates: ['equation|$a + $b'],
  38. conditions: 'foo:bar',
  39. columns: ['release'],
  40. name: 'query_1',
  41. },
  42. ],
  43. } as Widget;
  44. const metricQueries = getMetricExpressions(widget);
  45. expect(metricQueries).toEqual([
  46. {
  47. id: 0,
  48. formula: '$a + $b',
  49. type: MetricExpressionType.EQUATION,
  50. isHidden: false,
  51. } satisfies DashboardMetricsExpression,
  52. ]);
  53. });
  54. it('should return metricQueries with correct parameters with dashboardFilters', () => {
  55. const widget = {
  56. queries: [
  57. {
  58. aggregates: ['avg(d:transactions/duration@milisecond)'],
  59. conditions: 'foo:bar',
  60. columns: ['release'],
  61. name: '0',
  62. orderby: 'desc',
  63. },
  64. {
  65. aggregates: ['avg(d:transactions/duration@milisecond)'],
  66. conditions: 'foo:baz',
  67. columns: [],
  68. name: '1',
  69. orderby: '',
  70. },
  71. ],
  72. } as Widget;
  73. const metricQueries = getMetricExpressions(widget, {release: ['1.0']});
  74. expect(metricQueries).toEqual([
  75. {
  76. groupBy: ['release'],
  77. id: 0,
  78. mri: 'd:transactions/duration@milisecond',
  79. op: 'avg',
  80. query: 'foo:bar release:1.0',
  81. type: MetricExpressionType.QUERY,
  82. orderBy: 'desc',
  83. isHidden: false,
  84. } satisfies DashboardMetricsExpression,
  85. {
  86. groupBy: [],
  87. id: 1,
  88. mri: 'd:transactions/duration@milisecond',
  89. op: 'avg',
  90. query: 'foo:baz release:1.0',
  91. type: MetricExpressionType.QUERY,
  92. orderBy: undefined,
  93. isHidden: false,
  94. } satisfies DashboardMetricsExpression,
  95. ]);
  96. });
  97. it('should return metricQueries with correct parameters with multiple dashboardFilters', () => {
  98. const widget = {
  99. queries: [
  100. {
  101. aggregates: ['avg(d:transactions/duration@milisecond)'],
  102. conditions: '',
  103. columns: ['release'],
  104. name: '1',
  105. },
  106. ],
  107. } as Widget;
  108. const metricQueries = getMetricExpressions(widget, {release: ['1.0', '2.0']});
  109. expect(metricQueries).toEqual([
  110. {
  111. groupBy: ['release'],
  112. id: 1,
  113. mri: 'd:transactions/duration@milisecond',
  114. op: 'avg',
  115. query: 'release:[1.0,2.0]',
  116. type: MetricExpressionType.QUERY,
  117. orderBy: undefined,
  118. isHidden: false,
  119. } satisfies DashboardMetricsExpression,
  120. ]);
  121. });
  122. });
  123. describe('toMetricDisplayType', () => {
  124. it('should return the displayType if it is a valid MetricDisplayType', () => {
  125. expect(MetricDisplayType.BAR).toEqual(toMetricDisplayType(DisplayType.BAR));
  126. expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.LINE));
  127. expect(MetricDisplayType.AREA).toEqual(toMetricDisplayType(DisplayType.AREA));
  128. });
  129. it('should return MetricDisplayType.LINE if the displayType is invalid or unsupported', () => {
  130. expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.BIG_NUMBER));
  131. expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.TABLE));
  132. expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(DisplayType.TOP_N));
  133. expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(undefined));
  134. expect(MetricDisplayType.LINE).toEqual(toMetricDisplayType(''));
  135. });
  136. });
  137. describe('expressionsToWidget', () => {
  138. it('should return a widget with queries', () => {
  139. const metricExpressions = [
  140. {
  141. groupBy: ['release'],
  142. id: 0,
  143. mri: 'd:transactions/duration@milisecond',
  144. op: 'avg',
  145. query: 'foo:bar',
  146. type: MetricExpressionType.QUERY,
  147. orderBy: 'asc',
  148. isHidden: true,
  149. } satisfies DashboardMetricsExpression,
  150. ];
  151. const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
  152. expect(widget).toEqual({
  153. title: 'title',
  154. displayType: DisplayType.LINE,
  155. interval: '5m',
  156. limit: 10,
  157. widgetType: WidgetType.METRICS,
  158. queries: [
  159. {
  160. aggregates: ['avg(d:transactions/duration@milisecond)'],
  161. fields: ['avg(d:transactions/duration@milisecond)'],
  162. conditions: 'foo:bar',
  163. columns: ['release'],
  164. name: '0',
  165. orderby: 'asc',
  166. isHidden: true,
  167. },
  168. ],
  169. } satisfies Widget);
  170. });
  171. it('should return a widget with equations', () => {
  172. const metricExpressions = [
  173. {
  174. id: 1,
  175. formula: '$a + $b',
  176. type: MetricExpressionType.EQUATION,
  177. isHidden: false,
  178. } satisfies DashboardMetricsExpression,
  179. ];
  180. const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
  181. expect(widget).toEqual({
  182. title: 'title',
  183. displayType: DisplayType.LINE,
  184. interval: '5m',
  185. limit: 10,
  186. widgetType: WidgetType.METRICS,
  187. queries: [
  188. {
  189. aggregates: ['equation|$a + $b'],
  190. fields: ['equation|$a + $b'],
  191. conditions: '',
  192. columns: [],
  193. name: '1',
  194. orderby: '',
  195. isHidden: false,
  196. },
  197. ],
  198. } satisfies Widget);
  199. });
  200. it('should should be reversible by getMetricExpressions', () => {
  201. const metricExpressions = [
  202. {
  203. groupBy: ['release'],
  204. id: 0,
  205. mri: 'd:transactions/duration@milisecond',
  206. op: 'avg',
  207. query: 'foo:bar',
  208. type: MetricExpressionType.QUERY,
  209. orderBy: 'asc',
  210. isHidden: true,
  211. } satisfies DashboardMetricsExpression,
  212. {
  213. id: 1,
  214. formula: '$a + $b',
  215. type: MetricExpressionType.EQUATION,
  216. isHidden: false,
  217. } satisfies DashboardMetricsExpression,
  218. ];
  219. const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
  220. expect(getMetricExpressions(widget)).toEqual(metricExpressions);
  221. });
  222. });