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();
// 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);
}, [streamerName]);

View File

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

View File

@@ -66,9 +66,7 @@ def get_past_chat(stream_id: int):
db.close_connection()
# 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]
print(f"chat history: {chat_history}", flush=True)
# Pass the chat history to the proxy
return jsonify({"chat_history": chat_history}), 200

View File

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

View File

@@ -60,3 +60,11 @@ CREATE TABLE followed_categories
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
"""
with Database() as db:
data = db.fetchone("""
db = Database()
data = db.fetchone("""
SELECT category_id
FROM user_preferences
WHERE user_id = ?
@@ -20,14 +20,18 @@ def followed_categories_recommendations(user_id : int):
"""
Returns top 25 streams given a users category following
"""
with Database() as db:
categories = db.fetchall("""
SELECT user_id, title, num_viewers, categories.category_name
FROM streams
JOIN categories ON streams.category_id = categories.category_id
WHERE category_id IN (SELECT category_id FROM categories WHERE user_id = ?)
ORDER BY num_viewers DESC
LIMIT 25; """, (user_id,))
db = Database()
# TODO: Change this to do what the function says
categories = db.fetchall("""
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 = f.category_id
WHERE f.user_id = ?
ORDER BY s.num_viewers DESC
LIMIT 25;
""", (user_id,))
return 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
(user_id, title, username, num_viewers, category_name)
"""
with Database() as db:
data = db.fetchall("""
db = Database()
data = db.fetchall("""
SELECT streams.category_id, streams.user_id, streams.title, users.username, streams.num_viewers, categories.category_name
FROM streams
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
(user_id, title, username, num_viewers, category_name)
"""
with Database() as db:
data = db.fetchall("""
db = Database()
data = db.fetchall("""
SELECT stream_id, users.user_id, title, username, num_viewers, category_name
FROM streams
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
"""
with Database() as db:
categories = db.fetchall("""
db = Database()
categories = db.fetchall("""
SELECT categories.category_id, categories.category_name
FROM streams
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
"""
with Database() as db:
is_live = db.fetchone("""
db = Database()
is_live = db.fetchone("""
SELECT isLive
FROM streams
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
"""
with Database() as db:
live_streams = db.fetchall("""
db = Database()
live_streams = db.fetchall("""
SELECT user_id, stream_id, title, num_viewers
FROM streams
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
"""
with Database() as db:
followed_streamers = db.fetchall("""
db = Database()
followed_streamers = db.fetchall("""
SELECT user_id, username
FROM users
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
"""
with Database() as db:
most_recent_stream = db.fetchone("""
db = Database()
most_recent_stream = db.fetchone("""
SELECT * 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
"""
with Database() as db:
stream = db.fetchone("""
db = Database()
stream = db.fetchone("""
SELECT * FROM streams
WHERE user_id = ?
AND stream_id = ?

View File

@@ -13,11 +13,11 @@ def get_user_id(username: str) -> int:
"""
Returns user_id associated with given username
"""
with Database() as db:
data = db.fetchone("""
db = Database()
data = db.fetchone("""
SELECT user_id
FROM users
WHERE username = ?
WHERE username = ?;
""", (username,))
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
"""
with Database() as db:
data = db.fetchone("""
db = Database()
data = db.fetchone("""
SELECT username
FROM user
WHERE user_id = ?
@@ -37,8 +37,8 @@ def is_user_partner(user_id: int) -> bool:
"""
Returns True if user is a partner, else False
"""
with Database() as db:
data = db.fetchone("""
db = Database()
data = db.fetchone("""
SELECT is_partnered
FROM users
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
"""
with Database() as db:
result = db.fetchone("""
db = Database()
result = db.fetchone("""
SELECT 1
FROM subscribes
WHERE user_id = ?
@@ -63,8 +63,8 @@ def is_following(user_id: int, followed_id: int) -> bool:
"""
Returns where a user is following another
"""
with Database() as db:
result = db.fetchone("""
db = Database()
result = db.fetchone("""
SELECT 1
FROM follows
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
"""
with Database() as db:
data = db.fetchone("""
db = Database()
data = db.fetchone("""
SELECT expires
FROM subscriptions
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
"""
with Database() as db:
db.execute("""
db = Database()
db.execute("""
UPDATE users
SET password = ?
WHERE email = ?

View File

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