Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fetch unlabeled detections endpoint #366

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RonanMorgan
Copy link
Collaborator

When calling the fetch unlabeled detections endpoint with an ADMIN token, the pictures were not found in the S3 because the organisation_id of ADMIN is = 1, while we would like to get all the Detection of all the organization.

So for ADMIN token we should find the organization_id thanks to the Camera.detection_id

Copy link

codecov bot commented Sep 10, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 86.31%. Comparing base (db5a81a) to head (f05239f).

Files with missing lines Patch % Lines
src/app/api/api_v1/endpoints/detections.py 71.42% 2 Missing ⚠️
src/app/api/dependencies.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #366      +/-   ##
==========================================
- Coverage   86.39%   86.31%   -0.09%     
==========================================
  Files          34       34              
  Lines         941      950       +9     
==========================================
+ Hits          813      820       +7     
- Misses        128      130       +2     
Flag Coverage Δ
backend 85.90% <75.00%> (-0.09%) ⬇️
client 92.06% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@frgfm frgfm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I wanted to update the logic here as well, to adapt what I did on old-production (we've benchmarked it with Mateo to avoid veryyyyyyy slow queries). cf. my comments

src/app/api/api_v1/endpoints/detections.py Show resolved Hide resolved
src/app/api/api_v1/endpoints/detections.py Show resolved Hide resolved
@@ -165,19 +165,24 @@ async def fetch_unlabeled_detections(
def get_url(detection: Detection) -> str:
return bucket.get_public_url(detection.bucket_key)

async def get_url_with_bucket(detection: Detection) -> str:
camera = cast(Camera, await cameras.get(detection.camera_id, strict=True))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the size of the potential result, this could become quite slow. We've had to solve this on the old-production branch. I suggest taking the same approach : https://github.com/pyronear/pyro-api/blob/old-production/src/app/api/endpoints/events.py#L149

(otherwise if there are 150 detections, we'll have to do 150 invidual camera fetch)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I have implemented a pagination parameter in the endpoint (and the fetch_all function) is it ok for you ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On paper yes, but what is implemented on old-production was proofed with platform interactions and extremely fast. Pagination will for sure needed at some point, but for now, for the sake of "let's do what we know works well", I'd suggest taking only adapting that code. If you feel like we should introduce pagination now:

Considering this is only for the platform and for now, it fills that need, I think those param and pagination aren't necessary but I'll let you be the judge of that 👌

src/app/api/api_v1/endpoints/detections.py Outdated Show resolved Hide resolved
Copy link
Member

@frgfm frgfm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm pretty sure everything here works but as mentioned in the comment, I have no clue how fast. This route needs to be really low latency and we've iterated on old-production branch to make something that works. Out of safety, I think we should keep that design 👌

@@ -165,19 +165,24 @@ async def fetch_unlabeled_detections(
def get_url(detection: Detection) -> str:
return bucket.get_public_url(detection.bucket_key)

async def get_url_with_bucket(detection: Detection) -> str:
camera = cast(Camera, await cameras.get(detection.camera_id, strict=True))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On paper yes, but what is implemented on old-production was proofed with platform interactions and extremely fast. Pagination will for sure needed at some point, but for now, for the sake of "let's do what we know works well", I'd suggest taking only adapting that code. If you feel like we should introduce pagination now:

Considering this is only for the platform and for now, it fills that need, I think those param and pagination aren't necessary but I'll let you be the judge of that 👌

src/app/api/api_v1/endpoints/detections.py Show resolved Hide resolved
@@ -64,8 +64,7 @@ async def create_detection(
# guess_extension will return none if this fails
extension = guess_extension(magic.from_buffer(file.file.read(), mime=True)) or ""
# Concatenate timestamp & hash
bucket_key = f"{token_payload.sub}-{datetime.utcnow().strftime('%Y%m%d%H%M%S')}-{sha_hash[:8]}{extension}"
# Reset byte position of the file (cf. https://fastapi.tiangolo.com/tutorial/request-files/#uploadfile)
bucket_key = f"{token_payload.sub}-{datetime.utcnow().strftime('%Y%m%d%H%M%S')}-{sha_hash[:8]}{extension}" # Reset byte position of the file (cf. https://fastapi.tiangolo.com/tutorial/request-files/#uploadfile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert the comment position to the next line as previously? (gonna have formatting errors at some point otherwise)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants