|
1 | | -import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; |
| 1 | +import { |
| 2 | + ChartConfigWithDateRange, |
| 3 | + SourceKind, |
| 4 | + TSource, |
| 5 | +} from '@hyperdx/common-utils/dist/types'; |
2 | 6 |
|
3 | | -import { formatResponseForTimeChart } from '@/ChartUtils'; |
| 7 | +import { |
| 8 | + convertToNumberChartConfig, |
| 9 | + convertToTableChartConfig, |
| 10 | + convertToTimeChartConfig, |
| 11 | + formatResponseForTimeChart, |
| 12 | +} from '@/ChartUtils'; |
4 | 13 |
|
5 | 14 | describe('ChartUtils', () => { |
6 | 15 | describe('formatResponseForTimeChart', () => { |
@@ -529,4 +538,112 @@ describe('ChartUtils', () => { |
529 | 538 | ]); |
530 | 539 | }); |
531 | 540 | }); |
| 541 | + |
| 542 | + describe('convertToTimeChartConfig', () => { |
| 543 | + it('should set granularity when granularity is auto', () => { |
| 544 | + const config = { |
| 545 | + granularity: 'auto', |
| 546 | + dateRange: [ |
| 547 | + new Date('2025-11-26T00:00:00Z'), |
| 548 | + new Date('2025-11-27T00:00:00Z'), |
| 549 | + ], |
| 550 | + } as ChartConfigWithDateRange; |
| 551 | + |
| 552 | + const granularityFromFunction = |
| 553 | + convertToTimeChartConfig(config).granularity; |
| 554 | + |
| 555 | + expect(granularityFromFunction).toBe('30 minute'); |
| 556 | + }); |
| 557 | + |
| 558 | + it('should set granularity when granularity is undefined', () => { |
| 559 | + const config = { |
| 560 | + dateRange: [ |
| 561 | + new Date('2025-11-26T00:00:00Z'), |
| 562 | + new Date('2025-11-27T00:00:00Z'), |
| 563 | + ], |
| 564 | + } as ChartConfigWithDateRange; |
| 565 | + |
| 566 | + const granularityFromFunction = |
| 567 | + convertToTimeChartConfig(config).granularity; |
| 568 | + |
| 569 | + expect(granularityFromFunction).toBe('30 minute'); |
| 570 | + }); |
| 571 | + |
| 572 | + it('should retain the specified granularity when not auto', () => { |
| 573 | + const config = { |
| 574 | + granularity: '5 minute', |
| 575 | + dateRange: [ |
| 576 | + new Date('2025-11-26T00:00:00Z'), |
| 577 | + new Date('2025-11-27T00:00:00Z'), |
| 578 | + ], |
| 579 | + } as ChartConfigWithDateRange; |
| 580 | + |
| 581 | + const granularityFromFunction = |
| 582 | + convertToTimeChartConfig(config).granularity; |
| 583 | + |
| 584 | + expect(granularityFromFunction).toBe('5 minute'); |
| 585 | + }); |
| 586 | + }); |
| 587 | + |
| 588 | + describe('convertToNumberChartConfig', () => { |
| 589 | + it('should remove granularity and groupBy from the config', () => { |
| 590 | + const config = { |
| 591 | + granularity: '5 minute', |
| 592 | + groupBy: 'ServiceName', |
| 593 | + dateRange: [ |
| 594 | + new Date('2025-11-26T00:00:00Z'), |
| 595 | + new Date('2025-11-27T00:00:00Z'), |
| 596 | + ], |
| 597 | + } as ChartConfigWithDateRange; |
| 598 | + |
| 599 | + const convertedConfig = convertToNumberChartConfig(config); |
| 600 | + |
| 601 | + expect(convertedConfig.granularity).toBeUndefined(); |
| 602 | + expect(convertedConfig.groupBy).toBeUndefined(); |
| 603 | + }); |
| 604 | + }); |
| 605 | + |
| 606 | + describe('convertToTableChartConfig', () => { |
| 607 | + it('should remove granularity from the config', () => { |
| 608 | + const config = { |
| 609 | + granularity: '5 minute', |
| 610 | + dateRange: [ |
| 611 | + new Date('2025-11-26T00:00:00Z'), |
| 612 | + new Date('2025-11-27T00:00:00Z'), |
| 613 | + ], |
| 614 | + } as ChartConfigWithDateRange; |
| 615 | + |
| 616 | + const convertedConfig = convertToTableChartConfig(config); |
| 617 | + |
| 618 | + expect(convertedConfig.granularity).toBeUndefined(); |
| 619 | + }); |
| 620 | + |
| 621 | + it('should apply a default sort if none is provided', () => { |
| 622 | + const config = { |
| 623 | + groupBy: 'ServiceName', |
| 624 | + dateRange: [ |
| 625 | + new Date('2025-11-26T00:00:00Z'), |
| 626 | + new Date('2025-11-27T00:00:00Z'), |
| 627 | + ], |
| 628 | + } as ChartConfigWithDateRange; |
| 629 | + |
| 630 | + const convertedConfig = convertToTableChartConfig(config); |
| 631 | + |
| 632 | + expect(convertedConfig.orderBy).toEqual('ServiceName'); |
| 633 | + }); |
| 634 | + |
| 635 | + it('should apply a default limit if none is provided', () => { |
| 636 | + const config = { |
| 637 | + groupBy: 'ServiceName', |
| 638 | + dateRange: [ |
| 639 | + new Date('2025-11-26T00:00:00Z'), |
| 640 | + new Date('2025-11-27T00:00:00Z'), |
| 641 | + ], |
| 642 | + } as ChartConfigWithDateRange; |
| 643 | + |
| 644 | + const convertedConfig = convertToTableChartConfig(config); |
| 645 | + |
| 646 | + expect(convertedConfig.limit).toEqual({ limit: 200 }); |
| 647 | + }); |
| 648 | + }); |
532 | 649 | }); |
0 commit comments