airqo-platform / airqo-platform/AirQo-api

Fix unbounded activities lookup in listAirQoActive method

Đang mở
#5,174 0 bình luận 0 reaction 1 người được giao Được @Baalmart nhận Xem trên GitHub
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

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.