Include sort in the data frame

This commit is contained in:
Tomaž Jerman
2021-08-16 09:16:07 +02:00
parent 8d6e96a999
commit 762ea93c27
10 changed files with 77 additions and 8 deletions
+9
View File
@@ -1,6 +1,7 @@
package filter
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
@@ -46,6 +47,9 @@ func parseSort(in string) (set SortExprSet, err error) {
exprMatcher := regexp.MustCompile(`([0-9a-zA-Z_\.]+)(\s+(asc|ASC|desc|DESC))?`)
set = SortExprSet{}
if in == "" {
return
}
in = strings.TrimSpace(in)
if in == "" {
@@ -74,6 +78,11 @@ func parseSort(in string) (set SortExprSet, err error) {
// UnmarshalJSON parses sort expression when passed inside JSON
func (set *SortExprSet) UnmarshalJSON(in []byte) error {
// This is an edgecase where `sort: ""` is passed in
if bytes.Compare(in, []byte{34, 34}) == 0 {
return nil
}
tmp, err := parseSort(string(in))
*set = tmp
return err
+4 -4
View File
@@ -23,8 +23,8 @@ type (
Columns FrameColumnSet `json:"columns"`
Rows FrameRowSet `json:"rows"`
Paging *filter.Paging `json:"paging"`
Sort *filter.Sorting `json:"sort"`
Paging *filter.Paging `json:"paging"`
Sort filter.SortExprSet `json:"sort"`
// params to help us perform things in place
startIndex int
@@ -271,9 +271,9 @@ func (f *Frame) String() string {
out += fmt.Sprintf("< %s; =%s; > %s", f.Paging.PrevPage.String(), f.Paging.PageCursor.String(), f.Paging.NextPage.String())
}
if f.Sort != nil {
if len(f.Sort) > 0 {
out += "\n"
out += f.Sort.Sort.String()
out += f.Sort.String()
}
out += "\n"
+4 -1
View File
@@ -314,6 +314,7 @@ func (r *recordDatasource) load(ctx context.Context, def *report.FrameDefinition
Name: def.Name,
Source: def.Source,
Ref: def.Ref,
Sort: def.Sort,
}
checkCap := cap > 0
@@ -336,7 +337,9 @@ func (r *recordDatasource) load(ctx context.Context, def *report.FrameDefinition
// If the count goes over the capacity, then we have a next page
if checkCap && i > cap {
out := []*report.Frame{f}
f = &report.Frame{}
f = &report.Frame{
Sort: def.Sort,
}
i = 0
return r.calculatePaging(out, def.Sort, uint(cap), def.Paging.PageCursor), nil
}
+2
View File
@@ -72,6 +72,8 @@ func Test2003_grouping_sorting(t *testing.T) {
"Manu, 1, 61",
"Sascha, 1, 38",
"Sigi, 1, 67")
h.a.Equal("count DESC, by_name", r.Sort.String())
}
func Test2004_grouping_paging(t *testing.T) {
+8
View File
@@ -160,6 +160,7 @@ func Test3003_joining_sorting_local(t *testing.T) {
// local
h.a.Equal(12, local.Size())
h.a.Equal("id<Record>, join_key<String>, first_name<String>, last_name<String>", local.Columns.String())
h.a.Equal("first_name, last_name DESC", local.Sort.String())
checkRows(h, local,
", Engel, Loritz",
", Engel, Kiefer",
@@ -178,6 +179,7 @@ func Test3003_joining_sorting_local(t *testing.T) {
foreign = ix["Maria_Königsmann"]
h.a.NotNil(foreign)
h.a.Equal("id<Record>, usr<String>, name<String>, type<Select>, cost<Number>, time_spent<Number>", foreign.Columns.String())
h.a.Equal("type", foreign.Sort.String())
checkRows(h, foreign,
", Maria_Königsmann, u1 j1, a, 10, 2",
", Maria_Königsmann, u1 j5, a, 4, 4",
@@ -191,6 +193,7 @@ func Test3003_joining_sorting_local(t *testing.T) {
foreign = ix["Engel_Loritz"]
h.a.NotNil(foreign)
h.a.Equal("id<Record>, usr<String>, name<String>, type<Select>, cost<Number>, time_spent<Number>", foreign.Columns.String())
h.a.Equal("type", foreign.Sort.String())
checkRows(h, foreign,
", Engel_Loritz, u3 j1, a, 10, 1",
", Engel_Loritz, u3 j2, a, 0, 0",
@@ -200,6 +203,7 @@ func Test3003_joining_sorting_local(t *testing.T) {
foreign = ix["Sigi_Goldschmidt"]
h.a.NotNil(foreign)
h.a.Equal("id<Record>, usr<String>, name<String>, type<Select>, cost<Number>, time_spent<Number>", foreign.Columns.String())
h.a.Equal("type", foreign.Sort.String())
checkRows(h, foreign,
", Sigi_Goldschmidt, u7 j2, a, 10, 21",
", Sigi_Goldschmidt, u7 j3, b, 10, 99",
@@ -209,6 +213,7 @@ func Test3003_joining_sorting_local(t *testing.T) {
foreign = ix["Engel_Kiefer"]
h.a.NotNil(foreign)
h.a.Equal("id<Record>, usr<String>, name<String>, type<Select>, cost<Number>, time_spent<Number>", foreign.Columns.String())
h.a.Equal("type", foreign.Sort.String())
checkRows(h, foreign,
", Engel_Kiefer, u12 j1, a, 42, 69",
", Engel_Kiefer, u12 j4, a, 35, 26",
@@ -226,6 +231,7 @@ func Test3003_joining_sorting_local(t *testing.T) {
foreign = ix["Manu_Specht"]
h.a.NotNil(foreign)
h.a.Equal("id<Record>, usr<String>, name<String>, type<Select>, cost<Number>, time_spent<Number>", foreign.Columns.String())
h.a.Equal("type", foreign.Sort.String())
checkRows(h, foreign,
", Manu_Specht, u10 j3, b, 53, 12",
", Manu_Specht, u10 j4, b, 60, 22",
@@ -236,6 +242,7 @@ func Test3003_joining_sorting_local(t *testing.T) {
foreign = ix["Ulli_Böhler"]
h.a.NotNil(foreign)
h.a.Equal("id<Record>, usr<String>, name<String>, type<Select>, cost<Number>, time_spent<Number>", foreign.Columns.String())
h.a.Equal("type", foreign.Sort.String())
checkRows(h, foreign,
", Ulli_Böhler, u5 j1, a, 1, 2")
}
@@ -261,6 +268,7 @@ func Test3004_joining_sorting_foreign(t *testing.T) {
// local
h.a.Equal(6, local.Size())
h.a.Equal("id<Record>, join_key<String>, first_name<String>, last_name<String>", local.Columns.String())
h.a.Equal("jobs.type, first_name, last_name DESC", local.Sort.String())
checkRows(h, local,
", Engel, Loritz",
", Engel, Kiefer",
+2
View File
@@ -75,6 +75,8 @@ func Test1003_loading_sorting(t *testing.T) {
", Ulli, Haupt, 21",
", Ulli, Förstner, 87",
", Ulli, Böhler, 14")
h.a.Equal("first_name, last_name DESC, id", f.Sort.String())
}
func Test1004_loading_paging(t *testing.T) {
+21
View File
@@ -0,0 +1,21 @@
package reporter
import (
"testing"
"github.com/cortezaproject/corteza-server/pkg/report"
)
func Test8001_sorting_empty(t *testing.T) {
var (
ctx, h, s = setup(t)
m, _, dd = loadScenario(ctx, s, t, h)
ff = loadNoErrMulti(ctx, h, m, dd...)
f *report.Frame
)
h.a.Len(ff, 1)
f = ff[0]
h.a.Len(f.Rows, 12)
}
+1 -1
View File
@@ -18,6 +18,6 @@
{ "name": "last_name", "label": "last_name" },
{ "name": "number_of_numbers", "label": "number_of_numbers" }
],
"sort": "first_name ASC, last_name DESC, id ASC"
"sort": "first_name, last_name DESC, id"
}]
}
@@ -35,7 +35,7 @@
{ "name": "first_name", "label": "first_name" },
{ "name": "last_name", "label": "last_name" }
],
"sort": "first_name ASC, last_name DESC"
"sort": "first_name, last_name DESC"
}, {
"name": "result",
"source": "joined",
@@ -48,6 +48,6 @@
{ "name": "cost", "label": "cost" },
{ "name": "time_spent", "label": "time_spent" }
],
"sort": "type ASC"
"sort": "type"
}]
}
+24
View File
@@ -0,0 +1,24 @@
{
"handle": "testing_report",
"sources": [{
"step": { "load": {
"name": "users",
"source": "composeRecords",
"definition": {
"module": "user",
"namespace": "ns"
},
"sort": ""
}}}],
"frames": [{
"name": "result",
"source": "users",
"columns": [
{ "name": "first_name", "label": "first_name" },
{ "name": "last_name", "label": "last_name" }
],
"sort": ""
}]
}