introduce a performance backend
This commit is contained in:
25
src/performance/models/performance.model.ts
Normal file
25
src/performance/models/performance.model.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document } from 'mongoose';
|
||||
import { PerformanceEntry } from './performanceentry.model';
|
||||
|
||||
export type PerformanceDocument = Performance & Document;
|
||||
|
||||
@Schema()
|
||||
export class Performance {
|
||||
@Prop({ required: true })
|
||||
icaoCode: string;
|
||||
|
||||
@Prop({ required: true, type: PerformanceEntry })
|
||||
aboveFL240: PerformanceEntry;
|
||||
|
||||
@Prop({ required: true, type: PerformanceEntry })
|
||||
aboveFL100: PerformanceEntry;
|
||||
|
||||
@Prop({ required: true, type: PerformanceEntry })
|
||||
belowFL100: PerformanceEntry;
|
||||
|
||||
@Prop({ required: true })
|
||||
minimalApproachSpeed: number;
|
||||
}
|
||||
|
||||
export const PerformanceSchema = SchemaFactory.createForClass(Performance);
|
||||
16
src/performance/models/performanceentry.model.ts
Normal file
16
src/performance/models/performanceentry.model.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document } from 'mongoose';
|
||||
|
||||
export type PerformanceEntryDocument = PerformanceEntry & Document;
|
||||
|
||||
@Schema()
|
||||
export class PerformanceEntry {
|
||||
@Prop({ required: true })
|
||||
speed: number;
|
||||
|
||||
@Prop({ required: true })
|
||||
rateOfDescend: number;
|
||||
}
|
||||
|
||||
export const PerformanceEntrySchema =
|
||||
SchemaFactory.createForClass(PerformanceEntry);
|
||||
17
src/performance/performance.module.ts
Normal file
17
src/performance/performance.module.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { PerformanceSchema } from './models/performance.model';
|
||||
import { PerformanceService } from './performance.service';
|
||||
import { VersioningModule } from '../versioning/versioning.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: 'performance', schema: PerformanceSchema },
|
||||
]),
|
||||
VersioningModule,
|
||||
],
|
||||
providers: [PerformanceService],
|
||||
exports: [MongooseModule, PerformanceService],
|
||||
})
|
||||
export class PerformanceModule {}
|
||||
18
src/performance/performance.service.spec.ts
Normal file
18
src/performance/performance.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PerformanceService } from './performance.service';
|
||||
|
||||
describe('PerformanceService', () => {
|
||||
let service: PerformanceService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [PerformanceService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<PerformanceService>(PerformanceService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
99
src/performance/performance.service.ts
Normal file
99
src/performance/performance.service.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { readFileSync } from 'fs';
|
||||
import * as yaml from 'js-yaml';
|
||||
import { Model } from 'mongoose';
|
||||
import { join } from 'path';
|
||||
import { SemanticVersionDto } from 'src/versioning/dto/semanticversion.dto';
|
||||
import { VersioningService } from '../versioning/versioning.service';
|
||||
import { PerformanceDocument } from './models/performance.model';
|
||||
|
||||
const COMPONENT_NAME = 'performance';
|
||||
|
||||
@Injectable()
|
||||
export class PerformanceService {
|
||||
private static readPerformanceFile(): Record<string, any> {
|
||||
return yaml.load(
|
||||
readFileSync(join(process.cwd(), 'config/performance.yaml'), 'utf8'),
|
||||
) as Record<string, any>;
|
||||
}
|
||||
|
||||
private async updatePerformanceData(
|
||||
performanceData: Record<string, any>,
|
||||
version: SemanticVersionDto,
|
||||
): Promise<boolean> {
|
||||
return this.performanceModel.deleteMany({}).then(async () => {
|
||||
for (const key in performanceData) {
|
||||
if (key !== 'FILEINFO') {
|
||||
await this.performanceModel.create({
|
||||
icaoCode: key,
|
||||
aboveFL240: {
|
||||
speed: performanceData[key]['speedabovefl240'],
|
||||
rateOfDescend: performanceData[key]['rodabovefl240'],
|
||||
},
|
||||
aboveFL100: {
|
||||
speed: performanceData[key]['speedabovefl100'],
|
||||
rateOfDescend: performanceData[key]['rodabovefl100'],
|
||||
},
|
||||
belowFL100: {
|
||||
speed: performanceData[key]['speedbelowfl100'],
|
||||
rateOfDescend: performanceData[key]['rodbelowfl100'],
|
||||
},
|
||||
minimalApproachSpeed: performanceData[key]['speedapproach'],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await this.versioningService.updateComponent(
|
||||
COMPONENT_NAME,
|
||||
performanceData['FILEINFO']['createdAt'],
|
||||
version,
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private async validateAndUpdatePerformanceData(): Promise<boolean> {
|
||||
// read the performance file and validate the version length
|
||||
const data = PerformanceService.readPerformanceFile();
|
||||
const versionParts = (data['FILEINFO']['version'] as string).split('.');
|
||||
if (versionParts.length !== 3) return false;
|
||||
|
||||
// create the fileversion
|
||||
const fileversion: SemanticVersionDto = {
|
||||
main: parseInt(versionParts[0]),
|
||||
major: parseInt(versionParts[1]),
|
||||
minor: parseInt(versionParts[2]),
|
||||
};
|
||||
|
||||
return this.versioningService
|
||||
.findComponent(COMPONENT_NAME)
|
||||
.then((resp) => {
|
||||
if (
|
||||
resp === null ||
|
||||
VersioningService.newerVersion(
|
||||
{
|
||||
main: resp.versionMain,
|
||||
major: resp.versionMajor,
|
||||
minor: resp.versionMinor,
|
||||
},
|
||||
fileversion,
|
||||
)
|
||||
) {
|
||||
return this.updatePerformanceData(data, fileversion);
|
||||
}
|
||||
|
||||
return false;
|
||||
})
|
||||
.catch(() => this.updatePerformanceData(data, fileversion));
|
||||
}
|
||||
|
||||
constructor(
|
||||
@InjectModel(COMPONENT_NAME)
|
||||
private readonly performanceModel: Model<PerformanceDocument>,
|
||||
private readonly versioningService: VersioningService,
|
||||
) {
|
||||
this.validateAndUpdatePerformanceData();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user