Skip to content

Commit e3b0969

Browse files
refactor(api): update GetStats to fetch status from devices collection
1 parent 90772bf commit e3b0969

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

api/store/mongo/device.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ func (s *Store) DeviceSetOnline(ctx context.Context, uid models.UID, timestamp t
353353
UID: device.UID,
354354
TenantID: device.TenantID,
355355
LastSeen: device.LastSeen,
356-
Status: string(device.Status),
357356
}
358357

359358
updated := cd.LastSeen.Before(timestamp)

api/store/mongo/stats.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@ func (s *Store) GetStats(ctx context.Context) (*models.Stats, error) {
2323
}}, query...)
2424
}
2525

26-
query = append([]bson.M{{
27-
"$match": bson.M{
28-
"status": "accepted",
26+
query = append([]bson.M{
27+
{
28+
"$lookup": bson.M{
29+
"from": "devices",
30+
"localField": "uid",
31+
"foreignField": "uid",
32+
"as": "device",
33+
},
2934
},
30-
}}, query...)
35+
{
36+
"$addFields": bson.M{
37+
"status": "$device.status",
38+
},
39+
},
40+
{
41+
"$match": bson.M{
42+
"status": "accepted",
43+
},
44+
},
45+
}, query...)
3146

3247
onlineDevices, err := AggregateCount(ctx, s.db.Collection("connected_devices"), query)
3348
if err != nil {

pkg/models/device.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type ConnectedDevice struct {
8686
UID string `json:"uid"`
8787
TenantID string `json:"tenant_id" bson:"tenant_id"`
8888
LastSeen time.Time `json:"last_seen" bson:"last_seen"`
89-
Status string `json:"status" bson:"status"`
89+
Status string `json:"status" bson:",omitempty"`
9090
}
9191

9292
type DevicePosition struct {

0 commit comments

Comments
 (0)