utils.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. fieldAliases: [],
  168. },
  169. ],
  170. } satisfies Widget);
  171. });
  172. it('should return a widget with equations', () => {
  173. const metricExpressions = [
  174. {
  175. id: 1,
  176. formula: '$a + $b',
  177. type: MetricExpressionType.EQUATION,
  178. isHidden: false,
  179. } satisfies DashboardMetricsExpression,
  180. ];
  181. const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
  182. expect(widget).toEqual({
  183. title: 'title',
  184. displayType: DisplayType.LINE,
  185. interval: '5m',
  186. limit: 10,
  187. widgetType: WidgetType.METRICS,
  188. queries: [
  189. {
  190. aggregates: ['equation|$a + $b'],
  191. fields: ['equation|$a + $b'],
  192. conditions: '',
  193. columns: [],
  194. name: '1',
  195. orderby: '',
  196. isHidden: false,
  197. fieldAliases: [],
  198. },
  199. ],
  200. } satisfies Widget);
  201. });
  202. it('should should be reversible by getMetricExpressions', () => {
  203. const metricExpressions = [
  204. {
  205. groupBy: ['release'],
  206. id: 0,
  207. mri: 'd:transactions/duration@milisecond',
  208. op: 'avg',
  209. query: 'foo:bar',
  210. type: MetricExpressionType.QUERY,
  211. orderBy: 'asc',
  212. isHidden: true,
  213. } satisfies DashboardMetricsExpression,
  214. {
  215. id: 1,
  216. formula: '$a + $b',
  217. type: MetricExpressionType.EQUATION,
  218. isHidden: false,
  219. } satisfies DashboardMetricsExpression,
  220. ];
  221. const widget = expressionsToWidget(metricExpressions, 'title', DisplayType.LINE);
  222. expect(getMetricExpressions(widget)).toEqual(metricExpressions);
  223. });
  224. });