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

CLOUDP-70067: mongocli atlas|om|cm performanceAdvisor suggestedIndexes ls #448

Merged
merged 23 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
07f8588
CLOUDP-70053: mongocli atlas|om|cm performanceAdvisor namespaces list|ls
andreaangiolillo Sep 14, 2020
ac1167e
Revert "CLOUDP-70053: mongocli atlas|om|cm performanceAdvisor namespa…
andreaangiolillo Sep 14, 2020
ffd50b4
Merge remote-tracking branch 'origin/master'
andreaangiolillo Sep 14, 2020
c2c79b0
Merge remote-tracking branch 'origin/master'
andreaangiolillo Sep 15, 2020
3aa4b33
Merge remote-tracking branch 'origin/master' into CLOUDP-70067
andreaangiolillo Sep 15, 2020
03bfcf7
added list command
andreaangiolillo Sep 15, 2020
f5be760
added e2e test
andreaangiolillo Sep 15, 2020
7dfb208
added command to performaceAdvisor
andreaangiolillo Sep 15, 2020
b39c898
Update list.go
andreaangiolillo Sep 15, 2020
6e49993
Merge remote-tracking branch 'origin/master' into CLOUDP-70067
andreaangiolillo Sep 21, 2020
b3bc67f
Merge remote-tracking branch 'origin/master' into CLOUDP-70067
andreaangiolillo Sep 21, 2020
ba3b796
Merging master
andreaangiolillo Sep 21, 2020
5245a98
Update list.go
andreaangiolillo Sep 21, 2020
55ea933
Update list.go
andreaangiolillo Sep 21, 2020
1730b73
Update list.go
andreaangiolillo Sep 21, 2020
ec5a99c
Updated atlas go client
andreaangiolillo Sep 21, 2020
103c10b
Update list.go
andreaangiolillo Sep 21, 2020
36b6380
Update list.go
andreaangiolillo Sep 22, 2020
7253fb0
Update list.go
andreaangiolillo Sep 22, 2020
455c9ff
Update list.go
andreaangiolillo Sep 22, 2020
f53c789
Update list.go
andreaangiolillo Sep 22, 2020
a845bf3
Update list.go
andreaangiolillo Sep 22, 2020
a81843a
Update list.go
andreaangiolillo Sep 22, 2020
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
github.com/tangzero/inflector v1.0.0
go.mongodb.org/atlas v0.4.1-0.20200916093941-a86355b45816
go.mongodb.org/atlas v0.4.1-0.20200921142021-15be5f77e0ed
go.mongodb.org/ops-manager v0.9.4-0.20200918132531-11085c971c80
golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.mongodb.org/atlas v0.4.1-0.20200916093941-a86355b45816 h1:wnYPHWDNpG0XaMebz6LBe7RRb6c+2bzmZ7bcDJDbWcg=
go.mongodb.org/atlas v0.4.1-0.20200916093941-a86355b45816/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
go.mongodb.org/atlas v0.4.1-0.20200921142021-15be5f77e0ed h1:ZFLchdZ7U0JQWypTxzDGuSjrCsy2/35tW3gnpaKl/is=
go.mongodb.org/atlas v0.4.1-0.20200921142021-15be5f77e0ed/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
go.mongodb.org/ops-manager v0.9.4-0.20200918132531-11085c971c80 h1:P5XBWK4HoDHIkTxE+lN8lzVt1JxbS7Fg/S3bA8Wutyk=
go.mongodb.org/ops-manager v0.9.4-0.20200918132531-11085c971c80/go.mod h1:r7sU/JNPvYANcUxECB+zox4XR+vbrh//o6ejPWLfjCc=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/performanceadvisor/performance_advisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/cli/performanceadvisor/namespaces"
"github.com/mongodb/mongocli/internal/cli/performanceadvisor/slowquerylogs"
"github.com/mongodb/mongocli/internal/cli/performanceadvisor/suggestedindexes"
"github.com/spf13/cobra"
)

Expand All @@ -31,6 +32,7 @@ func Builder() *cobra.Command {
cmd.AddCommand(
namespaces.Builder(),
slowquerylogs.Builder(),
suggestedindexes.Builder(),
)

return cmd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
2,
3,
[]string{},
)
}
2 changes: 1 addition & 1 deletion internal/cli/performanceadvisor/slowquerylogs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func ListBuilder() *cobra.Command {
cmd.Flags().Int64Var(&opts.since, flag.Since, 0, usage.Since)
cmd.Flags().Int64Var(&opts.duration, flag.Duration, 0, usage.Duration)
cmd.Flags().Int64Var(&opts.nLog, flag.NLog, 20000, usage.NLog)
cmd.Flags().StringVar(&opts.namespaces, flag.Namespaces, "", usage.Namespaces)
cmd.Flags().StringVar(&opts.namespaces, flag.Namespaces, "", usage.SlowQueryNamespaces)

cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
Expand Down
19 changes: 19 additions & 0 deletions internal/cli/performanceadvisor/suggestedindexes/description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package suggestedindexes

const (
short = "Get suggested indexes for collections experiencing slow queries"
list = "Retrieves suggested indexes for collections experiencing slow queries"
)
106 changes: 106 additions & 0 deletions internal/cli/performanceadvisor/suggestedindexes/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package suggestedindexes

import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
atlas "go.mongodb.org/atlas/mongodbatlas"
)

const listTemplate = `ID NAMESPACE INDEX{{range .SuggestedIndexes}}
{{- $id := .ID}} {{- $name := .Namespace}}
{{ $id }} {{ $name }} { {{- range $i, $element := .Index}}
{{- range $key, $value := .}} {{if $i }},{{end}} "{{ $key }}":{{ $value }}{{ end }}{{ end }} }{{ end }}
`
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved

type ListOpts struct {
cli.GlobalOpts
cli.OutputOpts
cli.PerformanceAdvisorOpts
store store.PerformanceAdvisorIndexesLister
since int64
duration int64
namespaces string
nIndexes int64
nExamples int64
}

func (opts *ListOpts) initStore() error {
var err error
opts.store, err = store.New(config.Default())
return err
}

func (opts *ListOpts) Run() error {
host, err := opts.Host()
if err != nil {
return err
}
r, err := opts.store.PerformanceAdvisorIndexes(opts.ConfigProjectID(), host, opts.newSuggestedIndexOptions())
if err != nil {
return err
}

return opts.Print(r)
}

func (opts *ListOpts) newSuggestedIndexOptions() *atlas.SuggestedIndexOptions {
return &atlas.SuggestedIndexOptions{
Namespaces: opts.namespaces,
NIndexes: opts.nIndexes,
NExamples: opts.nExamples,
NamespaceOptions: atlas.NamespaceOptions{
Since: opts.since,
Duration: opts.duration,
},
}
}

// mongocli atlas performanceAdvisor suggestedIndexes list --processName processName --nIndexes nIndexes --nExamples nExamples --namespaces namespaces --since since --duration duration --projectId projectId
func ListBuilder() *cobra.Command {
opts := new(ListOpts)
cmd := &cobra.Command{
Use: "list",
Short: list,
Aliases: []string{"ls"},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(
opts.initStore,
opts.InitOutput(cmd.OutOrStdout(), listTemplate),
opts.MarkRequiredFlagsByService(cmd),
)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().StringVar(&opts.HostID, flag.HostID, "", usage.HostID)
cmd.Flags().StringVar(&opts.ProcessName, flag.ProcessName, "", usage.ProcessName)
cmd.Flags().Int64Var(&opts.since, flag.Since, 0, usage.Since)
cmd.Flags().Int64Var(&opts.duration, flag.Duration, 0, usage.Duration)
cmd.Flags().StringVar(&opts.namespaces, flag.Namespaces, "", usage.SuggestedIndexNamespaces)
cmd.Flags().Int64Var(&opts.nExamples, flag.NExamples, 0, usage.NExamples)
cmd.Flags().Int64Var(&opts.nIndexes, flag.NIndexes, 0, usage.NIndexes)

cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
return cmd
}
58 changes: 58 additions & 0 deletions internal/cli/performanceadvisor/suggestedindexes/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build unit

package suggestedindexes

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/mocks"
"go.mongodb.org/atlas/mongodbatlas"
)

func TestNamespacesList_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockPerformanceAdvisorIndexesLister(ctrl)
defer ctrl.Finish()

var expected *mongodbatlas.SuggestedIndexes

listOpts := &ListOpts{
store: mockStore,
}

mockStore.
EXPECT().
PerformanceAdvisorIndexes(listOpts.ProjectID, listOpts.ProcessName, listOpts.newSuggestedIndexOptions()).
Return(expected, nil).
Times(1)

err := listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}

func TestListBuilder(t *testing.T) {
cli.CmdValidator(
t,
ListBuilder(),
0,
[]string{flag.ProjectID, flag.Duration, flag.Since, flag.Namespaces, flag.NExamples, flag.NIndexes},
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package suggestedindexes

import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/spf13/cobra"
)

func Builder() *cobra.Command {
const use = "suggestedIndexes"
cmd := &cobra.Command{
Use: use,
Aliases: cli.GenerateAliases(use),
Short: short,
}
cmd.AddCommand(
ListBuilder())

return cmd
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build unit

package suggestedindexes

import (
"testing"

"github.com/mongodb/mongocli/internal/cli"
)

func TestBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
1,
[]string{},
)
}
4 changes: 3 additions & 1 deletion internal/flag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const (
Since = "since" // Since flag
Duration = "duration" // Duration Flag
NLog = "nLog" // NLog flag
Namespaces = "namespaces" // Namespaces flag
Namespaces = "namespaces" // Namespaces flag // Namespaces flag
NIndexes = "nIndexes" // NIndexes flag
NExamples = "nExamples" // NExamples flag
AuthDB = "authDB" // AuthDB flag
Hostname = "hostname" // Hostname flag
Port = "port" // Port flag
Expand Down
40 changes: 39 additions & 1 deletion internal/mocks/mock_performance_advisor.go

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

Loading