Skip to content

CLOUDP-57857: Describe available measurements for the given partition of the given process, Atlas #100

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

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ gen-mocks: ## Generate mocks
mockgen -source=internal/store/events.go -destination=internal/mocks/mock_events.go -package=mocks
mockgen -source=internal/store/process_measurements.go -destination=internal/mocks/mock_process_measurements.go -package=mocks
mockgen -source=internal/store/process_disks.go -destination=internal/mocks/mock_process_disks.go -package=mocks
mockgen -source=internal/store/process_disk_measurements.go -destination=internal/mocks/mock_process_disk_measurements.go -package=mocks
mockgen -source=internal/store/process_databases.go -destination=internal/mocks/mock_process_databases.go -package=mocks
mockgen -source=internal/store/host_measurements.go -destination=internal/mocks/mock_host_measurements.go -package=mocks
mockgen -source=internal/store/indexes.go -destination=internal/mocks/mock_indexes.go -package=mocks
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas_measurements_databases_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func AtlasMeasurementsDatabasesListBuilder() *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if opts.host, opts.port, err = GetHostNameAndPort(args[0]); err != nil {
if opts.host, opts.port, err = getHostNameAndPort(args[0]); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method should be private

return err
}

Expand Down
10 changes: 2 additions & 8 deletions internal/cli/atlas_measurements_databases_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ func TestAtlasMeasurementsDatabasesListsOpts_Run(t *testing.T) {
store: mockStore,
}

hostName, port, err := GetHostNameAndPort("hard-00-00.mongodb.net:27017")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not be testing this here

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

opts := listOpts.newListOptions()
mockStore.
EXPECT().ProcessDatabases(listOpts.projectID, hostName, port, opts).
EXPECT().ProcessDatabases(listOpts.projectID, listOpts.host, listOpts.port, opts).
Return(expected, nil).
Times(1)

err = listOpts.Run()
if err != nil {
if err := listOpts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
1 change: 1 addition & 0 deletions internal/cli/atlas_measurements_disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func AtlasMeasurementsDisksBuilder() *cobra.Command {
}

cmd.AddCommand(AtlasMeasurementsDisksListBuilder())
cmd.AddCommand(AtlasMeasurementsDisksDescribeBuilder())

return cmd
}
91 changes: 91 additions & 0 deletions internal/cli/atlas_measurements_disks_describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 cli

import (
"github.com/mongodb/mongocli/internal/description"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/json"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type atlasMeasurementsDisksDescribeOpts struct {
globalOpts
measurementsOpts
host string
port int
name string
store store.ProcessDiskMeasurementsLister
}

func (opts *atlasMeasurementsDisksDescribeOpts) init() error {
if opts.ProjectID() == "" {
return errMissingProjectID
}

var err error
opts.store, err = store.New()
return err
}

func (opts *atlasMeasurementsDisksDescribeOpts) Run() error {
listOpts := opts.newProcessMeasurementListOptions()
result, err := opts.store.ProcessDiskMeasurements(opts.ProjectID(), opts.host, opts.port, opts.name, listOpts)

if err != nil {
return err
}

return json.PrettyPrint(result)
}

// mcli atlas measurements disk(s) describe [host:port] [name] --granularity g --period p --start start --end end [--type type] [--projectId projectId]
func AtlasMeasurementsDisksDescribeBuilder() *cobra.Command {
opts := &atlasMeasurementsDisksDescribeOpts{}
cmd := &cobra.Command{
Use: "describe [host:port] [name]",
Short: description.ListDisks,
Args: cobra.ExactArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
opts.host, opts.port, err = getHostNameAndPort(args[0])
if err != nil {
return err
}
opts.name = args[1]
return opts.Run()
},
}

cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)

cmd.Flags().StringVar(&opts.granularity, flags.Granularity, "", usage.Granularity)
cmd.Flags().StringVar(&opts.period, flags.Period, "", usage.Period)
cmd.Flags().StringVar(&opts.start, flags.Start, "", usage.Start)
cmd.Flags().StringVar(&opts.end, flags.End, "", usage.End)
cmd.Flags().StringVar(&opts.measurementType, flags.MeasurementType, "", usage.MeasurementType)

cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

_ = cmd.MarkFlagRequired(flags.Granularity)

return cmd
}
49 changes: 49 additions & 0 deletions internal/cli/atlas_measurements_disks_describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 cli

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/fixtures"
"github.com/mongodb/mongocli/internal/mocks"
)

func TestAtlasMeasurementsDisksDescribeOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockProcessDiskMeasurementsLister(ctrl)

defer ctrl.Finish()

expected := fixtures.DiskMeasurements()

listOpts := &atlasMeasurementsDisksDescribeOpts{
host: "hard-00-00.mongodb.net",
port: 27017,
name: "test",
store: mockStore,
}

opts := listOpts.newProcessMeasurementListOptions()
mockStore.
EXPECT().ProcessDiskMeasurements(listOpts.projectID, listOpts.host, listOpts.port, listOpts.name, opts).
Return(expected, nil).
Times(1)

err := listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
6 changes: 3 additions & 3 deletions internal/cli/atlas_measurements_disks_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func (opts *atlasMeasurementsDisksListsOpts) Run() error {
return json.PrettyPrint(result)
}

// mongocli atlas measurements process(es) disks lists host:port
// mongocli atlas measurements process(es) disks lists [host:port]
func AtlasMeasurementsDisksListBuilder() *cobra.Command {
opts := &atlasMeasurementsDisksListsOpts{}
cmd := &cobra.Command{
Use: "list",
Use: "list [host:port]",
Short: description.ListDisks,
Aliases: []string{"ls"},
Args: cobra.ExactArgs(1),
Expand All @@ -65,7 +65,7 @@ func AtlasMeasurementsDisksListBuilder() *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
opts.host, opts.port, err = GetHostNameAndPort(args[0])
opts.host, opts.port, err = getHostNameAndPort(args[0])
if err != nil {
return err
}
Expand Down
10 changes: 2 additions & 8 deletions internal/cli/atlas_measurements_disks_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ func TestAtlasMeasurementsDisksListsOpts_Run(t *testing.T) {
store: mockStore,
}

hostName, port, err := GetHostNameAndPort("hard-00-00.mongodb.net:27017")
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}

opts := listOpts.newListOptions()
mockStore.
EXPECT().ProcessDisks(listOpts.projectID, hostName, port, opts).
EXPECT().ProcessDisks(listOpts.projectID, listOpts.host, listOpts.port, opts).
Return(expected, nil).
Times(1)

err = listOpts.Run()
if err != nil {
if err := listOpts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
4 changes: 3 additions & 1 deletion internal/cli/atlas_measurements_processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func AtlasMeasurementsProcessBuilder() *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
opts.host, opts.port, err = GetHostNameAndPort(args[0])
opts.host, opts.port, err = getHostNameAndPort(args[0])
if err != nil {
return err
}
Expand All @@ -84,5 +84,7 @@ func AtlasMeasurementsProcessBuilder() *cobra.Command {

cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

_ = cmd.MarkFlagRequired(flags.Granularity)

return cmd
}
10 changes: 2 additions & 8 deletions internal/cli/atlas_measurements_processes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,13 @@ func TestAtlasMeasurementsProcess_Run(t *testing.T) {
listOpts.granularity = "PT1M"
listOpts.period = "PT1M"

hostName, port, err := GetHostNameAndPort("hard-00-00.mongodb.net:27017")
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}

opts := listOpts.newProcessMeasurementListOptions()
mockStore.
EXPECT().ProcessMeasurements(listOpts.projectID, hostName, port, opts).
EXPECT().ProcessMeasurements(listOpts.projectID, listOpts.host, listOpts.port, opts).
Return(expected, nil).
Times(1)

err = listOpts.Run()
if err != nil {
if err := listOpts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
4 changes: 2 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ func (opts *listOpts) newListOptions() *atlas.ListOptions {
}
}

// GetHostNameAndPort return the hostname and the port starting from the string hostname:port
func GetHostNameAndPort(hostInfo string) (string, int, error) {
// getHostNameAndPort return the hostname and the port starting from the string hostname:port
func getHostNameAndPort(hostInfo string) (string, int, error) {
host := strings.SplitN(hostInfo, ":", -1)
if len(host) != 2 {
return "", 0, fmt.Errorf("expected hostname:port, got %s", host)
Expand Down
30 changes: 30 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cli

import "testing"

func TestGetHostNameAndPort(t *testing.T) {
t.Run("valid parameter", func(t *testing.T) {
host, port, err := getHostNameAndPort("test:2000")
if err != nil {
t.Fatalf("getHostNameAndPort unexpecteted err: %#v\n", err)
}
if host != "test" {
t.Errorf("Expected '%s', got '%s'\n", "test", host)
}
if port != 2000 {
t.Errorf("Expected '%d', got '%d'\n", 2000, port)
}
})
t.Run("incomplete format", func(t *testing.T) {
_, _, err := getHostNameAndPort("test")
if err == nil {
t.Fatal("getHostNameAndPort should return an error\n")
}
})
t.Run("incomplete format", func(t *testing.T) {
_, _, err := getHostNameAndPort(":test")
if err == nil {
t.Fatal("getHostNameAndPort should return an error\n")
}
})
}
1 change: 1 addition & 0 deletions internal/description/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ A user’s roles apply to all the clusters in the project.`
ProcessMeasurements = "Get measurements for a given host."
Disks = "List available disks or disks measurements for a given host."
ListDisks = "List available disks for a given host."
DescribeDisks = "Describe disks measurements for a given host partition."
Databases = "List available databases or databases measurements for a given host."
ListDatabases = "List available databases for a given host."
Whitelist = "Manage the IP whitelist for a project."
Expand Down
41 changes: 41 additions & 0 deletions internal/fixtures/measurements.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,47 @@ func ProcessDatabases() *atlas.ProcessDatabasesResponse {
}
}

func DiskMeasurements() *atlas.ProcessDiskMeasurements {
m := &atlas.ProcessDiskMeasurements{
ProcessMeasurements: &atlas.ProcessMeasurements{},
PartitionName: "test",
}
m.End = "2017-08-22T20:31:14Z"
m.Granularity = "PT1M"
m.GroupID = "12345678"
m.HostID = "shard-00-00.mongodb.net:27017"
m.ProcessID = "shard-00-00.mongodb.net:27017"
m.Start = "2017-08-22T20:30:45Z"
m.Links = []*atlas.Link{
{
Rel: "self",
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/disks/data/measurements?granularity=PT1M&period=PT1M",
},
{
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017",
Rel: "http://mms.mongodb.com/host",
},
}
m.Measurements = []*atlas.Measurements{
{
DataPoints: []*atlas.DataPoints{
{
Timestamp: "2017-08-22T20:31:12Z",
Value: nil,
},
{
Timestamp: "2017-08-22T20:31:14Z",
Value: nil,
},
},
Name: "ASSERT_REGULAR",
Units: "SCALAR_PER_SECOND",
},
}

return &atlas.ProcessDiskMeasurements{}
}

func ProcessMeasurements() *atlas.ProcessMeasurements {
return &atlas.ProcessMeasurements{
End: "2017-08-22T20:31:14Z",
Expand Down
Loading