|
@@ -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();
|
|
|
+ }
|
|
|
+}
|