Skip to content
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
2 changes: 0 additions & 2 deletions api/store/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,4 @@ type DeviceStore interface {
DeviceRename(ctx context.Context, uid models.UID, hostname string) error
DeviceUpdateStatus(ctx context.Context, uid models.UID, status models.DeviceStatus) error
DeviceSetPosition(ctx context.Context, uid models.UID, position models.DevicePosition) error
DeviceListByUsage(ctx context.Context, tenantID string) ([]models.UID, error)
DeviceChooser(ctx context.Context, tenantID string, chosen []string) error
}
48 changes: 0 additions & 48 deletions api/store/mocks/store.go

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

69 changes: 0 additions & 69 deletions api/store/mongo/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,52 +299,6 @@ func (s *Store) DeviceUpdateStatus(ctx context.Context, uid models.UID, status m
return nil
}

func (s *Store) DeviceListByUsage(ctx context.Context, tenant string) ([]models.UID, error) {
query := []bson.M{
{
"$match": bson.M{
"tenant_id": tenant,
},
},
{
"$group": bson.M{
"_id": "$device_uid",
"count": bson.M{
"$sum": 1,
},
},
},
{
"$sort": bson.M{
"count": -1,
},
},
{
"$limit": 3,
},
}

uids := make([]models.UID, 0)

cursor, err := s.db.Collection("sessions").Aggregate(ctx, query)
if err != nil {
return uids, FromMongoError(err)
}

for cursor.Next(ctx) {
var dev map[string]interface{}

err = cursor.Decode(&dev)
if err != nil {
return uids, err
}

uids = append(uids, models.UID(dev["_id"].(string)))
}

return uids, nil
}

func (s *Store) DeviceSetPosition(ctx context.Context, uid models.UID, position models.DevicePosition) error {
dev, err := s.db.Collection("devices").UpdateOne(ctx, bson.M{"uid": uid}, bson.M{"$set": bson.M{"position": position}})
if err != nil {
Expand All @@ -358,29 +312,6 @@ func (s *Store) DeviceSetPosition(ctx context.Context, uid models.UID, position
return nil
}

func (s *Store) DeviceChooser(ctx context.Context, tenantID string, chosen []string) error {
filter := bson.M{
"status": "accepted",
"tenant_id": tenantID,
"uid": bson.M{
"$nin": chosen,
},
}

update := bson.M{
"$set": bson.M{
"status": "pending",
},
}

_, err := s.db.Collection("devices").UpdateMany(ctx, filter, update)
if err != nil {
return err
}

return nil
}

func (s *Store) DeviceConflicts(ctx context.Context, target *models.DeviceConflicts) ([]string, bool, error) {
pipeline := []bson.M{
{
Expand Down
81 changes: 0 additions & 81 deletions api/store/mongo/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,55 +492,6 @@ func TestDeviceList(t *testing.T) {
}
}

func TestDeviceListByUsage(t *testing.T) {
type Expected struct {
uid []models.UID
len int
err error
}
cases := []struct {
description string
tenant string
fixtures []string
expected Expected
}{
{
description: "returns an empty list when tenant not exist",
tenant: "nonexistent",
fixtures: []string{fixtureSessions},
expected: Expected{
uid: []models.UID{},
len: 0,
err: nil,
},
},
{
description: "succeeds when has 1 or more device sessions",
tenant: "00000000-0000-4000-0000-000000000000",
fixtures: []string{fixtureSessions},
expected: Expected{
uid: []models.UID{"2300230e3ca2f637636b4d025d2235269014865db5204b6d115386cbee89809c"},
len: 1,
err: nil,
},
},
}

for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
ctx := context.Background()

assert.NoError(t, srv.Apply(tc.fixtures...))
t.Cleanup(func() {
assert.NoError(t, srv.Reset())
})

uids, err := s.DeviceListByUsage(ctx, tc.tenant)
assert.Equal(t, tc.expected, Expected{uid: uids, len: len(uids), err: err})
})
}
}

func TestDeviceResolve(t *testing.T) {
type Expected struct {
dev *models.Device
Expand Down Expand Up @@ -822,38 +773,6 @@ func TestDeviceSetPosition(t *testing.T) {
}
}

func TestDeviceChooser(t *testing.T) {
cases := []struct {
description string
tenant string
chosen []string
fixtures []string
expected error
}{
{
description: "",
tenant: "00000000-0000-4000-0000-000000000000",
chosen: []string{""},
fixtures: []string{fixtureDevices},
expected: nil,
},
}

for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
ctx := context.Background()

assert.NoError(t, srv.Apply(tc.fixtures...))
t.Cleanup(func() {
assert.NoError(t, srv.Reset())
})

err := s.DeviceChooser(ctx, tc.tenant, tc.chosen)
assert.Equal(t, tc.expected, err)
})
}
}

func TestDeviceConflicts(t *testing.T) {
type Expected struct {
conflicts []string
Expand Down
Loading