Skip to content

Commit

Permalink
[cli] Uses efficient interface to query multiple indicators in batch …
Browse files Browse the repository at this point in the history
…for standalone and cluster modes
  • Loading branch information
zhiyanliu authored and Jack47 committed Dec 29, 2017
1 parent a9eb3a3 commit 836dcfe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 51 deletions.
50 changes: 17 additions & 33 deletions src/cli/cluster_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func ClusterRetrievePipelineIndicatorNames(c *cli.Context) error {
return errs.Return()
}

func ClusterGetPipelineIndicatorValue(c *cli.Context) error {
func ClusterGetPipelineIndicatorsValue(c *cli.Context) error {
args := c.Args()
group := c.GlobalString("group")
timeoutSec := uint16(*c.GlobalGeneric("timeout").(*common.Uint16Value))
Expand All @@ -267,41 +267,25 @@ func ClusterGetPipelineIndicatorValue(c *cli.Context) error {
}

pipelineName := args[0]
indicatorNames := args[1:]

var expiredTime time.Duration
for i, indicatorName := range indicatorNames {
req := new(pdu.StatisticsClusterRequest)
req.TimeoutSec = uint16(timeout.Seconds())

if timeout <= expiredTime {
errs.append(fmt.Errorf(
"timeout: skip to handle [%s]", strings.Join(indicatorNames[i:], ", ")))
break
}
timeout -= expiredTime

startTime := common.Now()
value, apiResp, err := clusterStatApi().GetPipelineIndicatorValue(
group, pipelineName, indicatorName, req)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
continue
} else if apiResp.Error != nil {
errs.append(fmt.Errorf("%s-%s: %s", pipelineName, indicatorName, apiResp.Error.Error))
continue
}

data, err := json.Marshal(value)
req := new(pdu.ClusterPipelineIndicatorsValueRequest)
req.TimeoutSec = uint16(timeout.Seconds())
req.IndicatorNames = args[1:]

retrieveResp, apiResp, err := clusterStatApi().GetPipelineIndicatorsValue(
group, pipelineName, req)
if err != nil {
errs.append(fmt.Errorf("%s: %v", pipelineName, err))
} else if apiResp.Error != nil {
errs.append(fmt.Errorf("%s: %s", pipelineName, apiResp.Error.Error))
} else {
data, err := json.Marshal(retrieveResp.Values)
if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
continue
errs.append(fmt.Errorf("%s: %v", pipelineName, err))
} else {
// TODO: make it pretty
fmt.Printf("%s\n", data)
}

// TODO: make it pretty
fmt.Printf("%s\n", data)
}

return errs.Return()
Expand Down
31 changes: 15 additions & 16 deletions src/cli/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/hexdecteam/easegateway-go-client/rest/1.0/statistics/v1/pdu"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -169,7 +170,7 @@ func RetrievePipelineIndicatorNames(c *cli.Context) error {
return errs.Return()
}

func GetPipelineIndicatorValue(c *cli.Context) error {
func GetPipelineIndicatorsValue(c *cli.Context) error {
args := c.Args()

errs := &multipleErr{}
Expand All @@ -184,24 +185,22 @@ func GetPipelineIndicatorValue(c *cli.Context) error {

pipelineName := args[0]

for _, indicatorName := range args[1:] {
value, apiResp, err := statApi().GetPipelineIndicatorValue(pipelineName, indicatorName)
if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
continue
} else if apiResp.Error != nil {
errs.append(fmt.Errorf("%s-%s: %s", pipelineName, indicatorName, apiResp.Error.Error))
continue
}
req := new(pdu.PipelineIndicatorsValueRequest)
req.IndicatorNames = args[1:]

data, err := json.Marshal(value)
retrieveResp, apiResp, err := statApi().GetPipelineIndicatorsValue(pipelineName, req)
if err != nil {
errs.append(fmt.Errorf("%s: %v", pipelineName, err))
} else if apiResp.Error != nil {
errs.append(fmt.Errorf("%s: %s", pipelineName, apiResp.Error.Error))
} else {
data, err := json.Marshal(retrieveResp.Values)
if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
continue
errs.append(fmt.Errorf("%s: %v", pipelineName, err))
} else {
// TODO: make it pretty
fmt.Printf("%s\n", data)
}

// TODO: make it pretty
fmt.Printf("%s\n", data)
}

return errs.Return()
Expand Down
4 changes: 2 additions & 2 deletions src/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ var statCmd = urfavecli.Command{
{
Name: "value",
Usage: "Retrieve pipeline indicator value",
Action: cli.GetPipelineIndicatorValue,
Action: cli.GetPipelineIndicatorsValue,
},
{
Name: "desc",
Expand Down Expand Up @@ -394,7 +394,7 @@ var statcCmd = urfavecli.Command{
{
Name: "value",
Usage: "Retrieve pipeline indicator value",
Action: cli.ClusterGetPipelineIndicatorValue,
Action: cli.ClusterGetPipelineIndicatorsValue,
},
{
Name: "desc",
Expand Down

0 comments on commit 836dcfe

Please sign in to comment.