summaryrefslogtreecommitdiff
path: root/README.md
blob: 41ee1b42bbd56bcf5c14c9d511bd4175f4042018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Prism v2

Prism v2 is a monorepo rewrite of Prism's Overview experience with a FastAPI backend and a Next.js frontend. This first slice covers:

- ticker search
- selected ticker profile and quote
- market bar
- historical price chart
- overview signal and stat cards
- persisted watchlist backed by SQLite

The implementation copies and adapts finance logic from Prism v1 into `backend/`; it does not import code from the old repo at runtime.

## Repo Layout

- `backend/`
  - `app/main.py` FastAPI application and routes
  - `app/services/data_service.py` yfinance-backed Overview service layer with TTL caching
  - `app/db/watchlist.py` SQLite persistence for the default local profile
  - `data/prism.db` local watchlist database
  - `tests/` backend unit and API smoke tests
- `frontend/`
  - `app/page.tsx` Overview shell
  - `components/PriceChart.tsx` Plotly price chart
  - `lib/api.ts` typed REST client
  - `types/api.ts` shared frontend response types
- `scripts/stack.sh` unified start/stop/restart/status script
- `systemd/` systemd unit files for running as a system service
- `nginx/` nginx server block config (proxies `/api/` to backend, `/` to frontend)
- `.env.example` optional environment variables
- `pytest.ini` backend pytest import path config

## Requirements

- Python 3.14+
- Node.js 20+
- npm

## Environment

Copy `.env.example` to `.env` if you want to set optional keys.

Supported variable names are unchanged from Prism v1:

- `FMP_API_KEY`
- `FINNHUB_API_KEY`
- `NEXT_PUBLIC_API_BASE_URL`

The current Overview slice works without FMP or Finnhub keys because it relies on yfinance for the active endpoints.

## Backend Setup

```bash
cd backend
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

Run the API:

```bash
.venv/bin/uvicorn app.main:app --reload --host 127.0.0.1 --port 8001
```

Primary endpoints:

- `GET /health`
- `GET /api/search?q=...`
- `GET /api/market/indices`
- `GET /api/tickers/{symbol}/overview`
- `GET /api/tickers/{symbol}/history?period=1m|3m|6m|1y|5y`
- `GET /api/watchlist`
- `POST /api/watchlist/{symbol}`
- `DELETE /api/watchlist/{symbol}`

Notes:

- SQLite lives at `backend/data/prism.db`
- the backend seeds one `default` profile automatically
- watchlist symbols are normalized to uppercase
- watchlist size is capped at 10 symbols

## Frontend Setup

```bash
cd frontend
npm install
```

Run the frontend against the local backend:

```bash
NEXT_PUBLIC_API_BASE_URL=http://127.0.0.1:8001 npm run dev -- --hostname 127.0.0.1 --port 3001
```

The UI stores the selected ticker in the URL, for example `/?ticker=AAPL`.

## Quick Start

`scripts/stack.sh` manages both services as a local development stack:

```bash
./scripts/stack.sh start    # start backend + frontend in background
./scripts/stack.sh stop     # stop both
./scripts/stack.sh restart  # stop then start
./scripts/stack.sh status   # show running PIDs and URLs
```

Notes:

- expects `backend/.venv` and `frontend/node_modules` to already exist
- default ports are backend `8001` and frontend `3001`
- logs are written to `.run/logs/`
- override hosts/ports via env vars: `BACKEND_HOST`, `BACKEND_PORT`, `FRONTEND_HOST`, `FRONTEND_PORT`

## Running as a systemd Service

Unit files live in `systemd/`. Install them for the current user:

```bash
# Copy units
cp systemd/prismv2-backend.service  ~/.config/systemd/user/
cp systemd/prismv2-frontend.service ~/.config/systemd/user/
cp systemd/prismv2.target           ~/.config/systemd/user/

systemctl --user daemon-reload

# Start both services via the target
systemctl --user enable --now prismv2.target

# Control individual services
systemctl --user status  prismv2-backend.service
systemctl --user restart prismv2-frontend.service
systemctl --user stop    prismv2.target

# View logs
journalctl --user -u prismv2-backend.service  -f
journalctl --user -u prismv2-frontend.service -f
```

To start automatically on login, enable lingering once:

```bash
loginctl enable-linger $USER
```

## Verification

Backend:

```bash
backend/.venv/bin/python -m py_compile backend/app/main.py backend/app/schemas.py backend/app/services/data_service.py backend/app/db/watchlist.py
backend/.venv/bin/python -m pytest backend/tests
```

Frontend:

```bash
cd frontend
npm run lint
npm run build
```

## Current Local URLs

If port `8000` is already occupied on your machine, use:

- backend: `http://127.0.0.1:8001`
- frontend: `http://127.0.0.1:3001`

Those are the ports used by the current local bootstrap.