Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dotFionn committed Apr 20, 2023
2 parents bad0ff4 + 461c22a commit 859ac51
Show file tree
Hide file tree
Showing 14 changed files with 923 additions and 77 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/docker-image-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Docker Image CI

on:
push:
branches: [ "dev" ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
run: |
docker build --pull -t ${{ env.REGISTRY }}/vacdm/vacdm:dev .
docker push ${{ env.REGISTRY }}/vacdm/vacdm:dev
32 changes: 32 additions & 0 deletions .github/workflows/docker-image-main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Docker Image CI

on:
push:
branches: [ "main" ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
run: |
docker build --pull -t ${{ env.REGISTRY }}/vacdm/vacdm:latest .
docker push ${{ env.REGISTRY }}/vacdm/vacdm:latest
695 changes: 674 additions & 21 deletions LICENSE

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vacdm-backend",
"version": "0.0.1",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
Expand All @@ -11,10 +11,10 @@
},
"repository": {
"type": "git",
"url": "https://git.vatsim-germany.org/nav/tools/vacdm/vacdm"
"url": "https://github.com/vACDM/vACDM"
},
"author": "Fionn Sperath / vACDM Team",
"license": "ISC",
"license": "GPL3",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.8",
Expand Down
6 changes: 6 additions & 0 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ interface vacdmConfig {
timeSinceInactive: number;
};

eventUrl: string;
eventPrio: number;

clientId: string;
clientSecret: string;
publicUrl: string;
Expand Down Expand Up @@ -52,6 +55,9 @@ export default function config(): vacdmConfig {
timeSinceInactive: Number(process.env.TIME_INACTIVE) * 60 * 1000 ?? 15 * 60 * 1000,
},

eventUrl: process.env.EVENT_URL || 'https://slots.vatsim-germany.org/api/events/',
eventPrio: Number(process.env.EVENT_PRIO) || 5,

vatsimAuthUrl: options.vatsimAuthUrl,
clientId: options.vatsimAuthClientId,
clientSecret: process.env.CLIENT_SECRET ?? '',
Expand Down
1 change: 1 addition & 0 deletions backend/src/models/pilot.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const pilotSchema = new mongoose.Schema(
blockId: { type: Number, default: -1 },
block_rwy_designator: { type: String, default: '' },
},
hasBooking: { type: Boolean, default: false },

flightplan: {
flight_rules: { type: String, default: '' },
Expand Down
47 changes: 47 additions & 0 deletions backend/src/services/bookings.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import axios from 'axios';
import config from '../config';
import dayjs from 'dayjs';

export async function getAllBookings() {
try {
const events = await axios.get(
config().eventUrl
);

const relevantEvents = events.data.data.filter((e) =>
dayjs(new Date()) >= dayjs(e.startEvent) &&
dayjs(new Date()) <= dayjs(e.endEvent)
);

const relevantBookings: any[] = [];

for (let event of relevantEvents) {
const bookings = await axios.get(event.links.bookings);

for (let booking of bookings.data.data) {
relevantBookings.push(booking);
}
}

return relevantBookings;
} catch (error) {
throw error;
}
}

export async function pilotHasBooking(cid: number): Promise<boolean> {
try {
const bookings = await getAllBookings();

return bookings.findIndex((b) => b.user === cid) != -1;


} catch (error) {
throw error;
}
}

export default {
getAllBookings,
pilotHasBooking
};
70 changes: 66 additions & 4 deletions backend/src/services/cdm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { AirportCapacity } from '@shared/interfaces/airport.interface';
import config from '../config';
import pilotModel, { PilotDocument } from '../models/pilot.model';
import blockUtils from '../utils/block.utils';
import timeUtils from '../utils/time.utils';
import timeUtils, { emptyDate } from '../utils/time.utils';
import airportService from './airport.service';

import Logger from '@dotfionn/logger';
import pilotService from './pilot.service';

import bookingsService from './bookings.service';
import datafeedService from './datafeed.service';
import dayjs from 'dayjs';
const logger = new Logger('vACDM:services:cdm');

export function determineInitialBlock(pilot: PilotDocument): {
Expand Down Expand Up @@ -39,13 +43,14 @@ export async function putPilotIntoBlock(
allPilots: PilotDocument[]
): Promise<{ finalBlock: number; finalTtot: Date }> {
// count all pilots in block
const otherPilotsInBlock = allPilots.filter(
pilot.vacdm.ctot = emptyDate;
const otherPilotsOnRunway = allPilots.filter(
(plt) =>
plt.flightplan.departure == pilot.flightplan.departure &&
plt.vacdm.block_rwy_designator == pilot.vacdm.block_rwy_designator &&
plt.vacdm.blockId == pilot.vacdm.blockId &&
plt._id != pilot._id
);
const otherPilotsInBlock = otherPilotsOnRunway.filter(plt => plt.vacdm.blockId == pilot.vacdm.blockId);

const cap: AirportCapacity = await airportService.getCapacity(
pilot.flightplan.departure,
Expand All @@ -54,6 +59,29 @@ export async function putPilotIntoBlock(

if (cap.capacity > otherPilotsInBlock.length) {
// pilot fits into block
// now we have to check if pilots in block have same measures and need a MDI therefore

for (const measure of pilot.measures) {
const pilotsWithSameMeasures = otherPilotsOnRunway.filter(
(plt) =>
plt.measures.find((e) => e.ident === measure.ident) &&
plt.callsign != pilot.callsign
);
if (pilotsWithSameMeasures.length > 0) {
for (const smp of pilotsWithSameMeasures) {
if (
dayjs(smp.vacdm.ttot).diff(pilot.vacdm.ttot, 'minute') <
Math.ceil(measure.value / 60)
) {
pilot.vacdm.ctot = timeUtils.addMinutes(
smp.vacdm.ttot,
Math.ceil(measure.value / 60)
);
}
}
}
}

return setTime(pilot);
}

Expand Down Expand Up @@ -100,7 +128,10 @@ async function setTime(pilot: PilotDocument): Promise<{
finalBlock: number;
finalTtot: Date;
}> {
if (pilot.vacdm.tsat > pilot.vacdm.tobt || blockUtils.getBlockFromTime(pilot.vacdm.ttot) != pilot.vacdm.blockId) {
if (
pilot.vacdm.tsat > pilot.vacdm.tobt ||
blockUtils.getBlockFromTime(pilot.vacdm.ttot) != pilot.vacdm.blockId
) {
pilot.vacdm.ttot = blockUtils.getTimeFromBlock(pilot.vacdm.blockId);
pilot.vacdm.tsat = timeUtils.addMinutes(
pilot.vacdm.ttot,
Expand All @@ -113,6 +144,15 @@ async function setTime(pilot: PilotDocument): Promise<{
pilot.vacdm.ttot = timeUtils.addMinutes(pilot.vacdm.tsat, pilot.vacdm.exot);
}

if (!timeUtils.isTimeEmpty(pilot.vacdm.ctot)) {
pilot.vacdm.blockId = blockUtils.getBlockFromTime(pilot.vacdm.ctot);
pilot.vacdm.ttot = pilot.vacdm.ctot;
pilot.vacdm.tsat = timeUtils.addMinutes(
pilot.vacdm.ctot,
-pilot.vacdm.exot
);
}

await pilotService.addLog({
pilot: pilot.callsign,
namespace: 'cdmService',
Expand Down Expand Up @@ -189,6 +229,28 @@ export async function optimizeBlockAssignments() {

let allPilots = await pilotService.getAllPilots();

const datafeedData = await datafeedService.getRawDatafeed();

for (let pilot of allPilots) {
if (pilot.hasBooking) {
continue;
}

const datafeedPilot = await datafeedService.getFlight(pilot.callsign, datafeedData);

if (datafeedPilot) {
const pilotHasBooking = await bookingsService.pilotHasBooking(datafeedPilot.cid);

if (pilotHasBooking) {
pilot.hasBooking = true;

pilot.vacdm.prio += config().eventPrio;
}
}


}

const nowPlusTen = timeUtils.addMinutes(new Date(), 10);

for (let airport of allAirports) {
Expand Down
7 changes: 5 additions & 2 deletions backend/src/services/datafeed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ export async function getRawDatafeed(): Promise<VatsimTypes.VatsimDatafeed> {
}

export async function getFlight(
callsign: string
callsign: string,
datafeed: VatsimTypes.VatsimDatafeed | undefined = undefined
): Promise<VatsimTypes.VatsimPilot> {
try {
const datafeed = await getRawDatafeed();
if (!datafeed) {
datafeed = await getRawDatafeed();
}

const pilot = datafeed.pilots.find((p) => p.callsign == callsign);

Expand Down
Loading

0 comments on commit 859ac51

Please sign in to comment.