[add] Chart session data support

This commit is contained in:
Max 2022-07-13 15:39:44 +08:00
parent 00b4c8c17c
commit 48fb5b202d
6 changed files with 92 additions and 7 deletions

View file

@ -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 {

View file

@ -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))
}

View file

@ -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)
}

View file

@ -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"))
}

View file

@ -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"`

56
tests/charts/session.json Normal file
View file

@ -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": {}
}
}