106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
# rstat_tool/dashboard.py
|
|
|
|
from flask import Flask, render_template
|
|
from datetime import datetime, timedelta, timezone
|
|
from .logger_setup import get_logger
|
|
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
|
|
)
|
|
|
|
log = get_logger()
|
|
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."""
|
|
tickers = get_weekly_summary_for_subreddit(name)
|
|
|
|
# Create the date range string for the title
|
|
end_date = datetime.now(timezone.utc)
|
|
start_date = end_date - timedelta(days=7)
|
|
date_range_str = f"{start_date.strftime('%b %d')} - {end_date.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() |