File size: 23,799 Bytes
bfa25c1
 
 
 
 
 
2646299
bfa25c1
 
2646299
bfa25c1
86bec6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Chat With Data
emoji: 🏨
colorFrom: blue
colorTo: purple
sdk: streamlit
sdk_version: 1.39.0
app_file: frontend/dashboard.py
python_version: "3.11"
pinned: false
---
# Chat with your data β€” Enterprise AI Analytics Platform

> An enterprise-grade "Chat with Your Data" system that enables stakeholders to query hotel performance data in plain English, backed by a deterministic metrics engine, real-time KPI monitoring, and automated anomaly detection.

![Python](https://img.shields.io/badge/Python-3.11-blue?logo=python)
![Streamlit](https://img.shields.io/badge/Streamlit-1.30+-FF4B4B?logo=streamlit)
![PostgreSQL](https://img.shields.io/badge/PostgreSQL-Supabase-4169E1?logo=supabase)
![LLM](https://img.shields.io/badge/LLM-Grok%204.1-000000?logo=x)
![License](https://img.shields.io/badge/License-MIT-green)

<p align="center">
  <a href="#-live-demo">Live Demo</a> β€’
  <a href="#-features">Features</a> β€’
  <a href="#%EF%B8%8F-architecture">Architecture</a> β€’
  <a href="#-quick-start">Quick Start</a> β€’
  <a href="#-kpi-reference">KPI Reference</a>
</p>

---

## 🎯 Live Demo

πŸ”— **[Launch App on Streamlit Cloud](https://hospitality-acwk5lhj7j9dus5chmjbrs.streamlit.app/))**

### Dashboard Preview

<!-- 
  HOW TO ADD YOUR IMAGES:
  1. Create a folder called "assets/images/" in the root of your repo
  2. Take screenshots of your running app and save with the filenames below
  3. Git add, commit, push β€” GitHub will render them automatically
  Suggested tool for ER diagrams: https://dbdiagram.io or pgAdmin
-->

![Executive Dashboard](assets/images/dashboard_overview.png)
*Executive Dashboard β€” KPI cards, revenue trends, and city performance*

<details>
<summary>πŸ“Έ More Screenshots</summary>

<br>

![Chat with Data](assets/images/chat_with_data.png)
*AI Chat Interface β€” natural language queries with formatted business answers*

![KPI Monitoring](assets/images/kpi_monitoring.png)
*KPI Monitoring β€” anomaly detection alerts and property health scores*

</details>

---

## πŸ“Œ Problem Statement

In large hospitality organizations, performance data is spread across multiple tables with complex relationships. Business users need answers to questions like:

- *"What is our RevPAR for luxury hotels in Mumbai this month?"*
- *"Which booking platform has the highest cancellation rate?"*
- *"Compare week 25 vs week 29 occupancy across cities"*

Traditionally, each question requires an analyst to manually write SQL, build a report, and deliver it β€” a process that takes hours per query and doesn't scale.

**This platform solves that by enabling anyone to ask business questions in plain English and receive accurate, data-driven answers in seconds.**

---

## ✨ Features

### πŸ“Š Executive Dashboard
- **6 real-time KPI cards** with live Week-over-Week deltas
- **Multi-dimensional filtering** β€” City, Category, Room Class, Month, Week
- **Revenue by Category** β€” Luxury vs Business donut chart
- **Weekly Revenue Trend** β€” Interactive line chart
- **Weekend vs Weekday** β€” Performance comparison table
- **Realisation % & ADR by Platform** β€” Dual-axis combo chart
- **City Performance** β€” Horizontal bar comparison
- **Weekly Occupancy Trend** β€” Area chart with time series
- **Property Performance Table** β€” All 13 KPIs per hotel

### πŸ’¬ Chat with Your Data (AI Agent)
- **Natural language queries** β€” Ask in plain English, get data-driven answers
- **24 built-in KPI metrics** β€” Deterministic SQL generation (zero syntax errors)
- **Complex query support** β€” Rankings, comparisons, multi-step analysis
- **Custom SQL fallback** β€” LLM generates SQL for ad-hoc analytical questions
- **Suggested starter questions** β€” One-click query templates
- **Formatted business answers** β€” Currency, percentages, actionable insights

### πŸ” KPI Monitoring & Anomaly Detection
- **4-layer alert system:**
  - Threshold-based alerts (configurable per KPI)
  - Week-over-Week drop detection
  - Consecutive decline detection (3+ week downtrends)
  - Statistical anomaly detection (z-score across properties)
- **Property Health Scoring** β€” 0-100 score per hotel based on weighted KPI performance
- **Interactive trend analysis** β€” Select any metric, view with threshold lines
- **Configurable thresholds** β€” Adjust warning/critical levels per session

---

## πŸ—οΈ Architecture

![Architecture Diagram](assets/images/architecture_diagram.png)
*System architecture β€” export as PNG from draw.io, Excalidraw, or Lucidchart and save as `assets/images/architecture_diagram.png`*

<details>
<summary>View ASCII version</summary>

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     FRONTEND (Streamlit)                            β”‚
β”‚                                                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Dashboard    β”‚  β”‚  Chat with Data  β”‚  β”‚  KPI Monitoring       β”‚ β”‚
β”‚  β”‚  (KPI Cards,  β”‚  β”‚  (Natural Lang   β”‚  β”‚  (Anomaly Detection,  β”‚ β”‚
β”‚  β”‚   Charts,     β”‚  β”‚   Queries, AI    β”‚  β”‚   Health Scores,      β”‚ β”‚
β”‚  β”‚   Filters)    β”‚  β”‚   Agent)         β”‚  β”‚   Trend Analysis)     β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚         β”‚                   β”‚                        β”‚              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                   β”‚                        β”‚
          β–Ό                   β–Ό                        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   METRICS ENGINE (Single Source of Truth)            β”‚
β”‚                   utils/metrics_engine.py                            β”‚
β”‚                                                                     β”‚
β”‚  get_core_metrics() β”‚ get_wow_deltas() β”‚ get_property_table()       β”‚
β”‚  get_trend_data()   β”‚ get_city_comparison() β”‚ get_platform_perf()   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   DETERMINISTIC SQL BUILDER                         β”‚
β”‚                   tools/tools.py                                    β”‚
β”‚                                                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
β”‚  β”‚ _METRIC_CONFIG (24 metrics)                               β”‚      β”‚
β”‚  β”‚ _build_metric_sql() β†’ Perfect SQL every time              β”‚      β”‚
β”‚  β”‚ _build_wow_sql() β†’ Week-over-Week with window functions   β”‚      β”‚
β”‚  β”‚ _assemble_cross_table_query() β†’ CTE-based RevPAR          β”‚      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
β”‚                                                                     β”‚
β”‚  execute_metric_query()  β†’ Deterministic (95% of queries)          β”‚
β”‚  execute_custom_sql()    β†’ LLM-generated (complex ad-hoc)          β”‚
β”‚  get_database_context()  β†’ Live schema introspection               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β–Ό                   β”‚                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   AI AGENT       β”‚          β”‚         β”‚   SUPABASE       β”‚
β”‚   agents.py      β”‚          β”‚         β”‚   PostgreSQL     β”‚
β”‚                  β”‚          β”‚         β”‚                  β”‚
β”‚  LiteLLM +       β”‚          β”‚         β”‚  dim_date        β”‚
β”‚  Native Function β”‚β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚  dim_hotels      β”‚
β”‚  Calling         β”‚                    β”‚  dim_rooms       β”‚
β”‚                  β”‚                    β”‚  fact_bookings   β”‚
β”‚  3 Tools:        │───────────────────▢│  fact_aggregated β”‚
β”‚  calculate_metrics                    β”‚  _bookings       β”‚
β”‚  run_custom_sql  β”‚                    β”‚                  β”‚
β”‚  search_metric   β”‚                    β”‚  ETL Pipeline    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β”‚  (CSVβ†’Rawβ†’Clean) β”‚
                                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

</details>

### Why This Architecture?

| Design Decision | Rationale |
|---|---|
| **Deterministic SQL Builder** | LLMs can write bad SQL. Building SQL programmatically from metric configs eliminates syntax errors for known KPIs. |
| **Two-path query strategy** | 95% of questions use the reliable builder. Only truly novel questions fall back to LLM-generated SQL. |
| **Never direct-join fact tables** | `fact_bookings` and `fact_aggregated_bookings` have different granularity. Direct joins cause row multiplication. CTEs handle cross-table metrics. |
| **Single metrics engine** | Dashboard, agent, and monitoring all use the same SQL builder. Numbers always match. |
| **Native function calling** | More reliable than text-based ReAct parsing. Structured JSON tool calls work with any LLM. |
| **Separate ETL pipeline** | Raw β†’ Clean transformation with validation. Business rules (weekend = Fri+Sat) applied at ETL layer. |

---

## πŸ“ Project Structure

```
atliq-hospitality/
β”‚
β”œβ”€β”€ agents/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── agents.py              # AI agent with native function calling
β”‚
β”œβ”€β”€ assets/
β”‚   └── images/                # ← Drop your screenshots here
β”‚       β”œβ”€β”€ dashboard_overview.png
β”‚       β”œβ”€β”€ chat_with_data.png
β”‚       β”œβ”€β”€ kpi_monitoring.png
β”‚       β”œβ”€β”€ architecture_diagram.png
β”‚       β”œβ”€β”€ db_schema_er_diagram.png
β”‚       └── etl_pipeline_output.png
β”‚
β”œβ”€β”€ etl/
β”‚   └── etl_pipeline.py        # CSV β†’ Raw DB β†’ Clean DB pipeline
β”‚
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ dashboard.py            # Executive dashboard (main page)
β”‚   └── pages/
β”‚       β”œβ”€β”€ 02_Chat_with_Data.py    # Natural language query interface
β”‚       └── 03_KPI_Monitoring.py    # Anomaly detection & health scores
β”‚
β”œβ”€β”€ prompts/
β”‚   └── cot_prompts.py          # Chain-of-thought prompt templates
β”‚
β”œβ”€β”€ tools/
β”‚   └── tools.py                # Deterministic SQL builder + execution
β”‚
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ config.py               # Configuration, schema map, metric library
β”‚   └── metrics_engine.py       # Shared metrics API (single source of truth)
β”‚
β”œβ”€β”€ .streamlit/
β”‚   └── config.toml             # Streamlit theme & server config
β”‚
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .gitignore
└── README.md
```

---

## πŸš€ Quick Start

### Prerequisites

- Python 3.11+
- Supabase account (PostgreSQL database)
- OpenRouter API key (for LLM access)

### 1. Clone & Install

```bash
git clone https://github.com/YOUR_USERNAME/atliq-hospitality.git
cd atliq-hospitality
pip install -r requirements.txt
```

### 2. Configure Environment

Create `.env` in project root:

```env
CLEAN_SUPABASE_DB_URI="postgresql://postgres:PASSWORD@db.XXXXX.supabase.co:5432/postgres"
OPENROUTER_API_KEY="sk-or-v1-XXXXXXXX"
```

### 3. Run ETL Pipeline (First Time Only)

```bash
python etl/etl_pipeline.py
```

This loads CSV data β†’ Raw DB β†’ transforms β†’ Clean DB with validation.

**Expected output:**

![ETL Pipeline Output](assets/images/etl_pipeline_output.png)
*Screenshot of a successful ETL run β€” save your terminal output as `assets/images/etl_pipeline_output.png`*

### 4. Launch Application

```bash
streamlit run frontend/dashboard.py
```

Open `http://localhost:8501` in your browser.

---

## πŸ—„οΈ Data Model

### Entity Relationship Diagram

![Database Schema β€” ER Diagram](assets/images/db_schema_er_diagram.png)
*ER diagram of the Supabase PostgreSQL schema β€” generate from pgAdmin, DBeaver, or [dbdiagram.io](https://dbdiagram.io)*

### Schema Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  dim_date   β”‚       β”‚  dim_hotels   β”‚       β”‚  dim_rooms  β”‚
│─────────────│       │──────────────│       │─────────────│
β”‚ date (PK)   β”‚       β”‚ property_id   β”‚       β”‚ room_id (PK)β”‚
β”‚ mmm_yy      β”‚       β”‚  (PK)        β”‚       β”‚ room_class  β”‚
β”‚ week_no     β”‚       β”‚ property_name β”‚       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
β”‚ day_type    β”‚       β”‚ category      β”‚              β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜       β”‚ city          β”‚              β”‚
       β”‚              β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
       β”‚                     β”‚                      β”‚
       β–Ό                     β–Ό                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ fact_aggregated_       β”‚    β”‚      fact_bookings            β”‚
β”‚     bookings           β”‚    │──────────────────────────────│
│────────────────────────│    β”‚ booking_id (PK)              β”‚
β”‚ property_id (FK)       β”‚    β”‚ property_id (FK)             β”‚
β”‚ check_in_date (FK)     β”‚    β”‚ booking_date                 β”‚
β”‚ room_category (FK)     β”‚    β”‚ check_in_date (FK)           β”‚
β”‚ successful_bookings    β”‚    β”‚ checkout_date                β”‚
β”‚ capacity               β”‚    β”‚ room_category (FK)           β”‚
β”‚                        β”‚    β”‚ booking_platform             β”‚
β”‚ Grain: property +      β”‚    β”‚ booking_status               β”‚
β”‚   date + room_type     β”‚    β”‚ revenue_generated            β”‚
β”‚   (for occupancy)      β”‚    β”‚ revenue_realized             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚ ratings_given, no_guests     β”‚
                              β”‚                              β”‚
                              β”‚ Grain: individual booking    β”‚
                              β”‚   (for revenue, ADR, etc.)   β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Key Business Rules

| Rule | Detail |
|---|---|
| **Weekend** | Friday & Saturday (stakeholder-defined, non-standard) |
| **Weekday** | Sunday through Thursday |
| **Revenue** | Always use `revenue_realized` (net after cancellation adjustments) |
| **Cancellation** | Hotel keeps 40% of `revenue_generated`, refunds 60% |
| **No Show** | Full `revenue_generated` goes to hotel |
| **Ratings** | `0` means "not rated" β€” excluded from averages |
| **week_no** | Stored as TEXT β€” always quote in SQL: `'31'` not `31` |
| **Fact table join** | NEVER direct-join both fact tables β€” different granularity, use CTEs |

### Coverage

| Dimension | Values |
|---|---|
| Date Range | May – July 2022 (92 days) |
| Cities | Delhi, Mumbai, Hyderabad, Bangalore |
| Hotel Categories | Luxury, Business |
| Room Classes | Standard, Elite, Premium, Presidential |
| Booking Platforms | MakeYourTrip, LogTrip, Tripster, Direct Online, Direct Offline, Journey, Others |
| Booking Status | Checked Out, Cancelled, No Show |
| Weeks | 19 – 32 |

---

## πŸ“Š KPI Reference

### 24 Built-in Metrics

#### Base Metrics
| Metric | Formula | Source |
|---|---|---|
| Revenue | `SUM(revenue_realized)` | fact_bookings |
| Total Bookings | `COUNT(booking_id)` | fact_bookings |
| Total Capacity | `SUM(capacity)` | fact_aggregated_bookings |
| Total Successful Bookings | `SUM(successful_bookings)` | fact_aggregated_bookings |
| Average Rating | `AVG(ratings_given) WHERE rating > 0` | fact_bookings |
| No of Days | `COUNT(DISTINCT date)` | dim_date |

#### Derived KPIs
| Metric | Formula | Description |
|---|---|---|
| **Occupancy %** | Successful Bookings / Capacity Γ— 100 | Room utilization rate |
| **ADR** | Revenue / Total Bookings | Average revenue per booking |
| **RevPAR** | Revenue / Capacity | Revenue per available room (cross-table CTE) |
| **Realisation %** | 1 βˆ’ (Cancellation% + No Show%) | Booking-to-stay conversion |
| **Cancellation %** | Cancelled / Total Bookings Γ— 100 | Booking drop-off rate |
| **No Show Rate** | No Shows / Total Bookings Γ— 100 | Ghost booking rate |
| **DBRN** | Total Bookings / No of Days | Daily booked room nights |
| **DSRN** | Total Capacity / No of Days | Daily sellable room nights |
| **DURN** | Checked Out / No of Days | Daily utilized room nights |

#### Week-over-Week (WoW) Metrics
| Metric | Calculation |
|---|---|
| Revenue WoW | (Current Week / Previous Week) βˆ’ 1 |
| Occupancy WoW | Same pattern |
| ADR WoW | Same pattern |
| RevPAR WoW | Same pattern (cross-table) |
| Realisation WoW | Same pattern |
| DSRN WoW | Same pattern |

#### Breakdown Metrics
| Metric | Description |
|---|---|
| Booking % by Platform | Each platform's share of total bookings |
| Booking % by Room Class | Each room class's share of total bookings |

---

## πŸ€– AI Agent β€” How It Works

### Tool Calling Flow

```
User: "What is the RevPAR for luxury hotels in Mumbai?"
  β”‚
  β–Ό
LLM understands intent
  β”‚
  β–Ό
LLM calls: calculate_metrics({
    metrics: ["revpar"],
    filters: { city: "Mumbai", category: "Luxury" }
  })
  β”‚
  β–Ό
Python: _build_metric_sql("revpar", filters)
  β†’ Generates CTE query (cross-table)
  β†’ Executes against Supabase
  β†’ Returns DataFrame
  β”‚
  β–Ό
LLM receives data, formats business answer:
  "RevPAR for luxury hotels in Mumbai: β‚Ή10,234
   This is 15% above the portfolio average..."
```

### Three Tools

| Tool | Purpose | Reliability |
|---|---|---|
| `calculate_metrics` | 24 built-in KPIs with filters & grouping | βœ… 100% (deterministic SQL) |
| `run_custom_sql` | Complex ad-hoc queries (rankings, correlations) | ⚠️ 85-95% (LLM-generated SQL) |
| `search_metric` | Find correct metric name from business concept | βœ… 100% (alias matching) |

### Example Queries the Agent Handles

```
Simple:     "What is the total revenue?"
Filtered:   "Occupancy rate for Delhi luxury hotels in week 27"
Grouped:    "Revenue breakdown by city"
Comparison: "Weekend vs weekday ADR"
WoW:        "How did RevPAR change week over week for week 31?"
Ranking:    "Top 5 hotels by revenue in Mumbai"
Complex:    "For each city, identify the hotel with lowest RevPAR
             in week 27, show the gap to city average"
```

---

## πŸ” Anomaly Detection System

### 4-Layer Alert Engine

```
Layer 1: THRESHOLD ALERTS
  └─ Each KPI checked against configurable warning/critical levels
     Example: Occupancy < 45% β†’ πŸ”΄ Critical

Layer 2: WEEK-OVER-WEEK ALERTS
  └─ Detects significant WoW drops
     Example: Revenue dropped -12% WoW β†’ πŸ”΄ Critical

Layer 3: TREND ALERTS
  └─ Detects consecutive declining weeks
     Example: ADR declined 4 weeks straight β†’ πŸ”΄ Critical

Layer 4: PROPERTY ANOMALY DETECTION (Z-Score)
  └─ Flags properties deviating from portfolio mean
     Example: Hotel X occupancy z-score = -2.3 β†’ πŸ”΄ Critical
```

### Property Health Scoring

```
Score = Weighted average of normalized KPI performance

Weights:
  Occupancy %    β†’ 25%
  RevPAR         β†’ 25%
  Avg Rating     β†’ 20%
  ADR            β†’ 15%
  Realisation %  β†’ 15%

Score β‰₯ 80 β†’ 🟒 Healthy
Score 60-79 β†’ 🟑 Concern
Score < 60  β†’ πŸ”΄ Critical
```

---

## πŸ› οΈ Tech Stack

| Component | Technology |
|---|---|
| **Frontend** | Streamlit 1.30+ |
| **Visualization** | Plotly (interactive charts) |
| **Database** | Supabase (managed PostgreSQL) |
| **ETL** | Python + pandas + psycopg2 |
| **AI/LLM** | LiteLLM + OpenRouter (model-agnostic) |
| **LLM Model** | Grok 4.1 Fast (swappable) |
| **SQL Builder** | Custom deterministic engine |
| **Deployment** | Streamlit Community Cloud |
| **Version Control** | Git + GitHub |

---

## πŸ”’ Security

- Database credentials stored as environment secrets (never in code)
- Streamlit Cloud secrets encrypted at rest
- Read-only database user recommended for production
- SQL injection prevention: parameterized queries + SELECT/WITH-only enforcement
- Both fact tables direct-join blocked to prevent data corruption

---

## πŸ“ˆ Performance

| Metric | Value |
|---|---|
| KPI card load time | ~2-3 seconds (6 metrics Γ— individual queries) |
| Agent response time | 3-8 seconds (depends on query complexity) |
| Dashboard full render | ~5 seconds (with caching) |
| Cache TTL | 60 seconds (metrics), 300 seconds (context) |
| Max agent iterations | 5 tool calls per question |
| Supported concurrent users | Limited by Streamlit Cloud free tier |

---

## πŸ§ͺ Testing & Validation

All KPI calculations verified against a Power BI dashboard built on the same dataset.

| Test Category | Queries Tested | Pass Rate |
|---|---|---|
| Single KPI (no filter) | 24 | 100% |
| Single KPI + filters | 50+ | 100% |
| Multi-metric grouped | 30+ | 100% |
| WoW calculations | 12 | 100% |
| Cross-table (RevPAR) | 15 | 100% |
| Complex rankings (LLM SQL) | 10 | ~85% |

---

## πŸ—ΊοΈ Roadmap

- [ ] Conversation memory for multi-turn chat
- [ ] SQL validation layer for LLM-generated queries
- [ ] Export query results as CSV/Excel
- [ ] Scheduled KPI monitoring with email alerts
- [ ] LLM fallback chain (try multiple models)
- [ ] Query logging and analytics
- [ ] Role-based access control
- [ ] Mobile-responsive dashboard



---

## πŸ“„ License

This project is licensed under the MIT License β€” see the [LICENSE](LICENSE) file for details.

---

<p align="center">
  Built with ❀️ for data-driven hospitality management
</p>