Skip to content

Commit

Permalink
Merge pull request #12 from kcajmagic/fix/deviceList
Browse files Browse the repository at this point in the history
fix cassandra deviceList getter
  • Loading branch information
kristinapathak committed Oct 17, 2019
2 parents 36055bd + cc593d0 commit 3b1ad0f
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 29 deletions.
6 changes: 3 additions & 3 deletions cassandra/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"errors"
"time"

"github.com/InVisionApp/go-health"
"github.com/InVisionApp/go-health/v2"
"github.com/go-kit/kit/metrics/provider"
"github.com/goph/emperror"
db "github.com/xmidt-org/codex-db"
Expand Down Expand Up @@ -181,8 +181,8 @@ func (c *Connection) GetBlacklist() (list []blacklist.BlackListedItem, err error

// GetDeviceList returns a list of device ids where the device id is greater
// than the offset device id.
func (c *Connection) GetDeviceList(offset string, limit int) ([]string, error) {
list, err := c.deviceFinder.getList(offset, limit)
func (c *Connection) GetDeviceList(startDate time.Time, endDate time.Time, offset int, limit int) ([]string, error) {
list, err := c.deviceFinder.getList(startDate, endDate, offset, limit)
if err != nil {
c.measures.SQLQueryFailureCount.With(db.TypeLabel, db.ReadType).Add(1.0)
return []string{}, emperror.WrapWith(err, "Getting list of devices from database failed")
Expand Down
5 changes: 3 additions & 2 deletions cassandra/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/xmidt-org/webpa-common/xmetrics/xmetricstest"
"github.com/xmidt-org/wrp-go/wrp"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -203,11 +204,11 @@ func TestDeviceList(t *testing.T) {
measures: m,
deviceFinder: mockObj,
}
mockObj.On("getList", mock.Anything, mock.Anything, mock.Anything).Return(tc.expectedIDs, tc.expectedErr).Once()
mockObj.On("getList", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.expectedIDs, tc.expectedErr).Once()
p.Assert(t, SQLQuerySuccessCounter)(xmetricstest.Value(0.0))
p.Assert(t, SQLQueryFailureCounter)(xmetricstest.Value(0.0))

result, err := dbConnection.GetDeviceList("", 10)
result, err := dbConnection.GetDeviceList(time.Now(), time.Now(), 0, 10)
mockObj.AssertExpectations(t)
p.Assert(t, SQLQuerySuccessCounter, db.TypeLabel, db.ReadType)(xmetricstest.Value(tc.expectedSuccessMetric))
p.Assert(t, SQLQueryFailureCounter, db.TypeLabel, db.ReadType)(xmetricstest.Value(tc.expectedFailureMetric))
Expand Down
7 changes: 4 additions & 3 deletions cassandra/executer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
db "github.com/xmidt-org/codex-db"
"github.com/xmidt-org/codex-db/blacklist"
"github.com/yugabyte/gocql"
"time"
)

type (
Expand All @@ -33,7 +34,7 @@ type (
findBlacklist() ([]blacklist.BlackListedItem, error)
}
deviceFinder interface {
getList(offset string, limit int) ([]string, error)
getList(startDate time.Time, endDate time.Time, offset int, limit int) ([]string, error)
}
multiinserter interface {
insert(records []db.Record) (int, error)
Expand Down Expand Up @@ -86,12 +87,12 @@ func (b *dbDecorator) findRecords(limit int, filter string, where ...interface{}
return records, err
}

func (b *dbDecorator) getList(offset string, limit int) ([]string, error) {
func (b *dbDecorator) getList(startDate time.Time, endDate time.Time, offset int, limit int) ([]string, error) {
var result []string

var device string

iter := b.session.Query("SELECT device_id from devices.events WHERE device_id > ? GROUP BY device_id LIMIT ?", offset, limit).Iter()
iter := b.session.Query("SELECT device_id from devices.events WHERE birthdate >= ? AND birthdate <= ? GROUP BY device_id LIMIT ? OFFSET ?", startDate.UnixNano(), endDate.UnixNano(), limit, offset).Iter()
for iter.Scan(&device) {
result = append(result, device)
}
Expand Down
4 changes: 2 additions & 2 deletions cassandra/executermeasures.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (b *dbMeasuresDecorator) findRecords(limit int, filter string, where ...int
return records, err
}

func (b *dbMeasuresDecorator) getList(offset string, limit int) ([]string, error) {
func (b *dbMeasuresDecorator) getList(startDate time.Time, endDate time.Time, offset int, limit int) ([]string, error) {
b.measures.PoolInUseConnections.Add(1.0)
now := time.Now()
result, err := b.deviceFinder.getList(offset, limit)
result, err := b.deviceFinder.getList(startDate, endDate, offset, limit)
b.measures.SQLDuration.With(db.TypeLabel, db.ReadType, CountLabel, strconv.Itoa(len(result))).Observe(time.Since(now).Seconds())
b.measures.PoolInUseConnections.Add(-1.0)

Expand Down
5 changes: 3 additions & 2 deletions cassandra/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"github.com/stretchr/testify/mock"
db "github.com/xmidt-org/codex-db"
"time"
)

type mockFinder struct {
Expand All @@ -41,8 +42,8 @@ type mockDeviceFinder struct {
mock.Mock
}

func (df *mockDeviceFinder) getList(offset string, limit int) ([]string, error) {
args := df.Called(offset, limit)
func (df *mockDeviceFinder) getList(startDate time.Time, endDate time.Time, offset int, limit int) ([]string, error) {
args := df.Called(startDate, endDate, offset, limit)
return args.Get(0).([]string), args.Error(1)
}

Expand Down
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ module github.com/xmidt-org/codex-db
go 1.12

require (
github.com/InVisionApp/go-health v2.1.0+incompatible
github.com/InVisionApp/go-health/v2 v2.1.2
github.com/InVisionApp/go-logger v1.0.1
github.com/go-kit/kit v0.8.0
github.com/goph/emperror v0.17.2
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 // indirect
github.com/go-kit/kit v0.9.0

github.com/goph/emperror v0.17.3-0.20190703203600-60a8d9faa17b
github.com/influxdata/influxdb1-client v0.0.0-20190809212627-fc22c7df067e // indirect
github.com/jinzhu/gorm v1.9.10
github.com/prometheus/common v0.4.1
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.4.0
github.com/xmidt-org/capacityset v0.1.1
github.com/xmidt-org/webpa-common v1.3.0
github.com/xmidt-org/wrp-go v1.2.0
github.com/xmidt-org/webpa-common v1.4.0
github.com/xmidt-org/wrp-go v1.3.4

github.com/yugabyte/gocql v0.0.0-20190522232832-e049977574e9
)

0 comments on commit 3b1ad0f

Please sign in to comment.