Download frontend/src/components/AlertsPanel.jsx from bhuvi3/CIOAgent: direct link, hf CLI and curl.
- Browser
- Download file 5.37 kB
-
https://huggingface.co/spaces/bhuvi3/CIOAgent/resolve/main/frontend/src/components/AlertsPanel.jsx
- Command line
-
hf download hf://spaces/bhuvi3/CIOAgent/frontend/src/components/AlertsPanel.jsx
-
curl -L -o AlertsPanel.jsx https://huggingface.co/spaces/bhuvi3/CIOAgent/resolve/main/frontend/src/components/AlertsPanel.jsx
5.37 kB
| import React, { useState } from "react"; | |
| import { checkAlerts } from "../api"; | |
| const METRICS = [ | |
| { value: "sharpe_ratio", label: "Sharpe Ratio" }, | |
| { value: "annual_return", label: "Annual Return" }, | |
| { value: "annual_volatility", label: "Annual Volatility" }, | |
| { value: "max_drawdown", label: "Max Drawdown" }, | |
| { value: "sortino_ratio", label: "Sortino Ratio" }, | |
| { value: "VaR_95_daily", label: "VaR (95% Daily)" }, | |
| ]; | |
| const OPERATORS = [ | |
| { value: "lt", label: "< (less than)" }, | |
| { value: "gt", label: "> (greater than)" }, | |
| { value: "lte", label: "<= (at most)" }, | |
| { value: "gte", label: ">= (at least)" }, | |
| ]; | |
| export default function AlertsPanel({ portfolio }) { | |
| const [rules, setRules] = useState([ | |
| { id: "1", metric: "sharpe_ratio", operator: "lt", threshold: 0.5, label: "Low Sharpe" }, | |
| ]); | |
| const [results, setResults] = useState(null); | |
| const [checking, setChecking] = useState(false); | |
| const addRule = () => { | |
| setRules([...rules, { | |
| id: String(rules.length + 1), | |
| metric: "sharpe_ratio", | |
| operator: "lt", | |
| threshold: 0, | |
| label: "", | |
| }]); | |
| }; | |
| const updateRule = (idx, field, value) => { | |
| const updated = [...rules]; | |
| updated[idx] = { ...updated[idx], [field]: value }; | |
| setRules(updated); | |
| }; | |
| const removeRule = (idx) => { | |
| setRules(rules.filter((_, i) => i !== idx)); | |
| }; | |
| const handleCheck = async () => { | |
| setChecking(true); | |
| try { | |
| const data = await checkAlerts(portfolio, rules); | |
| setResults(data); | |
| } catch (e) { | |
| setResults({ error: e.message }); | |
| } | |
| setChecking(false); | |
| }; | |
| return ( | |
| <div className="section"> | |
| <h2>Watchlist & Alerts</h2> | |
| <p className="caption">Set metric thresholds and check if your portfolio triggers them.</p> | |
| {rules.map((rule, i) => ( | |
| <div key={i} style={{ display: "flex", gap: "0.5rem", marginBottom: "0.5rem", alignItems: "center", flexWrap: "wrap" }}> | |
| <input | |
| value={rule.label} | |
| placeholder="Label" | |
| onChange={(e) => updateRule(i, "label", e.target.value)} | |
| style={{ width: 120, padding: "0.35rem 0.5rem", background: "var(--surface-2)", border: "1px solid var(--border)", borderRadius: 4, color: "var(--text)", fontSize: "0.8rem" }} | |
| /> | |
| <select | |
| value={rule.metric} | |
| onChange={(e) => updateRule(i, "metric", e.target.value)} | |
| style={{ padding: "0.35rem", background: "var(--surface-2)", border: "1px solid var(--border)", borderRadius: 4, color: "var(--text)", fontSize: "0.8rem" }} | |
| > | |
| {METRICS.map((m) => <option key={m.value} value={m.value}>{m.label}</option>)} | |
| </select> | |
| <select | |
| value={rule.operator} | |
| onChange={(e) => updateRule(i, "operator", e.target.value)} | |
| style={{ padding: "0.35rem", background: "var(--surface-2)", border: "1px solid var(--border)", borderRadius: 4, color: "var(--text)", fontSize: "0.8rem" }} | |
| > | |
| {OPERATORS.map((o) => <option key={o.value} value={o.value}>{o.label}</option>)} | |
| </select> | |
| <input | |
| type="number" | |
| step="0.01" | |
| value={rule.threshold} | |
| onChange={(e) => updateRule(i, "threshold", parseFloat(e.target.value) || 0)} | |
| style={{ width: 80, padding: "0.35rem 0.5rem", background: "var(--surface-2)", border: "1px solid var(--border)", borderRadius: 4, color: "var(--text)", fontSize: "0.8rem" }} | |
| /> | |
| <button className="btn" style={{ padding: "0.25rem 0.5rem", fontSize: "0.75rem" }} onClick={() => removeRule(i)}>x</button> | |
| </div> | |
| ))} | |
| <div style={{ display: "flex", gap: "0.5rem", marginTop: "0.75rem" }}> | |
| <button className="btn" onClick={addRule}>+ Add Rule</button> | |
| <button className="btn primary" onClick={handleCheck} disabled={checking || rules.length === 0}> | |
| {checking ? "Checking..." : "Check Alerts"} | |
| </button> | |
| </div> | |
| {results && !results.error && ( | |
| <div style={{ marginTop: "1rem" }}> | |
| {results.total_triggered === 0 ? ( | |
| <div className="alert success">All clear — no alerts triggered ({results.total_rules} rules checked).</div> | |
| ) : ( | |
| <> | |
| <div className="alert warning"> | |
| {results.total_triggered} of {results.total_rules} alerts triggered! | |
| </div> | |
| <table className="data-table" style={{ marginTop: "0.5rem" }}> | |
| <thead><tr><th>Alert</th><th>Metric</th><th>Condition</th><th>Current</th></tr></thead> | |
| <tbody> | |
| {results.triggered.map((t, i) => ( | |
| <tr key={i}> | |
| <td><strong>{t.label || t.rule_id}</strong></td> | |
| <td>{t.metric}</td> | |
| <td>{t.operator} {t.threshold}</td> | |
| <td style={{ color: "var(--error)", fontWeight: 600 }}> | |
| {typeof t.current_value === "number" ? t.current_value.toFixed(4) : t.current_value} | |
| </td> | |
| </tr> | |
| ))} | |
| </tbody> | |
| </table> | |
| </> | |
| )} | |
| </div> | |
| )} | |
| {results?.error && <div className="alert warning">{results.error}</div>} | |
| </div> | |
| ); | |
| } | |