airqo-platform / airqo-platform/AirQo-api
Fix unbounded activities lookup in listAirQoActive method
- Ngôn ngữ chính
- JavaScript
- Star
- 26
- Fork
- 24
- Merge trung bình
- 5 giờ 36 phút
- Pull request đã merge (30 ngày)
- 81
Mô tả
## 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
```javascript
.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
- Original PR: https://github.com/airqo-platform/AirQo-api/pull/5170
- Code Review Comment: https://github.com/airqo-platform/AirQo-api/pull/5170#discussion_r2326476045
## Requested by
@Baalmart
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.