File size: 1,658 Bytes
fab54be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Flask Blueprints for flood detection endpoint with streaming response.
"""

from flask import Blueprint, request, Response, stream_with_context
from datetime import datetime
from src.services import detection as detection_service

detection_bp = Blueprint('detection', __name__, url_prefix='/api')


@detection_bp.route('/detect_stream', methods=['POST'])
def detect_stream():
    """
    Main detection endpoint with streaming response.
    
    Expects JSON POST body:
    {
        "bbox": {"north": float, "south": float, "east": float, "west": float},
        "apply_buffer": bool (optional, default true),
        "target_date": "YYYY-MM-DD" (optional, defaults to today)
    }
    """
    data = request.json
    coords = data.get('bbox')
    apply_buffer = data.get('apply_buffer', True)
    
    target_date_str = data.get('target_date')
    if target_date_str:
        target_date = datetime.strptime(target_date_str, '%Y-%m-%d')
    else:
        target_date = datetime.now()
    
    def generate_process():
        """Generator for streaming detection results."""
        # Extract base host URL path to capture local domain ports or cloud host spaces dynamically
        base_url = request.host_url.rstrip('/')
        
        # Run detection pipeline orchestration logic with the host URL correctly wired down
        for chunk in detection_service.run_detection_pipeline(
            coords=coords, 
            target_date=target_date, 
            apply_buffer=apply_buffer, 
            base_url=base_url
        ):
            yield chunk
    
    return Response(stream_with_context(generate_process()), mimetype='application/json')