Big improvements on image view.
This commit is contained in:
@@ -235,6 +235,22 @@ def get_ticker_info(conn, ticker_id):
|
||||
cursor.execute("SELECT * FROM tickers WHERE id = ?", (ticker_id,))
|
||||
return cursor.fetchone()
|
||||
|
||||
def get_week_start_end(for_date):
|
||||
"""
|
||||
Calculates the start (Monday, 00:00:00) and end (Sunday, 23:59:59)
|
||||
of the week that a given date falls into.
|
||||
Returns two datetime objects.
|
||||
"""
|
||||
# Monday is 0, Sunday is 6
|
||||
start_of_week = for_date - timedelta(days=for_date.weekday())
|
||||
end_of_week = start_of_week + timedelta(days=6)
|
||||
|
||||
# Set time to the very beginning and very end of the day for an inclusive range
|
||||
start_of_week = start_of_week.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
end_of_week = end_of_week.replace(hour=23, minute=59, second=59, microsecond=999999)
|
||||
|
||||
return start_of_week, end_of_week
|
||||
|
||||
def generate_summary_report(limit=20):
|
||||
"""Queries the DB to generate a summary for the command-line tool."""
|
||||
log.info(f"\n--- Top {limit} Tickers by Mention Count ---")
|
||||
@@ -334,49 +350,53 @@ def get_daily_summary_for_subreddit(subreddit_name):
|
||||
one_day_ago = datetime.now(timezone.utc) - timedelta(days=1)
|
||||
one_day_ago_timestamp = int(one_day_ago.timestamp())
|
||||
query = """
|
||||
SELECT t.symbol,
|
||||
COUNT(CASE WHEN m.mention_type = 'post' THEN 1 END) as post_mentions,
|
||||
COUNT(CASE WHEN m.mention_type = 'comment' THEN 1 END) as comment_mentions,
|
||||
SELECT
|
||||
t.symbol, t.market_cap, t.closing_price,
|
||||
COUNT(m.id) as total_mentions,
|
||||
COUNT(CASE WHEN m.mention_sentiment > 0.1 THEN 1 END) as bullish_mentions,
|
||||
COUNT(CASE WHEN m.mention_sentiment < -0.1 THEN 1 END) as bearish_mentions
|
||||
FROM mentions m JOIN tickers t ON m.ticker_id = t.id JOIN subreddits s ON m.subreddit_id = s.id
|
||||
WHERE LOWER(s.name) = LOWER(?) AND m.mention_timestamp >= ?
|
||||
GROUP BY t.symbol ORDER BY (post_mentions + comment_mentions) DESC LIMIT 10;
|
||||
GROUP BY t.symbol, t.market_cap, t.closing_price
|
||||
ORDER BY total_mentions DESC LIMIT 10;
|
||||
"""
|
||||
results = conn.execute(query, (subreddit_name, one_day_ago_timestamp)).fetchall()
|
||||
conn.close()
|
||||
return results
|
||||
|
||||
def get_weekly_summary_for_subreddit(subreddit_name):
|
||||
""" Gets a summary for the WEEKLY image view (last 7 days). """
|
||||
def get_weekly_summary_for_subreddit(subreddit_name, for_date):
|
||||
""" Gets a summary for the WEEKLY image view (full week). """
|
||||
conn = get_db_connection()
|
||||
seven_days_ago = datetime.now(timezone.utc) - timedelta(days=7)
|
||||
seven_days_ago_timestamp = int(seven_days_ago.timestamp())
|
||||
start_of_week, end_of_week = get_week_start_end(for_date)
|
||||
start_timestamp = int(start_of_week.timestamp())
|
||||
end_timestamp = int(end_of_week.timestamp())
|
||||
query = """
|
||||
SELECT t.symbol,
|
||||
COUNT(CASE WHEN m.mention_type = 'post' THEN 1 END) as post_mentions,
|
||||
COUNT(CASE WHEN m.mention_type = 'comment' THEN 1 END) as comment_mentions,
|
||||
SELECT
|
||||
t.symbol, t.market_cap, t.closing_price,
|
||||
COUNT(m.id) as total_mentions,
|
||||
COUNT(CASE WHEN m.mention_sentiment > 0.1 THEN 1 END) as bullish_mentions,
|
||||
COUNT(CASE WHEN m.mention_sentiment < -0.1 THEN 1 END) as bearish_mentions
|
||||
FROM mentions m JOIN tickers t ON m.ticker_id = t.id JOIN subreddits s ON m.subreddit_id = s.id
|
||||
WHERE LOWER(s.name) = LOWER(?) AND m.mention_timestamp >= ?
|
||||
GROUP BY t.symbol ORDER BY (post_mentions + comment_mentions) DESC LIMIT 10;
|
||||
WHERE LOWER(s.name) = LOWER(?) AND m.mention_timestamp BETWEEN ? AND ?
|
||||
GROUP BY t.symbol, t.market_cap, t.closing_price
|
||||
ORDER BY total_mentions DESC LIMIT 10;
|
||||
"""
|
||||
results = conn.execute(query, (subreddit_name, seven_days_ago_timestamp)).fetchall()
|
||||
results = conn.execute(query, (subreddit_name, start_timestamp, end_timestamp)).fetchall()
|
||||
conn.close()
|
||||
return results
|
||||
return results, start_of_week, end_of_week
|
||||
|
||||
def get_overall_image_view_summary():
|
||||
""" Gets a summary of top tickers across ALL subreddits for the image view. """
|
||||
conn = get_db_connection()
|
||||
query = """
|
||||
SELECT t.symbol,
|
||||
COUNT(CASE WHEN m.mention_type = 'post' THEN 1 END) as post_mentions,
|
||||
COUNT(CASE WHEN m.mention_type = 'comment' THEN 1 END) as comment_mentions,
|
||||
SELECT
|
||||
t.symbol, t.market_cap, t.closing_price,
|
||||
COUNT(m.id) as total_mentions,
|
||||
COUNT(CASE WHEN m.mention_sentiment > 0.1 THEN 1 END) as bullish_mentions,
|
||||
COUNT(CASE WHEN m.mention_sentiment < -0.1 THEN 1 END) as bearish_mentions
|
||||
FROM mentions m JOIN tickers t ON m.ticker_id = t.id
|
||||
GROUP BY t.symbol ORDER BY (post_mentions + comment_mentions) DESC LIMIT 10;
|
||||
GROUP BY t.symbol, t.market_cap, t.closing_price
|
||||
ORDER BY total_mentions DESC LIMIT 10;
|
||||
"""
|
||||
results = conn.execute(query).fetchall()
|
||||
conn.close()
|
||||
|
Reference in New Issue
Block a user