-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
676 lines (524 loc) · 26.1 KB
/
Copy pathserver.py
File metadata and controls
676 lines (524 loc) · 26.1 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
"""
server.py — Purplle Store Intelligence API
Usage:
python server.py # default port 8000
python server.py --port 9000
Open http://localhost:8000/dashboard in your browser.
"""
import argparse
import asyncio
import json
import logging
import os
import time
from collections import Counter
from typing import Any, Dict, List, Optional
import uvicorn
from fastapi import FastAPI, Query, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field
from config import OUTPUT_DIR, BASE_DIR
# ─────────────────────────────────────────────────────────────────
# Logging
# ─────────────────────────────────────────────────────────────────
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s │ %(levelname)-5s │ %(message)s",
datefmt="%H:%M:%S",
)
logger = logging.getLogger("purplle")
# ─────────────────────────────────────────────────────────────────
# Pydantic Response Models
# ─────────────────────────────────────────────────────────────────
class HealthStatus(BaseModel):
ready: bool
files: Dict[str, bool]
class StoreOverview(BaseModel):
store_id: str
total_unique_visitors: int
total_revenue: float
total_orders: int
cameras_active: int
class AllStoresOverview(BaseModel):
total_unique_visitors: int
total_revenue: float
total_orders: int
cameras_active: int
stores_active: int
class ZoneFootfall(BaseModel):
zone: str
camera_id: str
unique_visitors: int = 0
avg_dwell_sec: float = 0
max_dwell_sec: float = 0
class FootfallResponse(BaseModel):
store_id: str
zones: List[ZoneFootfall]
class HeatmapCell(BaseModel):
zone: str
camera_id: str
peak_count: int = 0
avg_count: float = 0
class HeatmapResponse(BaseModel):
store_id: str
heatmap: List[HeatmapCell]
class QueueResponse(BaseModel):
store_id: str
total_served: int
total_abandoned: int
abandonment_rate: float
avg_wait_seconds: float
class SalesSummary(BaseModel):
total_revenue: float
total_orders: int
top_brands: List[Dict[str, Any]]
class HourlyEntry(BaseModel):
hour: int
orders: int
revenue: float
class SalesHourly(BaseModel):
hourly: List[HourlyEntry]
class AnomalyItem(BaseModel):
type: str
severity: str
message: str
store_id: Optional[str] = None
camera_id: Optional[str] = None
zone: Optional[str] = None
peak_count: Optional[int] = None
avg_dwell_sec: Optional[float] = None
count: Optional[int] = None
class AnomalyResponse(BaseModel):
total: int
anomalies: List[AnomalyItem]
class EventRecord(BaseModel):
event_type: str
store_id: Optional[str] = None
store_code: Optional[str] = None
camera_id: Optional[str] = None
track_id: Optional[int] = None
id_token: Optional[str] = None
event_time: Optional[str] = None
event_timestamp: Optional[str] = None
class Config:
extra = "allow"
class EventsResponse(BaseModel):
total: int
offset: int
limit: int
events: List[EventRecord]
class FunnelStage(BaseModel):
stage: str
count: int
percentage: float = Field(..., description="Percentage relative to entry stage")
class FunnelResponse(BaseModel):
store_id: str
funnel: List[FunnelStage]
conversion_rate: float = Field(..., description="End-to-end conversion %")
class DemographicBucket(BaseModel):
label: str
count: int
percentage: float
class DemographicsResponse(BaseModel):
store_id: str
total_visitors: int
gender: List[DemographicBucket]
age_buckets: List[DemographicBucket]
class EventSchemaResponse(BaseModel):
version: str
description: str
common_fields: Dict[str, str]
event_types: Dict[str, List[str]]
# ─────────────────────────────────────────────────────────────────
# Data loading helpers
# ─────────────────────────────────────────────────────────────────
def _load(filename: str):
path = os.path.join(OUTPUT_DIR, filename)
if not os.path.exists(path):
return None
with open(path, "r") as f:
return json.load(f)
def _load_events() -> list:
path = os.path.join(OUTPUT_DIR, "generated_events.jsonl")
if not os.path.exists(path):
return []
with open(path, "r") as f:
return [json.loads(line) for line in f if line.strip()]
def _require(data, name: str):
if data is None:
raise HTTPException(
status_code=503,
detail=f"{name} not found. Run generate_demo_data.py or process_videos.py first.",
)
return data
def _store_matches(event: dict, store_id: str) -> bool:
return event.get("store_id") == store_id or event.get("store_code") == store_id
def _all_stores_overview_payload() -> dict:
analytics = _require(_load("store_analytics.json"), "store_analytics.json")
sales = _load("sales_analytics.json") or {}
events = _load_events()
entry_ids = {e["id_token"] for e in events if e.get("event_type") == "entry"}
total_cams = sum(len(cams) for cams in analytics.values())
return {
"total_unique_visitors": len(entry_ids),
"total_revenue": sales.get("total_revenue", 0),
"total_orders": sales.get("total_orders", 0),
"cameras_active": total_cams,
"stores_active": len(analytics),
}
EVENT_SCHEMA = {
"version": "1.0",
"description": "Normalized event contracts emitted by the CCTV intelligence pipeline.",
"common_fields": {
"event_type": "entry | exit | zone_entered | zone_exited | queue_completed | queue_abandoned",
"store_id/store_code": "Canonical store code such as ST1008 or ST1009.",
"camera_id": "Camera identifier from config.py.",
},
"event_types": {
"entry": ["event_type", "id_token", "store_code", "camera_id", "event_timestamp", "is_staff"],
"exit": ["event_type", "id_token", "store_code", "camera_id", "event_timestamp"],
"zone_entered": [
"event_type", "track_id", "store_id", "camera_id", "zone_id",
"zone_name", "zone_type", "event_time", "zone_hotspot_x", "zone_hotspot_y",
],
"zone_exited": [
"event_type", "track_id", "store_id", "camera_id", "zone_id",
"zone_name", "zone_type", "event_time",
],
"queue_completed": [
"queue_event_id", "event_type", "track_id", "store_id", "camera_id",
"queue_join_ts", "queue_served_ts", "queue_exit_ts", "wait_seconds",
],
"queue_abandoned": [
"queue_event_id", "event_type", "track_id", "store_id", "camera_id",
"queue_join_ts", "queue_exit_ts", "wait_seconds", "abandoned",
],
},
}
# ─────────────────────────────────────────────────────────────────
# App
# ─────────────────────────────────────────────────────────────────
app = FastAPI(
title="Purplle Store Intelligence API",
version="1.0.0",
description=(
"Real-time retail analytics from computer vision. "
"End-to-end pipeline: CCTV → YOLOv8 detection → ByteTrack tracking → "
"Zone analytics → Anomaly detection → REST APIs + Live Dashboard."
),
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ── Request logging middleware ────────────────────────────────────
@app.middleware("http")
async def log_requests(request: Request, call_next):
start = time.time()
response = await call_next(request)
duration = round((time.time() - start) * 1000, 1)
if request.url.path.startswith("/api/"):
logger.info(f"{request.method} {request.url.path} → {response.status_code} ({duration}ms)")
return response
# Serve compressed videos at /videos/<filename>
if os.path.isdir(OUTPUT_DIR):
app.mount("/videos", StaticFiles(directory=OUTPUT_DIR), name="videos")
# ─────────────────────────────────────────────────────────────────
# Root
# ─────────────────────────────────────────────────────────────────
@app.get("/", include_in_schema=False)
def root(request: Request):
base = str(request.base_url).rstrip("/")
return {
"service": "Purplle Store Intelligence API",
"version": "1.0.0",
"status": "live",
"dashboard": f"{base}/dashboard",
"docs": f"{base}/docs",
}
# Serve the dashboard HTML
@app.get("/dashboard", response_class=HTMLResponse, include_in_schema=False)
def dashboard():
html_path = os.path.join(BASE_DIR, "dashboard.html")
if not os.path.exists(html_path):
raise HTTPException(status_code=404, detail="dashboard.html not found")
with open(html_path, "r", encoding="utf-8") as f:
return HTMLResponse(content=f.read())
# ─────────────────────────────────────────────────────────────────
# /api/v1/health
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/health", response_model=HealthStatus, tags=["System"])
def health():
"""Check data readiness. Returns which analytics files are available."""
files = ["store_analytics.json", "sales_analytics.json",
"anomalies.json", "generated_events.jsonl"]
status = {f: os.path.exists(os.path.join(OUTPUT_DIR, f)) for f in files}
ready = all(status.values())
return {"ready": ready, "files": status}
# ─────────────────────────────────────────────────────────────────
# /api/v1/schema/events
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/schema/events", response_model=EventSchemaResponse, tags=["Schema"])
def event_schema():
"""Event contract documentation. Describes all event types and their fields."""
return EVENT_SCHEMA
# ─────────────────────────────────────────────────────────────────
# Store Overview
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/store/all/overview", response_model=AllStoresOverview, tags=["Overview"])
def all_stores_overview():
"""Combined KPI summary across all stores."""
return _all_stores_overview_payload()
@app.get("/api/v1/stores/overview", response_model=AllStoresOverview, tags=["Overview"])
def stores_overview():
"""Alias for combined KPI summary."""
return _all_stores_overview_payload()
@app.get("/api/v1/store/{store_id}/overview", response_model=StoreOverview, tags=["Overview"])
def store_overview(store_id: str):
"""Per-store KPI summary: visitors, revenue, orders, cameras."""
analytics = _require(_load("store_analytics.json"), "store_analytics.json")
sales = _load("sales_analytics.json") or {}
events = _load_events()
entry_ids = {
e["id_token"]
for e in events
if e.get("event_type") == "entry"
and _store_matches(e, store_id)
}
cameras_active = len(analytics.get(store_id, {}))
return {
"store_id": store_id,
"total_unique_visitors": len(entry_ids),
"total_revenue": sales.get("total_revenue", 0),
"total_orders": sales.get("total_orders", 0),
"cameras_active": cameras_active,
}
# ─────────────────────────────────────────────────────────────────
# Footfall
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/store/{store_id}/footfall", response_model=FootfallResponse, tags=["Footfall"])
def store_footfall(store_id: str):
"""Zone-level dwell time analytics for a store."""
analytics = _require(_load("store_analytics.json"), "store_analytics.json")
store_data = analytics.get(store_id, {})
zones = []
for cam_id, cam_data in store_data.items():
for zone_name, dwell in cam_data.get("dwell", {}).items():
zones.append({
"zone": zone_name,
"camera_id": cam_id,
"unique_visitors": dwell.get("unique_visitors", 0),
"avg_dwell_sec": dwell.get("avg_dwell_sec", 0),
"max_dwell_sec": dwell.get("max_dwell_sec", 0),
})
return {"store_id": store_id, "zones": zones}
# ─────────────────────────────────────────────────────────────────
# Heatmap
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/store/{store_id}/heatmap", response_model=HeatmapResponse, tags=["Footfall"])
def store_heatmap(store_id: str):
"""Peak and average occupancy by zone for heatmap visualization."""
analytics = _require(_load("store_analytics.json"), "store_analytics.json")
store_data = analytics.get(store_id, {})
heatmap = []
for cam_id, cam_data in store_data.items():
for zone_name, peak in cam_data.get("peak", {}).items():
heatmap.append({
"zone": zone_name,
"camera_id": cam_id,
"peak_count": peak.get("peak_count", 0),
"avg_count": peak.get("avg_count", 0),
})
return {"store_id": store_id, "heatmap": heatmap}
# ─────────────────────────────────────────────────────────────────
# Queue
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/store/{store_id}/queue", response_model=QueueResponse, tags=["Queue"])
def store_queue(store_id: str):
"""Billing queue metrics: served, abandoned, wait times."""
events = _load_events()
queue_events = [
e for e in events
if e.get("event_type") in ("queue_completed", "queue_abandoned")
and _store_matches(e, store_id)
]
completed = [e for e in queue_events if e["event_type"] == "queue_completed"]
abandoned = [e for e in queue_events if e["event_type"] == "queue_abandoned"]
wait_times = [e.get("wait_seconds", 0) for e in queue_events]
total = len(queue_events)
avg_wait = round(sum(wait_times) / max(len(wait_times), 1), 1)
abandon_rate = round(len(abandoned) / max(total, 1) * 100, 1)
return {
"store_id": store_id,
"total_served": len(completed),
"total_abandoned": len(abandoned),
"abandonment_rate": abandon_rate,
"avg_wait_seconds": avg_wait,
}
# ─────────────────────────────────────────────────────────────────
# Conversion Funnel (NEW)
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/store/{store_id}/funnel", response_model=FunnelResponse, tags=["Analytics"])
def store_funnel(store_id: str):
"""
Conversion funnel: Entry → Zone Browsing → Billing Queue → Purchase.
Tracks how many visitors progress through each stage.
"""
events = _load_events()
store_events = [e for e in events if _store_matches(e, store_id)]
# Stage 1: Entries (unique visitors)
entry_ids = {e.get("id_token") or e.get("track_id")
for e in store_events if e.get("event_type") == "entry"}
# Stage 2: Zone browsing (unique track_ids in zones)
zone_ids = {e.get("track_id")
for e in store_events if e.get("event_type") == "zone_entered"}
# Stage 3: Billing queue (joined queue)
queue_ids = {e.get("track_id")
for e in store_events
if e.get("event_type") in ("queue_completed", "queue_abandoned")}
# Stage 4: Purchase completed
purchase_ids = {e.get("track_id")
for e in store_events if e.get("event_type") == "queue_completed"}
entry_count = len(entry_ids) or 1 # avoid division by zero
stages = [
{"stage": "Store Entry", "count": len(entry_ids)},
{"stage": "Zone Browsing", "count": len(zone_ids)},
{"stage": "Billing Queue", "count": len(queue_ids)},
{"stage": "Purchase", "count": len(purchase_ids)},
]
for s in stages:
s["percentage"] = round(s["count"] / entry_count * 100, 1)
conversion = round(len(purchase_ids) / entry_count * 100, 1)
return {
"store_id": store_id,
"funnel": stages,
"conversion_rate": conversion,
}
# ─────────────────────────────────────────────────────────────────
# Demographics (NEW)
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/store/{store_id}/demographics", response_model=DemographicsResponse, tags=["Analytics"])
def store_demographics(store_id: str):
"""
Demo-mode aggregate demographic breakdown by gender and age bucket.
"""
events = _load_events()
store_events = [
e for e in events
if _store_matches(e, store_id) and e.get("event_type") == "entry"
]
total = len(store_events) or 1
# Gender breakdown
gender_counts = Counter(e.get("gender_pred", "Unknown") for e in store_events)
gender = [
{"label": g, "count": c, "percentage": round(c / total * 100, 1)}
for g, c in gender_counts.most_common()
]
# Age bucket breakdown
age_counts = Counter(e.get("age_bucket", "Unknown") for e in store_events)
age_buckets = [
{"label": a, "count": c, "percentage": round(c / total * 100, 1)}
for a, c in age_counts.most_common()
]
return {
"store_id": store_id,
"total_visitors": len(store_events),
"gender": gender,
"age_buckets": age_buckets,
}
# ─────────────────────────────────────────────────────────────────
# Sales
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/sales/summary", response_model=SalesSummary, tags=["Sales"])
def sales_summary():
"""Revenue, order count, and top-performing brands."""
sales = _require(_load("sales_analytics.json"), "sales_analytics.json")
return {
"total_revenue": sales.get("total_revenue", 0),
"total_orders": sales.get("total_orders", 0),
"top_brands": sales.get("top_brands", []),
}
@app.get("/api/v1/sales/hourly", response_model=SalesHourly, tags=["Sales"])
def sales_hourly():
"""Revenue and orders broken down by hour of day."""
sales = _require(_load("sales_analytics.json"), "sales_analytics.json")
return {"hourly": sales.get("hourly", [])}
# ─────────────────────────────────────────────────────────────────
# Anomalies
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/anomalies", response_model=AnomalyResponse, tags=["Anomalies"])
def get_anomalies(severity: Optional[str] = Query(None, description="Filter by severity: HIGH, MEDIUM, LOW, INFO")):
"""Detected operational anomalies with optional severity filter."""
raw = _require(_load("anomalies.json"), "anomalies.json")
items = raw if isinstance(raw, list) else []
if severity:
items = [a for a in items if a.get("severity") == severity.upper()]
return {"total": len(items), "anomalies": items}
# ─────────────────────────────────────────────────────────────────
# Events
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/events", response_model=EventsResponse, tags=["Events"])
def get_events(
event_type: Optional[str] = Query(None, description="Filter by event type"),
store_id: Optional[str] = Query(None, description="Filter by store code"),
camera_id: Optional[str] = Query(None, description="Filter by camera ID"),
limit: int = Query(100, ge=1, le=5000, description="Max results to return"),
offset: int = Query(0, ge=0, description="Pagination offset"),
):
"""Paginated event log with filters by type, store, and camera."""
events = _load_events()
if event_type:
events = [e for e in events if e.get("event_type") == event_type]
if store_id:
events = [e for e in events if _store_matches(e, store_id)]
if camera_id:
events = [e for e in events if e.get("camera_id") == camera_id]
total = len(events)
paged = events[offset: offset + limit]
return {"total": total, "offset": offset, "limit": limit, "events": paged}
# ─────────────────────────────────────────────────────────────────
# SSE Event Stream
# ─────────────────────────────────────────────────────────────────
@app.get("/api/v1/events/stream", tags=["Events"])
async def stream_events(
store_id: Optional[str] = Query(None),
event_type: Optional[str] = Query(None),
limit: int = Query(100, ge=1, le=1000),
interval_ms: int = Query(300, ge=50, le=5000),
):
"""Server-sent event replay stream. Replays stored events at configurable speed."""
events = _load_events()
if event_type:
events = [e for e in events if e.get("event_type") == event_type]
if store_id:
events = [e for e in events if _store_matches(e, store_id)]
events = events[:limit]
async def event_generator():
for event in events:
yield f"data: {json.dumps(event)}\n\n"
await asyncio.sleep(interval_ms / 1000)
return StreamingResponse(event_generator(), media_type="text/event-stream")
# ─────────────────────────────────────────────────────────────────
# Entry point
# ─────────────────────────────────────────────────────────────────
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=8000)
parser.add_argument("--host", default="127.0.0.1")
args = parser.parse_args()
print(f"""
+------------------------------------------------------+
| Purplle Store Intelligence v1.0.0 |
+------------------------------------------------------+
| Dashboard -> http://{args.host}:{args.port}/dashboard
| API Docs -> http://{args.host}:{args.port}/docs
| Health -> http://{args.host}:{args.port}/api/v1/health
+------------------------------------------------------+
""")
uvicorn.run("server:app", host=args.host, port=args.port, reload=True)