query.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { DataSourceJsonData, PluginMeta } from '@grafana/data';
  2. import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
  3. import { CombinedRule } from 'app/types/unified-alerting';
  4. import { GrafanaAlertStateDecision } from 'app/types/unified-alerting-dto';
  5. import { GRAFANA_RULES_SOURCE_NAME } from './datasource';
  6. import { alertRuleToQueries } from './query';
  7. describe('alertRuleToQueries', () => {
  8. it('it should convert grafana alert', () => {
  9. const combinedRule: CombinedRule = {
  10. name: 'Test alert',
  11. query: 'up',
  12. labels: {},
  13. annotations: {},
  14. group: {
  15. name: 'Prom up alert',
  16. rules: [],
  17. },
  18. namespace: {
  19. rulesSource: GRAFANA_RULES_SOURCE_NAME,
  20. name: 'Alerts',
  21. groups: [],
  22. },
  23. rulerRule: {
  24. for: '',
  25. annotations: {},
  26. labels: {},
  27. grafana_alert: grafanaAlert,
  28. },
  29. };
  30. const result = alertRuleToQueries(combinedRule);
  31. expect(result).toEqual(grafanaAlert.data);
  32. });
  33. it('shoulds convert cloud alert', () => {
  34. const combinedRule: CombinedRule = {
  35. name: 'cloud test',
  36. labels: {},
  37. query: 'up == 0',
  38. annotations: {},
  39. group: {
  40. name: 'test',
  41. rules: [],
  42. },
  43. namespace: {
  44. name: 'prom test alerts',
  45. groups: [],
  46. rulesSource: {
  47. name: 'prom test',
  48. type: 'prometheus',
  49. uid: 'asdf23',
  50. id: 1,
  51. access: 'proxy',
  52. meta: {} as PluginMeta,
  53. jsonData: {} as DataSourceJsonData,
  54. },
  55. },
  56. };
  57. const result = alertRuleToQueries(combinedRule);
  58. expect(result).toEqual([
  59. {
  60. refId: 'A',
  61. datasourceUid: 'asdf23',
  62. queryType: '',
  63. model: {
  64. refId: 'A',
  65. expr: 'up == 0',
  66. },
  67. relativeTimeRange: {
  68. from: 360,
  69. to: 0,
  70. },
  71. },
  72. ]);
  73. });
  74. });
  75. const grafanaAlert = {
  76. condition: 'B',
  77. exec_err_state: GrafanaAlertStateDecision.Alerting,
  78. namespace_id: 11,
  79. namespace_uid: 'namespaceuid123',
  80. no_data_state: GrafanaAlertStateDecision.NoData,
  81. title: 'Test alert',
  82. uid: 'asdf23',
  83. data: [
  84. {
  85. refId: 'A',
  86. queryType: '',
  87. relativeTimeRange: { from: 600, to: 0 },
  88. datasourceUid: 'asdf51',
  89. model: {
  90. expr: 'up',
  91. refId: 'A',
  92. },
  93. },
  94. {
  95. refId: 'B',
  96. queryType: '',
  97. relativeTimeRange: { from: 0, to: 0 },
  98. datasourceUid: '-100',
  99. model: {
  100. conditions: [
  101. {
  102. evaluator: { params: [1], type: 'lt' },
  103. operator: { type: 'and' },
  104. query: { params: ['A'] },
  105. reducer: { params: [], type: 'last' },
  106. type: 'query',
  107. },
  108. ],
  109. datasource: {
  110. uid: ExpressionDatasourceUID,
  111. },
  112. hide: false,
  113. refId: 'B',
  114. type: 'classic_conditions',
  115. },
  116. },
  117. ],
  118. };