AOSSIE-Org / AOSSIE-Org/PictoPy

Refactor/toggle-favourite endpoint to eliminate full-table query and improve query efficiency

Đang mở
#1,200 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
283
Fork
679
Merge trung bình
7 ngày 2 giờ
Pull request đã merge (30 ngày)
3

Mô tả

### Describe the feature

## Overview

The current implementation of the `/toggle-favourite` endpoint retrieves all images from the database after updating the `isFavourite` flag, and then filters the target image in memory.

Although functionally correct, this introduces unnecessary database and processing overhead.

---

## Current Implementation

After toggling the favourite status, the route executes:

```python
image = next(
(img for img in db_get_all_images() if img["id"] == image_id), None
)
```

`db_get_all_images()` performs:

- Multiple JOIN operations
- Metadata parsing
- Tag grouping
- Dataset sorting

This entire workflow runs even though only a single image record is required.

---

## Why This Matters

This approach:

- Scales linearly with total image count
- Increases latency for a frequently used endpoint
- Performs unnecessary joins and metadata parsing
- Couples a single-record use case with a full-collection query

As image collections grow, this can negatively impact responsiveness.

---

## Proposed Improvement

1. Introduce a dedicated method:

```python
db_get_image_by_id(image_id: str)
```

2. Query only the required record with necessary JOINs.
3. Refactor `/toggle-favourite` to use this optimized method instead of calling `db_get_all_images()`.

---

## Expected Benefits

- Reduced database load
- Improved scalability
- Cleaner endpoint behavior
- Better separation of concerns

I would be happy to implement this improvement.

### Record

- [x] I agree to follow this project's Code of Conduct
- [x] I want to work on this issue

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.