PATCH: Fixed database not returning data in recommendations

UPDATE: Decreased polling time to 1 second for later use
This commit is contained in:
white
2025-01-30 10:24:26 +00:00
parent 6586506c97
commit f981b69c94
9 changed files with 77 additions and 55 deletions

View File

@@ -25,7 +25,7 @@ const StreamerRoute: React.FC = () => {
checkStreamStatus(); checkStreamStatus();
// Poll for live status changes // Poll for live status changes
const interval = setInterval(checkStreamStatus, 20000); // Check every 20 seconds const interval = setInterval(checkStreamStatus, 1000); // Check every 1 second
return () => clearInterval(interval); return () => clearInterval(interval);
}, [streamerName]); }, [streamerName]);

View File

@@ -3,6 +3,7 @@ import Navbar from "../components/Layout/Navbar";
import ListRow from "../components/Layout/ListRow"; import ListRow from "../components/Layout/ListRow";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { useStreams } from "../context/StreamsContext"; import { useStreams } from "../context/StreamsContext";
import { useAuth } from "../context/AuthContext";
interface HomePageProps { interface HomePageProps {
variant?: "default" | "personalised"; variant?: "default" | "personalised";
@@ -11,6 +12,7 @@ interface HomePageProps {
const HomePage: React.FC<HomePageProps> = ({ variant = "default" }) => { const HomePage: React.FC<HomePageProps> = ({ variant = "default" }) => {
const { featuredStreams, featuredCategories } = useStreams(); const { featuredStreams, featuredCategories } = useStreams();
const navigate = useNavigate(); const navigate = useNavigate();
const { isLoggedIn } = useAuth();
const handleStreamClick = (streamId: number, streamerName: string) => { const handleStreamClick = (streamId: number, streamerName: string) => {
console.log(`Navigating to ${streamId}`); console.log(`Navigating to ${streamId}`);
@@ -25,13 +27,25 @@ const HomePage: React.FC<HomePageProps> = ({ variant = "default" }) => {
> >
<Navbar variant="home" /> <Navbar variant="home" />
{/* Not working - trying to display default streams */}
<ListRow <ListRow
type="stream" type="stream"
title={"Live Now" + (variant === "personalised" ? " - Recommended" : "")} title="Live Now"
description={variant === "personalised" ? "We think you might like these streams - Streamers recommended for you" : "Streamers that are currently live"} description="Streamers that are currently live"
items={featuredStreams} items={featuredStreams}
onClick={handleStreamClick} onClick={handleStreamClick}
/> />
{isLoggedIn && variant === "personalised" && (
<ListRow
type="stream"
title="Live Now - Recommended"
description="We think you might like these streams - Streamers recommended for you"
items={featuredStreams}
onClick={handleStreamClick}
/>
)}
<ListRow <ListRow
type="category" type="category"
title={variant === "personalised" ? "Followed Categories" : "Trending Categories"} title={variant === "personalised" ? "Followed Categories" : "Trending Categories"}

View File

@@ -66,9 +66,7 @@ def get_past_chat(stream_id: int):
db.close_connection() db.close_connection()
# Create JSON output of chat_history to pass through NGINX proxy # Create JSON output of chat_history to pass through NGINX proxy
print(f"Bollocks: {all_chats}", flush=True)
chat_history = [{"chatter_id": chat["chatter_id"], "message": chat["message"], "time_sent": chat["time_sent"]} for chat in all_chats] chat_history = [{"chatter_id": chat["chatter_id"], "message": chat["message"], "time_sent": chat["time_sent"]} for chat in all_chats]
print(f"chat history: {chat_history}", flush=True)
# Pass the chat history to the proxy # Pass the chat history to the proxy
return jsonify({"chat_history": chat_history}), 200 return jsonify({"chat_history": chat_history}), 200

View File

@@ -66,6 +66,4 @@ SELECT * FROM tags;
SELECT * FROM stream_tags; SELECT * FROM stream_tags;
-- To see all tables in the database -- To see all tables in the database
SELECT name FROM sqlite_master WHERE type='table'; SELECT name FROM sqlite_master WHERE type='table';
INSERT INTO users

View File

@@ -60,3 +60,11 @@ CREATE TABLE followed_categories
FOREIGN KEY(category_id) REFERENCES categories(category_id) ON DELETE CASCADE FOREIGN KEY(category_id) REFERENCES categories(category_id) ON DELETE CASCADE
); );
SELECT s.stream_id, s.title, u.username, s.num_viewers, c.category_name
FROM streams AS s
JOIN users AS u ON u.user_id = s.user_id
JOIN categories AS c ON s.category_id = c.category_id
JOIN followed_categories AS f ON s.category_id = c.category_id
WHERE f.user_id = ?
ORDER BY s.num_viewers DESC
LIMIT 25;

View File

@@ -5,8 +5,8 @@ def user_recommendation_category(user_id: int) -> Optional[int]:
""" """
Queries user_preferences database to find users favourite streaming category and returns the category Queries user_preferences database to find users favourite streaming category and returns the category
""" """
with Database() as db: db = Database()
data = db.fetchone(""" data = db.fetchone("""
SELECT category_id SELECT category_id
FROM user_preferences FROM user_preferences
WHERE user_id = ? WHERE user_id = ?
@@ -20,14 +20,18 @@ def followed_categories_recommendations(user_id : int):
""" """
Returns top 25 streams given a users category following Returns top 25 streams given a users category following
""" """
with Database() as db: db = Database()
categories = db.fetchall(""" # TODO: Change this to do what the function says
SELECT user_id, title, num_viewers, categories.category_name categories = db.fetchall("""
FROM streams SELECT s.stream_id, s.title, u.username, s.num_viewers, c.category_name
JOIN categories ON streams.category_id = categories.category_id FROM streams AS s
WHERE category_id IN (SELECT category_id FROM categories WHERE user_id = ?) JOIN users AS u ON u.user_id = s.user_id
ORDER BY num_viewers DESC JOIN categories AS c ON s.category_id = c.category_id
LIMIT 25; """, (user_id,)) JOIN followed_categories AS f ON s.category_id = f.category_id
WHERE f.user_id = ?
ORDER BY s.num_viewers DESC
LIMIT 25;
""", (user_id,))
return categories return categories
#TODO Needs to be reworked to get categories instead of streams of categories #TODO Needs to be reworked to get categories instead of streams of categories
@@ -36,8 +40,8 @@ def recommendations_based_on_category(category_id: int) -> Optional[List[Tuple[i
Queries stream database to get top 25 most viewed streams based on given category and returns Queries stream database to get top 25 most viewed streams based on given category and returns
(user_id, title, username, num_viewers, category_name) (user_id, title, username, num_viewers, category_name)
""" """
with Database() as db: db = Database()
data = db.fetchall(""" data = db.fetchall("""
SELECT streams.category_id, streams.user_id, streams.title, users.username, streams.num_viewers, categories.category_name SELECT streams.category_id, streams.user_id, streams.title, users.username, streams.num_viewers, categories.category_name
FROM streams FROM streams
JOIN users ON users.user_id = streams.user_id JOIN users ON users.user_id = streams.user_id
@@ -53,8 +57,8 @@ def default_recommendations():
Return a list of 25 recommended live streams by number of viewers Return a list of 25 recommended live streams by number of viewers
(user_id, title, username, num_viewers, category_name) (user_id, title, username, num_viewers, category_name)
""" """
with Database() as db: db = Database()
data = db.fetchall(""" data = db.fetchall("""
SELECT stream_id, users.user_id, title, username, num_viewers, category_name SELECT stream_id, users.user_id, title, username, num_viewers, category_name
FROM streams FROM streams
JOIN users ON users.user_id = streams.user_id JOIN users ON users.user_id = streams.user_id
@@ -68,8 +72,8 @@ def category_recommendations():
""" """
Returns a list of the top 5 most popular categories Returns a list of the top 5 most popular categories
""" """
with Database() as db: db = Database()
categories = db.fetchall(""" categories = db.fetchall("""
SELECT categories.category_id, categories.category_name SELECT categories.category_id, categories.category_name
FROM streams FROM streams
JOIN categories ON streams.category_id = categories.category_id JOIN categories ON streams.category_id = categories.category_id

View File

@@ -7,8 +7,8 @@ def streamer_live_status(user_id: int) -> bool:
""" """
Returns boolean on whether the given streamer is live Returns boolean on whether the given streamer is live
""" """
with Database() as db: db = Database()
is_live = db.fetchone(""" is_live = db.fetchone("""
SELECT isLive SELECT isLive
FROM streams FROM streams
WHERE user_id = ? WHERE user_id = ?
@@ -19,8 +19,8 @@ def followed_live_streams(user_id: int) -> list[dict]:
""" """
Searches for streamers who the user followed which are currently live Searches for streamers who the user followed which are currently live
""" """
with Database() as db: db = Database()
live_streams = db.fetchall(""" live_streams = db.fetchall("""
SELECT user_id, stream_id, title, num_viewers SELECT user_id, stream_id, title, num_viewers
FROM streams FROM streams
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?) WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?)
@@ -33,8 +33,8 @@ def followed_streamers(user_id: int) -> list[dict]:
""" """
Returns a list of streamers who the user follows Returns a list of streamers who the user follows
""" """
with Database() as db: db = Database()
followed_streamers = db.fetchall(""" followed_streamers = db.fetchall("""
SELECT user_id, username SELECT user_id, username
FROM users FROM users
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?); WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?);
@@ -45,8 +45,8 @@ def streamer_most_recent_stream(user_id: int) -> dict:
""" """
Returns data of the most recent stream by a streamer Returns data of the most recent stream by a streamer
""" """
with Database() as db: db = Database()
most_recent_stream = db.fetchone(""" most_recent_stream = db.fetchone("""
SELECT * FROM streams SELECT * FROM streams
WHERE user_id = ? WHERE user_id = ?
AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE user_id = ?) AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE user_id = ?)
@@ -57,8 +57,8 @@ def user_stream(user_id: int, stream_id: int) -> dict:
""" """
Returns data of a streamers selected stream Returns data of a streamers selected stream
""" """
with Database() as db: db = Database()
stream = db.fetchone(""" stream = db.fetchone("""
SELECT * FROM streams SELECT * FROM streams
WHERE user_id = ? WHERE user_id = ?
AND stream_id = ? AND stream_id = ?

View File

@@ -13,11 +13,11 @@ def get_user_id(username: str) -> int:
""" """
Returns user_id associated with given username Returns user_id associated with given username
""" """
with Database() as db: db = Database()
data = db.fetchone(""" data = db.fetchone("""
SELECT user_id SELECT user_id
FROM users FROM users
WHERE username = ? WHERE username = ?;
""", (username,)) """, (username,))
return data['user_id'] if data else None return data['user_id'] if data else None
@@ -25,8 +25,8 @@ def get_username(user_id: str) -> Optional[str]:
""" """
Returns username associated with given user_id Returns username associated with given user_id
""" """
with Database() as db: db = Database()
data = db.fetchone(""" data = db.fetchone("""
SELECT username SELECT username
FROM user FROM user
WHERE user_id = ? WHERE user_id = ?
@@ -37,8 +37,8 @@ def is_user_partner(user_id: int) -> bool:
""" """
Returns True if user is a partner, else False Returns True if user is a partner, else False
""" """
with Database() as db: db = Database()
data = db.fetchone(""" data = db.fetchone("""
SELECT is_partnered SELECT is_partnered
FROM users FROM users
WHERE user_id = ? WHERE user_id = ?
@@ -49,8 +49,8 @@ def is_subscribed(user_id: int, streamer_id: int) -> bool:
""" """
Returns True if user is subscribed to a streamer, else False Returns True if user is subscribed to a streamer, else False
""" """
with Database() as db: db = Database()
result = db.fetchone(""" result = db.fetchone("""
SELECT 1 SELECT 1
FROM subscribes FROM subscribes
WHERE user_id = ? WHERE user_id = ?
@@ -63,8 +63,8 @@ def is_following(user_id: int, followed_id: int) -> bool:
""" """
Returns where a user is following another Returns where a user is following another
""" """
with Database() as db: db = Database()
result = db.fetchone(""" result = db.fetchone("""
SELECT 1 SELECT 1
FROM follows FROM follows
WHERE user_id = ? WHERE user_id = ?
@@ -76,8 +76,8 @@ def subscription_expiration(user_id: int, subscribed_id: int) -> int:
""" """
Returns the amount of time left until user subscription to a streamer ends Returns the amount of time left until user subscription to a streamer ends
""" """
with Database() as db: db = Database()
data = db.fetchone(""" data = db.fetchone("""
SELECT expires SELECT expires
FROM subscriptions FROM subscriptions
WHERE user_id = ? WHERE user_id = ?
@@ -103,8 +103,8 @@ def reset_password(new_password: str, email: str):
""" """
Given email and new password reset the password for a given user Given email and new password reset the password for a given user
""" """
with Database() as db: db = Database()
db.execute(""" db.execute("""
UPDATE users UPDATE users
SET password = ? SET password = ?
WHERE email = ? WHERE email = ?

View File

@@ -4,8 +4,8 @@ def categories():
""" """
Returns all possible streaming categories Returns all possible streaming categories
""" """
with Database() as db: db = Database()
all_categories = db.fetchall("SELECT * FROM categories") all_categories = db.fetchall("SELECT * FROM categories")
return all_categories return all_categories
@@ -13,8 +13,8 @@ def tags():
""" """
Returns all possible streaming tags Returns all possible streaming tags
""" """
with Database() as db: db = Database()
all_tags = db.fetchall("SELECT * FROM tags") all_tags = db.fetchall("SELECT * FROM tags")
return all_tags return all_tags
@@ -22,8 +22,8 @@ def most_popular_category():
""" """
Returns the most popular category based on live stream viewers Returns the most popular category based on live stream viewers
""" """
with Database() as db: db = Database()
category = db.fetchone(""" category = db.fetchone("""
SELECT categories.category_id, categories.category_name SELECT categories.category_id, categories.category_name
FROM streams FROM streams
JOIN categories ON streams.category_id = categories.category_id JOIN categories ON streams.category_id = categories.category_id