Skip to content

Commit ed86583

Browse files
authored
Loki: Decouple from Prometheus operationUtils (#78830)
* Loki: Decouple from Prometheus operationUtils * Update comments
1 parent 4d375aa commit ed86583

16 files changed

+415
-45
lines changed

.betterer.results

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5861,6 +5861,9 @@ exports[`better eslint`] = {
58615861
[0, 0, 0, "Styles should be written using objects.", "17"],
58625862
[0, 0, 0, "Styles should be written using objects.", "18"]
58635863
],
5864+
"public/app/plugins/datasource/prometheus/querybuilder/operationUtils.ts:5381": [
5865+
[0, 0, 0, "Do not use any type assertions.", "0"]
5866+
],
58645867
"public/app/plugins/datasource/prometheus/querybuilder/shared/LabelFilterItem.tsx:5381": [
58655868
[0, 0, 0, "Use data-testid for E2E selectors instead of aria-label", "0"],
58665869
[0, 0, 0, "Do not use any type assertions.", "1"],
@@ -5889,9 +5892,6 @@ exports[`better eslint`] = {
58895892
[0, 0, 0, "Styles should be written using objects.", "0"],
58905893
[0, 0, 0, "Styles should be written using objects.", "1"]
58915894
],
5892-
"public/app/plugins/datasource/prometheus/querybuilder/shared/operationUtils.ts:5381": [
5893-
[0, 0, 0, "Do not use any type assertions.", "0"]
5894-
],
58955895
"public/app/plugins/datasource/prometheus/querybuilder/shared/parsingUtils.ts:5381": [
58965896
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
58975897
],

public/app/plugins/datasource/loki/querybuilder/binaryScalarOperations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { defaultAddOperationHandler } from '../../prometheus/querybuilder/shared/operationUtils';
21
import {
32
QueryBuilderOperation,
43
QueryBuilderOperationDef,
54
QueryBuilderOperationParamDef,
65
} from '../../prometheus/querybuilder/shared/types';
76

7+
import { defaultAddOperationHandler } from './operationUtils';
88
import { LokiOperationId, LokiVisualQueryOperationCategory } from './types';
99

1010
export const binaryScalarDefs = [

public/app/plugins/datasource/loki/querybuilder/components/UnwrapParamEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import React, { useState } from 'react';
33
import { SelectableValue, getDefaultTimeRange, toOption } from '@grafana/data';
44
import { Select } from '@grafana/ui';
55

6-
import { getOperationParamId } from '../../../prometheus/querybuilder/shared/operationUtils';
76
import { QueryBuilderOperationParamEditorProps } from '../../../prometheus/querybuilder/shared/types';
87
import { placeHolderScopedVars } from '../../components/monaco-query-field/monaco-completion-provider/validation';
98
import { LokiDatasource } from '../../datasource';
109
import { getLogQueryFromMetricsQuery, isQueryWithError } from '../../queryUtils';
1110
import { extractUnwrapLabelKeysFromDataFrame } from '../../responseUtils';
1211
import { lokiQueryModeller } from '../LokiQueryModeller';
12+
import { getOperationParamId } from '../operationUtils';
1313
import { LokiVisualQuery } from '../types';
1414

1515
export function UnwrapParamEditor({

public/app/plugins/datasource/loki/querybuilder/operationUtils.test.ts

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { QueryBuilderOperation, QueryBuilderOperationDef } from '../../prometheus/querybuilder/shared/types';
22

33
import {
4+
createAggregationOperation,
5+
createAggregationOperationWithParam,
46
createRangeOperation,
57
createRangeOperationWithGrouping,
68
getLineFilterRenderer,
@@ -322,3 +324,168 @@ describe('pipelineRenderer', () => {
322324
expect(pipelineRenderer(model, definition!, '{}')).toBe('{} | drop foo, bar, baz');
323325
});
324326
});
327+
328+
describe('createAggregationOperation', () => {
329+
it('returns correct aggregation definitions with overrides', () => {
330+
expect(createAggregationOperation('test_aggregation', { category: 'test_category' })).toMatchObject([
331+
{
332+
addOperationHandler: {},
333+
alternativesKey: 'plain aggregations',
334+
category: 'test_category',
335+
defaultParams: [],
336+
explainHandler: {},
337+
id: 'test_aggregation',
338+
name: 'Test aggregation',
339+
paramChangedHandler: {},
340+
params: [
341+
{
342+
name: 'By label',
343+
optional: true,
344+
restParam: true,
345+
type: 'string',
346+
},
347+
],
348+
renderer: {},
349+
},
350+
{
351+
alternativesKey: 'aggregations by',
352+
category: 'test_category',
353+
defaultParams: [''],
354+
explainHandler: {},
355+
hideFromList: true,
356+
id: '__test_aggregation_by',
357+
name: 'Test aggregation by',
358+
paramChangedHandler: {},
359+
params: [
360+
{
361+
editor: {},
362+
name: 'Label',
363+
optional: true,
364+
restParam: true,
365+
type: 'string',
366+
},
367+
],
368+
renderer: {},
369+
},
370+
{
371+
alternativesKey: 'aggregations by',
372+
category: 'test_category',
373+
defaultParams: [''],
374+
explainHandler: {},
375+
hideFromList: true,
376+
id: '__test_aggregation_without',
377+
name: 'Test aggregation without',
378+
paramChangedHandler: {},
379+
params: [
380+
{
381+
name: 'Label',
382+
optional: true,
383+
restParam: true,
384+
type: 'string',
385+
},
386+
],
387+
renderer: {},
388+
},
389+
]);
390+
});
391+
});
392+
393+
describe('createAggregationOperationWithParams', () => {
394+
it('returns correct aggregation definitions with overrides and params', () => {
395+
expect(
396+
createAggregationOperationWithParam(
397+
'test_aggregation',
398+
{
399+
params: [{ name: 'K-value', type: 'number' }],
400+
defaultParams: [5],
401+
},
402+
{ category: 'test_category' }
403+
)
404+
).toMatchObject([
405+
{
406+
addOperationHandler: {},
407+
alternativesKey: 'plain aggregations',
408+
category: 'test_category',
409+
defaultParams: [5],
410+
explainHandler: {},
411+
id: 'test_aggregation',
412+
name: 'Test aggregation',
413+
paramChangedHandler: {},
414+
params: [
415+
{ name: 'K-value', type: 'number' },
416+
{ name: 'By label', optional: true, restParam: true, type: 'string' },
417+
],
418+
renderer: {},
419+
},
420+
{
421+
alternativesKey: 'aggregations by',
422+
category: 'test_category',
423+
defaultParams: [5, ''],
424+
explainHandler: {},
425+
hideFromList: true,
426+
id: '__test_aggregation_by',
427+
name: 'Test aggregation by',
428+
paramChangedHandler: {},
429+
params: [
430+
{ name: 'K-value', type: 'number' },
431+
{ editor: {}, name: 'Label', optional: true, restParam: true, type: 'string' },
432+
],
433+
renderer: {},
434+
},
435+
{
436+
alternativesKey: 'aggregations by',
437+
category: 'test_category',
438+
defaultParams: [5, ''],
439+
explainHandler: {},
440+
hideFromList: true,
441+
id: '__test_aggregation_without',
442+
name: 'Test aggregation without',
443+
paramChangedHandler: {},
444+
params: [
445+
{ name: 'K-value', type: 'number' },
446+
{ name: 'Label', optional: true, restParam: true, type: 'string' },
447+
],
448+
renderer: {},
449+
},
450+
]);
451+
});
452+
it('returns correct query string using aggregation definitions with overrides and number type param', () => {
453+
const def = createAggregationOperationWithParam(
454+
'test_aggregation',
455+
{
456+
params: [{ name: 'K-value', type: 'number' }],
457+
defaultParams: [5],
458+
},
459+
{ category: 'test_category' }
460+
);
461+
462+
const topKByDefinition = def[1];
463+
expect(
464+
topKByDefinition.renderer(
465+
{ id: '__topk_by', params: ['5', 'source', 'place'] },
466+
def[1],
467+
'rate({place="luna"} |= `` [5m])'
468+
)
469+
).toBe('test_aggregation by(source, place) (5, rate({place="luna"} |= `` [5m]))');
470+
});
471+
472+
it('returns correct query string using aggregation definitions with overrides and string type param', () => {
473+
const def = createAggregationOperationWithParam(
474+
'test_aggregation',
475+
{
476+
params: [{ name: 'Identifier', type: 'string' }],
477+
defaultParams: ['count'],
478+
},
479+
{ category: 'test_category' }
480+
);
481+
482+
const countValueDefinition = def[1];
483+
expect(
484+
countValueDefinition.renderer(
485+
{ id: 'count_values', params: ['5', 'source', 'place'] },
486+
def[1],
487+
'rate({place="luna"} |= `` [5m])'
488+
)
489+
).toBe('test_aggregation by(source, place) ("5", rate({place="luna"} |= `` [5m]))');
490+
});
491+
});

0 commit comments

Comments
 (0)