querystring_builder.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import LogAnalyticsQuerystringBuilder from './querystring_builder';
  2. import moment from 'moment';
  3. describe('LogAnalyticsDatasource', () => {
  4. let builder: LogAnalyticsQuerystringBuilder;
  5. beforeEach(() => {
  6. builder = new LogAnalyticsQuerystringBuilder(
  7. 'query=Tablename | where $__timeFilter()',
  8. {
  9. interval: '5m',
  10. range: {
  11. from: moment().subtract(24, 'hours'),
  12. to: moment(),
  13. },
  14. rangeRaw: {
  15. from: 'now-24h',
  16. to: 'now',
  17. },
  18. },
  19. 'TimeGenerated'
  20. );
  21. });
  22. describe('when $__timeFilter has no column parameter', () => {
  23. it('should generate a time filter condition with TimeGenerated as the datetime field', () => {
  24. const query = builder.generate().uriString;
  25. expect(query).toContain('where%20TimeGenerated%20%3E%3D%20datetime(');
  26. });
  27. });
  28. describe('when $__timeFilter has a column parameter', () => {
  29. beforeEach(() => {
  30. builder.rawQueryString = 'query=Tablename | where $__timeFilter(myTime)';
  31. });
  32. it('should generate a time filter condition with myTime as the datetime field', () => {
  33. const query = builder.generate().uriString;
  34. expect(query).toContain('where%20myTime%20%3E%3D%20datetime(');
  35. });
  36. });
  37. describe('when $__contains and multi template variable has custom All value', () => {
  38. beforeEach(() => {
  39. builder.rawQueryString = 'query=Tablename | where $__contains(col, all)';
  40. });
  41. it('should generate a where..in clause', () => {
  42. const query = builder.generate().rawQuery;
  43. expect(query).toContain(`where 1 == 1`);
  44. });
  45. });
  46. describe('when $__contains and multi template variable has one selected value', () => {
  47. beforeEach(() => {
  48. builder.rawQueryString = `query=Tablename | where $__contains(col, 'val1')`;
  49. });
  50. it('should generate a where..in clause', () => {
  51. const query = builder.generate().rawQuery;
  52. expect(query).toContain(`where col in ('val1')`);
  53. });
  54. });
  55. describe('when $__contains and multi template variable has multiple selected values', () => {
  56. beforeEach(() => {
  57. builder.rawQueryString = `query=Tablename | where $__contains(col, 'val1','val2')`;
  58. });
  59. it('should generate a where..in clause', () => {
  60. const query = builder.generate().rawQuery;
  61. expect(query).toContain(`where col in ('val1','val2')`);
  62. });
  63. });
  64. describe('when $__interval is in the query', () => {
  65. beforeEach(() => {
  66. builder.rawQueryString = 'query=Tablename | summarize count() by Category, bin(TimeGenerated, $__interval)';
  67. });
  68. it('should replace $__interval with the inbuilt interval option', () => {
  69. const query = builder.generate().uriString;
  70. expect(query).toContain('bin(TimeGenerated%2C%205m');
  71. });
  72. });
  73. describe('when using $__from and $__to is in the query and range is until now', () => {
  74. beforeEach(() => {
  75. builder.rawQueryString = 'query=Tablename | where myTime >= $__from and myTime <= $__to';
  76. });
  77. it('should replace $__from and $__to with a datetime and the now() function', () => {
  78. const query = builder.generate().uriString;
  79. expect(query).toContain('where%20myTime%20%3E%3D%20datetime(');
  80. expect(query).toContain('myTime%20%3C%3D%20now()');
  81. });
  82. });
  83. describe('when using $__from and $__to is in the query and range is a specific interval', () => {
  84. beforeEach(() => {
  85. builder.rawQueryString = 'query=Tablename | where myTime >= $__from and myTime <= $__to';
  86. builder.options.range.to = moment().subtract(1, 'hour');
  87. builder.options.rangeRaw.to = 'now-1h';
  88. });
  89. it('should replace $__from and $__to with datetimes', () => {
  90. const query = builder.generate().uriString;
  91. expect(query).toContain('where%20myTime%20%3E%3D%20datetime(');
  92. expect(query).toContain('myTime%20%3C%3D%20datetime(');
  93. });
  94. });
  95. describe('when using $__escape and multi template variable has one selected value', () => {
  96. beforeEach(() => {
  97. builder.rawQueryString = `$__escapeMulti('\\grafana-vm\Network(eth0)\Total Bytes Received')`;
  98. });
  99. it('should replace $__escape(val) with KQL style escaped string', () => {
  100. const query = builder.generate().uriString;
  101. expect(query).toContain(`%40'%5Cgrafana-vmNetwork(eth0)Total%20Bytes%20Received'`);
  102. });
  103. });
  104. describe('when using $__escape and multi template variable has multiple selected values', () => {
  105. beforeEach(() => {
  106. builder.rawQueryString = `CounterPath in ($__escapeMulti('\\grafana-vm\Network(eth0)\Total','\\grafana-vm\Network(eth0)\Total'))`;
  107. });
  108. it('should replace $__escape(val) with multiple KQL style escaped string', () => {
  109. const query = builder.generate().uriString;
  110. expect(query).toContain(
  111. `CounterPath%20in%20(%40'%5Cgrafana-vmNetwork(eth0)Total'%2C%20%40'%5Cgrafana-vmNetwork(eth0)Total')`
  112. );
  113. });
  114. });
  115. describe('when using $__escape and multi template variable has one selected value that contains comma', () => {
  116. beforeEach(() => {
  117. builder.rawQueryString = `$__escapeMulti('\\grafana-vm,\Network(eth0)\Total Bytes Received')`;
  118. });
  119. it('should replace $__escape(val) with KQL style escaped string', () => {
  120. const query = builder.generate().uriString;
  121. expect(query).toContain(`%40'%5Cgrafana-vm%2CNetwork(eth0)Total%20Bytes%20Received'`);
  122. });
  123. });
  124. describe(`when using $__escape and multi template variable value is not wrapped in single '`, () => {
  125. beforeEach(() => {
  126. builder.rawQueryString = `$__escapeMulti(\\grafana-vm,\Network(eth0)\Total Bytes Received)`;
  127. });
  128. it('should not replace macro', () => {
  129. const query = builder.generate().uriString;
  130. expect(query).toContain(`%24__escapeMulti(%5Cgrafana-vm%2CNetwork(eth0)Total%20Bytes%20Received)`);
  131. });
  132. });
  133. });