-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
221 lines (176 loc) · 7.37 KB
/
Copy pathmain.py
File metadata and controls
221 lines (176 loc) · 7.37 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
"""
SkySync 2.0 — FastAPI backend
Run: uvicorn main:app --reload --host 127.0.0.1 --port 8000
"""
import os
from datetime import datetime
from typing import List, Optional
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, HTTPException, Depends, BackgroundTasks
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from starlette.requests import Request
from sqlalchemy.orm import Session
from pydantic import BaseModel
from database import engine, get_db, Base
from models import Tracker, PriceSnapshot
from analytics import get_buy_signal
from scheduler import (
start_scheduler,
stop_scheduler,
schedule_tracker,
unschedule_tracker,
trigger_scan_async,
trigger_all_scans_async,
)
# Create DB tables on startup
Base.metadata.create_all(bind=engine)
app = FastAPI(title="SkySync", version="2.0.0")
templates = Jinja2Templates(directory="templates")
# ── Lifecycle ─────────────────────────────────────────────────────────────────
@app.on_event("startup")
async def on_startup():
start_scheduler()
@app.on_event("shutdown")
async def on_shutdown():
stop_scheduler()
# ── Frontend ──────────────────────────────────────────────────────────────────
@app.get("/", response_class=HTMLResponse)
async def dashboard(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
# ── Pydantic schemas ──────────────────────────────────────────────────────────
class TrackerCreate(BaseModel):
name: str
origin: str # IATA e.g. "TLV"
destination: str # IATA e.g. "NRT"
duration_min: int = 7
duration_max: int = 14
window_start: str # "YYYY-MM-DD"
window_end: str # "YYYY-MM-DD"
target_price: Optional[float] = None
scan_interval_hours: int = 6
baggage_required: bool = False
alert_email: Optional[str] = None
currency: str = "USD"
class TrackerOut(BaseModel):
id: int
name: str
origin: str
destination: str
duration_min: int
duration_max: int
window_start: str
window_end: str
target_price: Optional[float]
scan_interval_hours: int
baggage_required: bool
alert_email: Optional[str]
active: bool
created_at: datetime
last_scanned: Optional[datetime]
current_best_price: Optional[float]
previous_best_price: Optional[float]
currency: str
class Config:
from_attributes = True
# ── Tracker CRUD ──────────────────────────────────────────────────────────────
@app.get("/api/trackers", response_model=List[TrackerOut])
def list_trackers(db: Session = Depends(get_db)):
return (
db.query(Tracker)
.filter(Tracker.active == True)
.order_by(Tracker.created_at.desc())
.all()
)
@app.post("/api/trackers", response_model=TrackerOut, status_code=201)
def create_tracker(data: TrackerCreate, db: Session = Depends(get_db)):
tracker = Tracker(**data.model_dump())
db.add(tracker)
db.commit()
db.refresh(tracker)
# Schedule periodic scans
schedule_tracker(tracker.id, tracker.scan_interval_hours)
# Immediate first scan
trigger_scan_async(tracker.id, trigger_source="initial")
return tracker
@app.delete("/api/trackers/{tracker_id}", status_code=204)
def delete_tracker(tracker_id: int, db: Session = Depends(get_db)):
tracker = db.query(Tracker).filter(Tracker.id == tracker_id).first()
if not tracker:
raise HTTPException(404, "Tracker not found")
tracker.active = False
unschedule_tracker(tracker_id)
db.commit()
@app.patch("/api/trackers/{tracker_id}", response_model=TrackerOut)
def update_tracker(tracker_id: int, data: TrackerCreate, db: Session = Depends(get_db)):
tracker = db.query(Tracker).filter(Tracker.id == tracker_id, Tracker.active == True).first()
if not tracker:
raise HTTPException(404, "Tracker not found")
for k, v in data.model_dump().items():
setattr(tracker, k, v)
db.commit()
db.refresh(tracker)
schedule_tracker(tracker.id, tracker.scan_interval_hours)
return tracker
# ── Scan control ──────────────────────────────────────────────────────────────
@app.post("/api/trackers/{tracker_id}/scan")
def manual_scan(tracker_id: int, db: Session = Depends(get_db)):
tracker = db.query(Tracker).filter(Tracker.id == tracker_id, Tracker.active == True).first()
if not tracker:
raise HTTPException(404, "Tracker not found")
trigger_scan_async(tracker_id, trigger_source="manual")
return {"status": "scanning", "tracker_id": tracker_id}
@app.post("/api/trackers/scan-all")
def manual_scan_all():
queued = trigger_all_scans_async()
return {"status": "scanning", "trackers_queued": queued}
# ── History & analytics ───────────────────────────────────────────────────────
@app.get("/api/trackers/{tracker_id}/history")
def get_history(tracker_id: int, db: Session = Depends(get_db)):
tracker = db.query(Tracker).filter(Tracker.id == tracker_id).first()
if not tracker:
raise HTTPException(404, "Tracker not found")
snaps = (
db.query(PriceSnapshot)
.filter(PriceSnapshot.tracker_id == tracker_id)
.order_by(PriceSnapshot.scanned_at.asc())
.all()
)
history = [
{
"id": s.id,
"timestamp": s.scanned_at.isoformat(),
"total_price": s.total_price,
"outbound_price": s.outbound_price,
"return_price": s.return_price,
"outbound_date": s.outbound_date,
"return_date": s.return_date,
"source": s.source,
"booking_url": s.booking_url,
"baggage_included": s.baggage_included,
"fare_class": s.fare_class,
"currency": s.currency or tracker.currency,
}
for s in snaps
]
prices = [s.total_price for s in snaps]
signal = get_buy_signal(prices)
return {"history": history, "signal": signal, "tracker": TrackerOut.model_validate(tracker).model_dump()}
@app.get("/api/stats")
def get_stats(db: Session = Depends(get_db)):
"""Dashboard-level aggregate stats."""
active_count = db.query(Tracker).filter(Tracker.active == True).count()
snap_count = db.query(PriceSnapshot).count()
# Trackers that are below target
alerts_triggered = 0
trackers = db.query(Tracker).filter(Tracker.active == True, Tracker.target_price != None).all()
for t in trackers:
if t.current_best_price and t.current_best_price <= t.target_price:
alerts_triggered += 1
return {
"active_trackers": active_count,
"total_scans": snap_count,
"alerts_triggered": alerts_triggered,
}