// composables/useFlightStats.ts import {computed, ComputedRef, Ref} from 'vue' import type {Airport, Flight} from '@/Types/types' const flightYear = (f: Flight): number => Number(new Intl.DateTimeFormat('en', { timeZone: f.departure_airport.timezone, year: 'numeric', }).format(new Date(f.departure_date))) const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] const dayIndex = (f: Flight): number => { const localDay = new Intl.DateTimeFormat('en', { timeZone: f.departure_airport.timezone, weekday: 'long', }).format(new Date(f.departure_date)) const idx = DAYS.indexOf(localDay) return idx === -1 ? 0 : idx } export function getFlightsPerDay(flights: Flight[], upcomingFlights: Flight[]) { const countByDay = (list: Flight[]) => DAYS.map((_, i) => list.filter(f => dayIndex(f) === i).length) return { days: DAYS, series: [ { name: 'Flown', data: countByDay(flights) }, { name: 'Upcoming', data: countByDay(upcomingFlights) }, ], } } const monthIndex = (f: Flight): number => Number(new Intl.DateTimeFormat('en', { timeZone: f.departure_airport.timezone, month: 'numeric', }).format(new Date(f.departure_date))) - 1 export function getFlightsPerMonth(flights: Flight[], upcomingFlights: Flight[]) { const countByMonth = (list: Flight[]) => MONTHS.map((_, i) => list.filter(f => monthIndex(f) === i).length) return { months: MONTHS, series: [ { name: 'Flown', data: countByMonth(flights) }, { name: 'Upcoming', data: countByMonth(upcomingFlights) }, ], } } export function getFlightsPerYear(flights: Flight[], upcomingFlights: Flight[]) { const allFlights = [...flights, ...upcomingFlights] const allYears = new Set() allFlights.forEach(f => allYears.add(flightYear(f))) const sorted = [...allYears].sort((a, b) => a - b) let min = sorted[0] let max = sorted[sorted.length - 1] while (max - min + 1 < 5) { min-- if (max - min + 1 < 5) max++ } const years = Array.from({ length: max - min + 1 }, (_, i) => min + i) const countByYear = (list: Flight[]) => years.map(year => list.filter(f => flightYear(f) === year).length) return { years, series: [ { name: 'Flown', data: countByYear(flights) }, { name: 'Upcoming', data: countByYear(upcomingFlights) }, ], } } function groupByName(flights: Flight[], accessor: (f: Flight) => string) { const counts = new Map() flights.forEach(f => { const key = accessor(f) ?? 'Unknown' counts.set(key, (counts.get(key) ?? 0) + 1) }) const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]) return { labels: sorted.map(([name]) => name), series: sorted.map(([, count]) => count), } } export function getFlightReasons(flights: Flight[]) { return groupByName(flights, f => f.flight_reason?.name ?? 'Unknown') } export function getFlightClasses(flights: Flight[]) { return groupByName(flights, f => f.flight_class?.name ?? 'Unknown') } export function getSeatTypes(flights: Flight[]) { return groupByName(flights, f => f.seat_type?.name ?? 'Unknown') } function countCountries(flights: Flight[]) { const counts = new Map() flights.forEach(f => { const dep = f.departure_airport?.region?.country const arr = f.arrival_airport?.region?.country if (dep?.name) { const existing = counts.get(dep.name) ?? { count: 0, code: dep.code } existing.count++ counts.set(dep.name, existing) } if (arr?.name && arr.name !== dep?.name) { const existing = counts.get(arr.name) ?? { count: 0, code: arr.code } existing.count++ counts.set(arr.name, existing) } }) return counts } export function getCountries(flights: Flight[], upcomingFlights: Flight[]) { const past = countCountries(flights) const upcoming = countCountries(upcomingFlights) const allCountries = new Set([...past.keys(), ...upcoming.keys()]) const sorted = [...allCountries] .map(name => ({ name, code: (past.get(name) ?? upcoming.get(name))!.code, past: past.get(name)?.count ?? 0, upcoming: upcoming.get(name)?.count ?? 0, })) .sort((a, b) => (b.past + b.upcoming) - (a.past + a.upcoming)) return { countries: sorted, series: [ { name: 'Flights', data: sorted.map(s => s.past) }, { name: 'Upcoming', data: sorted.map(s => s.upcoming) }, ], } } export function getContinents(flights: Flight[]) { const counts = new Map() flights.forEach(f => { const continents = new Set() const dep = f.departure_airport.region?.continent?.name const arr = f.arrival_airport.region?.continent?.name if (dep) continents.add(dep) if (arr) continents.add(arr) continents.forEach(c => counts.set(c, (counts.get(c) ?? 0) + 1)) }) const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]) return { labels: sorted.map(([name]) => name), series: sorted.map(([, count]) => count), } } function countAirlines(flights: Flight[]) { const counts = new Map() flights.forEach(f => { const airline = f.airline if (!airline?.name) return const existing = counts.get(airline.name) ?? { count: 0, id: airline.id } existing.count++ counts.set(airline.name, existing) }) return counts } export function getTopAirlines(flights: Flight[], upcomingFlights: Flight[]) { const past = countAirlines(flights) const upcoming = countAirlines(upcomingFlights) const allAirlines = new Set([...past.keys(), ...upcoming.keys()]) const sorted = [...allAirlines] .map(name => ({ name, id: (past.get(name) ?? upcoming.get(name))!.id, past: past.get(name)?.count ?? 0, upcoming: upcoming.get(name)?.count ?? 0, })) .sort((a, b) => (b.past + b.upcoming) - (a.past + a.upcoming)) return { airlines: sorted, series: [ { name: 'Flights', data: sorted.map(s => s.past) }, { name: 'Upcoming', data: sorted.map(s => s.upcoming) }, ], } } interface AirportItem { label: string fullName: string departures: number arrivals: number upcoming: number } function airportLabel(airport: Airport | null | undefined): string | null { if (!airport) return null return airport.iata_code ?? airport.icao_code ?? airport.name ?? null } export function getTopAirports(flights: Flight[], upcomingFlights: Flight[]) { const map = new Map() const empty = (): AirportItem => ({ departures: 0, arrivals: 0, upcoming: 0, label: '', fullName: '' }) flights.forEach(f => { const depLabel = airportLabel(f.departure_airport) const arrLabel = airportLabel(f.arrival_airport) if (depLabel) { const e = map.get(depLabel) ?? empty() e.departures++ e.label = depLabel e.fullName = f.departure_airport?.name ?? depLabel map.set(depLabel, e) } if (arrLabel) { const e = map.get(arrLabel) ?? empty() e.arrivals++ e.label = arrLabel e.fullName = f.arrival_airport?.name ?? arrLabel map.set(arrLabel, e) } }) upcomingFlights.forEach(f => { const depLabel = airportLabel(f.departure_airport) const arrLabel = airportLabel(f.arrival_airport) if (depLabel) { const e = map.get(depLabel) ?? empty() e.upcoming++ e.label = depLabel e.fullName = f.departure_airport?.name ?? depLabel map.set(depLabel, e) } if (arrLabel) { const e = map.get(arrLabel) ?? empty() e.upcoming++ e.label = arrLabel e.fullName = f.arrival_airport?.name ?? arrLabel map.set(arrLabel, e) } }) const sorted = [...map.values()] .sort((a, b) => (b.departures + b.arrivals + b.upcoming) - (a.departures + a.arrivals + a.upcoming) ) return { airports: sorted, series: [ { name: 'Departures', data: sorted.map(s => s.departures) }, { name: 'Arrivals', data: sorted.map(s => s.arrivals) }, { name: 'Upcoming', data: sorted.map(s => s.upcoming) }, ], } } export function getFlightTypes(flights: Flight[]) { const counts = { International: 0, Domestic: 0 } flights.forEach(f => { const dep = f.departure_airport.region?.country?.id const arr = f.arrival_airport.region?.country?.id if (dep && arr) { dep === arr ? counts.Domestic++ : counts.International++ } }) const sorted = Object.entries(counts).filter(([, count]) => count > 0) return { labels: sorted.map(([name]) => name), series: sorted.map(([, count]) => count), } } export type FlightStats = ReturnType export function useFlightStats(flights: Ref) { const now = new Date() const pastFlights = computed(() => flights.value.filter(f => new Date(f.departure_date) <= now) ) const upcomingFlights = computed(() => flights.value.filter(f => new Date(f.departure_date) > now) ) const allFlights = computed(() => flights.value) const perYear = computed(() => getFlightsPerYear(pastFlights.value, upcomingFlights.value)) const perMonth = computed(() => getFlightsPerMonth(pastFlights.value, upcomingFlights.value)) const perDay = computed(() => getFlightsPerDay(pastFlights.value, upcomingFlights.value)) const reasons = computed(() => getFlightReasons(allFlights.value)) const classes = computed(() => getFlightClasses(allFlights.value)) const seatTypes = computed(() => getSeatTypes(allFlights.value)) const countries = computed(() => getCountries(pastFlights.value, upcomingFlights.value)) const continents = computed(() => getContinents(allFlights.value)) const topAirlines = computed(() => getTopAirlines(pastFlights.value, upcomingFlights.value)) const topAirports = computed(() => getTopAirports(pastFlights.value, upcomingFlights.value)) const flightTypes = computed(() => getFlightTypes(allFlights.value)) return { pastFlights, upcomingFlights, perYear, perMonth, perDay, reasons, classes, seatTypes, countries, continents, flightTypes, topAirlines, topAirports, } }