|
1 | 1 | import { QueryBuilderOperation, QueryBuilderOperationDef } from '../../prometheus/querybuilder/shared/types'; |
2 | 2 |
|
3 | 3 | import { |
| 4 | + createAggregationOperation, |
| 5 | + createAggregationOperationWithParam, |
4 | 6 | createRangeOperation, |
5 | 7 | createRangeOperationWithGrouping, |
6 | 8 | getLineFilterRenderer, |
@@ -322,3 +324,168 @@ describe('pipelineRenderer', () => { |
322 | 324 | expect(pipelineRenderer(model, definition!, '{}')).toBe('{} | drop foo, bar, baz'); |
323 | 325 | }); |
324 | 326 | }); |
| 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