diff --git a/chart/chart.go b/chart/chart.go index 43da6e2e..4553607b 100644 --- a/chart/chart.go +++ b/chart/chart.go @@ -65,9 +65,7 @@ func LoadFrom(dir string, prefix string) error { // LoadChart 载入数据表格 func LoadChart(source []byte, name string) (*Chart, error) { chart := &Chart{ - Flow: gou.Flow{ - Name: name, - }, + Flow: &gou.Flow{Name: name}, } err := jsoniter.Unmarshal(source, chart) if err != nil { diff --git a/chart/chart_test.go b/chart/chart_test.go index 28f94b0d..6fcd1b15 100644 --- a/chart/chart_test.go +++ b/chart/chart_test.go @@ -25,5 +25,5 @@ func check(t *testing.T) { for key := range Charts { keys = append(keys, key) } - assert.Equal(t, 1, len(keys)) + assert.Equal(t, 2, len(keys)) } diff --git a/chart/process.go b/chart/process.go index f3593edc..d6c73ebd 100644 --- a/chart/process.go +++ b/chart/process.go @@ -33,6 +33,10 @@ func ProcessData(process *gou.Process) interface{} { } } + // with Session + chart.Flow.WithGlobal(process.Global) + chart.Flow.WithSID(process.Sid) + return chart.GetData(params) } diff --git a/chart/process_test.go b/chart/process_test.go index 91067a35..9a7ed568 100644 --- a/chart/process_test.go +++ b/chart/process_test.go @@ -3,12 +3,13 @@ package chart import ( "net/url" "testing" + "time" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" "github.com/yaoapp/gou" + "github.com/yaoapp/gou/session" "github.com/yaoapp/kun/any" - "github.com/yaoapp/kun/utils" "github.com/yaoapp/yao/config" _ "github.com/yaoapp/yao/helper" "github.com/yaoapp/yao/model" @@ -68,10 +69,36 @@ func TestProcessData(t *testing.T) { } process := gou.NewProcess("xiang.chart.Data", args...) response := ProcessData(process) - utils.Dump(response) + // utils.Dump(response) assert.NotNil(t, response) res := any.Of(response).Map().Dot() assert.Equal(t, "北京", res.Get("合并.0.城市")) } + +func TestProcessDataGlobalSession(t *testing.T) { + + params := url.Values{ + "from": []string{"1981-01-01", "1990-01-01"}, + } + params.Set("to", "2049-12-31") + + args := []interface{}{ + "session", + params, + &gin.Context{}, + } + + sid := session.ID() + data := time.Now().String() + session.Global().ID(sid).Set("id", data) + process := gou. + NewProcess("xiang.chart.Data", args...). + WithSID(sid). + WithGlobal(map[string]interface{}{"foo": "bar"}) + + response := ProcessData(process) + res := any.Of(response).Map().Dot() + assert.Equal(t, data, res.Get("ID")) +} diff --git a/chart/types.go b/chart/types.go index 446517fb..b7b7e588 100644 --- a/chart/types.go +++ b/chart/types.go @@ -7,7 +7,7 @@ import ( // Chart 图表格式 type Chart struct { - gou.Flow + *gou.Flow APIs map[string]share.API `json:"apis,omitempty"` Filters map[string]share.Filter `json:"filters,omitempty"` Page share.Page `json:"page,omitempty"` diff --git a/tests/charts/session.json b/tests/charts/session.json new file mode 100644 index 00000000..f806316d --- /dev/null +++ b/tests/charts/session.json @@ -0,0 +1,56 @@ +{ + "label": "指标对比", + "version": "1.0.0", + "description": "指标对比用于测试", + "nodes": [{ "name": "ID", "process": "session.Get", "args": ["id"] }], + "output": { + "ID": "{{$res.ID}}" + }, + "apis": { + "data": { + "disable": false, + "guard": "-", + "default": [{ "from": "1980-01-02", "to": "2050-12-31" }] + }, + "setting": { + "disable": false, + "guard": "-" + } + }, + "filters": { + "开始时间": { + "label": "开始时间", + "bind": "from", + "input": { + "type": "date", + "props": { + "placeholder": "请选择开始时间" + } + } + }, + "结束时间": { + "label": "结束时间", + "bind": "to", + "input": { + "type": "date", + "props": { + "placeholder": "请选择结束时间" + } + } + } + }, + "page": { + "primary": "城市", + "layout": { + "filters": [ + { "name": "开始时间", "width": 6 }, + { "name": "结束时间", "width": 6 } + ], + "charts": [ + { "type": "line", "props": {} }, + { "type": "bar", "props": {} } + ] + }, + "actions": {} + } +}