Skip to content

Commit

Permalink
fix predicted vote db insert
Browse files Browse the repository at this point in the history
  • Loading branch information
nitwhiz committed Sep 6, 2023
1 parent 24b9696 commit 58fd7e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
5 changes: 4 additions & 1 deletion recommender/scripts/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def get_db_connection():
db_password = os.getenv('MOVIE_MATCH_DB_PASSWORD')
db_host = os.getenv('MOVIE_MATCH_DB_HOST')

return create_engine(f'postgresql+psycopg2://{db_username}:{db_password}@{db_host}/movie_match', pool_recycle=3600).connect()
return create_engine(
f'postgresql+psycopg2://{db_username}:{db_password}@{db_host}/movie_match',
pool_recycle=3600
).connect()


def get_voted_media(conn: Connection, user_id: str) -> DataFrame:
Expand Down
24 changes: 17 additions & 7 deletions recommender/scripts/predict_media_votes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from sqlalchemy import text

from database import get_db_connection, get_all_user_ids
from predict import predict_votes

Expand All @@ -9,17 +11,25 @@
p_media = predict_votes(conn, user_id)

for idx, row in p_media.iterrows():
print(f'inserting vote prediction {row["vote_type"]} for {row["media_id"]}.')

conn.execute(
'insert into media_user_vote_prediction ('
'media_id, user_id, predicted_vote, created_at, updated_at'
') values ('
'%(media_id)s, %(user_id)s, %(predicted_vote)s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP'
')'
' on conflict (media_id, user_id) do update set'
' predicted_vote = %(predicted_vote)s, updated_at = CURRENT_TIMESTAMP',
text(
'insert into media_user_vote_prediction ('
'media_id, user_id, predicted_vote, created_at, updated_at'
') values ('
':media_id, :user_id, :predicted_vote, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP'
')'
' on conflict (media_id, user_id) do update set'
' predicted_vote = :predicted_vote, updated_at = CURRENT_TIMESTAMP'
),
{
'media_id': row['media_id'],
'user_id': user_id,
'predicted_vote': row['vote_type'],
}
)

conn.commit()

conn.close()

0 comments on commit 58fd7e1

Please sign in to comment.