[add] migrate widgets.chart
This commit is contained in:
parent
dc45eedece
commit
ed48fa468a
4 changed files with 107 additions and 78 deletions
|
|
@ -113,6 +113,6 @@ func exportAPI() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// load apis
|
// load apis
|
||||||
_, err = api.Load(string(source), "widgets.chart")
|
_, err = api.LoadSource("<widget.chart>.yao", source, "widgets.chart")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ package chart
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/yaoapp/gou/application"
|
||||||
"github.com/yaoapp/gou/process"
|
"github.com/yaoapp/gou/process"
|
||||||
"github.com/yaoapp/kun/exception"
|
"github.com/yaoapp/kun/exception"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
|
|
@ -55,61 +55,77 @@ func LoadAndExport(cfg config.Config) error {
|
||||||
|
|
||||||
// Load load task
|
// Load load task
|
||||||
func Load(cfg config.Config) error {
|
func Load(cfg config.Config) error {
|
||||||
var root = filepath.Join(cfg.Root, "charts")
|
|
||||||
return LoadFrom(root, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadFrom load from dir
|
|
||||||
func LoadFrom(dir string, prefix string) error {
|
|
||||||
|
|
||||||
if share.DirNotExists(dir) {
|
|
||||||
return fmt.Errorf("%s does not exists", dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
messages := []string{}
|
messages := []string{}
|
||||||
err := share.Walk(dir, ".json", func(root, filename string) {
|
exts := []string{"*.yao", "*.json", "*.jsonc"}
|
||||||
id := prefix + share.ID(root, filename)
|
err := application.App.Walk("charts", func(root, file string, isdir bool) error {
|
||||||
data := share.ReadFile(filename)
|
if isdir {
|
||||||
dsl := New(id)
|
return nil
|
||||||
err := jsoniter.Unmarshal(data, dsl)
|
}
|
||||||
if err != nil {
|
if err := LoadFile(root, file); err != nil {
|
||||||
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
|
messages = append(messages, err.Error())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if dsl.Action == nil {
|
return nil
|
||||||
dsl.Action = &ActionDSL{}
|
}, exts...)
|
||||||
}
|
|
||||||
dsl.Action.SetDefaultProcess()
|
|
||||||
|
|
||||||
if dsl.Layout == nil {
|
|
||||||
dsl.Layout = &LayoutDSL{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// mapping
|
|
||||||
err = dsl.mapping()
|
|
||||||
if err != nil {
|
|
||||||
messages = append(messages, fmt.Sprintf("[%s] Mapping %s", id, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
err = dsl.Validate()
|
|
||||||
if err != nil {
|
|
||||||
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Charts[id] = dsl
|
|
||||||
})
|
|
||||||
|
|
||||||
if len(messages) > 0 {
|
if len(messages) > 0 {
|
||||||
return fmt.Errorf(strings.Join(messages, ";"))
|
return fmt.Errorf(strings.Join(messages, ";\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadFile load table dsl by file
|
||||||
|
func LoadFile(root string, file string) error {
|
||||||
|
|
||||||
|
id := share.ID(root, file)
|
||||||
|
data, err := application.App.Read(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dsl := New(id)
|
||||||
|
err = application.Parse(file, data, dsl)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] %s", id, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dsl.parse(id, root)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
Charts[id] = dsl
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadData load via data
|
||||||
|
func (dsl *DSL) parse(id string, root string) error {
|
||||||
|
|
||||||
|
if dsl.Action == nil {
|
||||||
|
dsl.Action = &ActionDSL{}
|
||||||
|
}
|
||||||
|
dsl.Action.SetDefaultProcess()
|
||||||
|
|
||||||
|
if dsl.Layout == nil {
|
||||||
|
dsl.Layout = &LayoutDSL{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mapping
|
||||||
|
err := dsl.mapping()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
err = dsl.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Get chart via process or id
|
// Get chart via process or id
|
||||||
func Get(chart interface{}) (*DSL, error) {
|
func Get(chart interface{}) (*DSL, error) {
|
||||||
id := ""
|
id := ""
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,15 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/i18n"
|
"github.com/yaoapp/yao/i18n"
|
||||||
"github.com/yaoapp/yao/model"
|
"github.com/yaoapp/yao/test"
|
||||||
"github.com/yaoapp/yao/script"
|
|
||||||
"github.com/yaoapp/yao/share"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoad(t *testing.T) {
|
func TestLoad(t *testing.T) {
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
prepare(t)
|
prepare(t)
|
||||||
|
|
||||||
err := Load(config.Conf)
|
err := Load(config.Conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -21,18 +23,10 @@ func TestLoad(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepare(t *testing.T, language ...string) {
|
func prepare(t *testing.T, language ...string) {
|
||||||
// runtime.Load(config.Conf)
|
|
||||||
i18n.Load(config.Conf)
|
i18n.Load(config.Conf)
|
||||||
share.DBConnect(config.Conf.DB) // removed later
|
|
||||||
|
|
||||||
// load scripts
|
err := Load(config.Conf)
|
||||||
err := script.Load(config.Conf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// load models
|
|
||||||
err = model.Load(config.Conf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,16 @@ import (
|
||||||
"github.com/yaoapp/kun/any"
|
"github.com/yaoapp/kun/any"
|
||||||
"github.com/yaoapp/kun/maps"
|
"github.com/yaoapp/kun/maps"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
q "github.com/yaoapp/yao/query"
|
"github.com/yaoapp/yao/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProcessData(t *testing.T) {
|
func TestProcessData(t *testing.T) {
|
||||||
load(t)
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
|
testData(t)
|
||||||
|
|
||||||
args := []interface{}{"dashboard", map[string]interface{}{"range": "2022-01-02", "status": "checked"}}
|
args := []interface{}{"dashboard", map[string]interface{}{"range": "2022-01-02", "status": "checked"}}
|
||||||
res, err := process.New("yao.chart.Data", args...).Exec()
|
res, err := process.New("yao.chart.Data", args...).Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -26,7 +31,12 @@ func TestProcessData(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessComponent(t *testing.T) {
|
func TestProcessComponent(t *testing.T) {
|
||||||
load(t)
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
|
testData(t)
|
||||||
|
|
||||||
args := []interface{}{
|
args := []interface{}{
|
||||||
"dashboard",
|
"dashboard",
|
||||||
"fields.filter.状态.edit.props.xProps",
|
"fields.filter.状态.edit.props.xProps",
|
||||||
|
|
@ -49,7 +59,12 @@ func TestProcessComponent(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessComponentError(t *testing.T) {
|
func TestProcessComponentError(t *testing.T) {
|
||||||
load(t)
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
|
testData(t)
|
||||||
|
|
||||||
args := []interface{}{
|
args := []interface{}{
|
||||||
"dashboard",
|
"dashboard",
|
||||||
"fields.filter.edit.props.状态.::not-exist",
|
"fields.filter.edit.props.状态.::not-exist",
|
||||||
|
|
@ -61,7 +76,12 @@ func TestProcessComponentError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessSetting(t *testing.T) {
|
func TestProcessSetting(t *testing.T) {
|
||||||
load(t)
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
|
testData(t)
|
||||||
|
|
||||||
args := []interface{}{"dashboard"}
|
args := []interface{}{"dashboard"}
|
||||||
res, err := process.New("yao.chart.Setting", args...).Exec()
|
res, err := process.New("yao.chart.Setting", args...).Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -73,7 +93,12 @@ func TestProcessSetting(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgen(t *testing.T) {
|
func TestProcessXgen(t *testing.T) {
|
||||||
load(t)
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
|
testData(t)
|
||||||
|
|
||||||
args := []interface{}{"dashboard"}
|
args := []interface{}{"dashboard"}
|
||||||
res, err := process.New("yao.chart.Xgen", args...).Exec()
|
res, err := process.New("yao.chart.Xgen", args...).Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -85,7 +110,12 @@ func TestProcessXgen(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgenWithPermissions(t *testing.T) {
|
func TestProcessXgenWithPermissions(t *testing.T) {
|
||||||
load(t)
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
|
testData(t)
|
||||||
|
|
||||||
session.Global().Set("__permissions", map[string]interface{}{
|
session.Global().Set("__permissions", map[string]interface{}{
|
||||||
"charts.dashboard": []string{
|
"charts.dashboard": []string{
|
||||||
"7f46a38d7ff3f1832375ff63cd412f41", // operation.actions[0] 跳转至大屏
|
"7f46a38d7ff3f1832375ff63cd412f41", // operation.actions[0] 跳转至大屏
|
||||||
|
|
@ -119,17 +149,6 @@ func TestProcessXgenWithPermissions(t *testing.T) {
|
||||||
assert.NotEqual(t, nil, data.Get("fields.chart.综合评分"))
|
assert.NotEqual(t, nil, data.Get("fields.chart.综合评分"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(t *testing.T) {
|
|
||||||
prepare(t)
|
|
||||||
err := Load(config.Conf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
q.Load(config.Conf)
|
|
||||||
clear(t)
|
|
||||||
testData(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testData(t *testing.T) {
|
func testData(t *testing.T) {
|
||||||
pet := model.Select("pet")
|
pet := model.Select("pet")
|
||||||
err := pet.Insert(
|
err := pet.Insert(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue