Skip to content

Commit

Permalink
chore: add a working impl for getting attrib values for multiple attr…
Browse files Browse the repository at this point in the history
…ibutes
  • Loading branch information
raj-k-singh committed Aug 15, 2024
1 parent 97c3c01 commit f6aead5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 38 deletions.
81 changes: 44 additions & 37 deletions pkg/query-service/app/clickhouseReader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4437,29 +4437,29 @@ func (r *ClickHouseReader) GetQBFilterSuggestionsForLogs(
ctx, topAttribs, 2,
)

fmt.Printf("\n\nDEBUG: err: %v; %v\n\n", err, topAttribValues)

if err != nil {
// Do not fail the entire request if only example query generation fails
zap.L().Error("could not find attribute values for creating example query", zap.Error(err))

} else {

// TODO(Raj): Clean this up
for valueIdx := 0; valueIdx < 2; valueIdx++ {
for attrIdx, topAttr := range topAttribs {
attrValues := topAttribValues[attrIdx]

if valueIdx < len(attrValues) {
exampleQuery := newExampleQuery()
exampleQuery.Items = append(exampleQuery.Items, v3.FilterItem{
Key: topAttr,
Operator: "=",
Value: attrValues[valueIdx],
})

suggestions.ExampleQueries = append(
suggestions.ExampleQueries, exampleQuery,
)
if len(suggestions.ExampleQueries) < req.ExamplesLimit {
attrValues := topAttribValues[attrIdx]
if valueIdx < len(attrValues) {
exampleQuery := newExampleQuery()
exampleQuery.Items = append(exampleQuery.Items, v3.FilterItem{
Key: topAttr,
Operator: "=",
Value: attrValues[valueIdx],
})

suggestions.ExampleQueries = append(
suggestions.ExampleQueries, exampleQuery,
)
}
}
}
}
Expand Down Expand Up @@ -4507,7 +4507,7 @@ func (r *ClickHouseReader) GetQBFilterSuggestionsForLogs(
// }

// Suggest static example queries for standard log attributes if needed.
if len(suggestions.ExampleQueries) < req.Limit {
if len(suggestions.ExampleQueries) < req.ExamplesLimit {
exampleQuery := newExampleQuery()
exampleQuery.Items = append(exampleQuery.Items, v3.FilterItem{
Key: v3.AttributeKey{
Expand All @@ -4529,18 +4529,28 @@ func (r *ClickHouseReader) getValuesForLogAttributes(ctx context.Context, attrib
[][]any, *model.ApiError,
) {
query := fmt.Sprintf(
`select tagKey, stringTagValue, int64TagValue, float64TagValue
from (
`
select tagKey, stringTagValue, int64TagValue, float64TagValue
from (
select
tagKey,
stringTagValue,
int64TagValue,
float64TagValue,
row_number() over (partition by tagKey order by ts desc) as rank
from (
select
tagKey,
stringTagValue,
int64TagValue,
float64TagValue,
row_number() over (partition by tagKey order by timestamp desc) as rank
max(timestamp) as ts
from %s.%s
where tagKey in $1
group by (tagKey, stringTagValue, int64TagValue, float64TagValue)
)
where rank <= %d
)
where rank <= %d
`,
r.logsDB, r.logsTagAttributeTable, limit,
)
Expand All @@ -4550,6 +4560,7 @@ func (r *ClickHouseReader) getValuesForLogAttributes(ctx context.Context, attrib
for _, attrib := range attributes {
attribNames = append(attribNames, attrib.Key)
}

rows, err := r.db.Query(ctx, query, attribNames)
if err != nil {
zap.L().Error("couldn't query attrib values for suggestions", zap.Error(err))
Expand All @@ -4562,16 +4573,11 @@ func (r *ClickHouseReader) getValuesForLogAttributes(ctx context.Context, attrib
result := make([][]any, len(attributes))

// Helper for getting hold of the result to populate for each scanned row
resultForAttrib := func(key string, dataType v3.AttributeKeyDataType) []any {
resultIdx := slices.IndexFunc(attributes, func(attrib v3.AttributeKey) bool {

resultIdxForAttrib := func(key string, dataType v3.AttributeKeyDataType) int {
return slices.IndexFunc(attributes, func(attrib v3.AttributeKey) bool {
return attrib.Key == key && attrib.DataType == dataType
})

if resultIdx < 0 {
return nil
} else {
return result[resultIdx]
}
}

var tagKey string
Expand All @@ -4589,19 +4595,20 @@ func (r *ClickHouseReader) getValuesForLogAttributes(ctx context.Context, attrib
}

if len(stringValue) > 0 {
result := resultForAttrib(tagKey, v3.AttributeKeyDataTypeString)
if result != nil {
result = append(result, stringValue)

attrResultIdx := resultIdxForAttrib(tagKey, v3.AttributeKeyDataTypeString)
if attrResultIdx >= 0 {
result[attrResultIdx] = append(result[attrResultIdx], stringValue)
}
} else if int64Value.Valid {
result := resultForAttrib(tagKey, v3.AttributeKeyDataTypeInt64)
if result != nil {
result = append(result, int64Value.Int64)
attrResultIdx := resultIdxForAttrib(tagKey, v3.AttributeKeyDataTypeInt64)
if attrResultIdx >= 0 {
result[attrResultIdx] = append(result[attrResultIdx], int64Value.Int64)
}
} else if float64Value.Valid {
result := resultForAttrib(tagKey, v3.AttributeKeyDataTypeFloat64)
if result != nil {
result = append(result, float64Value.Float64)
attrResultIdx := resultIdxForAttrib(tagKey, v3.AttributeKeyDataTypeFloat64)
if attrResultIdx >= 0 {
result[attrResultIdx] = append(result[attrResultIdx], float64Value.Float64)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/query-service/app/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,17 @@ func parseQBFilterSuggestionsRequest(r *http.Request) (
}
}

examplesLimit := baseconstants.DefaultFilterSuggestionsExamplesLimit
examplesLimitStr := r.URL.Query().Get("exampleLimit")
if len(examplesLimitStr) > 0 {
examplesLimit, err := strconv.Atoi(examplesLimitStr)
if err != nil || examplesLimit < 1 {
return nil, model.BadRequest(fmt.Errorf(
"invalid examples limit: %s", limitStr,
))
}
}

var existingFilter *v3.FilterSet
existingFilterB64 := r.URL.Query().Get("existingFilter")
if len(existingFilterB64) > 0 {
Expand All @@ -878,6 +889,7 @@ func parseQBFilterSuggestionsRequest(r *http.Request) (
DataSource: dataSource,
Limit: limit,
SearchText: searchText,
ExamplesLimit: examplesLimit,
ExistingFilter: existingFilter,
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/query-service/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,4 @@ var TracesListViewDefaultSelectedColumns = []v3.AttributeKey{
}

const DefaultFilterSuggestionsLimit = 100
const DefaultFilterSuggestionsExamplesLimit = 2
3 changes: 2 additions & 1 deletion pkg/query-service/model/v3/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ type QBFilterSuggestionsRequest struct {
DataSource DataSource `json:"dataSource"`
SearchText string `json:"searchText"`
Limit int `json:"limit"`
ExistingFilter *FilterSet `json:"existing_filter"`
ExamplesLimit int `json:"examplesLimit"`
ExistingFilter *FilterSet `json:"existingFilter"`
}

type QBFilterSuggestionsResponse struct {
Expand Down

0 comments on commit f6aead5

Please sign in to comment.