1+ import { DEFAULT_PROMQL_REDUCER } from '@hyperdx/common-utils/dist/core/promql' ;
12import {
23 BuilderChartConfigWithDateRange ,
4+ DisplayType ,
5+ PromqlReducer ,
36 SourceKind ,
47 TSource ,
58} from '@hyperdx/common-utils/dist/types' ;
69
710import {
811 ChartKeyJoiner ,
912 convertToNumberChartConfig ,
13+ convertToPromqlNumberChartConfig ,
1014 convertToTableChartConfig ,
1115 convertToTimeChartConfig ,
1216 findNearestSeriesKey ,
@@ -20,6 +24,7 @@ import {
2024 resolveRenderedSeriesCap ,
2125} from '@/defaults' ;
2226import { COLORS } from '@/utils' ;
27+ import { stripClientSideConfigFields } from '@/utils/chartConfig' ;
2328
2429// Anchor info/error to concrete hexes rather than `getChartColorInfo()` /
2530// `getChartColorError()` so a regression that breaks the helpers can't
@@ -1279,6 +1284,124 @@ describe('ChartUtils', () => {
12791284 } ) ;
12801285 } ) ;
12811286
1287+ describe ( 'convertToPromqlNumberChartConfig' , ( ) => {
1288+ const promqlConfig = {
1289+ configType : 'promql' as const ,
1290+ displayType : DisplayType . Number ,
1291+ connection : 'conn' ,
1292+ promqlExpression : [
1293+ { expression : 'up' , queryType : 'range' as const } ,
1294+ { expression : 'down' , queryType : 'range' as const } ,
1295+ ] ,
1296+ dateRange : [
1297+ new Date ( '2025-11-26T00:00:14.076Z' ) ,
1298+ new Date ( '2025-11-26T01:00:14.076Z' ) ,
1299+ ] as [ Date , Date ] ,
1300+ } ;
1301+
1302+ // Prometheus answers at `start + k * step`, so an unaligned start or an
1303+ // unresolved step puts the samples between the buckets the sparkline plots
1304+ // on -- which the empty-bucket filler then fills with zeros.
1305+ it ( 'aligns the date range and resolves the granularity' , ( ) => {
1306+ const result = convertToPromqlNumberChartConfig ( promqlConfig , {
1307+ withReducer : true ,
1308+ } ) ;
1309+
1310+ expect ( result . dateRange ) . toEqual ( [
1311+ new Date ( '2025-11-26T00:00:00Z' ) ,
1312+ new Date ( '2025-11-26T01:01:00Z' ) ,
1313+ ] ) ;
1314+ expect ( result . granularity ) . toBe ( '1 minute' ) ;
1315+ } ) ;
1316+
1317+ it ( 'leaves the date range unaligned for an instant query' , ( ) => {
1318+ const result = convertToPromqlNumberChartConfig (
1319+ {
1320+ ...promqlConfig ,
1321+ promqlExpression : [ { expression : 'up' , queryType : 'instant' } ] ,
1322+ dateRange : [
1323+ new Date ( '2025-11-26T00:00:14.076Z' ) ,
1324+ new Date ( '2025-11-27T00:00:14.076Z' ) ,
1325+ ] ,
1326+ } ,
1327+ { withReducer : true } ,
1328+ ) ;
1329+
1330+ expect ( result . dateRange ) . toEqual ( [
1331+ new Date ( '2025-11-26T00:00:14.076Z' ) ,
1332+ new Date ( '2025-11-27T00:00:14.076Z' ) ,
1333+ ] ) ;
1334+ expect ( result . granularity ) . toBe ( '30 minute' ) ;
1335+ } ) ;
1336+
1337+ it ( 'names the reducer, defaulting it, and queries one expression' , ( ) => {
1338+ const result = convertToPromqlNumberChartConfig ( promqlConfig , {
1339+ withReducer : true ,
1340+ } ) ;
1341+
1342+ expect ( result ) . toEqual (
1343+ expect . objectContaining ( {
1344+ promqlExpression : [
1345+ {
1346+ expression : 'up' ,
1347+ queryType : 'range' ,
1348+ reducer : DEFAULT_PROMQL_REDUCER ,
1349+ } ,
1350+ ] ,
1351+ } ) ,
1352+ ) ;
1353+ } ) ;
1354+
1355+ it ( 'keeps a chosen reducer' , ( ) => {
1356+ const result = convertToPromqlNumberChartConfig (
1357+ {
1358+ ...promqlConfig ,
1359+ promqlExpression : [
1360+ {
1361+ expression : 'up' ,
1362+ queryType : 'range' as const ,
1363+ reducer : PromqlReducer . Max ,
1364+ } ,
1365+ ] ,
1366+ } ,
1367+ { withReducer : true } ,
1368+ ) ;
1369+
1370+ expect ( result ) . toEqual (
1371+ expect . objectContaining ( {
1372+ promqlExpression : [
1373+ {
1374+ expression : 'up' ,
1375+ queryType : 'range' ,
1376+ reducer : PromqlReducer . Max ,
1377+ } ,
1378+ ] ,
1379+ } ) ,
1380+ ) ;
1381+ } ) ;
1382+
1383+ // Without a reducer named, the hook leaves the buckets alone -- which is
1384+ // how the sparkline reads the same response as the value.
1385+ it ( 'names no reducer for the sparkline, otherwise matching the value' , ( ) => {
1386+ const value = convertToPromqlNumberChartConfig ( promqlConfig , {
1387+ withReducer : true ,
1388+ } ) ;
1389+ const sparkline = convertToPromqlNumberChartConfig ( promqlConfig , {
1390+ withReducer : false ,
1391+ } ) ;
1392+
1393+ expect ( sparkline ) . toEqual ( {
1394+ ...value ,
1395+ promqlExpression : [
1396+ { expression : 'up' , queryType : 'range' , reducer : undefined } ,
1397+ ] ,
1398+ } ) ;
1399+ expect ( stripClientSideConfigFields ( sparkline ) ) . toEqual (
1400+ stripClientSideConfigFields ( value ) ,
1401+ ) ;
1402+ } ) ;
1403+ } ) ;
1404+
12821405 describe ( 'convertToTableChartConfig' , ( ) => {
12831406 it ( 'should remove granularity from the config' , ( ) => {
12841407 const config = {
0 commit comments