mirror of
https://github.com/hyperdxio/hyperdx
synced 2026-04-21 13:37:15 +00:00
Fix the sum query to produce the correct results from the min/max test case from v1. Ref: HDX-1421
161 lines
4.8 KiB
TypeScript
161 lines
4.8 KiB
TypeScript
import { parameterizedQueryToSql } from '@/clickhouse';
|
|
import { Metadata } from '@/metadata';
|
|
import {
|
|
ChartConfigWithOptDateRange,
|
|
DisplayType,
|
|
MetricsDataType,
|
|
} from '@/types';
|
|
|
|
import { renderChartConfig } from '../renderChartConfig';
|
|
|
|
describe('renderChartConfig', () => {
|
|
let mockMetadata: Metadata;
|
|
|
|
beforeEach(() => {
|
|
mockMetadata = {
|
|
getColumns: jest.fn().mockResolvedValue([
|
|
{ name: 'timestamp', type: 'DateTime' },
|
|
{ name: 'value', type: 'Float64' },
|
|
]),
|
|
getMaterializedColumnsLookupTable: jest.fn().mockResolvedValue(null),
|
|
getColumn: jest.fn().mockResolvedValue({ type: 'DateTime' }),
|
|
} as unknown as Metadata;
|
|
});
|
|
|
|
it('should generate sql for a single gauge metric', async () => {
|
|
const config: ChartConfigWithOptDateRange = {
|
|
displayType: DisplayType.Line,
|
|
connection: 'test-connection',
|
|
// metricTables is added from the Source object via spread operator
|
|
metricTables: {
|
|
gauge: 'otel_metrics_gauge',
|
|
histogram: 'otel_metrics_histogram',
|
|
sum: 'otel_metrics_sum',
|
|
},
|
|
from: {
|
|
databaseName: 'default',
|
|
tableName: '',
|
|
},
|
|
select: [
|
|
{
|
|
aggFn: 'quantile',
|
|
aggCondition: '',
|
|
aggConditionLanguage: 'lucene',
|
|
valueExpression: 'Value',
|
|
level: 0.95,
|
|
metricName: 'nodejs.event_loop.utilization',
|
|
metricType: MetricsDataType.Gauge,
|
|
},
|
|
],
|
|
where: '',
|
|
whereLanguage: 'lucene',
|
|
timestampValueExpression: 'TimeUnix',
|
|
dateRange: [new Date('2025-02-12'), new Date('2025-12-14')],
|
|
granularity: '1 minute',
|
|
limit: { limit: 10 },
|
|
};
|
|
|
|
const generatedSql = await renderChartConfig(config, mockMetadata);
|
|
const actual = parameterizedQueryToSql(generatedSql);
|
|
expect(actual).toMatchSnapshot();
|
|
});
|
|
|
|
it('should generate sql for a single sum metric', async () => {
|
|
const config: ChartConfigWithOptDateRange = {
|
|
displayType: DisplayType.Line,
|
|
connection: 'test-connection',
|
|
// metricTables is added from the Source object via spread operator
|
|
metricTables: {
|
|
gauge: 'otel_metrics_gauge',
|
|
histogram: 'otel_metrics_histogram',
|
|
sum: 'otel_metrics_sum',
|
|
},
|
|
from: {
|
|
databaseName: 'default',
|
|
tableName: '',
|
|
},
|
|
select: [
|
|
{
|
|
aggFn: 'avg',
|
|
aggCondition: '',
|
|
aggConditionLanguage: 'lucene',
|
|
valueExpression: 'Value',
|
|
metricName: 'db.client.connections.usage',
|
|
metricType: MetricsDataType.Sum,
|
|
},
|
|
],
|
|
where: '',
|
|
whereLanguage: 'sql',
|
|
timestampValueExpression: 'TimeUnix',
|
|
dateRange: [new Date('2025-02-12'), new Date('2025-12-14')],
|
|
granularity: '5 minute',
|
|
limit: { limit: 10 },
|
|
};
|
|
|
|
const generatedSql = await renderChartConfig(config, mockMetadata);
|
|
const actual = parameterizedQueryToSql(generatedSql);
|
|
expect(actual).toMatchSnapshot();
|
|
});
|
|
|
|
it('should throw error for string select on sum metric', async () => {
|
|
const config: ChartConfigWithOptDateRange = {
|
|
displayType: DisplayType.Line,
|
|
connection: 'test-connection',
|
|
metricTables: {
|
|
gauge: 'otel_metrics_gauge',
|
|
histogram: 'otel_metrics_histogram',
|
|
sum: 'otel_metrics_sum',
|
|
},
|
|
from: {
|
|
databaseName: 'default',
|
|
tableName: '',
|
|
},
|
|
select: 'Value',
|
|
where: '',
|
|
whereLanguage: 'sql',
|
|
timestampValueExpression: 'TimeUnix',
|
|
dateRange: [new Date('2025-02-12'), new Date('2025-12-14')],
|
|
granularity: '5 minute',
|
|
limit: { limit: 10 },
|
|
};
|
|
|
|
await expect(renderChartConfig(config, mockMetadata)).rejects.toThrow(
|
|
'multi select or string select on metrics not supported',
|
|
);
|
|
});
|
|
|
|
it('should generate sql for a single histogram metric', async () => {
|
|
const config: ChartConfigWithOptDateRange = {
|
|
displayType: DisplayType.Line,
|
|
connection: 'test-connection',
|
|
// metricTables is added from the Source object via spread operator
|
|
metricTables: {
|
|
gauge: 'otel_metrics_gauge',
|
|
histogram: 'otel_metrics_histogram',
|
|
sum: 'otel_metrics_sum',
|
|
},
|
|
from: {
|
|
databaseName: 'default',
|
|
tableName: '',
|
|
},
|
|
select: [
|
|
{
|
|
aggFn: 'quantile',
|
|
level: 0.5,
|
|
valueExpression: 'Value',
|
|
metricName: 'http.server.duration',
|
|
metricType: MetricsDataType.Histogram,
|
|
},
|
|
],
|
|
where: '',
|
|
whereLanguage: 'sql',
|
|
timestampValueExpression: 'TimeUnix',
|
|
dateRange: [new Date('2025-02-12'), new Date('2025-12-14')],
|
|
limit: { limit: 10 },
|
|
};
|
|
|
|
const generatedSql = await renderChartConfig(config, mockMetadata);
|
|
const actual = parameterizedQueryToSql(generatedSql);
|
|
expect(actual).toMatchSnapshot();
|
|
});
|
|
});
|