initial commit — cassandra v0.1

Containerised macro-strategy dashboard: 4-panel web UI (indicators,
portfolio, flash news, AI strategic log), MariaDB store, hourly
ingestion jobs, OpenRouter-backed AI analysis.

Ports the four prototype scripts in the parent dir (market_pulse,
flash_news, trading212, strategic_log) into async services backed by a
persistent DB and served via FastAPI + Jinja2 + HTMX. APScheduler runs
as a separate compose service for crash-safety and easier restarts.

Portfolio composition + position names come live from Trading 212;
news per-ticker headlines reuse those names. Tone (NOVICE/INTERMEDIATE/
PRO) and analysis style (DRY/SPECULATIVE) are env-configurable and
stored on each log row so historical entries show what produced them.

Default model is deepseek/deepseek-v4-flash (overridable via env).
Light/dark theme toggle, sans-serif for prose surfaces, monospace for
data. Bearer-token auth, OpenRouter monthly cost cap, RSS feeds auto-
disabled on consecutive failures.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-05-15 21:56:10 +01:00
commit a10409c02b
61 changed files with 4890 additions and 0 deletions

71
app/templates/base.html Normal file
View file

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{% block title %}Cassandra{% endblock %}</title>
{# Apply saved theme before stylesheet renders to avoid a flash. #}
<script>
(function() {
try {
var t = localStorage.getItem('cassandra.theme') || 'dark';
document.documentElement.dataset.theme = t;
} catch (e) { document.documentElement.dataset.theme = 'dark'; }
})();
</script>
<link rel="stylesheet" href="{{ url_for('static', path='/css/cassandra.css') }}" />
<script src="{{ url_for('static', path='/js/htmx.min.js') }}" defer></script>
<script>
// Render any <time datetime="..."> in the browser's local timezone.
// Re-runs after every HTMX swap so freshly-loaded news rows pick up too.
function formatLocalTimes() {
document.querySelectorAll('time[datetime]:not([data-local])').forEach(function (t) {
try {
var d = new Date(t.getAttribute('datetime'));
if (isNaN(d.getTime())) return;
var date = d.toLocaleDateString(undefined, { day: '2-digit', month: 'short' });
var time = d.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', hour12: false });
t.textContent = date + ' ' + time;
t.title = d.toLocaleString();
t.setAttribute('data-local', '1');
} catch (e) {}
});
}
document.addEventListener('DOMContentLoaded', function () {
formatLocalTimes();
document.body.addEventListener('htmx:afterSwap', formatLocalTimes);
});
</script>
</head>
<body>
<div class="app">
<header class="app-header">
<div class="brand">Cassandra</div>
<nav>
<a href="/" class="{% if request.url.path == '/' %}active{% endif %}">Dashboard</a>
<a href="/news" class="{% if request.url.path == '/news' %}active{% endif %}">News</a>
<a href="/log" class="{% if request.url.path.startswith('/log') %}active{% endif %}">Log</a>
</nav>
<div class="header-right">
<button class="theme-toggle" type="button" aria-label="Toggle theme"
onclick="(function(){var d=document.documentElement;var t=d.dataset.theme==='light'?'dark':'light';d.dataset.theme=t;try{localStorage.setItem('cassandra.theme',t);}catch(e){}})()">
<span class="theme-toggle__label"></span>
</button>
<span class="meta">v0.1 · UTC</span>
</div>
</header>
<main class="app-main">
{% block main %}{% endblock %}
</main>
<footer class="app-footer"
hx-get="/api/health"
hx-trigger="load, every 30s"
hx-swap="innerHTML"
id="ops-footer">
<span class="led idle"></span> awaiting status…
</footer>
</div>
</body>
</html>

View file

@ -0,0 +1,77 @@
{% extends "base.html" %}
{% block title %}Cassandra · Dashboard{% endblock %}
{% block main %}
<section id="indicators-panel" class="panel">
<div class="panel-header">
<span class="title">Indicators</span>
<span class="meta">{% if anchor %}anchor {{ anchor }} · {% endif %}ingest hourly @ :05 UTC</span>
</div>
<div class="group-tabs" id="group-tabs">
{% for g in groups %}
<button
class="{% if loop.first %}active{% endif %}"
hx-get="/api/indicators/{{ g }}?as=html"
hx-target="#indicators-body"
hx-trigger="click"
onclick="document.querySelectorAll('#group-tabs button').forEach(b=>b.classList.remove('active'));this.classList.add('active')"
>{{ g }}</button>
{% endfor %}
</div>
<div id="indicators-body"
class="panel-body panel-body--scroll"
hx-get="/api/indicators/{{ groups[0] }}?as=html"
hx-trigger="load"
hx-swap="innerHTML">
<div class="empty">loading…</div>
</div>
</section>
<script>
// Auto-refresh the *currently selected* group every 60s by simulating a
// click on the active tab. Replaces the hard-coded `every 60s` on
// #indicators-body which always re-fetched groups[0].
setInterval(function () {
var active = document.querySelector('#group-tabs button.active');
if (active) active.click();
}, 60000);
</script>
<section id="portfolio-panel" class="panel">
<div class="panel-header">
<span class="title">Portfolio</span>
<span class="meta">ingest hourly @ :15 UTC</span>
</div>
<div class="panel-body"
hx-get="/api/portfolios?as=html"
hx-trigger="load, every 60s"
hx-swap="innerHTML">
<div class="empty">loading…</div>
</div>
</section>
<section id="log-panel" class="panel">
<div class="panel-header">
<span class="title">Strategic Log</span>
<span class="meta">generated hourly @ :20 UTC</span>
</div>
<div class="panel-body"
hx-get="/api/log/latest?as=html"
hx-trigger="load, every 300s"
hx-swap="innerHTML">
<div class="empty">awaiting first log…</div>
</div>
</section>
<section id="news-panel" class="panel">
<div class="panel-header">
<span class="title">Flash News</span>
<span class="meta">last 24h · ingest hourly @ :10 UTC</span>
</div>
<div class="panel-body panel-body--scroll"
hx-get="/api/news?as=html&limit=40"
hx-trigger="load, every 60s"
hx-swap="innerHTML">
<div class="empty">loading…</div>
</div>
</section>
{% endblock %}

55
app/templates/log.html Normal file
View file

@ -0,0 +1,55 @@
{% extends "base.html" %}
{% block title %}Cassandra · Strategic Log{% endblock %}
{% block main %}
<section class="panel log-page" style="grid-column: 1 / -1;">
<div class="panel-header">
<span class="title">Strategic Log Archive</span>
<span class="meta">
selected {{ selected_iso }}
&nbsp;·&nbsp;
<span class="meta__hint">new logs use:</span>
<span class="badge badge--tone-{{ current_tone | lower }}">tone {{ current_tone | lower }}</span>
<span class="badge badge--analysis-{{ current_analysis | lower }}">analysis {{ current_analysis | lower }}</span>
</span>
</div>
<div class="log-page__body">
<aside class="log-page__cal"
hx-get="/api/log/days?month={{ selected_month }}&selected={{ selected_iso }}"
hx-trigger="load"
hx-swap="innerHTML">
<div class="empty">loading calendar…</div>
</aside>
<article id="log-content"
class="log-page__content"
hx-get="/api/log/by-date/{{ selected_iso }}?as=html"
hx-trigger="load"
hx-swap="innerHTML">
<div class="empty">loading log…</div>
</article>
<aside id="chat-sidebar" class="log-page__chat">
<div class="chat-header">
<span class="chat-title">Ask Cassandra</span>
<span class="chat-hint">grounded on the latest log + live data</span>
</div>
<div id="chat-thread" class="chat-thread">
<div class="chat-msg chat-msg--system">
Ask about today's analysis. The model sees the latest strategic log,
live market readings across all groups, and the last 24h of
thesis-filtered headlines. Refresh wipes this conversation.
</div>
</div>
<form id="chat-form" class="chat-form" autocomplete="off">
<textarea id="chat-input" rows="2"
placeholder="e.g. why is the defence sleeve flat through Hormuz?"
required></textarea>
<button id="chat-send" type="submit">Send</button>
</form>
</aside>
</div>
</section>
<script src="{{ url_for('static', path='/js/chat.js') }}" defer></script>
{% endblock %}

17
app/templates/news.html Normal file
View file

@ -0,0 +1,17 @@
{% extends "base.html" %}
{% block title %}Cassandra · News{% endblock %}
{% block main %}
<section class="panel" style="grid-column: 1 / -1;">
<div class="panel-header">
<span class="title">News Feed</span>
<span class="meta">last 24h · ingest hourly @ :10 UTC</span>
</div>
<div class="panel-body panel-body--scroll"
hx-get="/api/news?as=html&limit=200"
hx-trigger="load, every 60s"
hx-swap="innerHTML">
<div class="empty">loading…</div>
</div>
</section>
{% endblock %}

View file

@ -0,0 +1,48 @@
<div class="cal" id="cal-widget">
<div class="cal__nav">
<button class="cal__btn"
hx-get="/api/log/days?month={{ prev_month }}{% if selected %}&selected={{ selected.isoformat() }}{% endif %}"
hx-target="#cal-widget"
hx-swap="outerHTML">&lsaquo;</button>
<div class="cal__title">{{ month_name }} {{ year }}</div>
<button class="cal__btn"
hx-get="/api/log/days?month={{ next_month }}{% if selected %}&selected={{ selected.isoformat() }}{% endif %}"
hx-target="#cal-widget"
hx-swap="outerHTML">&rsaquo;</button>
</div>
<div class="cal__grid">
<div class="cal__h">Mo</div>
<div class="cal__h">Tu</div>
<div class="cal__h">We</div>
<div class="cal__h">Th</div>
<div class="cal__h">Fr</div>
<div class="cal__h">Sa</div>
<div class="cal__h">Su</div>
{% for week in grid %}
{% for d in week %}
{% if d is none %}
<div class="cal__d cal__d--empty"></div>
{% else %}
{% set has_log = d in days_with_logs %}
{% set is_selected = (selected and selected.day == d and selected.month == month and selected.year == year) %}
{% set is_today = (today.day == d and today.month == month and today.year == year) %}
{% set iso = "%04d-%02d-%02d" | format(year, month, d) %}
<button class="cal__d
{% if has_log %}cal__d--has-log{% else %}cal__d--no-log{% endif %}
{% if is_selected %}cal__d--selected{% endif %}
{% if is_today %}cal__d--today{% endif %}"
{% if has_log %}
hx-get="/api/log/by-date/{{ iso }}?as=html"
hx-target="#log-content"
hx-swap="innerHTML"
hx-push-url="/log/{{ iso }}"
onclick="document.querySelectorAll('.cal__d--selected').forEach(b=>b.classList.remove('cal__d--selected'));this.classList.add('cal__d--selected')"
{% else %}
disabled
{% endif %}
>{{ d }}</button>
{% endif %}
{% endfor %}
{% endfor %}
</div>
</div>

View file

@ -0,0 +1,38 @@
{% if not quotes %}
<div class="empty">no data yet — scheduler may not have run</div>
{% else %}
<table class="dense">
<thead>
<tr>
<th>Symbol</th><th>Label</th>
<th class="num">Price</th><th>Ccy</th>
<th class="num">1d</th><th class="num">1m</th><th class="num">1y</th>
{% if has_anchor %}<th class="num">anchor</th>{% endif %}
<th>as-of</th>
</tr>
</thead>
<tbody>
{% for q in quotes %}
<tr>
<td class="label">{{ q.symbol }}</td>
<td>{{ q.label or "" }}</td>
<td class="num">{{ q.price | price }}</td>
<td class="neu">{{ q.currency or "" }}</td>
{% for k in ["1d","1m","1y"] %}
{% set v = q.changes.get(k) if q.changes else None %}
<td class="num {% if v is none %}neu{% elif v >= 0 %}pos{% else %}neg{% endif %}">
{% if v is none %}—{% else %}{{ "%+.2f"|format(v) }}%{% endif %}
</td>
{% endfor %}
{% if has_anchor %}
{% set va = q.changes.get('anchor') if q.changes else None %}
<td class="num {% if va is none %}neu{% elif va >= 0 %}pos{% else %}neg{% endif %}">
{% if va is none %}—{% else %}{{ "%+.2f"|format(va) }}%{% endif %}
</td>
{% endif %}
<td class="neu">{{ q.as_of or "" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}

View file

@ -0,0 +1,18 @@
{% if not log %}
<div class="empty">awaiting first generated log</div>
{% else %}
<div class="log-content">{{ log.content_html | safe }}</div>
<div class="log-meta">
<div class="log-meta__row">
{% if log.tone %}<span class="badge badge--tone-{{ log.tone | lower }}">tone {{ log.tone | lower }}</span>{% endif %}
{% if log.analysis %}<span class="badge badge--analysis-{{ log.analysis | lower }}">analysis {{ log.analysis | lower }}</span>{% endif %}
{% if log.prompt_version %}<span class="badge badge--ver">prompt v{{ log.prompt_version }}</span>{% endif %}
</div>
<div class="log-meta__row log-meta__row--dim">
generated {{ log.generated_at.strftime("%Y-%m-%d %H:%M UTC") }}
&nbsp;·&nbsp; model <span class="neu">{{ log.model }}</span>
{% if log.prompt_tokens %} &nbsp;·&nbsp; {{ log.prompt_tokens }}↑/{{ log.completion_tokens }}↓ tokens{% endif %}
{% if log.cost_usd is not none %} &nbsp;·&nbsp; ${{ "%.4f"|format(log.cost_usd) }}{% endif %}
</div>
</div>
{% endif %}

View file

@ -0,0 +1,16 @@
{% if not headlines %}
<div class="empty">no headlines in window</div>
{% else %}
{% for h in headlines %}
<div class="news-row">
<span class="age">{{ h.age }}</span>
<span class="source">{{ h.source }}</span>
<a class="title" href="{{ h.url }}" target="_blank" rel="noopener">{{ h.title }}</a>
{% if h.iso %}
<time class="local" datetime="{{ h.iso }}" title="{{ h.iso }}">{{ h.utc_short }}</time>
{% else %}
<span class="local"></span>
{% endif %}
</div>
{% endfor %}
{% endif %}

View file

@ -0,0 +1,7 @@
<span><span class="led {% if db_ok %}ok{% else %}err{% endif %}"></span>DB</span>
{% for j in jobs %}
<span title="{{ j.name }}">
<span class="led {{ j.led }}"></span>{{ j.name }}
{% if j.last_finished %}· {{ j.age }}{% endif %}
</span>
{% endfor %}

View file

@ -0,0 +1,80 @@
{% if not portfolios %}
<div class="empty">no portfolio snapshots yet</div>
{% else %}
{% for p in portfolios %}
{# --- overall block --- #}
<div class="pf-overall">
<div class="pf-overall__head">
<span class="pf-name">{{ p.name }}</span>
<span class="pf-as-of">
{% if p.snapshot_at %}{{ p.snapshot_at.strftime("%Y-%m-%d %H:%M UTC") }}{% else %}—{% endif %}
</span>
</div>
<div class="pf-overall__grid">
<div class="pf-stat">
<div class="pf-stat-label">Total</div>
<div class="pf-stat-value">{{ p.total_value | money }} <span class="pf-ccy">{{ p.currency }}</span></div>
</div>
<div class="pf-stat">
<div class="pf-stat-label">Invested</div>
<div class="pf-stat-value">{{ p.invested | money }}</div>
</div>
<div class="pf-stat">
<div class="pf-stat-label">Cash</div>
<div class="pf-stat-value">{{ p.cash | money }}</div>
</div>
<div class="pf-stat">
<div class="pf-stat-label">Unrealised P/L</div>
<div class="pf-stat-value {% if p.unrealized_ppl is none %}neu{% elif p.unrealized_ppl >= 0 %}pos{% else %}neg{% endif %}">
{{ p.unrealized_ppl | signed }}
{% if p.total_cost and p.unrealized_ppl is not none %}
<span class="pf-pct">({{ "%+.2f"|format(p.unrealized_ppl / p.total_cost * 100) }}%)</span>
{% endif %}
</div>
</div>
<div class="pf-stat">
<div class="pf-stat-label">Realised P/L</div>
<div class="pf-stat-value {% if p.realized_ppl is none %}neu{% elif p.realized_ppl >= 0 %}pos{% else %}neg{% endif %}">
{{ p.realized_ppl | signed }}
</div>
</div>
<div class="pf-stat">
<div class="pf-stat-label">Positions</div>
<div class="pf-stat-value">{{ p.positions | length }}</div>
</div>
</div>
</div>
{# --- per-position table --- #}
<table class="dense">
<thead>
<tr>
<th>Ticker</th>
<th>Name</th>
<th class="num">Qty</th>
<th class="num">Avg</th>
<th class="num">Last</th>
<th class="num">P/L</th>
<th class="num">%</th>
</tr>
</thead>
<tbody>
{% for pos in p.positions %}
<tr>
<td class="label">{{ pos.ticker }}</td>
<td>{{ pos.name or "" }}</td>
<td class="num">{{ pos.quantity | price }}</td>
<td class="num neu">{{ pos.average_price | price }}</td>
<td class="num">{{ pos.current_price | price }}</td>
<td class="num {% if pos.ppl is none %}neu{% elif pos.ppl >= 0 %}pos{% else %}neg{% endif %}">
{{ pos.ppl | signed }}
</td>
<td class="num {% if pos.ppl_pct is none %}neu{% elif pos.ppl_pct >= 0 %}pos{% else %}neg{% endif %}">
{% if pos.ppl_pct is not none %}{{ "%+.2f"|format(pos.ppl_pct) }}%{% else %}—{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
{% endif %}