|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { esql } from '../esql'; |
| 9 | +import { pqlFunc, pqlSel, pqlNum } from '../synth'; |
| 10 | + |
| 11 | +test('simple instant vector', () => { |
| 12 | + const query = esql.promql(pqlSel('up')); |
| 13 | + |
| 14 | + expect(query.print()).toBe('PROMQL (up)'); |
| 15 | +}); |
| 16 | + |
| 17 | +test('range vector selector', () => { |
| 18 | + const query = esql.promql(pqlSel('http_requests_total', '5m')); |
| 19 | + |
| 20 | + expect(query.print()).toBe('PROMQL (http_requests_total[5m])'); |
| 21 | +}); |
| 22 | + |
| 23 | +test('from PromQL synth-created node', () => { |
| 24 | + const query = esql.promql(esql.pql`http_requests_total[5m]`); |
| 25 | + |
| 26 | + expect(query.print()).toBe('PROMQL (http_requests_total[5m])'); |
| 27 | +}); |
| 28 | + |
| 29 | +test('from PromQL synth-created node - 2', () => { |
| 30 | + const query = esql.promql(esql.pql`http_requests_total[5m]`, { params: { a: 'b' } }); |
| 31 | + |
| 32 | + expect(query.print()).toBe('PROMQL a = b (http_requests_total[5m])'); |
| 33 | +}); |
| 34 | + |
| 35 | +test('from PromQL synth-created node - 3', () => { |
| 36 | + const query = esql.promql(esql.pql`http_requests_total[5m]`, { |
| 37 | + params: { a: 'b' }, |
| 38 | + outputName: 'result', |
| 39 | + }); |
| 40 | + |
| 41 | + expect(query.print()).toBe('PROMQL a = b result = (http_requests_total[5m])'); |
| 42 | +}); |
| 43 | + |
| 44 | +test('function expression', () => { |
| 45 | + const query = esql.promql(pqlFunc('rate', pqlSel('http_requests_total', '5m'))); |
| 46 | + |
| 47 | + expect(query.print()).toBe('PROMQL (rate(http_requests_total[5m]))'); |
| 48 | +}); |
| 49 | + |
| 50 | +test('aggregation with grouping', () => { |
| 51 | + const expr = pqlFunc('sum', pqlFunc('rate', pqlSel('metric', '5m')), { by: ['job'] }); |
| 52 | + const query = esql.promql(expr); |
| 53 | + |
| 54 | + expect(query.print()).toBe('PROMQL (sum(rate(metric[5m])) by (job))'); |
| 55 | +}); |
| 56 | + |
| 57 | +test('accepts a raw PromQL string', () => { |
| 58 | + const query = esql.promql('rate(http_requests_total[5m])'); |
| 59 | + |
| 60 | + expect(query.print()).toBe('PROMQL (rate(http_requests_total[5m]))'); |
| 61 | +}); |
| 62 | + |
| 63 | +test('accepts a simple metric string', () => { |
| 64 | + const query = esql.promql('up'); |
| 65 | + |
| 66 | + expect(query.print()).toBe('PROMQL (up)'); |
| 67 | +}); |
| 68 | + |
| 69 | +test('accepts a pql-tagged expression', () => { |
| 70 | + const expr = esql.pql`sum(rate(metric[5m])) by (job)`; |
| 71 | + const query = esql.promql(expr); |
| 72 | + |
| 73 | + expect(query.print()).toBe('PROMQL (sum(rate(metric[5m])) by (job))'); |
| 74 | +}); |
| 75 | + |
| 76 | +test('single command-level param', () => { |
| 77 | + const query = esql.promql(pqlSel('up'), { params: { index: 'k8s' } }); |
| 78 | + |
| 79 | + expect(query.print()).toBe('PROMQL index = k8s (up)'); |
| 80 | +}); |
| 81 | + |
| 82 | +test('multiple command-level params', () => { |
| 83 | + const query = esql.promql(pqlSel('up'), { params: { index: 'k8s', timeout: '10s' } }); |
| 84 | + |
| 85 | + expect(query.print()).toBe('PROMQL index = k8s timeout = 10s (up)'); |
| 86 | +}); |
| 87 | + |
| 88 | +test('named output column', () => { |
| 89 | + const query = esql.promql(pqlFunc('sum', pqlSel('metric', '5m')), { outputName: 'result' }); |
| 90 | + |
| 91 | + expect(query.print()).toBe('PROMQL result = (sum(metric[5m]))'); |
| 92 | +}); |
| 93 | + |
| 94 | +test('params and output name together', () => { |
| 95 | + const query = esql.promql(pqlSel('up'), { |
| 96 | + params: { index: 'k8s' }, |
| 97 | + outputName: 'health', |
| 98 | + }); |
| 99 | + |
| 100 | + expect(query.print()).toBe('PROMQL index = k8s health = (up)'); |
| 101 | +}); |
| 102 | + |
| 103 | +test('result is a ComposerQuery that can be piped', () => { |
| 104 | + const query = esql.promql(pqlSel('up')).limit(100); |
| 105 | + |
| 106 | + expect(query.print()).toBe('PROMQL (up) | LIMIT 100'); |
| 107 | +}); |
| 108 | + |
| 109 | +test('result can be sorted', () => { |
| 110 | + const query = esql.promql(pqlFunc('rate', pqlSel('metric', '5m'))) |
| 111 | + .where`value > ${pqlNum(0)}`.limit(50); |
| 112 | + |
| 113 | + expect(query.print()).toBe('PROMQL (rate(metric[5m])) | WHERE value > 0 | LIMIT 50'); |
| 114 | +}); |
| 115 | + |
| 116 | +test('first command is a PROMQL command', () => { |
| 117 | + const query = esql.promql(pqlSel('up')); |
| 118 | + const [cmd] = query.ast.commands; |
| 119 | + |
| 120 | + expect(cmd.type).toBe('command'); |
| 121 | + expect(cmd.name).toBe('promql'); |
| 122 | +}); |
0 commit comments