Skip to content

Fix unbounded activities lookup in listAirQoActive method #5174

@coderabbitai

Description

@coderabbitai

Problem

The activities lookup in the listAirQoActive method in src/device-registry/models/Site.js (around lines 976-982) is currently unbounded and can cause memory issues and exploded response sizes.

Current Implementation

.lookup({
  from: "activities",
  localField: "_id",
  foreignField: "site_id",
  as: "activities",
})

Proposed Solution

Mirror the guarded lookup pattern used in the list() method by:

  1. Adding maxActivities parsing and capping at the start of listAirQoActive
  2. Replacing the simple lookup with a pipeline-based lookup that includes:
    • $match on site_id
    • $sort by createdAt descending
    • $limit using maxActivities
    • $project to only include needed fields

Benefits

  • Prevents memory exhaustion from large activity datasets
  • Maintains consistent behavior with the list() method
  • Allows configurable activity limits while providing sensible defaults

References

Requested by

@Baalmart

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions