Skip to content

Commit 574ff3a

Browse files
kristinapathakkcajmagic
authored andcommitted
Added a way to get a list of device ids (#83)
1 parent 2efc206 commit 574ff3a

File tree

8 files changed

+111
-28
lines changed

8 files changed

+111
-28
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [v0.8.0]
10+
- Refactored pruning
811
- Added comments for godocs
12+
- Added a way to get a list of device ids
913

1014
## [v0.7.0]
1115
- Added `record_id` to index
@@ -122,7 +126,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
122126
- Initial creation
123127
- Created `db` and `xvault` package
124128

125-
[Unreleased]: https://github.com/Comcast/codex/compare/v0.7.0..HEAD
129+
[Unreleased]: https://github.com/Comcast/codex/compare/v0.8.0..HEAD
130+
[v0.8.0]: https://github.com/Comcast/codex/compare/v0.7.0...v0.8.0
126131
[v0.7.0]: https://github.com/Comcast/codex/compare/v0.6.0...v0.7.0
127132
[v0.6.0]: https://github.com/Comcast/codex/compare/v0.5.0...v0.6.0
128133
[v0.5.0]: https://github.com/Comcast/codex/compare/v0.4.3...v0.5.0

db/db.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ const (
2424
// TypeLabel is for labeling metrics; if there is a single metric for
2525
// successful queries, the typeLabel and corresponding type can be used
2626
// when incrementing the metric.
27-
TypeLabel = "type"
28-
InsertType = "insert"
29-
DeleteType = "delete"
30-
ReadType = "read"
31-
PingType = "ping"
32-
// ListReadType is for reading from the blacklist.
33-
ListReadType = "listRead"
27+
TypeLabel = "type"
28+
InsertType = "insert"
29+
DeleteType = "delete"
30+
ReadType = "read"
31+
PingType = "ping"
32+
BlacklistReadType = "blacklistRead"
3433
)
3534

3635
// Record is the struct used to insert an event into the database. It includes

db/postgresql/db.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ type Config struct {
8080
// Connection manages the connection to the postgresql database, and maintains
8181
// a health check on the database connection.
8282
type Connection struct {
83-
finder finder
84-
findList findList
85-
mutliInsert multiinserter
86-
deleter deleter
87-
closer closer
88-
pinger pinger
89-
stats stats
90-
gennericDB *sql.DB
83+
finder finder
84+
findList findList
85+
deviceFinder deviceFinder
86+
mutliInsert multiinserter
87+
deleter deleter
88+
closer closer
89+
pinger pinger
90+
stats stats
91+
gennericDB *sql.DB
9192

9293
pruneLimit int
9394
health *health.Health
@@ -150,6 +151,7 @@ func CreateDbConnection(config Config, provider provider.Provider, health *healt
150151

151152
dbConn.finder = conn
152153
dbConn.findList = conn
154+
dbConn.deviceFinder = conn
153155
dbConn.mutliInsert = conn
154156
dbConn.deleter = conn
155157
dbConn.closer = conn
@@ -293,13 +295,25 @@ func (c *Connection) GetRecordsToDelete(shard int, limit int, deathDate int64) (
293295
func (c *Connection) GetBlacklist() (list []blacklist.BlackListedItem, err error) {
294296
err = c.findList.findBlacklist(&list)
295297
if err != nil {
296-
c.measures.SQLQueryFailureCount.With(db.TypeLabel, db.ListReadType).Add(1.0)
298+
c.measures.SQLQueryFailureCount.With(db.TypeLabel, db.BlacklistReadType).Add(1.0)
297299
return []blacklist.BlackListedItem{}, emperror.WrapWith(err, "Getting records from database failed")
298300
}
299-
c.measures.SQLQuerySuccessCount.With(db.TypeLabel, db.ListReadType).Add(1.0)
301+
c.measures.SQLQuerySuccessCount.With(db.TypeLabel, db.BlacklistReadType).Add(1.0)
300302
return
301303
}
302304

305+
// GetDeviceList returns a list of device ids where the device id is greater
306+
// than the offset device id.
307+
func (c *Connection) GetDeviceList(offset string, limit int) ([]string, error) {
308+
list, err := c.deviceFinder.getList(offset, limit)
309+
if err != nil {
310+
c.measures.SQLQueryFailureCount.With(db.TypeLabel, db.ReadType).Add(1.0)
311+
return []string{}, emperror.WrapWith(err, "Getting list of devices from database failed")
312+
}
313+
c.measures.SQLQuerySuccessCount.With(db.TypeLabel, db.ReadType).Add(1.0)
314+
return list, nil
315+
}
316+
303317
// DeleteRecord removes a record.
304318
func (c *Connection) DeleteRecord(shard int, deathDate int64, recordID int64) error {
305319
rowsAffected, err := c.deleter.delete(&db.Record{}, 1, "shard = ? AND death_date = ? AND record_id = ?", shard, deathDate, recordID)

db/postgresql/db_test.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,20 @@ func TestGetRecordIDs(t *testing.T) {
181181
expectedSuccessMetric float64
182182
expectedFailureMetric float64
183183
expectedErr error
184-
expectedCalls int
185184
}{
186185
{
187186
description: "Success",
188187
deviceID: "1234",
189188
expectedRecords: []db.RecordToDelete{{DeathDate: 222, RecordID: 12345}},
190189
expectedSuccessMetric: 1.0,
191190
expectedErr: nil,
192-
expectedCalls: 1,
193191
},
194192
{
195193
description: "Get Error",
196194
deviceID: "1234",
197195
expectedRecords: []db.RecordToDelete{},
198196
expectedFailureMetric: 1.0,
199197
expectedErr: errors.New("test Get error"),
200-
expectedCalls: 1,
201198
},
202199
}
203200

@@ -211,9 +208,7 @@ func TestGetRecordIDs(t *testing.T) {
211208
measures: m,
212209
finder: mockObj,
213210
}
214-
if tc.expectedCalls > 0 {
215-
mockObj.On("findRecordsToDelete", mock.Anything, mock.Anything, mock.Anything).Return(tc.expectedRecords, tc.expectedErr).Times(tc.expectedCalls)
216-
}
211+
mockObj.On("findRecordsToDelete", mock.Anything, mock.Anything, mock.Anything).Return(tc.expectedRecords, tc.expectedErr).Once()
217212
p.Assert(t, SQLQuerySuccessCounter)(xmetricstest.Value(0.0))
218213
p.Assert(t, SQLQueryFailureCounter)(xmetricstest.Value(0.0))
219214

@@ -231,6 +226,56 @@ func TestGetRecordIDs(t *testing.T) {
231226
}
232227
}
233228

229+
func TestDeviceList(t *testing.T) {
230+
tests := []struct {
231+
description string
232+
expectedIDs []string
233+
expectedSuccessMetric float64
234+
expectedFailureMetric float64
235+
expectedErr error
236+
}{
237+
{
238+
description: "Success",
239+
expectedIDs: []string{"aaa", "bbb", "ccc"},
240+
expectedSuccessMetric: 1.0,
241+
expectedErr: nil,
242+
},
243+
{
244+
description: "Get Error",
245+
expectedIDs: []string{},
246+
expectedFailureMetric: 1.0,
247+
expectedErr: errors.New("test Get error"),
248+
},
249+
}
250+
251+
for _, tc := range tests {
252+
t.Run(tc.description, func(t *testing.T) {
253+
assert := assert.New(t)
254+
mockObj := new(mockDeviceFinder)
255+
p := xmetricstest.NewProvider(nil, Metrics)
256+
m := NewMeasures(p)
257+
dbConnection := Connection{
258+
measures: m,
259+
deviceFinder: mockObj,
260+
}
261+
mockObj.On("getList", mock.Anything, mock.Anything, mock.Anything).Return(tc.expectedIDs, tc.expectedErr).Once()
262+
p.Assert(t, SQLQuerySuccessCounter)(xmetricstest.Value(0.0))
263+
p.Assert(t, SQLQueryFailureCounter)(xmetricstest.Value(0.0))
264+
265+
result, err := dbConnection.GetDeviceList("", 10)
266+
mockObj.AssertExpectations(t)
267+
p.Assert(t, SQLQuerySuccessCounter, db.TypeLabel, db.ReadType)(xmetricstest.Value(tc.expectedSuccessMetric))
268+
p.Assert(t, SQLQueryFailureCounter, db.TypeLabel, db.ReadType)(xmetricstest.Value(tc.expectedFailureMetric))
269+
if tc.expectedErr == nil || err == nil {
270+
assert.Equal(tc.expectedErr, err)
271+
} else {
272+
assert.Contains(err.Error(), tc.expectedErr.Error())
273+
}
274+
assert.Equal(tc.expectedIDs, result)
275+
})
276+
}
277+
}
278+
234279
func TestPruneRecords(t *testing.T) {
235280
pruneTestErr := errors.New("test prune history error")
236281
tests := []struct {

db/postgresql/executer.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type (
3939
findList interface {
4040
findBlacklist(out *[]blacklist.BlackListedItem) error
4141
}
42+
deviceFinder interface {
43+
getList(offset string, limit int, where ...interface{}) ([]string, error)
44+
}
4245
multiinserter interface {
4346
insert(records []db.Record) (int64, error)
4447
}
@@ -79,6 +82,14 @@ func (b *dbDecorator) findBlacklist(out *[]blacklist.BlackListedItem) error {
7982
return db.Error
8083
}
8184

85+
func (b *dbDecorator) getList(offset string, limit int, where ...interface{}) ([]string, error) {
86+
var result []string
87+
// Raw SQL
88+
db := b.Raw("SELECT device_id from devices.events WHERE device_id > ? GROUP BY device_id LIMIT ?", offset, limit).Pluck("device_id", &result)
89+
//db := b.Limit(limit).Select("device_id").Find(&[]Record{}, where).Group("device_id").Where("device_id > ?", offset).Pluck("device_id", &result)
90+
return result, db.Error
91+
}
92+
8293
func (b *dbDecorator) insert(records []db.Record) (int64, error) {
8394
if len(records) == 0 {
8495
return 0, errNoEvents

db/postgresql/mocks_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func (f *mockFinder) findRecordsToDelete(limit int, shard int, deathDate int64)
4242
return args.Get(0).([]db.RecordToDelete), args.Error(1)
4343
}
4444

45+
type mockDeviceFinder struct {
46+
mock.Mock
47+
}
48+
49+
func (df *mockDeviceFinder) getList(offset string, limit int, where ...interface{}) ([]string, error) {
50+
args := df.Called(offset, limit, where)
51+
return args.Get(0).([]string), args.Error(1)
52+
}
53+
4554
type mockMultiInsert struct {
4655
mock.Mock
4756
}

db/retry/retry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (ltg RetryListGService) GetBlacklist() (list []blacklist.BlackListedItem, e
265265
sleepTime := ltg.config.interval
266266
for i := 0; i < retries+1; i++ {
267267
if i > 0 {
268-
ltg.config.measures.SQLQueryRetryCount.With(db.TypeLabel, db.ListReadType).Add(1.0)
268+
ltg.config.measures.SQLQueryRetryCount.With(db.TypeLabel, db.BlacklistReadType).Add(1.0)
269269
ltg.config.sleep(sleepTime)
270270
sleepTime = sleepTime * ltg.config.intervalMult
271271
}
@@ -274,7 +274,7 @@ func (ltg RetryListGService) GetBlacklist() (list []blacklist.BlackListedItem, e
274274
}
275275
}
276276

277-
ltg.config.measures.SQLQueryEndCount.With(db.TypeLabel, db.ListReadType).Add(1.0)
277+
ltg.config.measures.SQLQueryEndCount.With(db.TypeLabel, db.BlacklistReadType).Add(1.0)
278278
return
279279
}
280280

db/retry/retry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ func TestRetryGetBlacklist(t *testing.T) {
389389
p.Assert(t, SQLQueryEndCounter)(xmetricstest.Value(0.0))
390390
_, err := retryListGService.GetBlacklist()
391391
mockObj.AssertExpectations(t)
392-
p.Assert(t, SQLQueryRetryCounter, db.TypeLabel, db.ListReadType)(xmetricstest.Value(tc.expectedRetryMetric))
393-
p.Assert(t, SQLQueryEndCounter, db.TypeLabel, db.ListReadType)(xmetricstest.Value(1.0))
392+
p.Assert(t, SQLQueryRetryCounter, db.TypeLabel, db.BlacklistReadType)(xmetricstest.Value(tc.expectedRetryMetric))
393+
p.Assert(t, SQLQueryEndCounter, db.TypeLabel, db.BlacklistReadType)(xmetricstest.Value(1.0))
394394
if tc.expectedErr == nil || err == nil {
395395
assert.Equal(tc.expectedErr, err)
396396
} else {

0 commit comments

Comments
 (0)