utils.spec.tsx 6.7 KB

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