File size: 2,613 Bytes
b3b089b
2542be6
 
bacf700
2542be6
c0aac4f
2253306
2542be6
 
a9ab215
c0aac4f
b71f5c4
 
 
 
 
 
 
 
 
 
 
 
 
 
c0aac4f
 
 
 
 
 
 
 
 
a9ab215
c0aac4f
 
 
 
2542be6
c0aac4f
2b9d1a7
 
 
 
f90914e
c0aac4f
 
 
 
 
 
2b9d1a7
c0aac4f
2542be6
 
 
b71f5c4
2542be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from fastapi import APIRouter, HTTPException , Body,Query
from models.location_models import BodyData,ErrorResponse
from services.location_service import LocationService
from concurrent.futures import ThreadPoolExecutor, as_completed
import core.init_supabase as sp
import asyncio
import json
router = APIRouter()

async def get_coordinates_v1(cord):
    try:
        # Assuming LocationService.get_coordinates is an async function
        result = await LocationService.get_coordinates(cord)
        
        # If the result is a coroutine, await it
        if asyncio.iscoroutine(result):
            result = await result
        
        # Ensure the result is JSON serializable
        json.dumps(result)  # This will raise an error if result is not JSON serializable
        
        return result
    except json.JSONDecodeError:
        print(f'Coordinate {cord} result is not JSON serializable')
        return None
    except Exception as exc:
        print(f'Coordinate {cord} generated an exception: {exc}')
        return None

async def process_coordinates(supabase_user_data, max_concurrency=15):
    semaphore = asyncio.Semaphore(max_concurrency)

    async def bounded_get_coordinates(cord):
        async with semaphore:
            return await get_coordinates_v1(cord)

    coords = await asyncio.gather(*[bounded_get_coordinates(cord) for cord in supabase_user_data])
    return [coord for coord in coords if coord is not None]

@router.post("/get_coordinates")
async def get_coordinates_route(user_id: str = Query(..., description="User's hush ID")):
    print(user_id)
    supabase_user_data = sp.fetch_data(user_id)
    print("supabase data")
    print(supabase_user_data)
    print(len(supabase_user_data))

    coords = await process_coordinates(supabase_user_data)

    if not coords:
        return {"message": "An unexpected error occurred please try again!"}

    print(coords)
    return {"data": coords}


@router.post("/get_card_reccomendation")
async def get_coordinates_v2(data: BodyData = Body(...)):
    token = data.jwt_token
    user_id = sp.authenticate_user(token)
    if user_id == "Exceptional error":
        return {"User not Authenticated!"}
    else:
        print(user_id)
        try:
            supabase_card_data = sp.fetch_cards_data(user_id)
            print("supabase data")
            print(supabase_card_data)
        except Exception as e:
            print(HTTPException(status_code=400, detail=e))
            return {"message":"An unexpected error occured please try again !!"}
    print("Returning the data")
    return {"data":supabase_card_data}