forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 数据图表 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package chart | ||
|
||
import ( | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/yaoapp/kun/exception" | ||
"github.com/yaoapp/xiang/config" | ||
"github.com/yaoapp/xiang/share" | ||
"github.com/yaoapp/xiang/xlog" | ||
) | ||
|
||
// Charts 已载入图表 | ||
var Charts = map[string]*Chart{} | ||
|
||
// Load 加载数据表格 | ||
func Load(cfg config.Config) { | ||
LoadFrom(cfg.RootChart, "") | ||
} | ||
|
||
// LoadFrom 从特定目录加载 | ||
func LoadFrom(dir string, prefix string) { | ||
|
||
if share.DirNotExists(dir) { | ||
return | ||
} | ||
|
||
share.Walk(dir, ".json", func(root, filename string) { | ||
name := share.SpecName(root, filename) | ||
content := share.ReadFile(filename) | ||
chart, err := LoadChart(content, name) | ||
if err != nil { | ||
exception.New("%s 图表格式错误", 400, name).Ctx(filename).Throw() | ||
} | ||
Charts[name] = chart | ||
}) | ||
} | ||
|
||
// LoadChart 载入数据表格 | ||
func LoadChart(source []byte, name string) (*Chart, error) { | ||
chart := Chart{} | ||
err := jsoniter.Unmarshal(source, &chart) | ||
if err != nil { | ||
xlog.Println(name) | ||
xlog.Println(err.Error()) | ||
xlog.Println(string(source)) | ||
return nil, err | ||
} | ||
chart.Prepare() | ||
return &chart, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package chart | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yaoapp/xiang/config" | ||
"github.com/yaoapp/xiang/model" | ||
"github.com/yaoapp/xiang/query" | ||
"github.com/yaoapp/xiang/share" | ||
) | ||
|
||
func TestLoad(t *testing.T) { | ||
share.DBConnect(config.Conf.Database) | ||
model.Load(config.Conf) | ||
query.Load(config.Conf) | ||
|
||
Load(config.Conf) | ||
LoadFrom("not a path", "404.") | ||
check(t) | ||
} | ||
|
||
func check(t *testing.T) { | ||
keys := []string{} | ||
for key := range Charts { | ||
keys = append(keys, key) | ||
} | ||
assert.Equal(t, 1, len(keys)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package chart | ||
|
||
import ( | ||
"github.com/yaoapp/gou" | ||
"github.com/yaoapp/xiang/share" | ||
) | ||
|
||
// Chart 图表格式 | ||
type Chart struct { | ||
gou.Flow | ||
APIs map[string]API `json:"apis,omitempty"` | ||
Filters map[string]share.Filter `json:"filters,omitempty"` | ||
Page share.Page `json:"page,omitempty"` | ||
} | ||
|
||
// API 图表 API | ||
type API struct { | ||
Disable bool `json:"disable,omitempty"` | ||
Guard string `json:"guard,omitempty"` | ||
Default interface{} `json:"default,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 数据分析查询引擎 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package query | ||
|
||
import ( | ||
"github.com/yaoapp/gou" | ||
dsl "github.com/yaoapp/gou/query/gou" | ||
"github.com/yaoapp/kun/exception" | ||
"github.com/yaoapp/xiang/config" | ||
"github.com/yaoapp/xun/capsule" | ||
) | ||
|
||
// Load 加载查询引擎 | ||
func Load(cfg config.Config) { | ||
XiangQuery() | ||
} | ||
|
||
// XiangQuery 注册应用引擎象 xiang | ||
func XiangQuery() { | ||
gou.RegisterEngine("xiang", &dsl.Query{ | ||
Query: capsule.Query(), | ||
GetTableName: func(s string) string { | ||
if mod, has := gou.Models[s]; has { | ||
return mod.MetaData.Table.Name | ||
} | ||
exception.New("%s 数据模型尚未加载", 404).Throw() | ||
return s | ||
}, | ||
AESKey: config.Conf.Database.AESKey, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters