Files
reddit_stock_analyzer/rstat_tool/dashboard.py

122 lines
4.0 KiB
Python

# rstat_tool/dashboard.py
from flask import Flask, render_template, request
from datetime import datetime, timedelta, timezone
from .logger_setup import logger as log
from .database import (
get_overall_summary,
get_subreddit_summary,
get_all_scanned_subreddits,
get_deep_dive_details,
get_daily_summary_for_subreddit,
get_weekly_summary_for_subreddit,
get_overall_image_view_summary
)
app = Flask(__name__, template_folder='../templates')
@app.template_filter('format_mc')
def format_market_cap(mc):
"""Formats a large number into a readable market cap string."""
if mc is None or mc == 0:
return "N/A"
if mc >= 1e12:
return f"${mc/1e12:.2f}T"
elif mc >= 1e9:
return f"${mc/1e9:.2f}B"
elif mc >= 1e6:
return f"${mc/1e6:.2f}M"
else:
return f"${mc:,}"
@app.context_processor
def inject_subreddits():
"""Makes the list of all scanned subreddits available to every template."""
subreddits = get_all_scanned_subreddits()
return dict(subreddits=subreddits)
@app.route("/")
def index():
"""The handler for the main dashboard page."""
tickers = get_overall_summary(limit=10)
return render_template("index.html", tickers=tickers)
@app.route("/subreddit/<name>")
def subreddit_dashboard(name):
"""A dynamic route for per-subreddit dashboards."""
tickers = get_subreddit_summary(name, limit=10)
return render_template("subreddit.html", tickers=tickers, subreddit_name=name)
@app.route("/deep-dive/<symbol>")
def deep_dive(symbol):
"""The handler for the deep-dive page for a specific ticker."""
# --- FIX #2: Call the function directly, without the 'database.' prefix ---
posts = get_deep_dive_details(symbol)
return render_template("deep_dive.html", posts=posts, symbol=symbol)
@app.route("/image/daily/<name>")
def daily_image_view(name):
"""The handler for the image-style dashboard."""
tickers = get_daily_summary_for_subreddit(name)
current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
return render_template(
"daily_image_view.html",
tickers=tickers,
subreddit_name=name,
current_date=current_date
)
@app.route("/image/weekly/<name>")
def weekly_image_view(name):
"""
The handler for the WEEKLY image-style dashboard.
Accepts an optional 'date' query parameter in YYYY-MM-DD format.
"""
# Get the date from the URL query string, e.g., ?date=2025-07-21
date_str = request.args.get('date')
target_date = None
if date_str:
try:
# Convert the string to a datetime object
target_date = datetime.strptime(date_str, "%Y-%m-%d").replace(tzinfo=timezone.utc)
except ValueError:
return "Invalid date format. Please use YYYY-MM-DD.", 400
else:
# If no date is provided, default to showing LAST week
today = datetime.now(timezone.utc)
target_date = today - timedelta(days=7)
# The query now returns the results and the date objects used
tickers, start_of_week, end_of_week = get_weekly_summary_for_subreddit(name, target_date)
# Format the date range for the title
date_range_str = f"{start_of_week.strftime('%b %d')} - {end_of_week.strftime('%b %d, %Y')}"
return render_template(
"weekly_image_view.html",
tickers=tickers,
subreddit_name=name,
date_range=date_range_str
)
@app.route("/image/overall")
def overall_image_view():
"""The handler for the overall image-style dashboard."""
tickers = get_overall_image_view_summary()
current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
return render_template(
"overall_image_view.html",
tickers=tickers,
current_date=current_date
)
def start_dashboard():
"""The main function called by the 'rstat-dashboard' command."""
log.info("Starting Flask server...")
log.info("Open http://127.0.0.1:5000 in your browser.")
log.info("Press CTRL+C to stop the server.")
app.run(debug=True)
if __name__ == "__main__":
start_dashboard()