Skip to content

Commit

Permalink
Add database measurements Endpoint (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn authored Apr 13, 2020
1 parent 1fbdd98 commit 5574d7c
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 8 deletions.
8 changes: 5 additions & 3 deletions mongodbatlas/mongodbatlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
libraryVersion = "0.1"
defaultBaseURL = "https://cloud.mongodb.com/api/atlas/v1.0/"
userAgent = "go-mongodbatlas" + libraryVersion
mediaType = "application/json"
jsonMediaType = "application/json"
gzipMediaType = "application/gzip"
)

Expand Down Expand Up @@ -85,6 +85,7 @@ type Client struct {
ProcessDisks ProcessDisksService
ProcessDiskMeasurements ProcessDiskMeasurementsService
ProcessDatabases ProcessDatabasesService
ProcessDatabaseMeasurements ProcessDatabaseMeasurementsService
Indexes IndexesService
Logs LogsService

Expand Down Expand Up @@ -210,6 +211,7 @@ func NewClient(httpClient *http.Client) *Client {
c.ProcessDisks = &ProcessDisksServiceOp{Client: c}
c.ProcessDiskMeasurements = &ProcessDiskMeasurementsServiceOp{Client: c}
c.ProcessDatabases = &ProcessDatabasesServiceOp{Client: c}
c.ProcessDatabaseMeasurements = &ProcessDatabaseMeasurementsServiceOp{Client: c}
c.Indexes = &IndexesServiceOp{Client: c}
c.Logs = &LogsServiceOp{Client: c}

Expand Down Expand Up @@ -273,9 +275,9 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int
}

if body != nil {
req.Header.Set("Content-Type", mediaType)
req.Header.Set("Content-Type", jsonMediaType)
}
req.Header.Add("Accept", mediaType)
req.Header.Add("Accept", jsonMediaType)
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
Expand Down
68 changes: 68 additions & 0 deletions mongodbatlas/process_database_measurements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package mongodbatlas

import (
"context"
"fmt"
"net/http"
)

const processDatabaseMeasurementsPath = processesDatabasesPath + "/%s/measurements"

// ProcessDatabaseMeasurementsService is an interface for interfacing with the process database measurements
// endpoints of the MongoDB Atlas API.
// See more: https://docs.atlas.mongodb.com/reference/api/process-databases-measurements/
type ProcessDatabaseMeasurementsService interface {
List(context.Context, string, string, int, string, *ProcessMeasurementListOptions) (*ProcessDatabaseMeasurements, *Response, error)
}

// ProcessDatabaseMeasurementsServiceOp handles communication with the process database measurements related methods of the
// MongoDB Atlas API
type ProcessDatabaseMeasurementsServiceOp struct {
Client RequestDoer
}

// ProcessDatabaseMeasurements represents a MongoDB process database measurements.
type ProcessDatabaseMeasurements struct {
*ProcessMeasurements
DatabaseName string `json:"databaseName"`
}

var _ ProcessDatabaseMeasurementsService = &ProcessDatabaseMeasurementsServiceOp{}

// List list measurements for a specific Atlas MongoDB database.
// See more: https://docs.atlas.mongodb.com/reference/api/process-databases-measurements/
func (s *ProcessDatabaseMeasurementsServiceOp) List(ctx context.Context, groupID, hostName string, port int, databaseName string, opts *ProcessMeasurementListOptions) (*ProcessDatabaseMeasurements, *Response, error) {

if groupID == "" {
return nil, nil, NewArgError("groupID", "must be set")
}

if hostName == "" {
return nil, nil, NewArgError("hostName", "must be set")
}

if databaseName == "" {
return nil, nil, NewArgError("databaseName", "must be set")
}

basePath := fmt.Sprintf(processDatabaseMeasurementsPath, groupID, hostName, port, databaseName)

//Add query params from listOptions
path, err := setListOptions(basePath, opts)
if err != nil {
return nil, nil, err
}

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(ProcessDatabaseMeasurements)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}
102 changes: 102 additions & 0 deletions mongodbatlas/process_database_measurements_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package mongodbatlas

import (
"fmt"
"net/http"
"testing"

"github.com/go-test/deep"
)

func TestProcessDatabaseMeasurements_List(t *testing.T) {
setup()
defer teardown()

groups := "12345678"
host := "shard-00-00.mongodb.net"
port := 27017
database := "database"

mux.HandleFunc(fmt.Sprintf("/groups/%s/processes/%s:%d/databases/%s/measurements", groups, host, port, database), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"end" : "2017-08-22T20:31:14Z",
"granularity" : "PT1M",
"groupId" : "12345678",
"hostId" : "shard-00-00.mongodb.net:27017",
"links" : [ {
"href" : "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/dataabses/database/measurements?granularity=PT1M&period=PT1M",
"rel" : "self"
}, {
"href" : "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017",
"rel" : "http://mms.mongodb.com/host"
} ],
"measurements" : [ {
"dataPoints" : [ {
"timestamp" : "2017-08-22T20:31:12Z",
"value" : null
}, {
"timestamp" : "2017-08-22T20:31:14Z",
"value" : null
} ],
"name" : "DISK_PARTITION_IOPS_READ",
"units" : "SCALAR_PER_SECOND"
}],
"databaseName":"database",
"processId" : "shard-00-00.mongodb.net:27017",
"start" : "2017-08-22T20:30:45Z"
}`)
})

opts := &ProcessMeasurementListOptions{
Granularity: "PT1M",
Period: "PT1M",
}

measurements, _, err := client.ProcessDatabaseMeasurements.List(ctx, groups, host, port, database, opts)
if err != nil {
t.Fatalf("ProcessDatabaseMeasurements.List returned error: %v", err)
}

expected := &ProcessDatabaseMeasurements{
ProcessMeasurements: &ProcessMeasurements{
End: "2017-08-22T20:31:14Z",
Granularity: "PT1M",
GroupID: "12345678",
HostID: "shard-00-00.mongodb.net:27017",
Links: []*Link{
{
Rel: "self",
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/dataabses/database/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",
},
},
Measurements: []*Measurements{
{
DataPoints: []*DataPoints{
{
Timestamp: "2017-08-22T20:31:12Z",
Value: nil,
},
{
Timestamp: "2017-08-22T20:31:14Z",
Value: nil,
},
},
Name: "DISK_PARTITION_IOPS_READ",
Units: "SCALAR_PER_SECOND",
},
},
ProcessID: "shard-00-00.mongodb.net:27017",
Start: "2017-08-22T20:30:45Z",
},
DatabaseName: "database",
}

if diff := deep.Equal(measurements, expected); diff != nil {
t.Error(diff)
}
}
6 changes: 3 additions & 3 deletions mongodbatlas/process_disk_measurements.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
)

const ProcessDiskMeasurementsPath = processesDisksPath + "/%s/measurements"
const processDiskMeasurementsPath = processesDisksPath + "/%s/measurements"

// ProcessDiskMeasurementsService is an interface for interfacing with the Process Disk Measurements
// endpoints of the MongoDB Atlas API.
Expand All @@ -29,7 +29,7 @@ type ProcessDiskMeasurements struct {

var _ ProcessDiskMeasurementsService = &ProcessDiskMeasurementsServiceOp{}

// Get gets measurements for a specific Atlas MongoDB disk.
// List lists measurements for a specific Atlas MongoDB disk.
// See more: https://docs.atlas.mongodb.com/reference/api/process-disks-measurements/#get-measurements-of-a-disk-for-a-mongodb-process
func (s *ProcessDiskMeasurementsServiceOp) List(ctx context.Context, groupID, hostName string, port int, diskName string, opts *ProcessMeasurementListOptions) (*ProcessDiskMeasurements, *Response, error) {

Expand All @@ -45,7 +45,7 @@ func (s *ProcessDiskMeasurementsServiceOp) List(ctx context.Context, groupID, ho
return nil, nil, NewArgError("diskName", "must be set")
}

basePath := fmt.Sprintf(ProcessDiskMeasurementsPath, groupID, hostName, port, diskName)
basePath := fmt.Sprintf(processDiskMeasurementsPath, groupID, hostName, port, diskName)

//Add query params from listOptions
path, err := setListOptions(basePath, opts)
Expand Down
4 changes: 2 additions & 2 deletions mongodbatlas/process_disk_measurements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/go-test/deep"
)

func TestDiskMeasurements_List(t *testing.T) {
func TestProcessDiskMeasurements_List(t *testing.T) {
setup()
defer teardown()

Expand Down Expand Up @@ -55,7 +55,7 @@ func TestDiskMeasurements_List(t *testing.T) {

measurements, _, err := client.ProcessDiskMeasurements.List(ctx, groups, host, port, disk, opts)
if err != nil {
t.Fatalf("Teams.Get returned error: %v", err)
t.Fatalf("ProcessDiskMeasurements.List returned error: %v", err)
}

expected := &ProcessDiskMeasurements{
Expand Down

0 comments on commit 5574d7c

Please sign in to comment.