Skip to content

Commit

Permalink
start implemeting ecfmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Frenkii committed Jun 23, 2024
1 parent f4e6392 commit 3e17559
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/backend/ecfmp/ecfmp-measure.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ const EcfmpMeasureSchema = new mongoose.Schema<EcfmpMeasure>({

export const EcfmpMeasureProvider = {
provide: ECFMP_MEASURE_MODEL,
useFactory: (connection: mongoose.Connection) => connection.model('EcfmpMeasure', EcfmpMeasureSchema),
useFactory: (connection: typeof mongoose) => connection.model('EcfmpMeasure', EcfmpMeasureSchema),
inject: [DB_PROVIDER],
};
6 changes: 4 additions & 2 deletions src/backend/ecfmp/ecfmp.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Module } from '@nestjs/common';

import { DatabaseModule } from '../database.module';
import { ScheduleModule } from '../schedule.module';

import { EcfmpMeasureProvider } from './ecfmp-measure.model';
import { EcfmpService } from './ecfmp.service';

@Module({
imports: [DatabaseModule],
providers: [EcfmpService],
imports: [DatabaseModule, ScheduleModule],
providers: [EcfmpService, EcfmpMeasureProvider],
exports: [EcfmpService],
})
export class EcfmpModule {}
103 changes: 101 additions & 2 deletions src/backend/ecfmp/ecfmp.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,103 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import Agenda from 'agenda';
import axios from 'axios';
import dayjs from 'dayjs';

import { EcfmpMeasure, EcfmpPlugin } from '../../shared/interfaces/ecfmp.interface';
import logger from '../logger';
import { AGENDA_PROVIDER } from '../schedule.module';

import { ECFMP_MEASURE_MODEL, EcfmpMeasureDocument, EcfmpMeasureModel } from './ecfmp-measure.model';



@Injectable()
export class EcfmpService {}
export class EcfmpService {
constructor(
@Inject(ECFMP_MEASURE_MODEL) private ecfmpMeasureModel: EcfmpMeasureModel,
@Inject(AGENDA_PROVIDER) private agenda: Agenda,
) {
this.agenda.define('ECFMP_processEcfmpMeasures', this.processMeasures.bind(this));
this.agenda.every('1 minute', 'ECFMP_processEcfmpMeasures');
}

async getEcfmpMeasures(): Promise<EcfmpMeasure[]> {
//TODO: replace dev url
const ecfmpUrl = 'https://flow-dev.ecfmp.net/api/v1/plugin';

try {
const ecfmpMeasures = await axios.get<EcfmpPlugin>(ecfmpUrl);

return ecfmpMeasures.data.flow_measures;

} catch (error) {
logger.error('Error on retrieving Measures from ECFMP %o', error);
throw error;
}
}

async getInternalMeasures(): Promise<EcfmpMeasureDocument[]> {
return this.ecfmpMeasureModel.find({}).exec();
}

async upsertMeasure(data: EcfmpMeasure): Promise<EcfmpMeasureDocument> {
const measure = await this.ecfmpMeasureModel.findOneAndUpdate({
ident: data.ident,
}, {
$set: {
id: data.id,
event_id: data.event_id,
reason: data.reason,
starttime: data.starttime,
measure: data.measure,
filters: data.filters,
endtime: data.endtime,
notified_flight_information_regions: data.notified_flight_information_regions,
withdrawn_at: data.withdrawn_at,
},
}, {
new: true,
upsert: true,
});

return measure;
}

async removeOldMeasures() {
const measures = await this.getInternalMeasures();

const oldMeasures = measures
.filter(
(measure) =>
dayjs(measure.withdrawn_at).isBefore(new Date()) ||
dayjs(measure.endtime).isBefore(new Date()),
)
.map((e) => e._id);

await this.ecfmpMeasureModel.deleteMany({ _id: { $in: oldMeasures } }).exec();

logger.debug('Removed Measures %j', oldMeasures);
}

async processMeasures() {
logger.debug('Process Measures');
const measures = await this.getEcfmpMeasures();

const filteredMeasures = measures.filter(
(measure) => measure.measure.type === 'minimum_departure_interval' ||
measure.measure.type === 'average_departure_interval' ||
measure.measure.type === 'prohibit' ||
measure.measure.type === 'mandatory_route');

for (const measure of filteredMeasures) {
await this.upsertMeasure(measure);
}


await this.removeOldMeasures();

}

}


0 comments on commit 3e17559

Please sign in to comment.