Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] dashboard widget #296

Merged
merged 3 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[add] dashboard widget
  • Loading branch information
trheyi committed Dec 18, 2022
commit 70a3d6345095c4aac4a272fd38fc7557a291283b
5 changes: 3 additions & 2 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package cmd
import (
"bufio"
"fmt"
"os"

"github.com/blang/semver"
"github.com/fatih/color"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/spf13/cobra"
"github.com/yaoapp/yao/share"
"os"
)

var upgradeCmd = &cobra.Command{
Expand All @@ -27,7 +28,7 @@ var upgradeCmd = &cobra.Command{
currentVersion := semver.MustParse(share.VERSION)
if !found || latest.Version.LTE(currentVersion) {
fmt.Println(color.GreenString(L("🎉Current version is the latest🎉")))
os.Exit(1)
os.Exit(0)
}
fmt.Println(color.WhiteString(L("Do you want to update to %s ? (y/n): "), latest.Version))
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
Expand Down
7 changes: 4 additions & 3 deletions widgets/component/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ type Instances []InstanceDSL

// InstanceDSL the component instance DSL
type InstanceDSL struct {
Name string `json:"name,omitempty"`
Width interface{} `json:"width,omitempty"`
Height interface{} `json:"height,omitempty"`
Name string `json:"name,omitempty"`
Width interface{} `json:"width,omitempty"`
Height interface{} `json:"height,omitempty"`
Rows []InstanceDSL `json:"rows,omitempty"`
}

// ActionsExport the export actions
Expand Down
42 changes: 42 additions & 0 deletions widgets/dashboard/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dashboard

import (
"github.com/yaoapp/yao/widgets/action"
)

var processActionDefaults = map[string]*action.Process{

"Setting": {
Name: "yao.dashboard.Setting",
Guard: "bearer-jwt",
Process: "yao.dashboard.Xgen",
Default: []interface{}{nil},
},
"Component": {
Name: "yao.dashboard.Component",
Guard: "bearer-jwt",
Default: []interface{}{nil, nil, nil},
},
"Data": {
Name: "yao.dashboard.Data",
Guard: "bearer-jwt",
Default: []interface{}{nil},
},
}

// SetDefaultProcess set the default value of action
func (act *ActionDSL) SetDefaultProcess() {

act.Setting = action.ProcessOf(act.Setting).
Merge(processActionDefaults["Setting"]).
SetHandler(processHandler)

act.Component = action.ProcessOf(act.Component).
Merge(processActionDefaults["Component"]).
SetHandler(processHandler)

act.Data = action.ProcessOf(act.Data).
WithBefore(act.BeforeData).WithAfter(act.AfterData).
Merge(processActionDefaults["Data"]).
SetHandler(processHandler)
}
118 changes: 118 additions & 0 deletions widgets/dashboard/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package dashboard

import (
"fmt"

"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/widgets/action"
)

// Guard form widget dashboard
func Guard(c *gin.Context) {

id := c.Param("id")
if id == "" {
abort(c, 400, "the dashboard widget id does not found")
return
}

dashboard, has := Dashboards[id]
if !has {
abort(c, 404, fmt.Sprintf("the dashboard widget %s does not exist", id))
return
}

act, err := dashboard.getAction(c.FullPath())
if err != nil {
abort(c, 404, err.Error())
return
}

err = act.UseGuard(c, id)
if err != nil {
abort(c, 400, err.Error())
return
}

}

func abort(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{"code": code, "message": message})
c.Abort()
}

func (dashboard *DSL) getAction(path string) (*action.Process, error) {

switch path {
case "/api/__yao/dashboard/:id/setting":
return dashboard.Action.Setting, nil
case "/api/__yao/dashboard/:id/component/:xpath/:method":
return dashboard.Action.Component, nil
case "/api/__yao/dashboard/:id/data":
return dashboard.Action.Data, nil
}

return nil, fmt.Errorf("the form widget %s %s action does not exist", dashboard.ID, path)
}

// export API
func exportAPI() error {

http := gou.HTTP{
Name: "Widget Dashboard API",
Description: "Widget Dashboard API",
Version: share.VERSION,
Guard: "widget-dashboard",
Group: "__yao/dashboard",
Paths: []gou.Path{},
}

// GET /api/__yao/dashboard/:id/setting -> Default process: yao.dashboard.Xgen
path := gou.Path{
Label: "Setting",
Description: "Setting",
Path: "/:id/setting",
Method: "GET",
Process: "yao.dashboard.Setting",
In: []string{"$param.id"},
Out: gou.Out{Status: 200, Type: "application/json"},
}
http.Paths = append(http.Paths, path)

// GET /api/__yao/dashboard/:id/data -> Default process: yao.dashboard.Data $param.id :query
path = gou.Path{
Label: "Data",
Description: "Data",
Path: "/:id/data",
Method: "GET",
Process: "yao.dashboard.Data",
In: []string{"$param.id", ":query"},
Out: gou.Out{Status: 200, Type: "application/json"},
}
http.Paths = append(http.Paths, path)

// GET /api/__yao/dashboard/:id/component/:xpath/:method -> Default process: yao.dashboard.Component $param.id $param.xpath $param.method :query
path = gou.Path{
Label: "Component",
Description: "Component",
Path: "/:id/component/:xpath/:method",
Method: "GET",
Process: "yao.dashboard.Component",
In: []string{"$param.id", "$param.xpath", "$param.method", ":query"},
Out: gou.Out{Status: 200, Type: "application/json"},
}
http.Paths = append(http.Paths, path)

// api source
source, err := jsoniter.Marshal(http)
if err != nil {
return err
}

// load apis
_, err = gou.LoadAPIReturn(string(source), "widgets.dashboard")
return err
}
Loading