CloudWatch Logs: Fixes grouping of results by numeric field (#26298)
* CloudWatch Logs: Fixes grouping of results by numeric field Closes #25721
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package cloudwatch
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
)
|
||||
@@ -108,9 +110,19 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d
|
||||
func groupResults(results *data.Frame, groupingFieldNames []string) ([]*data.Frame, error) {
|
||||
groupingFields := make([]*data.Field, 0)
|
||||
|
||||
for _, field := range results.Fields {
|
||||
for i, field := range results.Fields {
|
||||
for _, groupingField := range groupingFieldNames {
|
||||
if field.Name == groupingField {
|
||||
// convert numeric grouping field to string field
|
||||
if field.Type().Numeric() {
|
||||
newField, err := numericFieldToStringField(field)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results.Fields[i] = newField
|
||||
field = newField
|
||||
}
|
||||
|
||||
groupingFields = append(groupingFields, field)
|
||||
}
|
||||
}
|
||||
@@ -153,3 +165,25 @@ func generateGroupKey(fields []*data.Field, row int) string {
|
||||
|
||||
return groupKey
|
||||
}
|
||||
|
||||
func numericFieldToStringField(field *data.Field) (*data.Field, error) {
|
||||
if !field.Type().Numeric() {
|
||||
return nil, fmt.Errorf("field is not numeric")
|
||||
}
|
||||
|
||||
strings := make([]*string, field.Len())
|
||||
for i := 0; i < field.Len(); i++ {
|
||||
floatVal, err := field.FloatAt(i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
strVal := fmt.Sprintf("%g", floatVal)
|
||||
strings[i] = aws.String(strVal)
|
||||
}
|
||||
|
||||
newField := data.NewField(field.Name, field.Labels, strings)
|
||||
newField.Config = field.Config
|
||||
|
||||
return newField, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user