socialboost / app /main.py
ezequiellopez
working version v1
3b86501
raw
history blame
No virus
3.98 kB
# Import required libraries
from fastapi import FastAPI, HTTPException
#import redis
from dotenv import load_dotenv
import os
from modules.redistribute import redistribute, insert_element_at_position
from modules.models.api import Input, Output, NewItem, UUID
from modules.database import BoostDatabase, UserDatabase, User
# Load environment variables from .env file
load_dotenv('../.env')
# Access environment variables
redis_port = os.getenv("REDIS_PORT")
fastapi_port = os.getenv("FASTAPI_PORT")
print("Redis port:", redis_port)
print("FastAPI port:", fastapi_port)
app = FastAPI()
boost_db = BoostDatabase('data/boost_bank.csv')
user_db = UserDatabase()
# Define a health check endpoint
@app.get("/")
async def health_check():
return {"status": "ok"}
# Define FastAPI routes and logic
@app.post("/rerank/")
async def rerank_items(input_data: Input) -> Output:
# who is the user?
user = input_data.session.user_id
date = input_data.session.current_time
platform = input_data.session.platform
items = input_data.items
# TODO consider sampling them?
print(items)
reranked_ids, first_topic, insertion_pos = redistribute(items=items)
#reranked_ids = [ for id_ in reranked_ids]
print("here!")
print(reranked_ids)
user_in_db = user_db.get_user(user_id=user)
# if user already exists -> has boosting records
if user_in_db:
# has been boosted today?
if user_in_db.is_boosted_today():
# return only reranked items, no insertion
return Output(reranked_ids=reranked_ids, new_items=[])
# user exists and not boosted today yet
else:
new_items = []
boosts_received = user_in_db.boosts
# there was some civic content in the batch
if first_topic != "non-civic":
fetched_boost = boost_db.get_random_boost(topic=first_topic,
platform=platform,
blacklist_ids=boosts_received)
user_db.add_boost_to_user(user_id=user, boost=fetched_boost)
user_db.update_user_boosted_today(user_id=user, date=date)
# insert boost before first civic in batch
reranked_ids = insert_element_at_position(lst=reranked_ids,
element=UUID(fetched_boost['id']),
position=insertion_pos)
return Output(ranked_ids=reranked_ids, new_items=[NewItem(id=UUID(fetched_boost["id"]), url=fetched_boost["url"])])
# no civic content to boost on
else:
return Output(ranked_ids=reranked_ids, new_items=[])
# user doesn't exist
else:
print(first_topic)
print(platform)
if first_topic != "non-civic":
fetched_boost = boost_db.get_random_boost(topic=first_topic,
platform=platform,
blacklist_ids=[])
print(fetched_boost)
print(type(fetched_boost))
user_db.add_user(user_id=user,
user=User(user_id=user, last_boost=date, boosts=[fetched_boost]))
# insert boost before first civic in batch
reranked_ids = insert_element_at_position(lst=reranked_ids,
element=UUID(fetched_boost['id']),
position=insertion_pos)
return Output(ranked_ids=reranked_ids, new_items=[NewItem(id=UUID(fetched_boost["id"]), url=fetched_boost["url"])])
# no civic content to boost on
else:
print("there")
return Output(ranked_ids=reranked_ids, new_items=[])