dashboardImport.spec.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import type {MetricMeta, MRI} from 'sentry/types';
  2. import type {ImportDashboard, ImportWidget} from 'sentry/utils/metrics/dashboardImport';
  3. import {parseDashboard, WidgetParser} from 'sentry/utils/metrics/dashboardImport';
  4. import {parseMRI} from 'sentry/utils/metrics/mri';
  5. const mockRequests = (queryStrings: string[]) => {
  6. const queries = queryStrings.map((queryStr, i) => {
  7. return {
  8. data_source: 'metrics',
  9. name: `query${i}`,
  10. query: queryStr,
  11. };
  12. });
  13. const formulas = queries.map(query => {
  14. return {
  15. formula: query.name,
  16. };
  17. });
  18. return [
  19. {
  20. formulas,
  21. queries,
  22. response_format: 'timeseries',
  23. style: {line_type: 'solid'},
  24. display_type: 'line',
  25. },
  26. ];
  27. };
  28. const mockWidget = (overrides = {}) => {
  29. return {
  30. id: 1,
  31. definition: {
  32. title: 'Test widget',
  33. legend_columns: ['avg', 'min', 'max', 'value', 'sum'],
  34. type: 'timeseries',
  35. requests: mockRequests(['sum:sentry.foo.bar{foo:bar} by {baz}']),
  36. ...overrides,
  37. },
  38. } as ImportWidget;
  39. };
  40. const mockAvailableMetrics = (mris: MRI[]): MetricMeta[] => {
  41. return mris.map(mri => ({
  42. ...parseMRI(mri),
  43. mri,
  44. operations: [],
  45. blockingStatus: [],
  46. })) as MetricMeta[];
  47. };
  48. describe('WidgetParser', () => {
  49. beforeEach(function () {
  50. jest.clearAllMocks();
  51. MockApiClient.clearMockResponses();
  52. MockApiClient.addMockResponse({
  53. url: '/organizations/sentry/metrics/tags/',
  54. method: 'GET',
  55. body: [
  56. {key: 'foo', name: 'foo'},
  57. {key: 'bar', name: 'bar'},
  58. {key: 'baz', name: 'baz'},
  59. ],
  60. });
  61. });
  62. const availableMetrics = mockAvailableMetrics([
  63. 'c:custom/sentry.foo.bar@none',
  64. 'c:custom/sentry.bar.baz@none',
  65. ]);
  66. it('should parse a widget with single timeseries', async () => {
  67. const widget = mockWidget();
  68. const result = new WidgetParser(widget, availableMetrics).parse();
  69. const {report, widgets} = await result;
  70. expect(report.outcome).toEqual('success');
  71. expect(report.errors).toEqual([]);
  72. expect(widgets).toHaveLength(1);
  73. expect(widgets[0]).toEqual({
  74. displayType: 'line',
  75. groupBy: ['baz'],
  76. mri: 'c:custom/sentry.foo.bar@none',
  77. op: 'sum',
  78. query: 'foo:bar',
  79. title: 'Test widget',
  80. });
  81. });
  82. it('should parse a widget with 2 timeseries', async () => {
  83. const widget = mockWidget({
  84. requests: mockRequests([
  85. 'sum:sentry.foo.bar{foo:bar} by {baz}',
  86. 'sum:sentry.bar.baz{}',
  87. ]),
  88. });
  89. const result = new WidgetParser(widget, availableMetrics).parse();
  90. const {report, widgets} = await result;
  91. expect(report.outcome).toEqual('warning');
  92. expect(report.errors).toEqual([]);
  93. expect(report.notes.length).toEqual(1);
  94. expect(widgets).toHaveLength(2);
  95. expect(widgets[0]).toEqual({
  96. displayType: 'line',
  97. groupBy: ['baz'],
  98. mri: 'c:custom/sentry.foo.bar@none',
  99. op: 'sum',
  100. query: 'foo:bar',
  101. title: 'Test widget',
  102. });
  103. expect(widgets[1]).toEqual({
  104. displayType: 'line',
  105. groupBy: [],
  106. mri: 'c:custom/sentry.bar.baz@none',
  107. op: 'sum',
  108. query: '',
  109. title: 'Test widget',
  110. });
  111. });
  112. it('should parse a widget with operation appended to metric name', async () => {
  113. const widget = mockWidget({
  114. requests: mockRequests(['sum:sentry.foo.bar.avg{foo:bar} by {baz}']),
  115. });
  116. const result = new WidgetParser(widget, availableMetrics).parse();
  117. const {report, widgets} = await result;
  118. expect(report.outcome).toEqual('success');
  119. expect(widgets[0]).toEqual({
  120. displayType: 'line',
  121. groupBy: ['baz'],
  122. mri: 'c:custom/sentry.foo.bar@none',
  123. op: 'avg',
  124. query: 'foo:bar',
  125. title: 'Test widget',
  126. });
  127. });
  128. it('should fail to parse widget with unknown metric', async () => {
  129. const widget = mockWidget({
  130. requests: mockRequests(['sum:sentry.unknown-metric{foo:bar} by {baz}']),
  131. });
  132. const result = new WidgetParser(widget, availableMetrics).parse();
  133. const {report} = await result;
  134. expect(report.outcome).toEqual('error');
  135. expect(report.errors).toEqual([
  136. 'widget - no queries found',
  137. 'widget.request.query - metric not found: sentry.unknown-metric',
  138. ]);
  139. });
  140. it('should remove unknown tag', async () => {
  141. const widget = mockWidget({
  142. requests: mockRequests(['sum:sentry.foo.bar{not-a-tag:bar} by {baz}']),
  143. });
  144. const result = new WidgetParser(widget, availableMetrics).parse();
  145. const {report} = await result;
  146. expect(report.outcome).toEqual('warning');
  147. expect(report.errors).toEqual([
  148. 'widget.request.query - unsupported filter: not-a-tag',
  149. ]);
  150. });
  151. it('should strip globs form tags', async () => {
  152. const widget = mockWidget({
  153. requests: mockRequests(['sum:sentry.foo.bar{foo:bar*} by {baz}']),
  154. });
  155. const result = new WidgetParser(widget, availableMetrics).parse();
  156. const {report} = await result;
  157. expect(report.outcome).toEqual('warning');
  158. expect(report.errors).toEqual([
  159. 'widget.request.query.filter - unsupported value: bar*, using bar',
  160. ]);
  161. });
  162. });
  163. describe('parseDashboard', () => {
  164. beforeEach(function () {
  165. jest.clearAllMocks();
  166. MockApiClient.clearMockResponses();
  167. MockApiClient.addMockResponse({
  168. url: '/organizations/sentry/metrics/tags/',
  169. method: 'GET',
  170. body: [
  171. {key: 'foo', name: 'foo'},
  172. {key: 'bar', name: 'bar'},
  173. {key: 'baz', name: 'baz'},
  174. ],
  175. });
  176. });
  177. const availableMetrics = mockAvailableMetrics([
  178. 'c:custom/sentry.foo.bar@none',
  179. 'c:custom/sentry.bar.baz@none',
  180. ]);
  181. it('should parse a dashboard with single widget', async () => {
  182. const dashboard = {
  183. id: 1,
  184. title: 'Test dashboard',
  185. description: 'Test description',
  186. widgets: [mockWidget(), mockWidget()],
  187. } as ImportDashboard;
  188. const result = await parseDashboard(dashboard, availableMetrics);
  189. const {report, widgets} = result;
  190. expect(report.length).toEqual(2);
  191. expect(report[0].outcome).toEqual('success');
  192. expect(widgets.length).toEqual(2);
  193. });
  194. it('should parse a dashboard and explode widgets', async () => {
  195. const dashboard = {
  196. id: 1,
  197. title: 'Test dashboard',
  198. description: 'Test description',
  199. widgets: [
  200. mockWidget(),
  201. mockWidget({
  202. definition: {
  203. title: 'Test widget 2',
  204. legend_columns: ['avg', 'min', 'max', 'value', 'sum'],
  205. type: 'timeseries',
  206. requests: mockRequests(['sum:sentry.bar.baz{}', 'sum:sentry.foo.bar{}']),
  207. },
  208. }),
  209. ],
  210. } as ImportDashboard;
  211. const result = await parseDashboard(dashboard, availableMetrics);
  212. const {report, widgets} = result;
  213. expect(report.length).toEqual(2);
  214. expect(report[0].outcome).toEqual('success');
  215. expect(widgets.length).toEqual(2);
  216. });
  217. });