Cloudwatch Logs: Ignore non-time grouping fields in expressions and alerts (#67608)
This commit is contained in:
@@ -163,8 +163,9 @@ func changeToStringField(lengthOfValues int, rows [][]*cloudwatchlogs.ResultFiel
|
||||
return fieldValuesAsStrings
|
||||
}
|
||||
|
||||
func groupResults(results *data.Frame, groupingFieldNames []string) ([]*data.Frame, error) {
|
||||
func groupResults(results *data.Frame, groupingFieldNames []string, removeNonTime bool) ([]*data.Frame, error) {
|
||||
groupingFields := make([]*data.Field, 0)
|
||||
removeFieldIndices := make([]int, 0)
|
||||
|
||||
for i, field := range results.Fields {
|
||||
for _, groupingField := range groupingFieldNames {
|
||||
@@ -178,6 +179,10 @@ func groupResults(results *data.Frame, groupingFieldNames []string) ([]*data.Fra
|
||||
results.Fields[i] = newField
|
||||
field = newField
|
||||
}
|
||||
// For expressions and alerts to work properly we need to remove non-time grouping fields
|
||||
if removeNonTime && !field.Type().Time() {
|
||||
removeFieldIndices = append(removeFieldIndices, i)
|
||||
}
|
||||
|
||||
groupingFields = append(groupingFields, field)
|
||||
}
|
||||
@@ -192,14 +197,19 @@ func groupResults(results *data.Frame, groupingFieldNames []string) ([]*data.Fra
|
||||
groupedDataFrames := make(map[string]*data.Frame)
|
||||
for i := 0; i < rowLength; i++ {
|
||||
groupKey := generateGroupKey(groupingFields, i)
|
||||
// if group key doesn't exist create it
|
||||
if _, exists := groupedDataFrames[groupKey]; !exists {
|
||||
newFrame := results.EmptyCopy()
|
||||
newFrame.Name = groupKey
|
||||
newFrame.Meta = results.Meta
|
||||
// remove grouping indices
|
||||
newFrame.Fields = removeFieldsByIndex(newFrame.Fields, removeFieldIndices)
|
||||
groupedDataFrames[groupKey] = newFrame
|
||||
}
|
||||
|
||||
groupedDataFrames[groupKey].AppendRow(results.RowCopy(i)...)
|
||||
// add row to frame
|
||||
row := copyRowWithoutValues(results, i, removeFieldIndices)
|
||||
groupedDataFrames[groupKey].AppendRow(row...)
|
||||
}
|
||||
|
||||
newDataFrames := make([]*data.Frame, 0, len(groupedDataFrames))
|
||||
@@ -210,6 +220,40 @@ func groupResults(results *data.Frame, groupingFieldNames []string) ([]*data.Fra
|
||||
return newDataFrames, nil
|
||||
}
|
||||
|
||||
// remove fields at the listed indices
|
||||
func removeFieldsByIndex(fields []*data.Field, removeIndices []int) []*data.Field {
|
||||
newGroupingFields := make([]*data.Field, 0)
|
||||
removeIndicesIndex := 0
|
||||
for i, field := range fields {
|
||||
if removeIndicesIndex < len(removeIndices) && i == removeIndices[removeIndicesIndex] {
|
||||
removeIndicesIndex++
|
||||
if removeIndicesIndex > len(removeIndices) {
|
||||
newGroupingFields = append(newGroupingFields, fields[i+1:]...)
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
newGroupingFields = append(newGroupingFields, field)
|
||||
}
|
||||
return newGroupingFields
|
||||
}
|
||||
|
||||
// copy a row without the listed values
|
||||
func copyRowWithoutValues(f *data.Frame, rowIdx int, removeIndices []int) []interface{} {
|
||||
vals := make([]interface{}, len(f.Fields)-len(removeIndices))
|
||||
valsIdx := 0
|
||||
removeIndicesIndex := 0
|
||||
for i := range f.Fields {
|
||||
if removeIndicesIndex < len(removeIndices) && i == removeIndices[removeIndicesIndex] {
|
||||
removeIndicesIndex++
|
||||
continue
|
||||
}
|
||||
vals[valsIdx] = f.CopyAt(i, rowIdx)
|
||||
valsIdx++
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
func generateGroupKey(fields []*data.Field, row int) string {
|
||||
groupKey := ""
|
||||
for _, field := range fields {
|
||||
|
||||
Reference in New Issue
Block a user