[add] migrate widgets.dashboard
This commit is contained in:
parent
61c101d025
commit
dc45eedece
4 changed files with 93 additions and 79 deletions
|
|
@ -113,6 +113,6 @@ func exportAPI() error {
|
|||
}
|
||||
|
||||
// load apis
|
||||
_, err = api.Load(string(source), "widgets.dashboard")
|
||||
_, err = api.LoadSource("<widget.dashboard>.yao", source, "widgets.dashboard")
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@ package dashboard
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/yaoapp/gou/application"
|
||||
"github.com/yaoapp/gou/process"
|
||||
"github.com/yaoapp/kun/exception"
|
||||
"github.com/yaoapp/yao/config"
|
||||
|
|
@ -55,61 +55,77 @@ func LoadAndExport(cfg config.Config) error {
|
|||
|
||||
// Load load task
|
||||
func Load(cfg config.Config) error {
|
||||
var root = filepath.Join(cfg.Root, "dashboards")
|
||||
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{}
|
||||
err := share.Walk(dir, ".json", func(root, filename string) {
|
||||
id := prefix + share.ID(root, filename)
|
||||
data := share.ReadFile(filename)
|
||||
dsl := New(id)
|
||||
err := jsoniter.Unmarshal(data, dsl)
|
||||
if err != nil {
|
||||
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
|
||||
return
|
||||
exts := []string{"*.yao", "*.json", "*.jsonc"}
|
||||
err := application.App.Walk("dashboards", func(root, file string, isdir bool) error {
|
||||
if isdir {
|
||||
return nil
|
||||
}
|
||||
if err := LoadFile(root, file); err != nil {
|
||||
messages = append(messages, err.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 {
|
||||
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
|
||||
}
|
||||
|
||||
Dashboards[id] = dsl
|
||||
})
|
||||
return nil
|
||||
}, exts...)
|
||||
|
||||
if len(messages) > 0 {
|
||||
return fmt.Errorf(strings.Join(messages, ";"))
|
||||
return fmt.Errorf(strings.Join(messages, ";\n"))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Dashboards[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 dashboard via process or id
|
||||
func Get(dashboard interface{}) (*DSL, error) {
|
||||
id := ""
|
||||
|
|
|
|||
|
|
@ -7,13 +7,14 @@ import (
|
|||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/flow"
|
||||
"github.com/yaoapp/yao/i18n"
|
||||
"github.com/yaoapp/yao/model"
|
||||
"github.com/yaoapp/yao/script"
|
||||
"github.com/yaoapp/yao/share"
|
||||
"github.com/yaoapp/yao/test"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
prepare(t)
|
||||
|
||||
err := Load(config.Conf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -22,23 +23,13 @@ func TestLoad(t *testing.T) {
|
|||
}
|
||||
|
||||
func prepare(t *testing.T, language ...string) {
|
||||
// runtime.Load(config.Conf)
|
||||
i18n.Load(config.Conf)
|
||||
share.DBConnect(config.Conf.DB) // removed later
|
||||
|
||||
// load scripts
|
||||
err := script.Load(config.Conf)
|
||||
err := Load(config.Conf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// load models
|
||||
err = model.Load(config.Conf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// load flows
|
||||
err = flow.Load(config.Conf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -11,11 +11,14 @@ import (
|
|||
"github.com/yaoapp/kun/any"
|
||||
"github.com/yaoapp/kun/maps"
|
||||
"github.com/yaoapp/yao/config"
|
||||
q "github.com/yaoapp/yao/query"
|
||||
"github.com/yaoapp/yao/test"
|
||||
)
|
||||
|
||||
func TestProcessData(t *testing.T) {
|
||||
load(t)
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
prepare(t)
|
||||
|
||||
args := []interface{}{"workspace", map[string]interface{}{"range": "2022-01-02", "status": "checked"}}
|
||||
res, err := process.New("yao.dashboard.Data", args...).Exec()
|
||||
if err != nil {
|
||||
|
|
@ -26,7 +29,10 @@ func TestProcessData(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProcessComponent(t *testing.T) {
|
||||
load(t)
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
prepare(t)
|
||||
|
||||
args := []interface{}{
|
||||
"workspace",
|
||||
"fields.filter.状态.edit.props.xProps",
|
||||
|
|
@ -82,7 +88,10 @@ func TestProcessComponent(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProcessComponentError(t *testing.T) {
|
||||
load(t)
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
prepare(t)
|
||||
|
||||
args := []interface{}{
|
||||
"workspace",
|
||||
"fields.filter.edit.props.状态.::not-exist",
|
||||
|
|
@ -94,7 +103,10 @@ func TestProcessComponentError(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProcessSetting(t *testing.T) {
|
||||
load(t)
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
prepare(t)
|
||||
|
||||
args := []interface{}{"workspace"}
|
||||
res, err := process.New("yao.dashboard.Setting", args...).Exec()
|
||||
if err != nil {
|
||||
|
|
@ -109,7 +121,10 @@ func TestProcessSetting(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProcessXgen(t *testing.T) {
|
||||
load(t)
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
prepare(t)
|
||||
|
||||
args := []interface{}{"workspace"}
|
||||
res, err := process.New("yao.dashboard.Xgen", args...).Exec()
|
||||
if err != nil {
|
||||
|
|
@ -124,7 +139,10 @@ func TestProcessXgen(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProcessXgenWithPermissions(t *testing.T) {
|
||||
load(t)
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
prepare(t)
|
||||
|
||||
session.Global().Set("__permissions", map[string]interface{}{
|
||||
"dashboards.workspace": []string{
|
||||
"7f46a38d7ff3f1832375ff63cd412f41", // operation.actions[0] 跳转至大屏
|
||||
|
|
@ -161,17 +179,6 @@ func TestProcessXgenWithPermissions(t *testing.T) {
|
|||
assert.NotEqual(t, nil, data.Get("fields.dashboard.图表展示2"))
|
||||
}
|
||||
|
||||
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) {
|
||||
pet := model.Select("pet")
|
||||
err := pet.Insert(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue