Skip to content

Commit

Permalink
feat: add RPL cycle field (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh authored Sep 17, 2023
1 parent e3a9ded commit 5ee0aa8
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/app/app/api/flights/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion packages/app/app/api/flights/all/route.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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 &&
Expand Down
4 changes: 3 additions & 1 deletion packages/app/app/api/flights/route.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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 &&
Expand Down
4 changes: 3 additions & 1 deletion packages/app/src/services/fetch-aircraft-icao-codes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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
.select({
aircraftIcaoCode: sql<string>`DISTINCT(${flights.aircraft}->>"$.icaoCode")`,
})
.from(flights)
.where(eq(flights.cycle, currentCycleSubquery))

return aircrafts?.map((v) => v.aircraftIcaoCode) ?? []
}
4 changes: 3 additions & 1 deletion packages/app/src/services/fetch-airports.ts
Original file line number Diff line number Diff line change
@@ -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))
}
Expand Down
4 changes: 3 additions & 1 deletion packages/app/src/services/fetch-companies.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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
.select({
company: sql`DISTINCT(${flights.company})`,
})
.from(flights)
.where(eq(flights.cycle, currentCycleSubquery))

return companies.map((v) => String(v.company))
}
2 changes: 2 additions & 0 deletions packages/app/src/services/fetch-flights.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,6 +33,7 @@ export async function fetchFlights(searchParams: Record<string, unknown>) {
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,
Expand Down
8 changes: 8 additions & 0 deletions packages/app/src/utils/currentCycleSubquery.ts
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions packages/database/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion packages/rpl-crawler/src/flight-decoder/flight-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './flight-decoder-utils'

const makeFlightDecoder = ({ uuid }: { uuid: (line: string) => string }) => {
return (line: string): Flight => {
return (line: string): Omit<Flight, 'cycle'> => {
const LINES = line.split('\n').map((line) => line.trim())

const LINE_1 = LINES[0]
Expand Down
6 changes: 4 additions & 2 deletions packages/rpl-crawler/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type MainDependencies = {
updateChecker: (date: string) => Promise<boolean>
rplFileDownloader: (fir: string, date: string) => Promise<Buffer>
rplFileLinesExtractor: (file: Buffer) => string[]
flightDecoder: (line: string) => Flight
flightDecoder: (line: string) => Omit<Flight, 'cycle'>
saveFlights: (flights: Flight[]) => Promise<void>
}

Expand Down Expand Up @@ -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}`)
Expand Down
1 change: 0 additions & 1 deletion packages/rpl-crawler/src/save-flights/save-flights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function sliceArray(flights: Flight[]) {
const makeSaveFlights = () => {
return async (flights: Flight[]): Promise<void> => {
await db.transaction(async (tx) => {
await tx.delete(flightsSchema)
const sliced = sliceArray(flights)
for (const slice of sliced) {
await tx.insert(flightsSchema).values(slice)
Expand Down

0 comments on commit 5ee0aa8

Please sign in to comment.