11import { useMemo } from 'react' ;
22import { add } from 'date-fns' ;
3+ import SqlString from 'sqlstring' ;
34import { z } from 'zod' ;
45import {
56 ColumnMetaType ,
@@ -8,17 +9,20 @@ import {
89 JSDataType ,
910 ResponseJSON ,
1011} from '@hyperdx/common-utils/dist/clickhouse' ;
12+ import { isMetricChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig' ;
1113import {
1214 AggregateFunction as AggFnV2 ,
1315 ChartConfigWithDateRange ,
1416 DisplayType ,
17+ Filter ,
1518 MetricsDataType as MetricsDataTypeV2 ,
1619 SavedChartConfig ,
1720 SourceKind ,
1821 SQLInterval ,
1922 TSource ,
2023} from '@hyperdx/common-utils/dist/types' ;
2124import { SegmentedControl } from '@mantine/core' ;
25+ import { notifications } from '@mantine/notifications' ;
2226
2327import { getMetricNameSql } from './otelSemanticConventions' ;
2428import {
@@ -45,16 +49,20 @@ export const TABLES = [
4549] ;
4650
4751export const AGG_FNS = [
48- { value : 'count' as const , label : 'Count of Events' } ,
49- { value : 'sum' as const , label : 'Sum' } ,
52+ { value : 'count' as const , label : 'Count of Events' , isAttributable : false } ,
53+ { value : 'sum' as const , label : 'Sum' , isAttributable : false } ,
5054 { value : 'p99' as const , label : '99th Percentile' } ,
5155 { value : 'p95' as const , label : '95th Percentile' } ,
5256 { value : 'p90' as const , label : '90th Percentile' } ,
5357 { value : 'p50' as const , label : 'Median' } ,
5458 { value : 'avg' as const , label : 'Average' } ,
5559 { value : 'max' as const , label : 'Maximum' } ,
5660 { value : 'min' as const , label : 'Minimum' } ,
57- { value : 'count_distinct' as const , label : 'Count Distinct' } ,
61+ {
62+ value : 'count_distinct' as const ,
63+ label : 'Count Distinct' ,
64+ isAttributable : false ,
65+ } ,
5866 { value : 'any' as const , label : 'Any' } ,
5967 { value : 'none' as const , label : 'None' } ,
6068] ;
@@ -269,6 +277,9 @@ export function convertDateRangeToGranularityString(
269277 return Granularity . ThirtyDay ;
270278}
271279
280+ export const ChartKeyJoiner = ' · ' ;
281+ export const PreviousPeriodSuffix = ' (previous)' ;
282+
272283export function convertGranularityToSeconds ( granularity : SQLInterval ) : number {
273284 const [ num , unit ] = granularity . split ( ' ' ) ;
274285 const numInt = Number . parseInt ( num ) ;
@@ -462,7 +473,9 @@ export const isAggregateFunction = (value: string) => {
462473 'kurtSamp' ,
463474 ] ;
464475
465- return fns . some ( fn => value . includes ( fn + '(' ) ) ;
476+ // Make case-insensitive since ClickHouse function names are case-insensitive
477+ const lowerValue = value . toLowerCase ( ) ;
478+ return fns . some ( fn => lowerValue . includes ( fn . toLowerCase ( ) + '(' ) ) ;
466479} ;
467480
468481export const INTEGER_NUMBER_FORMAT : NumberFormat = {
@@ -636,8 +649,8 @@ function addResponseToFormattedData({
636649 // Simplify the display name if there's only one series and a group by
637650 ...( isSingleValueColumn && hasGroupColumns ? [ ] : [ valueColumn . name ] ) ,
638651 ...groupColumns . map ( g => row [ g . name ] ) ,
639- ] . join ( ' · ' ) ;
640- const previousPeriodKey = `${ currentPeriodKey } (previous) ` ;
652+ ] . join ( ChartKeyJoiner ) ;
653+ const previousPeriodKey = `${ currentPeriodKey } ${ PreviousPeriodSuffix } ` ;
641654 const keyName = isPreviousPeriod ? previousPeriodKey : currentPeriodKey ;
642655
643656 // UInt64 are returned as strings, we'll convert to number
@@ -691,6 +704,9 @@ export function formatResponseForTimeChart({
691704 }
692705
693706 const timestampColumn = inferTimestampColumn ( meta ) ;
707+ const valueColumns = inferValueColumns ( meta ) ?? [ ] ;
708+ const groupColumns = inferGroupColumns ( meta ) ?? [ ] ;
709+ const isSingleValueColumn = valueColumns . length === 1 ;
694710
695711 if ( timestampColumn == null ) {
696712 throw new Error (
@@ -776,6 +792,9 @@ export function formatResponseForTimeChart({
776792 graphResults,
777793 timestampColumn,
778794 lineData : sortedLineDataWithColors ,
795+ groupColumns : groupColumns . map ( g => g . name ) ,
796+ valueColumns : valueColumns . map ( v => v . name ) ,
797+ isSingleValueColumn,
779798 } ;
780799}
781800
@@ -919,3 +938,198 @@ export const convertV1ChartConfigToV2 = (
919938 }
920939 throw new Error ( `unsupported table in v2: ${ firstSeries . table } ` ) ;
921940} ;
941+
942+ /**
943+ * Build search URL for viewing events based on group-by values
944+ * Used by both chart clicks and table row clicks
945+ */
946+ export function buildEventsSearchUrl ( {
947+ source,
948+ config,
949+ dateRange,
950+ groupFilters,
951+ valueRangeFilter,
952+ } : {
953+ source : TSource ;
954+ config : ChartConfigWithDateRange ;
955+ dateRange : [ Date , Date ] ;
956+ groupFilters ?: Array < { column : string ; value : any } > ;
957+ valueRangeFilter ?: { expression : string ; value : number ; threshold ?: number } ;
958+ } ) : string | null {
959+ if ( ! source ?. id ) {
960+ return null ;
961+ }
962+
963+ const isMetricChart = isMetricChartConfig ( config ) ;
964+ if ( isMetricChart && source ?. logSourceId == null ) {
965+ notifications . show ( {
966+ color : 'yellow' ,
967+ message : 'No log source is associated with the selected metric source.' ,
968+ } ) ;
969+ return null ;
970+ }
971+
972+ let where = config . where ;
973+ let whereLanguage = config . whereLanguage || 'lucene' ;
974+ if (
975+ where . length === 0 &&
976+ Array . isArray ( config . select ) &&
977+ config . select . length === 1
978+ ) {
979+ where = config . select [ 0 ] . aggCondition ?? '' ;
980+ whereLanguage = config . select [ 0 ] . aggConditionLanguage ?? 'lucene' ;
981+ }
982+
983+ const additionalFilters : Filter [ ] = [ ] ;
984+
985+ // Add group-by column filters
986+ if ( groupFilters && groupFilters . length > 0 ) {
987+ groupFilters . forEach ( ( { column, value } ) => {
988+ if ( column && value != null ) {
989+ // Can't use SQLString.escape here because the search endpoint relies on exist match for UI
990+ const condition = `${ column } IN (${ SqlString . escape ( value ) } )` ;
991+ additionalFilters . push ( { type : 'sql' , condition } ) ;
992+ }
993+ } ) ;
994+ }
995+
996+ // Add Y-axis value range filter (±threshold) for charts
997+ if ( valueRangeFilter ) {
998+ const { expression, value, threshold = 0.05 } = valueRangeFilter ;
999+ const hasAggregateFunction = isAggregateFunction ( expression ) ;
1000+
1001+ if ( ! hasAggregateFunction ) {
1002+ const lowerBound = value * ( 1 - threshold ) ;
1003+ const upperBound = value * ( 1 + threshold ) ;
1004+ // Can't use SQLString.escape here because the search endpoint relies on exist match for UI
1005+ const condition = `${ expression } BETWEEN ${ SqlString . escape ( lowerBound ) } AND ${ SqlString . escape ( upperBound ) } ` ;
1006+
1007+ additionalFilters . push ( {
1008+ type : 'sql' ,
1009+ condition,
1010+ } ) ;
1011+ }
1012+ }
1013+
1014+ // Get the time range
1015+ const from = dateRange [ 0 ] . getTime ( ) ;
1016+ const to = dateRange [ 1 ] . getTime ( ) ;
1017+
1018+ const params : Record < string , string > = {
1019+ source : source ?. id ?? '' ,
1020+ where : where ,
1021+ whereLanguage : whereLanguage ,
1022+ filters : JSON . stringify ( [ ...( config . filters ?? [ ] ) , ...additionalFilters ] ) ,
1023+ isLive : 'false' ,
1024+ from : from . toString ( ) ,
1025+ to : to . toString ( ) ,
1026+ } ;
1027+
1028+ // If its a metric chart, we don't pass the where and filters
1029+ if ( isMetricChart ) {
1030+ params . where = '' ;
1031+ params . whereLanguage = 'lucene' ;
1032+ params . filters = JSON . stringify ( [ ] ) ;
1033+ params . source = source ?. logSourceId ?? '' ;
1034+ }
1035+
1036+ // Include the select parameter if provided to preserve custom columns
1037+ // eventTableSelect is used for charts that override select (like histograms with count)
1038+ // to preserve the original table's select expression
1039+ if ( config . eventTableSelect ) {
1040+ params . select = config . eventTableSelect ;
1041+ }
1042+
1043+ return `/search?${ new URLSearchParams ( params ) . toString ( ) } ` ;
1044+ }
1045+
1046+ /**
1047+ * Extract group column names from chart config's groupBy field
1048+ * Handles both string format ("col1, col2") and array format ([{ valueExpression: "col1" }, ...])
1049+ */
1050+ function extractGroupColumns (
1051+ groupBy : ChartConfigWithDateRange [ 'groupBy' ] ,
1052+ ) : string [ ] {
1053+ if ( ! groupBy ) return [ ] ;
1054+
1055+ if ( typeof groupBy === 'string' ) {
1056+ // String GROUP BY: "col1, col2"
1057+ return groupBy . split ( ',' ) . map ( v => v . trim ( ) ) ;
1058+ }
1059+
1060+ // Array GROUP BY: [{ valueExpression: "col1" }, ...] or ["col1", ...]
1061+ return groupBy . map ( g => ( typeof g === 'string' ? g : g . valueExpression ) ) ;
1062+ }
1063+
1064+ /**
1065+ * Build search URL from a table row click
1066+ * Extracts group filters and value range filter from the row data
1067+ */
1068+ export function buildTableRowSearchUrl ( {
1069+ row,
1070+ source,
1071+ config,
1072+ dateRange,
1073+ } : {
1074+ row : Record < string , any > ;
1075+ source : TSource | undefined ;
1076+ config : ChartConfigWithDateRange ;
1077+ dateRange : [ Date , Date ] ;
1078+ } ) : string | null {
1079+ if ( ! source ?. id ) {
1080+ return null ;
1081+ }
1082+
1083+ // Extract group-by column names and build filters from row values
1084+ const groupFilters : Array < { column : string ; value : any } > = [ ] ;
1085+ const groupColumns = extractGroupColumns ( config . groupBy ) ;
1086+
1087+ groupColumns . forEach ( col => {
1088+ if ( row [ col ] != null ) {
1089+ groupFilters . push ( { column : col , value : row [ col ] } ) ;
1090+ }
1091+ } ) ;
1092+
1093+ // Build value range filter from the first select column
1094+ let valueRangeFilter : { expression : string ; value : number } | undefined ;
1095+
1096+ const firstSelect = config . select ?. [ 0 ] ;
1097+ if ( firstSelect ) {
1098+ const aggFn =
1099+ typeof firstSelect === 'string' ? undefined : firstSelect . aggFn ;
1100+ const isAttributable =
1101+ AGG_FNS . find ( fn => fn . value === aggFn ) ?. isAttributable !== false ;
1102+
1103+ if ( isAttributable ) {
1104+ const valueExpression =
1105+ typeof firstSelect === 'string'
1106+ ? firstSelect
1107+ : firstSelect . valueExpression ;
1108+
1109+ // Extract group column names to exclude them from value columns
1110+ const groupColumnSet = new Set ( extractGroupColumns ( config . groupBy ) ) ;
1111+
1112+ // Find the first value column (non-group column)
1113+ const valueColumn = Object . keys ( row ) . find (
1114+ key => ! groupColumnSet . has ( key ) ,
1115+ ) ;
1116+
1117+ const rowValue = valueColumn ? row [ valueColumn ] : undefined ;
1118+
1119+ if ( rowValue != null && typeof rowValue === 'number' ) {
1120+ valueRangeFilter = {
1121+ expression : valueExpression ,
1122+ value : rowValue ,
1123+ } ;
1124+ }
1125+ }
1126+ }
1127+
1128+ return buildEventsSearchUrl ( {
1129+ source,
1130+ config,
1131+ dateRange,
1132+ groupFilters,
1133+ valueRangeFilter,
1134+ } ) ;
1135+ }
0 commit comments