|
1 | 1 | import React from 'react'; |
2 | 2 | import { ResponseJSON } from '@hyperdx/common-utils/dist/clickhouse'; |
3 | 3 | import { ClickhouseClient } from '@hyperdx/common-utils/dist/clickhouse/browser'; |
4 | | -import { ChartConfigWithOptDateRange } from '@hyperdx/common-utils/dist/types'; |
| 4 | +import { |
| 5 | + ChartConfigWithOptDateRange, |
| 6 | + MetricsDataType, |
| 7 | +} from '@hyperdx/common-utils/dist/types'; |
5 | 8 | import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
6 | 9 | import { renderHook, waitFor } from '@testing-library/react'; |
7 | 10 |
|
@@ -97,6 +100,48 @@ describe('useChartConfig', () => { |
97 | 100 | ).toEqual([undefined]); |
98 | 101 | }); |
99 | 102 |
|
| 103 | + it('returns windows aligned to the granularity if the granularity is auto', () => { |
| 104 | + expect( |
| 105 | + getGranularityAlignedTimeWindows( |
| 106 | + { |
| 107 | + dateRange: [ |
| 108 | + new Date('2023-01-10 00:00:00'), |
| 109 | + new Date('2023-01-10 01:00:00'), |
| 110 | + ], |
| 111 | + granularity: 'auto', // will be 1 minute |
| 112 | + timestampValueExpression: 'TimestampTime', |
| 113 | + } as ChartConfigWithOptDateRange, |
| 114 | + [ |
| 115 | + 30, // 30s |
| 116 | + 5 * 60, // 5m |
| 117 | + 60 * 60, // 1hr |
| 118 | + ], |
| 119 | + ), |
| 120 | + ).toEqual([ |
| 121 | + { |
| 122 | + dateRange: [ |
| 123 | + new Date('2023-01-10 00:59:00'), // Aligned to minute, the auto-inferred granularity |
| 124 | + new Date('2023-01-10 01:00:00'), |
| 125 | + ], |
| 126 | + dateRangeEndInclusive: undefined, |
| 127 | + }, |
| 128 | + { |
| 129 | + dateRange: [ |
| 130 | + new Date('2023-01-10 00:54:00'), |
| 131 | + new Date('2023-01-10 00:59:00'), |
| 132 | + ], |
| 133 | + dateRangeEndInclusive: false, |
| 134 | + }, |
| 135 | + { |
| 136 | + dateRange: [ |
| 137 | + new Date('2023-01-10 00:00:00'), |
| 138 | + new Date('2023-01-10 00:54:00'), |
| 139 | + ], |
| 140 | + dateRangeEndInclusive: false, |
| 141 | + }, |
| 142 | + ]); |
| 143 | + }); |
| 144 | + |
100 | 145 | it('returns windows aligned to the granularity if the granularity is larger than the window size', () => { |
101 | 146 | expect( |
102 | 147 | getGranularityAlignedTimeWindows( |
@@ -436,6 +481,81 @@ describe('useChartConfig', () => { |
436 | 481 | }); |
437 | 482 | }); |
438 | 483 |
|
| 484 | + it('fetches data without chunking for metric chart configs', async () => { |
| 485 | + const config: ChartConfigWithOptDateRange = { |
| 486 | + select: [ |
| 487 | + { |
| 488 | + aggFn: 'min', |
| 489 | + aggCondition: '', |
| 490 | + aggConditionLanguage: 'lucene', |
| 491 | + valueExpression: 'Value', |
| 492 | + metricName: 'system.network.io', |
| 493 | + metricType: MetricsDataType.Sum, |
| 494 | + }, |
| 495 | + ], |
| 496 | + where: '', |
| 497 | + whereLanguage: 'lucene', |
| 498 | + granularity: '1 minute', |
| 499 | + from: { |
| 500 | + databaseName: 'default', |
| 501 | + tableName: '', |
| 502 | + }, |
| 503 | + timestampValueExpression: 'TimeUnix', |
| 504 | + dateRange: [ |
| 505 | + new Date('2025-10-06T18:35:47.599Z'), |
| 506 | + new Date('2025-10-10T19:35:47.599Z'), |
| 507 | + ], |
| 508 | + connection: 'foo', |
| 509 | + metricTables: { |
| 510 | + gauge: 'otel_metrics_gauge', |
| 511 | + histogram: 'otel_metrics_histogram', |
| 512 | + sum: 'otel_metrics_sum', |
| 513 | + summary: '', |
| 514 | + 'exponential histogram': '', |
| 515 | + }, |
| 516 | + limit: { |
| 517 | + limit: 100000, |
| 518 | + }, |
| 519 | + }; |
| 520 | + |
| 521 | + const mockResponse = createMockQueryResponse([ |
| 522 | + { |
| 523 | + 'count()': '71', |
| 524 | + SeverityText: 'info', |
| 525 | + __hdx_time_bucket: '2025-10-01T00:00:00Z', |
| 526 | + }, |
| 527 | + { |
| 528 | + 'count()': '73', |
| 529 | + SeverityText: 'info', |
| 530 | + __hdx_time_bucket: '2025-10-02T00:00:00Z', |
| 531 | + }, |
| 532 | + ]); |
| 533 | + |
| 534 | + mockClickhouseClient.queryChartConfig.mockResolvedValue(mockResponse); |
| 535 | + |
| 536 | + const { result } = renderHook(() => useQueriedChartConfig(config), { |
| 537 | + wrapper, |
| 538 | + }); |
| 539 | + |
| 540 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 541 | + await waitFor(() => expect(result.current.isFetching).toBe(false)); |
| 542 | + |
| 543 | + // Should only be called once since chunking is disabled without timestampValueExpression |
| 544 | + expect(mockClickhouseClient.queryChartConfig).toHaveBeenCalledTimes(1); |
| 545 | + expect(mockClickhouseClient.queryChartConfig).toHaveBeenCalledWith({ |
| 546 | + config, |
| 547 | + metadata: expect.any(Object), |
| 548 | + opts: { |
| 549 | + abort_signal: expect.any(AbortSignal), |
| 550 | + }, |
| 551 | + }); |
| 552 | + expect(result.current.data).toEqual({ |
| 553 | + data: mockResponse.data, |
| 554 | + meta: mockResponse.meta, |
| 555 | + rows: mockResponse.rows, |
| 556 | + }); |
| 557 | + }); |
| 558 | + |
439 | 559 | it('fetches data without chunking when disableQueryChunking is true', async () => { |
440 | 560 | const config = createMockChartConfig({ |
441 | 561 | dateRange: [ |
|
0 commit comments