Skip to content

Commit

Permalink
akash command: query deployment-group
Browse files Browse the repository at this point in the history
fixes #241
  • Loading branch information
boz committed Jun 13, 2018
1 parent 6c0cdc8 commit a303d99
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 18 deletions.
30 changes: 30 additions & 0 deletions app/deployment/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (a *app) Query(state appstate.State, req tmtypes.RequestQuery) tmtypes.Resp

if strings.HasPrefix(req.GetPath(), appstate.DeploymentGroupPath) {
id := strings.TrimPrefix(req.Path, appstate.DeploymentGroupPath)

if len(id) == 0 {
return a.doDeploymentGroupRangeQuery(state)
}

key, err := keys.ParseGroupPath(id)
if err != nil {
return tmtypes.ResponseQuery{
Expand Down Expand Up @@ -160,6 +165,31 @@ func (a *app) doRangeQuery(state appstate.State) tmtypes.ResponseQuery {
}
}

func (a *app) doDeploymentGroupRangeQuery(state appstate.State) tmtypes.ResponseQuery {
objs, err := state.DeploymentGroup().All()
if err != nil {
return tmtypes.ResponseQuery{
Code: code.ERROR,
Log: err.Error(),
}
}

bytes, err := proto.Marshal(&types.DeploymentGroups{
Items: objs,
})
if err != nil {
return tmtypes.ResponseQuery{
Code: code.ERROR,
Log: err.Error(),
}
}

return tmtypes.ResponseQuery{
Value: bytes,
Height: state.Version(),
}
}

func (a *app) doDeploymentGroupQuery(state appstate.State, key keys.DeploymentGroup) tmtypes.ResponseQuery {

dep, err := state.DeploymentGroup().Get(key.ID())
Expand Down
34 changes: 34 additions & 0 deletions cmd/akash/query/deployment_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package query

import (
"github.com/ovrclk/akash/cmd/akash/session"
"github.com/ovrclk/akash/keys"
"github.com/spf13/cobra"
)

func queryDeploymentGroupCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "deployment-group [deployment-group ...]",
Short: "query deployment groups",
RunE: session.WithSession(session.RequireNode(doQueryDeploymentGroupCommand)),
}

return cmd
}

func doQueryDeploymentGroupCommand(session session.Session, cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return handleMessage(session.QueryClient().DeploymentGroups(session.Ctx()))
}
for _, arg := range args {
key, err := keys.ParseGroupPath(arg)
if err != nil {
return err
}
if err := handleMessage(session.QueryClient().DeploymentGroup(session.Ctx(), key.ID())); err != nil {
return err
}
}
return nil
}
3 changes: 2 additions & 1 deletion cmd/akash/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func QueryCommand() *cobra.Command {
cmd.AddCommand(
queryAccountCommand(),
queryDeploymentCommand(),
queryDeploymentGroupCommand(),
queryProviderCommand(),
queryOrderCommand(),
queryLeaseCommand(),
queryFulfillmentCommand(),
queryLeaseCommand(),
)

return cmd
Expand Down
10 changes: 10 additions & 0 deletions query/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Client interface {
Deployment(ctx context.Context, id []byte) (*types.Deployment, error)
DeploymentLeases(ctx context.Context, id []byte) (*types.Leases, error)

DeploymentGroups(ctx context.Context) (*types.DeploymentGroups, error)
DeploymentGroup(ctx context.Context, id types.DeploymentGroupID) (*types.DeploymentGroup, error)

Orders(ctx context.Context) (*types.Orders, error)
Expand Down Expand Up @@ -86,6 +87,15 @@ func (c *client) Deployment(ctx context.Context, id []byte) (*types.Deployment,
return obj, nil
}

func (c *client) DeploymentGroups(ctx context.Context) (*types.DeploymentGroups, error) {
obj := &types.DeploymentGroups{}
path := DeploymentGroupsPath()
if err := c.Get(ctx, path, obj); err != nil {
return nil, err
}
return obj, nil
}

func (c *client) DeploymentGroup(ctx context.Context, id types.DeploymentGroupID) (*types.DeploymentGroup, error) {
obj := &types.DeploymentGroup{}
path := DeploymentGroupPath(id)
Expand Down
23 changes: 23 additions & 0 deletions query/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions query/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func DeploymentLeasesPath(address []byte) string {
return state.LeasePath + util.X(address)
}

func DeploymentGroupsPath() string {
return state.DeploymentGroupPath
}

func DeploymentGroupPath(id types.DeploymentGroupID) string {
return state.DeploymentGroupPath + keys.DeploymentGroupID(id).Path()
}
Expand Down
57 changes: 40 additions & 17 deletions state/deployment_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type DeploymentGroupAdapter interface {
Save(*types.DeploymentGroup) error
Get(id types.DeploymentGroupID) (*types.DeploymentGroup, error)
All() ([]*types.DeploymentGroup, error)
ForDeployment(addr base.Bytes) ([]*types.DeploymentGroup, error)
}

Expand All @@ -38,33 +39,37 @@ func (a *deploymentGroupAdapter) Get(id types.DeploymentGroupID) (*types.Deploym
return obj, proto.Unmarshal(buf, obj)
}

func (a *deploymentGroupAdapter) All() ([]*types.DeploymentGroup, error) {
min := a.allMinRange()
max := a.allMaxRange()
return a.forRange(min, max)
}

func (a *deploymentGroupAdapter) ForDeployment(deployment base.Bytes) ([]*types.DeploymentGroup, error) {
min := a.deploymentMinRange(deployment)
max := a.deploymentMaxRange(deployment)

_, bufs, err := a.state.GetRange(min, max, MaxRangeLimit)
if err != nil {
return nil, err
}

var items []*types.DeploymentGroup

for _, buf := range bufs {
item := &types.DeploymentGroup{}
if err := item.Unmarshal(buf); err != nil {
return nil, err
}
items = append(items, item)
}

return items, nil
return a.forRange(min, max)
}

func (a *deploymentGroupAdapter) keyFor(id types.DeploymentGroupID) base.Bytes {
key := keys.DeploymentGroupID(id).Bytes()
return append(base.Bytes(DeploymentGroupPath), key...)
}

func (a *deploymentGroupAdapter) allMinRange() []byte {
return a.keyFor(types.DeploymentGroupID{
Deployment: MinAddress(),
Seq: 0,
})
}

func (a *deploymentGroupAdapter) allMaxRange() []byte {
return a.keyFor(types.DeploymentGroupID{
Deployment: MaxAddress(),
Seq: math.MaxUint64,
})
}

func (a *deploymentGroupAdapter) deploymentMinRange(deployment []byte) []byte {
return a.keyFor(types.DeploymentGroupID{
Deployment: deployment,
Expand All @@ -78,3 +83,21 @@ func (a *deploymentGroupAdapter) deploymentMaxRange(deployment []byte) []byte {
Seq: math.MaxUint64,
})
}

func (a *deploymentGroupAdapter) forRange(min, max []byte) ([]*types.DeploymentGroup, error) {
_, bufs, err := a.state.GetRange(min, max, MaxRangeLimit)
if err != nil {
return nil, err
}

var items []*types.DeploymentGroup

for _, buf := range bufs {
item := &types.DeploymentGroup{}
if err := item.Unmarshal(buf); err != nil {
return nil, err
}
items = append(items, item)
}
return items, nil
}

0 comments on commit a303d99

Please sign in to comment.