Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Add debug logs and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
anduong96 committed Nov 26, 2023
1 parent 7239b76 commit 69fd9eb
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 59 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"typescript": "^5.2.2"
},
"peerDependencies": {
"typescript": "^5.2.2",
"class-validator": "^0.14.0"
"class-validator": "^0.14.0",
"typescript": "^5.2.2"
},
"dependencies": {
"@apollo/server": "^4.9.5",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ CREATE TYPE "FlightStatus" AS ENUM ('SCHEDULED', 'DEPARTED', 'DELAYED', 'ARRIVED
-- CreateEnum
CREATE TYPE "MeasurementType" AS ENUM ('AMERICAN', 'METRIC', 'IMPERIAL');

-- CreateEnum
CREATE TYPE "DateFormatType" AS ENUM ('AMERICAN', 'WORLD');

-- CreateTable
CREATE TABLE "Airline" (
"id" TEXT NOT NULL,
Expand Down Expand Up @@ -319,6 +322,7 @@ CREATE TABLE "UserPreference" (
"id" TEXT NOT NULL,
"userID" TEXT NOT NULL,
"measurement" "MeasurementType" NOT NULL DEFAULT 'AMERICAN',
"dateFormat" "DateFormatType" NOT NULL DEFAULT 'AMERICAN',

CONSTRAINT "UserPreference_pkey" PRIMARY KEY ("id")
);
Expand Down
1 change: 1 addition & 0 deletions src/@generated/graphql/models/FlightEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GQL_ValueType } from '../enums/ValueType';
import { GQL_ChangeType } from '../enums/ChangeType';
import { Prisma } from '@prisma/client';
import * as GraphQLScalars from 'graphql-scalars';
import GraphQLScalars from 'graphql-scalars';

@ObjectType('FlightEvent')
export class GQL_FlightEvent {
Expand Down
1 change: 1 addition & 0 deletions src/@generated/graphql/models/JsonCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Field, ObjectType, ID } from 'type-graphql';
import { Prisma } from '@prisma/client';
import * as GraphQLScalars from 'graphql-scalars';
import GraphQLScalars from 'graphql-scalars';

@ObjectType('JsonCache')
export class GQL_JsonCache {
Expand Down
57 changes: 8 additions & 49 deletions src/api/graphql/resolvers/user.flights.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FlightStatus } from '@prisma/client';
import {
Arg,
Authorized,
Expand All @@ -12,6 +11,9 @@ import {
import { prisma } from '@app/prisma';
import { GQL_Flight } from '@app/@generated/graphql/models/Flight';
import { GQL_UserFlight } from '@app/@generated/graphql/models/UserFlight';
import { saveFlightToUser } from '@app/services/user.flight/save.flight.to.user';
import { getUserActiveFlights } from '@app/services/user.flight/get.user.active.flights';
import { getUserArchivedFlights } from '@app/services/user.flight/get.user.archived.flights';

import { CurrentUserID } from '../_decorators/current.user.id.decorator';

Expand All @@ -36,20 +38,7 @@ export class UserFlightResolver {
@CurrentUserID() userID: string,
@Arg('flightID') flightID: string,
): Promise<string> {
const record = await prisma.userFlight.upsert({
create: {
flightID,
userID,
},
update: {},
where: {
flightID_userID: {
flightID,
userID,
},
},
});

const record = await saveFlightToUser(flightID, userID);
return record.id;
}

Expand All @@ -74,45 +63,15 @@ export class UserFlightResolver {
@Authorized()
@Query(() => [GQL_UserFlight])
async userActiveFlights(@CurrentUserID() userID: string) {
const result = await prisma.userFlight.findMany({
orderBy: {
Flight: {
estimatedGateDeparture: 'asc',
},
},
where: {
Flight: {
status: {
notIn: [FlightStatus.ARCHIVED, FlightStatus.CANCELED],
},
},
userID,
},
});

return result;
const flights = await getUserActiveFlights(userID);
return flights;
}

@Authorized()
@Query(() => [GQL_UserFlight])
async userArchivedFlights(@CurrentUserID() userID: string) {
const result = await prisma.userFlight.findMany({
orderBy: {
Flight: {
estimatedGateDeparture: 'desc',
},
},
where: {
Flight: {
status: {
in: [FlightStatus.ARCHIVED, FlightStatus.CANCELED],
},
},
userID,
},
});

return result;
const flights = await getUserArchivedFlights(userID);
return flights;
}

@Authorized()
Expand Down
6 changes: 5 additions & 1 deletion src/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ export const firebase = firebaseAdmin.initializeApp({
credential,
});

Logger.getSubLogger({ name: 'Firebase' }).debug('initialized');
Logger.getSubLogger({ name: 'Firebase' }).debug(
'initialized projectID=%s clientEmail=%s',
ENV.FIREBASE_PROJECT_ID,
ENV.FIREBASE_CLIENT_EMAIL,
);
4 changes: 4 additions & 0 deletions src/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PrismaClient } from '@prisma/client';

import { ENV } from './env';
import { Logger } from './lib/logger';

class _prisma extends PrismaClient {
Expand All @@ -26,6 +27,9 @@ class _prisma extends PrismaClient {
this.$connect()
.then(() => {
this.logger.debug('Connected to Prisma');
if (ENV.NODE_ENV === 'development') {
this.logger.warn('Prisma database url=%s', ENV.DATABASE_URL);
}
})
.catch(error => {
this.logger.error('Unable to connect to Prisma', error);
Expand Down
5 changes: 5 additions & 0 deletions src/scheduler/defined.jobs/plane.sync.job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { Job } from '../job';
export class SyncActivePlaneLocationJob extends Job {
override cronTime = CronTime.every(7).minutes();

constructor() {
super();
this.onProcess();
}

private async getTailNumbers() {
const ceil = moment().add(10, 'hours').toDate();
const floor = moment().subtract(12, 'hours').toDate();
Expand Down
7 changes: 6 additions & 1 deletion src/services/aircraft/update.aircraft.position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export async function updateAircraftPosition(
) {
const position = await RadarBox.getAircraft(aircraft.tailNumber);

Logger.debug('Plane position', position);
Logger.debug(
'Plane tailNumber=%s airlineIata=%s position=%o',
aircraft.tailNumber,
aircraft.airlineIata,
position,
);

if (!position || !position?.destinationIata || !position.originIata) {
Logger.debug(
Expand Down
26 changes: 26 additions & 0 deletions src/services/user.flight/get.user.active.flights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FlightStatus, User } from '@prisma/client';

import { prisma } from '@app/prisma';
import { Logger } from '@app/lib/logger';

export async function getUserActiveFlights(userID: User['id']) {
Logger.debug('Getting user=%s active flights', userID);
const flights = await prisma.userFlight.findMany({
orderBy: {
Flight: {
estimatedGateDeparture: 'asc',
},
},
where: {
Flight: {
status: {
notIn: [FlightStatus.ARCHIVED, FlightStatus.CANCELED],
},
},
userID,
},
});

Logger.debug('Got user=%s active flights=%s', userID, flights.length);
return flights;
}
26 changes: 26 additions & 0 deletions src/services/user.flight/get.user.archived.flights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FlightStatus, User } from '@prisma/client';

import { prisma } from '@app/prisma';
import { Logger } from '@app/lib/logger';

export async function getUserArchivedFlights(userID: User['id']) {
Logger.debug('Getting user=%s archived flights', userID);
const flights = await prisma.userFlight.findMany({
orderBy: {
Flight: {
estimatedGateDeparture: 'desc',
},
},
where: {
Flight: {
status: {
in: [FlightStatus.ARCHIVED, FlightStatus.CANCELED],
},
},
userID,
},
});

Logger.debug('Got user=%s archived flights=%s', userID, flights.length);
return flights;
}
27 changes: 27 additions & 0 deletions src/services/user.flight/save.flight.to.user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Flight, User } from '@prisma/client';

import { prisma } from '@app/prisma';
import { Logger } from '@app/lib/logger';

export async function saveFlightToUser(
flightID: Flight['id'],
userID: User['id'],
) {
Logger.debug('Adding flight[%s] to user[%s]', flightID, userID);

const record = await prisma.userFlight.upsert({
create: {
flightID,
userID,
},
update: {},
where: {
flightID_userID: {
flightID,
userID,
},
},
});

return record;
}
2 changes: 1 addition & 1 deletion src/vendors/aircraft/radar.box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class RadarBox {
const html = await request.text();
const data = this.crawlAircraftHtml(html);
this.logger.debug(
'RadarBox request[%s/%s] status[%s]',
'RadarBox request[%s] status[%s]',
request.url,
request.statusText,
);
Expand Down

0 comments on commit 69fd9eb

Please sign in to comment.