Overflights implementation

This commit is contained in:
2025-12-19 05:07:46 -05:00
parent 658d4c4ff8
commit b46a88d471
10 changed files with 959 additions and 5 deletions

View File

@@ -173,12 +173,43 @@ def upgrade() -> None:
# Create indexes for circuits # Create indexes for circuits
op.create_index('idx_circuit_local_flight_id', 'circuits', ['local_flight_id']) op.create_index('idx_circuit_local_flight_id', 'circuits', ['local_flight_id'])
op.create_index('idx_circuit_timestamp', 'circuits', ['circuit_timestamp']) op.create_index('idx_circuit_timestamp', 'circuits', ['circuit_timestamp'])
# Create overflights table for tracking aircraft talking to the tower but not departing/landing
op.create_table('overflights',
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
sa.Column('registration', sa.String(length=16), nullable=False),
sa.Column('pob', sa.Integer(), nullable=True),
sa.Column('type', sa.String(length=32), nullable=True),
sa.Column('departure_airfield', sa.String(length=64), nullable=True),
sa.Column('destination_airfield', sa.String(length=64), nullable=True),
sa.Column('status', sa.Enum('ACTIVE', 'INACTIVE', 'CANCELLED', name='overflightstatus'), nullable=False, server_default='ACTIVE'),
sa.Column('call_dt', sa.DateTime(), nullable=False),
sa.Column('qsy_dt', sa.DateTime(), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('created_dt', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
sa.Column('created_by', sa.String(length=16), nullable=True),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), nullable=False),
sa.PrimaryKeyConstraint('id'),
mysql_engine='InnoDB',
mysql_charset='utf8mb4',
mysql_collate='utf8mb4_unicode_ci'
)
# Create indexes for overflights
op.create_index('idx_ovf_registration', 'overflights', ['registration'])
op.create_index('idx_ovf_departure_airfield', 'overflights', ['departure_airfield'])
op.create_index('idx_ovf_destination_airfield', 'overflights', ['destination_airfield'])
op.create_index('idx_ovf_status', 'overflights', ['status'])
op.create_index('idx_ovf_call_dt', 'overflights', ['call_dt'])
op.create_index('idx_ovf_created_dt', 'overflights', ['created_dt'])
op.create_index('idx_ovf_created_by', 'overflights', ['created_by'])
def downgrade() -> None: def downgrade() -> None:
""" """
Drop the circuits, arrivals, departures, and local_flights tables. Drop the overflights, circuits, arrivals, departures, and local_flights tables.
""" """
op.drop_table('overflights')
op.drop_table('circuits') op.drop_table('circuits')
op.drop_table('arrivals') op.drop_table('arrivals')
op.drop_table('departures') op.drop_table('departures')

View File

@@ -1,5 +1,5 @@
from fastapi import APIRouter from fastapi import APIRouter
from app.api.endpoints import auth, pprs, public, aircraft, airport, local_flights, departures, arrivals, circuits, journal from app.api.endpoints import auth, pprs, public, aircraft, airport, local_flights, departures, arrivals, circuits, journal, overflights
api_router = APIRouter() api_router = APIRouter()
@@ -8,6 +8,7 @@ api_router.include_router(pprs.router, prefix="/pprs", tags=["pprs"])
api_router.include_router(local_flights.router, prefix="/local-flights", tags=["local_flights"]) api_router.include_router(local_flights.router, prefix="/local-flights", tags=["local_flights"])
api_router.include_router(departures.router, prefix="/departures", tags=["departures"]) api_router.include_router(departures.router, prefix="/departures", tags=["departures"])
api_router.include_router(arrivals.router, prefix="/arrivals", tags=["arrivals"]) api_router.include_router(arrivals.router, prefix="/arrivals", tags=["arrivals"])
api_router.include_router(overflights.router, prefix="/overflights", tags=["overflights"])
api_router.include_router(circuits.router, prefix="/circuits", tags=["circuits"]) api_router.include_router(circuits.router, prefix="/circuits", tags=["circuits"])
api_router.include_router(journal.router, prefix="/journal", tags=["journal"]) api_router.include_router(journal.router, prefix="/journal", tags=["journal"])
api_router.include_router(public.router, prefix="/public", tags=["public"]) api_router.include_router(public.router, prefix="/public", tags=["public"])

View File

@@ -0,0 +1,206 @@
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, status, Request
from sqlalchemy.orm import Session
from datetime import date
from app.api.deps import get_db, get_current_read_user, get_current_operator_user
from app.crud.crud_overflight import overflight as crud_overflight
from app.schemas.overflight import Overflight, OverflightCreate, OverflightUpdate, OverflightStatus, OverflightStatusUpdate
from app.models.ppr import User
from app.core.utils import get_client_ip
router = APIRouter()
@router.get("/", response_model=List[Overflight])
async def get_overflights(
request: Request,
skip: int = 0,
limit: int = 100,
status: Optional[OverflightStatus] = None,
date_from: Optional[date] = None,
date_to: Optional[date] = None,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_read_user)
):
"""Get overflight records with optional filtering"""
overflights = crud_overflight.get_multi(
db, skip=skip, limit=limit, status=status,
date_from=date_from, date_to=date_to
)
return overflights
@router.post("/", response_model=Overflight)
async def create_overflight(
request: Request,
overflight_in: OverflightCreate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_operator_user)
):
"""Create a new overflight record"""
overflight = crud_overflight.create(db, obj_in=overflight_in, created_by=current_user.username)
# Send real-time update via WebSocket
if hasattr(request.app.state, 'connection_manager'):
await request.app.state.connection_manager.broadcast({
"type": "overflight_created",
"data": {
"id": overflight.id,
"registration": overflight.registration,
"departure_airfield": overflight.departure_airfield,
"destination_airfield": overflight.destination_airfield,
"status": overflight.status.value
}
})
return overflight
@router.get("/{overflight_id}", response_model=Overflight)
async def get_overflight(
overflight_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_read_user)
):
"""Get a specific overflight record"""
overflight = crud_overflight.get(db, overflight_id=overflight_id)
if not overflight:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Overflight record not found"
)
return overflight
@router.put("/{overflight_id}", response_model=Overflight)
async def update_overflight(
request: Request,
overflight_id: int,
overflight_in: OverflightUpdate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_operator_user)
):
"""Update an overflight record"""
db_overflight = crud_overflight.get(db, overflight_id=overflight_id)
if not db_overflight:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Overflight record not found"
)
# Get user IP from request
user_ip = request.client.host if request.client else None
overflight = crud_overflight.update(
db,
db_obj=db_overflight,
obj_in=overflight_in,
user=current_user.username,
user_ip=user_ip
)
# Send real-time update
if hasattr(request.app.state, 'connection_manager'):
await request.app.state.connection_manager.broadcast({
"type": "overflight_updated",
"data": {
"id": overflight.id,
"registration": overflight.registration,
"status": overflight.status.value
}
})
return overflight
@router.patch("/{overflight_id}/status", response_model=Overflight)
async def update_overflight_status(
request: Request,
overflight_id: int,
status_update: OverflightStatusUpdate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_operator_user)
):
"""Update overflight status (ACTIVE -> INACTIVE for QSY)"""
client_ip = get_client_ip(request)
overflight = crud_overflight.update_status(
db,
overflight_id=overflight_id,
status=status_update.status,
timestamp=status_update.timestamp if hasattr(status_update, 'timestamp') else None,
user=current_user.username,
user_ip=client_ip
)
if not overflight:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Overflight record not found"
)
# Send real-time update
if hasattr(request.app.state, 'connection_manager'):
await request.app.state.connection_manager.broadcast({
"type": "overflight_status_update",
"data": {
"id": overflight.id,
"registration": overflight.registration,
"status": overflight.status.value,
"qsy_dt": overflight.qsy_dt.isoformat() if overflight.qsy_dt else None
}
})
return overflight
@router.delete("/{overflight_id}", response_model=Overflight)
async def cancel_overflight(
request: Request,
overflight_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_operator_user)
):
"""Cancel an overflight record"""
client_ip = get_client_ip(request)
overflight = crud_overflight.cancel(
db,
overflight_id=overflight_id,
user=current_user.username,
user_ip=client_ip
)
if not overflight:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Overflight record not found"
)
# Send real-time update
if hasattr(request.app.state, 'connection_manager'):
await request.app.state.connection_manager.broadcast({
"type": "overflight_cancelled",
"data": {
"id": overflight.id,
"registration": overflight.registration
}
})
return overflight
@router.get("/active/list", response_model=List[Overflight])
async def get_active_overflights(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_read_user)
):
"""Get currently active overflights"""
overflights = crud_overflight.get_active_overflights(db)
return overflights
@router.get("/today/list", response_model=List[Overflight])
async def get_overflights_today(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_read_user)
):
"""Get today's overflights"""
overflights = crud_overflight.get_overflights_today(db)
return overflights

View File

@@ -0,0 +1,172 @@
from typing import List, Optional
from sqlalchemy.orm import Session
from sqlalchemy import and_, or_, func, desc
from datetime import date, datetime
from app.models.overflight import Overflight, OverflightStatus
from app.schemas.overflight import OverflightCreate, OverflightUpdate, OverflightStatusUpdate
from app.models.journal import EntityType
from app.crud.crud_journal import journal
class CRUDOverflight:
def get(self, db: Session, overflight_id: int) -> Optional[Overflight]:
return db.query(Overflight).filter(Overflight.id == overflight_id).first()
def get_multi(
self,
db: Session,
skip: int = 0,
limit: int = 100,
status: Optional[OverflightStatus] = None,
date_from: Optional[date] = None,
date_to: Optional[date] = None
) -> List[Overflight]:
query = db.query(Overflight)
if status:
query = query.filter(Overflight.status == status)
if date_from:
query = query.filter(func.date(Overflight.created_dt) >= date_from)
if date_to:
query = query.filter(func.date(Overflight.created_dt) <= date_to)
return query.order_by(desc(Overflight.created_dt)).offset(skip).limit(limit).all()
def get_active_overflights(self, db: Session) -> List[Overflight]:
"""Get currently active overflights"""
return db.query(Overflight).filter(
Overflight.status == OverflightStatus.ACTIVE
).order_by(desc(Overflight.created_dt)).all()
def get_overflights_today(self, db: Session) -> List[Overflight]:
"""Get today's overflights"""
today = date.today()
return db.query(Overflight).filter(
func.date(Overflight.created_dt) == today
).order_by(Overflight.created_dt).all()
def create(self, db: Session, obj_in: OverflightCreate, created_by: str) -> Overflight:
db_obj = Overflight(
**obj_in.dict(),
created_by=created_by,
status=OverflightStatus.ACTIVE
)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
# Log creation in journal
journal.log_change(
db,
EntityType.OVERFLIGHT,
db_obj.id,
f"Overflight created: {obj_in.registration} from {obj_in.departure_airfield} to {obj_in.destination_airfield}",
created_by,
None
)
return db_obj
def update(self, db: Session, db_obj: Overflight, obj_in: OverflightUpdate, user: str = "system", user_ip: Optional[str] = None) -> Overflight:
from datetime import datetime as dt
update_data = obj_in.dict(exclude_unset=True)
changes = []
for field, value in update_data.items():
old_value = getattr(db_obj, field)
# Normalize datetime values for comparison (ignore timezone differences)
if isinstance(old_value, dt) and isinstance(value, dt):
old_normalized = old_value.replace(tzinfo=None) if old_value.tzinfo else old_value
new_normalized = value.replace(tzinfo=None) if value.tzinfo else value
if old_normalized == new_normalized:
continue
if old_value != value:
changes.append(f"{field} changed from '{old_value}' to '{value}'")
setattr(db_obj, field, value)
if changes:
db.add(db_obj)
db.commit()
db.refresh(db_obj)
# Log changes in journal
for change in changes:
journal.log_change(
db,
EntityType.OVERFLIGHT,
db_obj.id,
change,
user,
user_ip
)
return db_obj
def update_status(
self,
db: Session,
overflight_id: int,
status: OverflightStatus,
timestamp: Optional[datetime] = None,
user: str = "system",
user_ip: Optional[str] = None
) -> Optional[Overflight]:
db_obj = self.get(db, overflight_id)
if not db_obj:
return None
# Ensure status is an OverflightStatus enum
if isinstance(status, str):
status = OverflightStatus(status)
old_status = db_obj.status
db_obj.status = status
# Set timestamp if transitioning to INACTIVE (QSY'd)
current_time = timestamp if timestamp is not None else datetime.utcnow()
if status == OverflightStatus.INACTIVE:
db_obj.qsy_dt = current_time
db.add(db_obj)
db.commit()
db.refresh(db_obj)
# Log status change in journal
journal.log_change(
db,
EntityType.OVERFLIGHT,
overflight_id,
f"Status changed from {old_status.value} to {status.value}",
user,
user_ip
)
return db_obj
def cancel(self, db: Session, overflight_id: int, user: str = "system", user_ip: Optional[str] = None) -> Optional[Overflight]:
db_obj = self.get(db, overflight_id)
if db_obj:
old_status = db_obj.status
db_obj.status = OverflightStatus.CANCELLED
db.add(db_obj)
db.commit()
db.refresh(db_obj)
# Log cancellation in journal
journal.log_change(
db,
EntityType.OVERFLIGHT,
overflight_id,
f"Status changed from {old_status.value} to CANCELLED",
user,
user_ip
)
return db_obj
overflight = CRUDOverflight()

View File

@@ -10,6 +10,7 @@ class EntityType(str, PyEnum):
LOCAL_FLIGHT = "LOCAL_FLIGHT" LOCAL_FLIGHT = "LOCAL_FLIGHT"
ARRIVAL = "ARRIVAL" ARRIVAL = "ARRIVAL"
DEPARTURE = "DEPARTURE" DEPARTURE = "DEPARTURE"
OVERFLIGHT = "OVERFLIGHT"
class JournalEntry(Base): class JournalEntry(Base):

View File

@@ -0,0 +1,28 @@
from sqlalchemy import Column, Integer, String, DateTime, Text, Enum as SQLEnum, BigInteger
from sqlalchemy.sql import func
from enum import Enum
from app.db.session import Base
class OverflightStatus(str, Enum):
ACTIVE = "ACTIVE"
INACTIVE = "INACTIVE"
CANCELLED = "CANCELLED"
class Overflight(Base):
__tablename__ = "overflights"
id = Column(BigInteger, primary_key=True, autoincrement=True)
registration = Column(String(16), nullable=False, index=True)
pob = Column(Integer, nullable=True) # Persons on board
type = Column(String(32), nullable=True) # Aircraft type
departure_airfield = Column(String(64), nullable=True, index=True) # Airfield they departed from
destination_airfield = Column(String(64), nullable=True, index=True) # Where they're heading
status = Column(SQLEnum(OverflightStatus), nullable=False, default=OverflightStatus.ACTIVE, index=True)
call_dt = Column(DateTime, nullable=False, index=True) # Time of initial call
qsy_dt = Column(DateTime, nullable=True) # Time of frequency change (QSY)
notes = Column(Text, nullable=True)
created_dt = Column(DateTime, nullable=False, server_default=func.current_timestamp(), index=True)
created_by = Column(String(16), nullable=True, index=True)
updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())

View File

@@ -0,0 +1,106 @@
from pydantic import BaseModel, validator
from datetime import datetime
from typing import Optional
from enum import Enum
class OverflightStatus(str, Enum):
ACTIVE = "ACTIVE"
INACTIVE = "INACTIVE"
CANCELLED = "CANCELLED"
class OverflightBase(BaseModel):
registration: str # Using registration as callsign
pob: Optional[int] = None
type: Optional[str] = None # Aircraft type
departure_airfield: Optional[str] = None
destination_airfield: Optional[str] = None
call_dt: datetime # Time of initial call
qsy_dt: Optional[datetime] = None # Time of frequency change
notes: Optional[str] = None
@validator('registration')
def validate_registration(cls, v):
if not v or len(v.strip()) == 0:
raise ValueError('Aircraft registration is required')
return v.strip().upper()
@validator('type')
def validate_type(cls, v):
if v and len(v.strip()) > 0:
return v.strip()
return v
@validator('departure_airfield')
def validate_departure_airfield(cls, v):
if v and len(v.strip()) > 0:
return v.strip().upper()
return v
@validator('destination_airfield')
def validate_destination_airfield(cls, v):
if v and len(v.strip()) > 0:
return v.strip().upper()
return v
@validator('pob')
def validate_pob(cls, v):
if v is not None and v < 1:
raise ValueError('Persons on board must be at least 1')
return v
class OverflightCreate(OverflightBase):
pass
class OverflightUpdate(BaseModel):
callsign: Optional[str] = None
pob: Optional[int] = None
type: Optional[str] = None
departure_airfield: Optional[str] = None
destination_airfield: Optional[str] = None
call_dt: Optional[datetime] = None
qsy_dt: Optional[datetime] = None
status: Optional[OverflightStatus] = None
notes: Optional[str] = None
@validator('type')
def validate_type(cls, v):
if v is not None and len(v.strip()) == 0:
return None
return v.strip() if v else v
@validator('departure_airfield')
def validate_departure_airfield(cls, v):
if v is not None and len(v.strip()) == 0:
return None
return v.strip().upper() if v else v
@validator('destination_airfield')
def validate_destination_airfield(cls, v):
if v is not None and len(v.strip()) == 0:
return None
return v.strip().upper() if v else v
@validator('pob')
def validate_pob(cls, v):
if v is not None and v < 1:
raise ValueError('Persons on board must be at least 1')
return v
class OverflightStatusUpdate(BaseModel):
status: OverflightStatus
class Overflight(OverflightBase):
id: int
status: OverflightStatus
created_dt: datetime
created_by: Optional[str] = None
updated_at: datetime
class Config:
from_attributes = True

View File

@@ -96,6 +96,15 @@ body {
background-color: #2980b9; background-color: #2980b9;
} }
.btn-secondary {
background-color: #95a5a6;
color: white;
}
.btn-secondary:hover {
background-color: #7f8c8d;
}
.btn-danger { .btn-danger {
background-color: #e74c3c; background-color: #e74c3c;
color: white; color: white;

View File

@@ -22,6 +22,9 @@
<button class="btn btn-info" onclick="openBookInModal()"> <button class="btn btn-info" onclick="openBookInModal()">
🛬 Book In 🛬 Book In
</button> </button>
<button class="btn btn-secondary" onclick="openOverflightModal()">
🔄 Overflight
</button>
<button class="btn btn-primary" onclick="window.location.href = '/reports'"> <button class="btn btn-primary" onclick="window.location.href = '/reports'">
📊 Reports 📊 Reports
</button> </button>
@@ -117,6 +120,46 @@
<p>No aircraft currently landed and ready to depart.</p> <p>No aircraft currently landed and ready to depart.</p>
</div> </div>
</div> </div>
<!-- Overflights Table -->
<div class="ppr-table" style="margin-top: 2rem;">
<div class="table-header">
<div style="display: flex; justify-content: space-between; align-items: center;">
<span>🔄 Active Overflights - <span id="overflights-count">0</span></span>
<span class="info-icon" onclick="showTableHelp('overflights')" title="What is this?"></span>
</div>
</div>
<div id="overflights-loading" class="loading">
<div class="spinner"></div>
Loading overflights...
</div>
<div id="overflights-table-content" style="display: none;">
<table>
<thead>
<tr>
<th>Registration</th>
<th style="width: 30px; text-align: center;"></th>
<th>Callsign</th>
<th>Type</th>
<th>From</th>
<th>To</th>
<th>Called</th>
<th>POB</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="overflights-table-body">
</tbody>
</table>
</div>
<div id="overflights-no-data" class="no-data" style="display: none;">
<h3>No Active Overflights</h3>
</div>
</div>
<br> <br>
<!-- Departed and Parked Tables --> <!-- Departed and Parked Tables -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-top: 1rem;"> <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-top: 1rem;">
@@ -598,6 +641,64 @@
</div> </div>
</div> </div>
<!-- Overflight Modal -->
<div id="overflightModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<h2>Register Overflight</h2>
<button class="close" onclick="closeOverflightModal()">&times;</button>
</div>
<div class="modal-body">
<form id="overflight-form">
<input type="hidden" id="overflight-id" name="id">
<div class="form-grid">
<div class="form-group">
<label for="overflight_registration">Callsign/Registration *</label>
<input type="text" id="overflight_registration" name="registration" required oninput="handleOverflightAircraftLookup(this.value)" tabindex="1">
<div id="overflight-aircraft-lookup-results"></div>
</div>
<div class="form-group">
<label for="overflight_type">Aircraft Type</label>
<input type="text" id="overflight_type" name="type" placeholder="e.g., C172, PA34, AA5" tabindex="2">
</div>
<div class="form-group">
<label for="overflight_pob">Persons on Board</label>
<input type="number" id="overflight_pob" name="pob" min="1" tabindex="3">
</div>
<div class="form-group">
<label for="overflight_departure_airfield">Departure Airfield</label>
<input type="text" id="overflight_departure_airfield" name="departure_airfield" placeholder="ICAO Code or Airport Name" oninput="handleOverflightDepartureAirportLookup(this.value)" tabindex="4">
<div id="overflight-departure-airport-lookup-results"></div>
</div>
<div class="form-group">
<label for="overflight_destination_airfield">Destination Airfield</label>
<input type="text" id="overflight_destination_airfield" name="destination_airfield" placeholder="ICAO Code or Airport Name" oninput="handleOverflightDestinationAirportLookup(this.value)" tabindex="5">
<div id="overflight-destination-airport-lookup-results"></div>
</div>
<div class="form-group">
<label for="overflight_call_dt">Time of Call *</label>
<input type="datetime-local" id="overflight_call_dt" name="call_dt" required tabindex="6">
</div>
<div class="form-group full-width">
<label for="overflight_notes">Notes</label>
<textarea id="overflight_notes" name="notes" rows="3" placeholder="e.g., flight plan, special remarks" tabindex="7"></textarea>
</div>
</div>
<div class="form-actions">
<button type="button" class="btn btn-info" onclick="closeOverflightModal()">
Close
</button>
<button type="submit" class="btn btn-success">
🔄 Register Overflight
</button>
</div>
</form>
</div>
</div>
</div>
<!-- Departure Edit Modal --> <!-- Departure Edit Modal -->
<div id="departureEditModal" class="modal"> <div id="departureEditModal" class="modal">
<div class="modal-content"> <div class="modal-content">
@@ -1226,6 +1327,13 @@
closeBookInModal(); closeBookInModal();
return; return;
} }
// Press 'Escape' to close Overflight modal if it's open (allow even when typing in inputs)
if (e.key === 'Escape' && document.getElementById('overflightModal').style.display === 'block') {
e.preventDefault();
closeOverflightModal();
return;
}
// Press 'Escape' to close local flight edit modal if it's open (allow even when typing in inputs) // Press 'Escape' to close local flight edit modal if it's open (allow even when typing in inputs)
if (e.key === 'Escape' && document.getElementById('localFlightEditModal').style.display === 'block') { if (e.key === 'Escape' && document.getElementById('localFlightEditModal').style.display === 'block') {
@@ -1259,12 +1367,18 @@
openNewPPRModal(); openNewPPRModal();
} }
// Press 'o' to book out local flight (LOCAL type) // Press 'l' to book out local flight (LOCAL type)
if (e.key === 'o' || e.key === 'O') { if (e.key === 'l' || e.key === 'L') {
e.preventDefault(); e.preventDefault();
openLocalFlightModal('LOCAL'); openLocalFlightModal('LOCAL');
} }
// Press 'o' to open overflight modal
if (e.key === 'o' || e.key === 'O') {
e.preventDefault();
openOverflightModal();
}
// Press 'c' to book out circuits // Press 'c' to book out circuits
if (e.key === 'c' || e.key === 'C') { if (e.key === 'c' || e.key === 'C') {
e.preventDefault(); e.preventDefault();
@@ -1414,7 +1528,7 @@
loadPPRsTimeout = setTimeout(async () => { loadPPRsTimeout = setTimeout(async () => {
// Load all tables simultaneously // Load all tables simultaneously
await Promise.all([loadArrivals(), loadDepartures(), loadDeparted(), loadParked(), loadUpcoming()]); await Promise.all([loadArrivals(), loadDepartures(), loadOverflights(), loadDeparted(), loadParked(), loadUpcoming()]);
loadPPRsTimeout = null; loadPPRsTimeout = null;
}, 100); // Wait 100ms before executing to batch multiple calls }, 100); // Wait 100ms before executing to batch multiple calls
} }
@@ -1567,6 +1681,79 @@
document.getElementById('departures-loading').style.display = 'none'; document.getElementById('departures-loading').style.display = 'none';
} }
// Load overflights (ACTIVE status only)
async function loadOverflights() {
document.getElementById('overflights-loading').style.display = 'block';
document.getElementById('overflights-table-content').style.display = 'none';
document.getElementById('overflights-no-data').style.display = 'none';
try {
const response = await authenticatedFetch('/api/v1/overflights/?status=ACTIVE&limit=100');
if (!response.ok) {
throw new Error('Failed to fetch overflights');
}
const overflights = await response.json();
displayOverflights(overflights);
} catch (error) {
console.error('Error loading overflights:', error);
if (error.message !== 'Session expired. Please log in again.') {
showNotification('Error loading overflights', true);
}
}
document.getElementById('overflights-loading').style.display = 'none';
}
function displayOverflights(overflights) {
const tbody = document.getElementById('overflights-table-body');
document.getElementById('overflights-count').textContent = overflights.length;
if (overflights.length === 0) {
document.getElementById('overflights-no-data').style.display = 'block';
return;
}
// Sort by call_dt most recent
overflights.sort((a, b) => new Date(b.call_dt) - new Date(a.call_dt));
tbody.innerHTML = '';
for (const flight of overflights) {
const row = document.createElement('tr');
row.style.cursor = 'pointer';
row.onclick = () => {
openOverflightEditModal(flight.id);
};
const statusBadge = flight.status === 'ACTIVE' ?
'<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 3px; font-size: 0.8rem;">ACTIVE</span>' :
'<span style="background-color: #6c757d; color: white; padding: 2px 6px; border-radius: 3px; font-size: 0.8rem;">QSY\'D</span>';
row.innerHTML = `
<td>${flight.registration || '-'}</td>
<td style="width: 30px; text-align: center;"><span style="color: #ff6b6b; font-weight: bold;" title="Overflight">🔄</span></td>
<td>${flight.callsign || '-'}</td>
<td>${flight.type || '-'}</td>
<td>${flight.departure_airfield || '-'}</td>
<td>${flight.destination_airfield || '-'}</td>
<td>${formatTimeOnly(flight.call_dt)}</td>
<td>${flight.pob || '-'}</td>
<td>${statusBadge}</td>
<td>
<button class="btn btn-sm btn-primary" onclick="markQSY(event, ${flight.id})">QSY</button>
<button class="btn btn-sm btn-danger" onclick="cancelOverflight(event, ${flight.id})">Cancel</button>
</td>
`;
tbody.appendChild(row);
}
document.getElementById('overflights-table-content').style.display = 'block';
}
// Load departed aircraft (DEPARTED status with departed_dt today) // Load departed aircraft (DEPARTED status with departed_dt today)
async function loadDeparted() { async function loadDeparted() {
document.getElementById('departed-loading').style.display = 'block'; document.getElementById('departed-loading').style.display = 'block';
@@ -3255,6 +3442,93 @@
document.getElementById('bookInModal').style.display = 'none'; document.getElementById('bookInModal').style.display = 'none';
} }
function openOverflightModal() {
document.getElementById('overflight-form').reset();
document.getElementById('overflight-id').value = '';
document.getElementById('overflightModal').style.display = 'block';
// Clear aircraft lookup results
clearOverflightAircraftLookup();
clearOverflightDepartureAirportLookup();
clearOverflightDestinationAirportLookup();
// Set current time as call time
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
document.getElementById('overflight_call_dt').value = `${year}-${month}-${day}T${hours}:${minutes}`;
// Auto-focus on registration field
setTimeout(() => {
document.getElementById('overflight_registration').focus();
}, 100);
}
function closeOverflightModal() {
document.getElementById('overflightModal').style.display = 'none';
}
function openOverflightEditModal(id) {
// Open a simple modal or dialog for editing/managing overflight
// For now, show a confirmation for QSY
showNotification(`Overflight ${id} - Use QSY button to mark frequency change`, false);
}
async function markQSY(event, overflightId) {
event.stopPropagation();
if (!accessToken) return;
try {
const response = await authenticatedFetch(`/api/v1/overflights/${overflightId}/status`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
status: 'INACTIVE'
})
});
if (!response.ok) {
throw new Error('Failed to mark QSY');
}
loadPPRs();
showNotification('Overflight marked as QSY (frequency changed)', false);
} catch (error) {
console.error('Error marking QSY:', error);
showNotification(`Error: ${error.message}`, true);
}
}
async function cancelOverflight(event, overflightId) {
event.stopPropagation();
if (!accessToken) return;
if (!confirm('Are you sure you want to cancel this overflight?')) {
return;
}
try {
const response = await authenticatedFetch(`/api/v1/overflights/${overflightId}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error('Failed to cancel overflight');
}
loadPPRs();
showNotification('Overflight cancelled', false);
} catch (error) {
console.error('Error cancelling overflight:', error);
showNotification(`Error: ${error.message}`, true);
}
}
function populateETATimeSlots() { function populateETATimeSlots() {
const select = document.getElementById('book_in_eta_time'); const select = document.getElementById('book_in_eta_time');
const next15MinSlot = getNext10MinuteSlot(); const next15MinSlot = getNext10MinuteSlot();
@@ -4100,6 +4374,104 @@
} }
}); });
// Overflight form submission
document.getElementById('overflight-form').addEventListener('submit', async function(e) {
e.preventDefault();
if (!accessToken) return;
const formData = new FormData(this);
const overflightData = {};
formData.forEach((value, key) => {
if (key === 'id') return;
if (typeof value === 'number' || (typeof value === 'string' && value.trim() !== '')) {
if (key === 'pob') {
overflightData[key] = parseInt(value);
} else if (key === 'call_dt') {
// Convert datetime-local to ISO string
if (value.trim()) {
overflightData[key] = new Date(value).toISOString();
}
} else if (value.trim) {
overflightData[key] = value.trim();
} else {
overflightData[key] = value;
}
}
});
console.log('Submitting overflight data:', overflightData);
try {
const response = await fetch('/api/v1/overflights/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify(overflightData)
});
if (!response.ok) {
let errorMessage = 'Failed to register overflight';
try {
const errorData = await response.json();
if (errorData.detail) {
errorMessage = typeof errorData.detail === 'string' ? errorData.detail : JSON.stringify(errorData.detail);
} else if (errorData.errors) {
errorMessage = errorData.errors.map(e => e.msg).join(', ');
}
} catch (e) {
const text = await response.text();
console.error('Server response:', text);
errorMessage = `Server error (${response.status})`;
}
throw new Error(errorMessage);
}
const result = await response.json();
closeOverflightModal();
loadPPRs();
showNotification(`Overflight ${result.registration} registered successfully!`);
} catch (error) {
console.error('Error registering overflight:', error);
showNotification(`Error: ${error.message}`, true);
}
});
// Overflight lookup handlers - use lookupManager
function handleOverflightAircraftLookup(input) {
const lookup = lookupManager.lookups['overflight-aircraft'];
if (lookup) lookup.handle(input);
}
function clearOverflightAircraftLookup() {
const lookup = lookupManager.lookups['overflight-aircraft'];
if (lookup) lookup.clear();
}
function handleOverflightDepartureAirportLookup(input) {
const lookup = lookupManager.lookups['overflight-departure'];
if (lookup) lookup.handle(input);
}
function clearOverflightDepartureAirportLookup() {
const lookup = lookupManager.lookups['overflight-departure'];
if (lookup) lookup.clear();
}
function handleOverflightDestinationAirportLookup(input) {
const lookup = lookupManager.lookups['overflight-destination'];
if (lookup) lookup.handle(input);
}
function clearOverflightDestinationAirportLookup() {
const lookup = lookupManager.lookups['overflight-destination'];
if (lookup) lookup.clear();
}
// Add hover listeners to all notes tooltips // Add hover listeners to all notes tooltips
function setupTooltips() { function setupTooltips() {
document.querySelectorAll('.notes-tooltip').forEach(tooltip => { document.querySelectorAll('.notes-tooltip').forEach(tooltip => {

View File

@@ -193,6 +193,8 @@ function createLookup(fieldId, resultsId, selectCallback, options = {}) {
typeFieldId = 'local_type'; typeFieldId = 'local_type';
} else if (fieldId === 'book_in_registration') { } else if (fieldId === 'book_in_registration') {
typeFieldId = 'book_in_type'; typeFieldId = 'book_in_type';
} else if (fieldId === 'overflight_registration') {
typeFieldId = 'overflight_type';
} }
if (typeFieldId) { if (typeFieldId) {
@@ -360,6 +362,30 @@ function initializeLookups() {
{ isAirport: true, minLength: 2 } { isAirport: true, minLength: 2 }
); );
lookupManager.register('book-in-arrival-airport', bookInArrivalAirportLookup); lookupManager.register('book-in-arrival-airport', bookInArrivalAirportLookup);
const overflightAircraftLookup = createLookup(
'overflight_registration',
'overflight-aircraft-lookup-results',
null,
{ isAircraft: true, minLength: 4, debounceMs: 300 }
);
lookupManager.register('overflight-aircraft', overflightAircraftLookup);
const overflightDepartureLookup = createLookup(
'overflight_departure_airfield',
'overflight-departure-airport-lookup-results',
null,
{ isAirport: true, minLength: 2 }
);
lookupManager.register('overflight-departure', overflightDepartureLookup);
const overflightDestinationLookup = createLookup(
'overflight_destination_airfield',
'overflight-destination-airport-lookup-results',
null,
{ isAirport: true, minLength: 2 }
);
lookupManager.register('overflight-destination', overflightDestinationLookup);
// Attach keyboard handlers to airport input fields // Attach keyboard handlers to airport input fields
setTimeout(() => { setTimeout(() => {
@@ -367,6 +393,8 @@ function initializeLookups() {
if (departureAirportLookup.attachKeyboardHandler) departureAirportLookup.attachKeyboardHandler(); if (departureAirportLookup.attachKeyboardHandler) departureAirportLookup.attachKeyboardHandler();
if (localOutToLookup.attachKeyboardHandler) localOutToLookup.attachKeyboardHandler(); if (localOutToLookup.attachKeyboardHandler) localOutToLookup.attachKeyboardHandler();
if (bookInArrivalAirportLookup.attachKeyboardHandler) bookInArrivalAirportLookup.attachKeyboardHandler(); if (bookInArrivalAirportLookup.attachKeyboardHandler) bookInArrivalAirportLookup.attachKeyboardHandler();
if (overflightDepartureLookup.attachKeyboardHandler) overflightDepartureLookup.attachKeyboardHandler();
if (overflightDestinationLookup.attachKeyboardHandler) overflightDestinationLookup.attachKeyboardHandler();
}, 100); }, 100);
} }