-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,576 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cassandra | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/gocql/gocql" | ||
) | ||
|
||
type Row struct { | ||
Columns []string | ||
Fields map[string]interface{} | ||
} | ||
|
||
// filterUnsupportedTypes checks the type of returned field and in case if | ||
// it is not supported by grafana tries to convert it to supported type. | ||
// Fields with types that cannot be converted are removed from row. Filtration | ||
// is not stable, so elements could change their position. | ||
// Type mappings are based on these: | ||
// Cassandra gocql types: https://github.com/gocql/gocql/blob/master/marshal.go#L164 | ||
// Grafana field types: https://github.com/grafana/grafana-plugin-sdk-go/blob/main/data/field.go#L39 | ||
func (r *Row) filterUnsupportedTypes() { | ||
i, j := 0, len(r.Columns)-1 | ||
for i <= j { | ||
colName := r.Columns[i] | ||
switch v := r.Fields[colName].(type) { | ||
case int8, int16, int32, int64, float32, float64, string, bool, time.Time: | ||
i++ | ||
case int: | ||
r.Fields[colName] = int64(v) | ||
i++ | ||
case []byte: | ||
r.Fields[colName] = string(v) | ||
i++ | ||
case net.IP: | ||
r.Fields[colName] = v.String() | ||
i++ | ||
case gocql.UUID: | ||
r.Fields[colName] = v.String() | ||
i++ | ||
default: | ||
delete(r.Fields, colName) | ||
r.Columns[i], r.Columns[j] = r.Columns[j], r.Columns[i] | ||
j-- | ||
} | ||
} | ||
r.Columns = r.Columns[0:i] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cassandra | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_filterUnsupportedTypes(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input *Row | ||
want *Row | ||
}{ | ||
{ | ||
name: "empty", | ||
input: &Row{}, | ||
want: &Row{}, | ||
}, | ||
{ | ||
name: "normal row", | ||
input: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
}, | ||
{ | ||
name: "normal row with nil in the beginning", | ||
input: &Row{ | ||
Columns: []string{"field1", "id", "time", "value"}, | ||
Fields: map[string]interface{}{"field1": nil, "id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
}, | ||
{ | ||
name: "normal row with nil in the end", | ||
input: &Row{ | ||
Columns: []string{"id", "time", "value", "field1"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1, "field1": nil}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
}, | ||
{ | ||
name: "normal row with unsupported", | ||
input: &Row{ | ||
Columns: []string{"id", "field1", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "field1": struct{}{}, "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
}, | ||
{ | ||
name: "normal row with multiple unsupported fields", | ||
input: &Row{ | ||
Columns: []string{"field1", "id", "field2", "time", "field3", "value"}, | ||
Fields: map[string]interface{}{"field1": struct{}{}, "id": "id", "field2": struct{}{}, "time": time.UnixMilli(1257894000000).UTC(), "field3": struct{}{}, "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tc.input.filterUnsupportedTypes() | ||
sort.Slice(tc.input.Columns, func(i, j int) bool { | ||
return tc.input.Columns[i] < tc.input.Columns[j] | ||
}) | ||
sort.Slice(tc.want.Columns, func(i, j int) bool { | ||
return tc.want.Columns[i] < tc.want.Columns[j] | ||
}) | ||
assert.Equal(t, tc.want, tc.input, tc.name) | ||
}) | ||
} | ||
} |
Oops, something went wrong.