Skip to content

Commit

Permalink
[add] migrate widgets.dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Mar 25, 2023
1 parent 61c101d commit dc45eed
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 74 deletions.
2 changes: 1 addition & 1 deletion widgets/dashboard/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
98 changes: 57 additions & 41 deletions widgets/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -55,59 +55,75 @@ 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, "")
messages := []string{}
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())
}

return nil
}, exts...)

if len(messages) > 0 {
return fmt.Errorf(strings.Join(messages, ";\n"))
}

return err
}

// LoadFrom load from dir
func LoadFrom(dir string, prefix string) error {
// LoadFile load table dsl by file
func LoadFile(root string, file string) error {

if share.DirNotExists(dir) {
return fmt.Errorf("%s does not exists", dir)
id := share.ID(root, file)
data, err := application.App.Read(file)
if err != nil {
return err
}

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
}
dsl := New(id)
err = application.Parse(file, data, dsl)
if err != nil {
return fmt.Errorf("[%s] %s", id, err.Error())
}

if dsl.Action == nil {
dsl.Action = &ActionDSL{}
}
dsl.Action.SetDefaultProcess()
err = dsl.parse(id, root)
if err != nil {
return err
}

if dsl.Layout == nil {
dsl.Layout = &LayoutDSL{}
}
Dashboards[id] = dsl
return nil
}

// mapping
err = dsl.mapping()
if err != nil {
messages = append(messages, fmt.Sprintf("[%s] Mapping %s", id, err.Error()))
return
}
// LoadData load via data
func (dsl *DSL) parse(id string, root string) error {

// Validate
err = dsl.Validate()
if err != nil {
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
return
}
if dsl.Action == nil {
dsl.Action = &ActionDSL{}
}
dsl.Action.SetDefaultProcess()

Dashboards[id] = dsl
})
if dsl.Layout == nil {
dsl.Layout = &LayoutDSL{}
}

if len(messages) > 0 {
return fmt.Errorf(strings.Join(messages, ";"))
// mapping
err := dsl.mapping()
if err != nil {
return err
}

return err
// Validate
err = dsl.Validate()
if err != nil {
return err
}

return nil
}

// Get dashboard via process or id
Expand Down
19 changes: 5 additions & 14 deletions widgets/dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
if err != nil {
t.Fatal(err)
}

// load models
err = model.Load(config.Conf)
err := Load(config.Conf)
if err != nil {
t.Fatal(err)
}

// load flows
err = flow.Load(config.Conf)
if err != nil {
t.Fatal(err)
Expand Down
43 changes: 25 additions & 18 deletions widgets/dashboard/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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] 跳转至大屏
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit dc45eed

Please sign in to comment.