From 5ee0aa872e35becae7853e784741c8f974e0fc74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Henrique?= Date: Sun, 17 Sep 2023 17:52:59 -0300 Subject: [PATCH] feat: add RPL cycle field (#266) --- packages/app/app/api/flights/[id]/route.ts | 1 + packages/app/app/api/flights/all/route.ts | 5 ++++- packages/app/app/api/flights/route.ts | 4 +++- packages/app/src/services/fetch-aircraft-icao-codes.ts | 4 +++- packages/app/src/services/fetch-airports.ts | 4 +++- packages/app/src/services/fetch-companies.ts | 4 +++- packages/app/src/services/fetch-flights.ts | 2 ++ packages/app/src/utils/currentCycleSubquery.ts | 8 ++++++++ packages/database/src/schema.ts | 1 + packages/rpl-crawler/src/flight-decoder/flight-decoder.ts | 2 +- packages/rpl-crawler/src/main.ts | 6 ++++-- packages/rpl-crawler/src/save-flights/save-flights.ts | 1 - 12 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 packages/app/src/utils/currentCycleSubquery.ts diff --git a/packages/app/app/api/flights/[id]/route.ts b/packages/app/app/api/flights/[id]/route.ts index 20ccafbd..d3e9b5bd 100644 --- a/packages/app/app/api/flights/[id]/route.ts +++ b/packages/app/app/api/flights/[id]/route.ts @@ -29,6 +29,7 @@ export async function GET( } const flight = await db.query.flights.findFirst({ + columns: { cycle: false }, where: (flights, { eq }) => eq(flights.id, data.data.id), }) if (flight === null) { diff --git a/packages/app/app/api/flights/all/route.ts b/packages/app/app/api/flights/all/route.ts index f926e4f4..f0a6cd45 100644 --- a/packages/app/app/api/flights/all/route.ts +++ b/packages/app/app/api/flights/all/route.ts @@ -1,6 +1,7 @@ import { db } from '@mach/database' import { NextResponse } from 'next/server' import z from 'zod' +import { currentCycleSubquery } from '../../../../src/utils/currentCycleSubquery' export const dynamic = 'force-dynamic' @@ -54,9 +55,11 @@ export async function GET(request: Request) { } const items = await db.query.flights.findMany({ + columns: { cycle: false }, orderBy: (fields, { desc }) => desc(fields.id), - where: (fields, { sql, and }) => + where: (fields, { sql, and, eq }) => and( + eq(fields.cycle, currentCycleSubquery), data.data.departureIcao && sql`${fields.departureIcao} IN ${data.data.departureIcao}`, data.data.arrivalIcao && diff --git a/packages/app/app/api/flights/route.ts b/packages/app/app/api/flights/route.ts index a7b779fa..96c9756f 100644 --- a/packages/app/app/api/flights/route.ts +++ b/packages/app/app/api/flights/route.ts @@ -1,7 +1,8 @@ import { db, flights } from '@mach/database' -import { sql, and } from 'drizzle-orm' +import { sql, and, eq } from 'drizzle-orm' import { NextResponse } from 'next/server' import z from 'zod' +import { currentCycleSubquery } from '../../../src/utils/currentCycleSubquery' export const dynamic = 'force-dynamic' @@ -63,6 +64,7 @@ export async function GET(request: Request) { } const criteria = and( + eq(flights.cycle, currentCycleSubquery), data.data.departureIcao && sql`${flights.departureIcao} IN ${data.data.departureIcao}`, data.data.arrivalIcao && diff --git a/packages/app/src/services/fetch-aircraft-icao-codes.ts b/packages/app/src/services/fetch-aircraft-icao-codes.ts index 44d58f42..9df9158c 100644 --- a/packages/app/src/services/fetch-aircraft-icao-codes.ts +++ b/packages/app/src/services/fetch-aircraft-icao-codes.ts @@ -1,5 +1,6 @@ import { db, flights } from '@mach/database' -import { sql } from 'drizzle-orm' +import { eq, sql } from 'drizzle-orm' +import { currentCycleSubquery } from '../utils/currentCycleSubquery' export async function fetchAircraftIcaoCodes() { const aircrafts = await db @@ -7,6 +8,7 @@ export async function fetchAircraftIcaoCodes() { aircraftIcaoCode: sql`DISTINCT(${flights.aircraft}->>"$.icaoCode")`, }) .from(flights) + .where(eq(flights.cycle, currentCycleSubquery)) return aircrafts?.map((v) => v.aircraftIcaoCode) ?? [] } diff --git a/packages/app/src/services/fetch-airports.ts b/packages/app/src/services/fetch-airports.ts index c0babdbb..edc1c8bd 100644 --- a/packages/app/src/services/fetch-airports.ts +++ b/packages/app/src/services/fetch-airports.ts @@ -1,13 +1,15 @@ import { db, flights } from '@mach/database' -import { sql } from 'drizzle-orm' +import { eq, sql } from 'drizzle-orm' import { XMLParser } from 'fast-xml-parser' import z from 'zod' import { environment } from '../utils/env' +import { currentCycleSubquery } from '../utils/currentCycleSubquery' async function fetchAirportsFromDb(column: 'departureIcao' | 'arrivalIcao') { const airports = await db .select({ icaoCode: sql`DISTINCT(${flights[column]})` }) .from(flights) + .where(eq(flights.cycle, currentCycleSubquery)) return airports.map((v) => String(v.icaoCode)) } diff --git a/packages/app/src/services/fetch-companies.ts b/packages/app/src/services/fetch-companies.ts index 5b46f21a..1df86905 100644 --- a/packages/app/src/services/fetch-companies.ts +++ b/packages/app/src/services/fetch-companies.ts @@ -1,5 +1,6 @@ import { db, flights } from '@mach/database' -import { sql } from 'drizzle-orm' +import { eq, sql } from 'drizzle-orm' +import { currentCycleSubquery } from '../utils/currentCycleSubquery' export async function fetchCompanies() { const companies = await db @@ -7,6 +8,7 @@ export async function fetchCompanies() { company: sql`DISTINCT(${flights.company})`, }) .from(flights) + .where(eq(flights.cycle, currentCycleSubquery)) return companies.map((v) => String(v.company)) } diff --git a/packages/app/src/services/fetch-flights.ts b/packages/app/src/services/fetch-flights.ts index dc3fc74e..92b23d52 100644 --- a/packages/app/src/services/fetch-flights.ts +++ b/packages/app/src/services/fetch-flights.ts @@ -1,6 +1,7 @@ import { db } from '@mach/database' import z from 'zod' import { fetchAirportsData } from './fetch-airports' +import { currentCycleSubquery } from '../utils/currentCycleSubquery' const schema = z.object({ departureIcao: z @@ -32,6 +33,7 @@ export async function fetchFlights(searchParams: Record) { const flights = await db.query.flights.findMany({ where: (fields, { sql, and, eq, or }) => and( + eq(fields.cycle, currentCycleSubquery), where.departureIcao ? eq(fields.departureIcao, where.departureIcao) : undefined, diff --git a/packages/app/src/utils/currentCycleSubquery.ts b/packages/app/src/utils/currentCycleSubquery.ts new file mode 100644 index 00000000..6685cebc --- /dev/null +++ b/packages/app/src/utils/currentCycleSubquery.ts @@ -0,0 +1,8 @@ +import { db, flights } from '@mach/database' +import { desc } from 'drizzle-orm' + +export const currentCycleSubquery = db + .select({ cycle: flights.cycle }) + .from(flights) + .limit(1) + .orderBy(desc(flights.cycle)) diff --git a/packages/database/src/schema.ts b/packages/database/src/schema.ts index 24f78140..36993fb8 100644 --- a/packages/database/src/schema.ts +++ b/packages/database/src/schema.ts @@ -35,6 +35,7 @@ export const flights = mysqlTable('flights', { estimatedEnrouteMinutes: int('estimated_enroute_minutes').notNull(), flightRules: mysqlEnum('flight_rules', ['IFR', 'Y', 'Z']).notNull(), remarks: text('remarks').notNull(), + cycle: date('cycle', { mode: 'date' }).notNull(), }) export type Flight = typeof flights.$inferSelect diff --git a/packages/rpl-crawler/src/flight-decoder/flight-decoder.ts b/packages/rpl-crawler/src/flight-decoder/flight-decoder.ts index eda57407..ffa7d678 100644 --- a/packages/rpl-crawler/src/flight-decoder/flight-decoder.ts +++ b/packages/rpl-crawler/src/flight-decoder/flight-decoder.ts @@ -8,7 +8,7 @@ import { } from './flight-decoder-utils' const makeFlightDecoder = ({ uuid }: { uuid: (line: string) => string }) => { - return (line: string): Flight => { + return (line: string): Omit => { const LINES = line.split('\n').map((line) => line.trim()) const LINE_1 = LINES[0] diff --git a/packages/rpl-crawler/src/main.ts b/packages/rpl-crawler/src/main.ts index 35b206a4..c42616fa 100644 --- a/packages/rpl-crawler/src/main.ts +++ b/packages/rpl-crawler/src/main.ts @@ -10,7 +10,7 @@ type MainDependencies = { updateChecker: (date: string) => Promise rplFileDownloader: (fir: string, date: string) => Promise rplFileLinesExtractor: (file: Buffer) => string[] - flightDecoder: (line: string) => Flight + flightDecoder: (line: string) => Omit saveFlights: (flights: Flight[]) => Promise } @@ -62,7 +62,9 @@ const main = async ( Logger.info(`COMPLETED DECODING OF RPL FILES DATA`) Logger.info(`STARTING SAVING DECODED DATA TO DATABASE`) - await saveFlights(flights) + await saveFlights( + flights.map((flight) => ({ ...flight, cycle: new Date(date) })) + ) Logger.info(`COMPLETED SAVING DECODED DATA TO DATABASE`) Logger.info(`COMPLETED RPL UPDATE FOR ${date}`) diff --git a/packages/rpl-crawler/src/save-flights/save-flights.ts b/packages/rpl-crawler/src/save-flights/save-flights.ts index 53f8c58e..4e26300a 100644 --- a/packages/rpl-crawler/src/save-flights/save-flights.ts +++ b/packages/rpl-crawler/src/save-flights/save-flights.ts @@ -20,7 +20,6 @@ function sliceArray(flights: Flight[]) { const makeSaveFlights = () => { return async (flights: Flight[]): Promise => { await db.transaction(async (tx) => { - await tx.delete(flightsSchema) const sliced = sliceArray(flights) for (const slice of sliced) { await tx.insert(flightsSchema).values(slice)