File size: 2,756 Bytes
9e42d3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Backend API (FastAPI)

The frontend app now lives in `frontend/` and should be started from there.

## 1) Install and start API

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r backend/requirements.txt
uvicorn backend.app.main:app --reload --host 0.0.0.0 --port 8000
```

Health check:

```bash
curl -s http://localhost:8000/health | jq .
```

## 2) Seed command (with valid CSV path)

```bash
python3 scripts/seed_db.py \
  --db backend/app/db/local.db \
  --csv data/students_base.csv \
  --seed 42 \
  --reset
```

Alternative via API:

```bash
curl -X POST http://localhost:8000/admin/reset-and-seed \
  -H "Content-Type: application/json" \
  -d '{
    "csv_path": "data/students_base.csv",
    "seed": 42,
    "reset": true
  }'
```

## 3) Sample curl calls

List leads:

```bash
curl -s "http://localhost:8000/leads?fair_id=1&limit=20&offset=0&sort_by=total_score&sort_order=desc" | jq .
```

Lead detail:

```bash
curl -s "http://localhost:8000/leads/1" | jq .
```

Lead score:

```bash
curl -s "http://localhost:8000/leads/1/score" | jq .
```

Fair KPIs:

```bash
curl -s "http://localhost:8000/kpis/fair/1" | jq .
```

Stand KPIs:

```bash
curl -s "http://localhost:8000/kpis/stands?fair_id=1&period=all" | jq .
```

Conference KPIs:

```bash
curl -s "http://localhost:8000/kpis/conferences?fair_id=1&period=all" | jq .
```

Create one event and recompute:

```bash
curl -X POST "http://localhost:8000/events?recompute=true" \
  -H "Content-Type: application/json" \
  -d '{
    "fair_id": 1,
    "student_id": 1,
    "event_type": "site_page_view",
    "source_channel": "web",
    "payload": {"page": "programme"}
  }' | jq .
```

Bulk events:

```bash
curl -X POST "http://localhost:8000/events/bulk" \
  -H "Content-Type: application/json" \
  -d '{
    "events": [
      {"fair_id": 1, "student_id": 1, "event_type": "check_in"},
      {"fair_id": 1, "student_id": 1, "event_type": "stand_tap", "stand_id": 1}
    ]
  }' | jq .
```

Manual recompute:

```bash
curl -X POST http://localhost:8000/model/recompute | jq .
```

DB stats:

```bash
curl -s http://localhost:8000/admin/db-stats | jq .
```

## 4) Troubleshooting

### FileNotFoundError / CSV path issues

- Use an absolute path for `--csv` and `csv_path`.
- Confirm file exists:

```bash
ls -lh /absolute/path/to/your/students_base.csv
```

- Required CSV headers: `row_id,audience_type,grade,student_subgroup,visited_site,used_ORI,took_test,digital_experience`

### Empty DB responses

If seeding has not run or failed:

- `GET /leads` returns `{ "data": [], "total": 0, ... }`
- `GET /kpis/fair/{fair_id}` returns zero metrics instead of server errors.

Use:

```bash
curl -s http://localhost:8000/admin/db-stats | jq .
```

to verify row counts by table.