122 lines
3.9 KiB
Python
122 lines
3.9 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_all_scanned_subreddits,
|
|
get_deep_dive_details,
|
|
get_daily_summary_for_subreddit,
|
|
get_weekly_summary_for_subreddit,
|
|
get_overall_daily_summary, # Now correctly imported
|
|
get_overall_weekly_summary, # Now correctly imported
|
|
)
|
|
|
|
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 subreddits available to every template for the navbar."""
|
|
return dict(all_subreddits=get_all_scanned_subreddits())
|
|
|
|
|
|
@app.route("/")
|
|
def overall_dashboard():
|
|
"""Handler for the main, overall dashboard."""
|
|
view_type = request.args.get("view", "daily")
|
|
is_image_mode = request.args.get("image") == "true"
|
|
|
|
if view_type == "weekly":
|
|
tickers, start, end = get_overall_weekly_summary()
|
|
date_string = f"{start.strftime('%b %d')} - {end.strftime('%b %d, %Y')}"
|
|
subtitle = "All Subreddits - Top 10 Weekly"
|
|
else: # Default to daily
|
|
tickers = get_overall_daily_summary()
|
|
date_string = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
|
subtitle = "All Subreddits - Top 10 Daily"
|
|
|
|
return render_template(
|
|
"dashboard_view.html",
|
|
title="Overall Dashboard",
|
|
subtitle=subtitle,
|
|
date_string=date_string,
|
|
tickers=tickers,
|
|
view_type=view_type,
|
|
subreddit_name=None,
|
|
is_image_mode=is_image_mode,
|
|
base_url="/",
|
|
)
|
|
|
|
|
|
@app.route("/subreddit/<name>")
|
|
def subreddit_dashboard(name):
|
|
"""Handler for per-subreddit dashboards."""
|
|
view_type = request.args.get("view", "daily")
|
|
is_image_mode = request.args.get("image") == "true"
|
|
|
|
if view_type == "weekly":
|
|
today = datetime.now(timezone.utc)
|
|
target_date = today - timedelta(days=7)
|
|
tickers, start, end = get_weekly_summary_for_subreddit(name, target_date)
|
|
date_string = f"{start.strftime('%b %d')} - {end.strftime('%b %d, %Y')}"
|
|
subtitle = f"r/{name} - Top 10 Weekly"
|
|
else: # Default to daily
|
|
tickers = get_daily_summary_for_subreddit(name)
|
|
date_string = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
|
subtitle = f"r/{name} - Top 10 Daily"
|
|
|
|
return render_template(
|
|
"dashboard_view.html",
|
|
title=f"r/{name} Dashboard",
|
|
subtitle=subtitle,
|
|
date_string=date_string,
|
|
tickers=tickers,
|
|
view_type=view_type,
|
|
subreddit_name=name,
|
|
is_image_mode=is_image_mode,
|
|
base_url=f"/subreddit/{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("/about")
|
|
def about_page():
|
|
"""Handler for the static About page."""
|
|
# We need to pass these so the navbar knows which items to highlight
|
|
return render_template("about.html", subreddit_name=None, view_type='daily')
|
|
|
|
|
|
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()
|