Skip to content

Commit

Permalink
Resolves #259 , #263 - Print HTTP Statistics and Improve Alias Perfor…
Browse files Browse the repository at this point in the history
…mance (#264)
  • Loading branch information
steve-r-west authored Dec 3, 2022
1 parent 1f69cd4 commit bcc3d69
Show file tree
Hide file tree
Showing 35 changed files with 623 additions and 123 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
indent_size=2
insert_final_newline = true
indent_style = space

[*.go]
indent_style = tab
indent_size = 4
1 change: 0 additions & 1 deletion cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var configure = &cobra.Command{
Short: "Creates a profile by prompting for input over the command line.",
Long: "Will first prompt for a name then a series of variable specific for the user being created",
Run: func(cmd *cobra.Command, args []string) {

configPath := profiles.GetConfigFilePath()
cfg, err := ini.Load(configPath)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var create = &cobra.Command{
Short: "Creates an entity of a resource.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
body, err := createInternal(args, crud.AutoFillOnCreate)
body, err := createInternal(context.Background(), args, crud.AutoFillOnCreate)

if err != nil {
return err
Expand Down Expand Up @@ -92,7 +92,10 @@ var create = &cobra.Command{
},
}

func createInternal(args []string, autoFillOnCreate bool) (string, error) {
func createInternal(ctx context.Context, args []string, autoFillOnCreate bool) (string, error) {
crud.OutstandingRequestCounter.Add(1)
defer crud.OutstandingRequestCounter.Done()

// Find Resource
resource, ok := resources.GetResourceByName(args[0])
if !ok {
Expand Down Expand Up @@ -135,7 +138,7 @@ func createInternal(args []string, autoFillOnCreate bool) (string, error) {
}

// Submit request
resp, err = httpclient.DoFileRequest(context.TODO(), resourceURL, byteBuf, contentType)
resp, err = httpclient.DoFileRequest(ctx, resourceURL, byteBuf, contentType)

} else {
// Assume it's application/json
Expand Down Expand Up @@ -169,7 +172,7 @@ func createInternal(args []string, autoFillOnCreate bool) (string, error) {
}

// Submit request
resp, err = httpclient.DoRequest(context.TODO(), "POST", resourceURL, params.Encode(), strings.NewReader(body))
resp, err = httpclient.DoRequest(ctx, "POST", resourceURL, params.Encode(), strings.NewReader(body))

}

Expand Down
18 changes: 9 additions & 9 deletions cmd/delete-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var DeleteAll = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Hidden: false,
RunE: func(cmd *cobra.Command, args []string) error {
return deleteAllInternal(args)
return deleteAllInternal(context.Background(), args)
},

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand All @@ -39,7 +39,7 @@ var DeleteAll = &cobra.Command{
},
}

func deleteAllInternal(args []string) error {
func deleteAllInternal(ctx context.Context, args []string) error {
// Find Resource
resource, ok := resources.GetResourceByName(args[0])
if !ok {
Expand All @@ -54,7 +54,7 @@ func deleteAllInternal(args []string) error {
return fmt.Errorf("resource %s doesn't support DELETE", args[0])
}

allParentEntityIds, err := getParentIds(context.Background(), resource)
allParentEntityIds, err := getParentIds(ctx, resource)

if err != nil {
return fmt.Errorf("could not retrieve parent ids for for resource %s, error: %w", resource.PluralName, err)
Expand All @@ -78,7 +78,7 @@ func deleteAllInternal(args []string) error {
params := url.Values{}
params.Add("page[limit]", "25")

resp, err := httpclient.DoRequest(context.Background(), "GET", resourceURL, params.Encode(), nil)
resp, err := httpclient.DoRequest(ctx, "GET", resourceURL, params.Encode(), nil)

if err != nil {
return err
Expand Down Expand Up @@ -122,7 +122,7 @@ func deleteAllInternal(args []string) error {

}

delPage(resource.DeleteEntityInfo, allIds)
delPage(ctx, resource.DeleteEntityInfo, allIds)
}
}

Expand Down Expand Up @@ -160,7 +160,7 @@ func getParentIds(ctx context.Context, resource resources.Resource) ([][]id.Idab

var flowsUrlRegex = regexp.MustCompile("^/v2/flows/([^/]+)$")

func delPage(urlInfo *resources.CrudEntityInfo, ids [][]id.IdableAttributes) {
func delPage(ctx context.Context, urlInfo *resources.CrudEntityInfo, ids [][]id.IdableAttributes) {
// Create a wait group to run DELETE in parallel
wg := sync.WaitGroup{}
for _, idAttr := range ids {
Expand All @@ -177,7 +177,7 @@ func delPage(urlInfo *resources.CrudEntityInfo, ids [][]id.IdableAttributes) {
}

// Submit request
resp, err := httpclient.DoRequest(context.TODO(), "DELETE", resourceURL, "", nil)
resp, err := httpclient.DoRequest(ctx, "DELETE", resourceURL, "", nil)
if err != nil {
return
}
Expand All @@ -200,7 +200,7 @@ func delPage(urlInfo *resources.CrudEntityInfo, ids [][]id.IdableAttributes) {

id := matches[1]
jsonBody := fmt.Sprintf(`{ "data": { "id":"%s", "type": "flow", "slug": "delete-%s" }}`, id, id)
resp2, err := httpclient.DoRequest(context.TODO(), "PUT", resourceURL, "", strings.NewReader(jsonBody))
resp2, err := httpclient.DoRequest(ctx, "PUT", resourceURL, "", strings.NewReader(jsonBody))

if err != nil {
return
Expand All @@ -212,7 +212,7 @@ func delPage(urlInfo *resources.CrudEntityInfo, ids [][]id.IdableAttributes) {
}
}

resp3, err := httpclient.DoRequest(context.TODO(), "DELETE", resourceURL, "", nil)
resp3, err := httpclient.DoRequest(ctx, "DELETE", resourceURL, "", nil)
if err != nil {
return
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var delete = &cobra.Command{
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

body, err := deleteInternal(args)
body, err := deleteInternal(context.Background(), args)
if err != nil {
return err
}
Expand Down Expand Up @@ -96,13 +96,16 @@ var delete = &cobra.Command{
},
}

func deleteInternal(args []string) (string, error) {
func deleteInternal(ctx context.Context, args []string) (string, error) {
crud.OutstandingRequestCounter.Add(1)
defer crud.OutstandingRequestCounter.Done()

resource, ok := resources.GetResourceByName(args[0])
if !ok {
return "", fmt.Errorf("could not find resource %s", args[0])
}

resp, err := deleteResource(args)
resp, err := deleteResource(ctx, args)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -132,7 +135,7 @@ func deleteInternal(args []string) (string, error) {

}

func deleteResource(args []string) (*http.Response, error) {
func deleteResource(ctx context.Context, args []string) (*http.Response, error) {
// Find Resource
resource, ok := resources.GetResourceByName(args[0])
if !ok {
Expand Down Expand Up @@ -186,7 +189,7 @@ func deleteResource(args []string) (*http.Response, error) {
}

// Submit request
resp, err := httpclient.DoRequest(context.TODO(), "DELETE", resourceURL, params.Encode(), payload)
resp, err := httpclient.DoRequest(ctx, "DELETE", resourceURL, params.Encode(), payload)
if err != nil {
return nil, fmt.Errorf("got error %s", err.Error())
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var get = &cobra.Command{
Short: "Retrieves a single resource.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
body, err := getInternal(args)
body, err := getInternal(context.Background(), args)
if err != nil {
return err
}
Expand Down Expand Up @@ -109,8 +109,8 @@ var get = &cobra.Command{
},
}

func getInternal(args []string) (string, error) {
resp, err := getResource(args)
func getInternal(ctx context.Context, args []string) (string, error) {
resp, err := getResource(ctx, args)

if err != nil {
return "", err
Expand Down Expand Up @@ -158,7 +158,10 @@ func getUrl(resource resources.Resource, args []string) (*resources.CrudEntityIn
}
}

func getResource(args []string) (*http.Response, error) {
func getResource(ctx context.Context, args []string) (*http.Response, error) {
crud.OutstandingRequestCounter.Add(1)
defer crud.OutstandingRequestCounter.Done()

// Find Resource
resource, ok := resources.GetResourceByName(args[0])
if !ok {
Expand Down Expand Up @@ -210,7 +213,7 @@ func getResource(args []string) (*http.Response, error) {
}

// Submit request
resp, err := httpclient.DoRequest(context.TODO(), "GET", resourceURL, params.Encode(), nil)
resp, err := httpclient.DoRequest(ctx, "GET", resourceURL, params.Encode(), nil)

if err != nil {
return nil, fmt.Errorf("got error %s", err.Error())
Expand Down
6 changes: 4 additions & 2 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
gojson "encoding/json"
"fmt"
"github.com/elasticpath/epcc-cli/config"
Expand Down Expand Up @@ -273,11 +274,12 @@ var loginCustomer = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {

ctx := context.Background()
newArgs := make([]string, 0)
newArgs = append(newArgs, "customer-token")
newArgs = append(newArgs, args...)

body, err := createInternal(newArgs, crud.AutoFillOnCreate)
body, err := createInternal(ctx, newArgs, crud.AutoFillOnCreate)

if err != nil {
log.Warnf("Login not completed successfully")
Expand All @@ -303,7 +305,7 @@ var loginCustomer = &cobra.Command{
if customerTokenResponse != nil {

// Get the customer so we have aliases where we need the id.
getCustomerBody, err := getInternal([]string{"customer", customerTokenResponse.Data.CustomerId})
getCustomerBody, err := getInternal(ctx, []string{"customer", customerTokenResponse.Data.CustomerId})

if err != nil {
log.Warnf("Could not retrieve customer")
Expand Down
16 changes: 9 additions & 7 deletions cmd/reset-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ var ResetStore = &cobra.Command{
Long: "This command resets a store to it's initial state. There are some limitations to this as for instance orders cannot be deleted, nor can audit entries.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
storeId, err := getStoreId(args)
ctx := context.Background()

storeId, err := getStoreId(ctx, args)
if err != nil {
return fmt.Errorf("could not determine store id: %w", err)
}
Expand All @@ -49,25 +51,25 @@ var ResetStore = &cobra.Command{
// We would also need locking to go faster.

// Get customer and account authentication settings to populate the aliases
_, err = getInternal([]string{"customer-authentication-settings"})
_, err = getInternal(ctx, []string{"customer-authentication-settings"})

if err != nil {
errors = append(errors, err.Error())
}

_, err = getInternal([]string{"account-authentication-settings"})
_, err = getInternal(ctx, []string{"account-authentication-settings"})

if err != nil {
errors = append(errors, err.Error())
}

_, err = getInternal([]string{"merchant-realm-mappings"})
_, err = getInternal(ctx, []string{"merchant-realm-mappings"})

if err != nil {
errors = append(errors, err.Error())
}

_, err = getInternal([]string{"authentication-realms"})
_, err = getInternal(ctx, []string{"authentication-realms"})

if err != nil {
errors = append(errors, err.Error())
Expand Down Expand Up @@ -101,7 +103,7 @@ var ResetStore = &cobra.Command{
},
}

func getStoreId(args []string) (string, error) {
func getStoreId(ctx context.Context, args []string) (string, error) {
resource, ok := resources.GetResourceByName("settings")

if !ok {
Expand All @@ -116,7 +118,7 @@ func getStoreId(args []string) (string, error) {

params := url.Values{}

resp, err := httpclient.DoRequest(context.Background(), "GET", resourceURL, params.Encode(), nil)
resp, err := httpclient.DoRequest(ctx, "GET", resourceURL, params.Encode(), nil)

defer resp.Body.Close()

Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/logger"
"github.com/elasticpath/epcc-cli/external/profiles"
"github.com/elasticpath/epcc-cli/external/shutdown"
"github.com/elasticpath/epcc-cli/external/version"
log "github.com/sirupsen/logrus"
"github.com/thediveo/enumflag"
Expand Down Expand Up @@ -69,6 +70,7 @@ func init() {
RootCmd.PersistentFlags().Uint16VarP(&rateLimit, "rate-limit", "", 10, "Request limit per second")
RootCmd.PersistentFlags().BoolVarP(&httpclient.Retry5xx, "retry-5xx", "", false, "Whether we should retry requests with HTTP 5xx response code")
RootCmd.PersistentFlags().BoolVarP(&httpclient.Retry429, "retry-429", "", false, "Whether we should retry requests with HTTP 429 response code")
RootCmd.PersistentFlags().BoolVarP(&httpclient.DontLog2xxs, "silence-2xx", "", false, "Whether we should silence HTTP 2xx response code logging")

RootCmd.PersistentFlags().Float32VarP(&requestTimeout, "timeout", "", 10, "Request timeout in seconds (fractional values allowed)")

Expand Down Expand Up @@ -160,6 +162,7 @@ func Execute() {
select {
case sig := <-sigs:
log.Warnf("Shutting down program due to signal [%v]", sig)
shutdown.ShutdownFlag.Store(true)
exit = true
case <-normalShutdown:
}
Expand All @@ -168,6 +171,9 @@ func Execute() {
shutdownHandlerDone <- true
}()

log.Infof("Waiting for all outstanding requests to finish")
crud.OutstandingRequestCounter.Wait()

httpclient.LogStats()
aliases.FlushAliases()

Expand Down
Loading

0 comments on commit bcc3d69

Please sign in to comment.