update search endpoint
This commit is contained in:
parent
5b9ee7d7e7
commit
7eb28ceb19
59
app/main.py
59
app/main.py
@ -25,7 +25,7 @@ from contextlib import contextmanager
|
|||||||
from pocketbase import PocketBase
|
from pocketbase import PocketBase
|
||||||
|
|
||||||
# FastAPI and related imports
|
# FastAPI and related imports
|
||||||
from fastapi import FastAPI, Depends, HTTPException, Security, Request, status
|
from fastapi import FastAPI, Depends, HTTPException, Security, Query, Request, status
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.openapi.docs import get_swagger_ui_html
|
from fastapi.openapi.docs import get_swagger_ui_html
|
||||||
from fastapi.openapi.utils import get_openapi
|
from fastapi.openapi.utils import get_openapi
|
||||||
@ -1577,31 +1577,46 @@ async def get_all_hedge_funds_data(api_key: str = Security(get_api_key)):
|
|||||||
headers={"Content-Encoding": "gzip"}
|
headers={"Content-Encoding": "gzip"}
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/searchbar-data")
|
@app.get("/searchbar")
|
||||||
async def get_stock(api_key: str = Security(get_api_key)):
|
async def get_stock(
|
||||||
cache_key = f"searchbar-data"
|
query: str = Query(""),
|
||||||
cached_result = redis_client.get(cache_key)
|
api_key: str = Security(get_api_key)
|
||||||
|
):
|
||||||
|
# Return an empty list if query is empty
|
||||||
|
if not query:
|
||||||
|
return JSONResponse(content=[])
|
||||||
|
|
||||||
if cached_result:
|
prioritize_without_dots_for_names = {"apple"}
|
||||||
return StreamingResponse(
|
normalized_search_query = query.lower()
|
||||||
io.BytesIO(cached_result),
|
|
||||||
media_type="application/json",
|
|
||||||
headers={"Content-Encoding": "gzip"}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Compress the JSON data
|
# Create a generator for the filtered data to minimize memory usage
|
||||||
searchbar_data_json = orjson.dumps(searchbar_data)
|
filtered_gen = (
|
||||||
compressed_data = gzip.compress(searchbar_data_json)
|
{
|
||||||
|
"original": item,
|
||||||
redis_client.set(cache_key, compressed_data)
|
"nameLower": item["name"].lower(),
|
||||||
redis_client.expire(cache_key, 3600 * 24) # Set cache expiration time to 1 day
|
"symbolLower": item["symbol"].lower()
|
||||||
|
}
|
||||||
return StreamingResponse(
|
for item in searchbar_data
|
||||||
io.BytesIO(compressed_data),
|
if normalized_search_query in item["name"].lower() or normalized_search_query in item["symbol"].lower()
|
||||||
media_type="application/json",
|
|
||||||
headers={"Content-Encoding": "gzip"}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Sort and limit to top 5 directly
|
||||||
|
top_results = sorted(
|
||||||
|
filtered_gen,
|
||||||
|
key=lambda item: (
|
||||||
|
item["symbolLower"] != normalized_search_query,
|
||||||
|
item["symbolLower"].find(normalized_search_query) + item["nameLower"].find(normalized_search_query),
|
||||||
|
item["symbolLower"].count('.') if normalized_search_query in prioritize_without_dots_for_names else 0
|
||||||
|
)
|
||||||
|
)[:5]
|
||||||
|
|
||||||
|
# Re-map to the original structure before returning
|
||||||
|
result = [item["original"] for item in top_results]
|
||||||
|
|
||||||
|
return JSONResponse(content=orjson.loads(orjson.dumps(result)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/revenue-segmentation")
|
@app.post("/revenue-segmentation")
|
||||||
async def revenue_segmentation(data: TickerData, api_key: str = Security(get_api_key)):
|
async def revenue_segmentation(data: TickerData, api_key: str = Security(get_api_key)):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user