wires up dashboards page to be able to sort by usage stats (sprinkles) (#99479)
* wires up dashboards page to be able to sort by usage stats (sprinkles) * dont mutate field * use better type for field * adds tests. Had to export some types and put the field type back to object. * frontend asks for sort field in response if needed * adds some unit tests for getSortOptions * use Record instead of object * prettier * adds ternaries, another unit test
This commit is contained in:
@@ -655,6 +655,10 @@ func getSortFields(req *resource.ResourceSearchRequest) []string {
|
||||
input = field
|
||||
}
|
||||
|
||||
if slices.Contains(DashboardFields(), input) {
|
||||
input = "fields." + input
|
||||
}
|
||||
|
||||
if sort.Desc {
|
||||
input = "-" + input
|
||||
}
|
||||
|
||||
@@ -529,6 +529,36 @@ func TestBleveBackend(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetSortFields(t *testing.T) {
|
||||
t.Run("will prepend 'fields.' to sort fields when they are dashboard fields", func(t *testing.T) {
|
||||
searchReq := &resource.ResourceSearchRequest{
|
||||
SortBy: []*resource.ResourceSearchRequest_Sort{
|
||||
{Field: "views_total", Desc: false},
|
||||
},
|
||||
}
|
||||
sortFields := getSortFields(searchReq)
|
||||
assert.Equal(t, []string{"fields.views_total"}, sortFields)
|
||||
})
|
||||
t.Run("will prepend sort fields with a '-' when sort is Desc", func(t *testing.T) {
|
||||
searchReq := &resource.ResourceSearchRequest{
|
||||
SortBy: []*resource.ResourceSearchRequest_Sort{
|
||||
{Field: "views_total", Desc: true},
|
||||
},
|
||||
}
|
||||
sortFields := getSortFields(searchReq)
|
||||
assert.Equal(t, []string{"-fields.views_total"}, sortFields)
|
||||
})
|
||||
t.Run("will not prepend 'fields.' to common fields", func(t *testing.T) {
|
||||
searchReq := &resource.ResourceSearchRequest{
|
||||
SortBy: []*resource.ResourceSearchRequest_Sort{
|
||||
{Field: "description", Desc: false},
|
||||
},
|
||||
}
|
||||
sortFields := getSortFields(searchReq)
|
||||
assert.Equal(t, []string{"description"}, sortFields)
|
||||
})
|
||||
}
|
||||
|
||||
func asTimePointer(milli int64) *time.Time {
|
||||
if milli > 0 {
|
||||
t := time.UnixMilli(milli)
|
||||
|
||||
Reference in New Issue
Block a user