File size: 2,925 Bytes
b3b089b
2542be6
 
61f1b64
2542be6
c0aac4f
2253306
2542be6
 
a9ab215
c0aac4f
b71f5c4
 
 
 
 
 
 
 
 
 
 
 
 
 
c0aac4f
 
 
 
 
d1754b1
75f8f0c
 
d1754b1
75f8f0c
d1754b1
 
 
 
 
 
c0aac4f
d1754b1
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
76
77
78
79
80
81
from fastapi import APIRouter, HTTPException , Body,Query
from models.location_models import BodyData,ErrorResponse
from services.location_service import LocationService
import concurrent.futures
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):
    coords=[]
    with ThreadPoolExecutor(max_workers=15) as executor::
        futures = [executor.submit(LocationService.get_coordinates,cord) for cord in supabase_user_data]
        for future in concurrent.futures.as_completed(futures):
            
            try:
                result = future.result()
                coords.append(result)
                print(f"{task_name} completed with result: {result}")
            except Exception as exc:
                print(f"{task_name} generated an exception: {exc}")

    # 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}