From 90d44bb8e5fdfa0a00eb1e853a820bd3bbd28ec8 Mon Sep 17 00:00:00 2001 From: "p.hollmann" <31701319+paulhollmann@users.noreply.github.com> Date: Mon, 11 Mar 2024 09:38:02 +0100 Subject: [PATCH] Trainingsstationen synchronisieren --- db/config/options.js | 9 ---- .../TrainingStationAdminController.ts | 40 +----------------- src/core/Config.ts | 3 +- src/core/TaskScheduler.ts | 5 +++ src/core/tasks/UpdateTrainingStationsTask.ts | 5 +++ src/libraries/vatger/GithubLibrary.ts | 42 +++++++++++++++++++ 6 files changed, 56 insertions(+), 48 deletions(-) delete mode 100644 db/config/options.js create mode 100644 src/libraries/vatger/GithubLibrary.ts diff --git a/db/config/options.js b/db/config/options.js deleted file mode 100644 index 41dfb57..0000000 --- a/db/config/options.js +++ /dev/null @@ -1,9 +0,0 @@ -const path = require("path"); - -const dir = process.cwd(); - -module.exports = { - config: path.join(dir, "db/config/config.js"), - "migrations-path": path.join(dir, "dist/db/migrations"), - "seeders-path": path.join(__dirname, "dist/db/seeders"), -}; diff --git a/src/controllers/training-station/TrainingStationAdminController.ts b/src/controllers/training-station/TrainingStationAdminController.ts index 58120b3..c7fa6af 100644 --- a/src/controllers/training-station/TrainingStationAdminController.ts +++ b/src/controllers/training-station/TrainingStationAdminController.ts @@ -4,16 +4,7 @@ import { sequelize } from "../../core/Sequelize"; import { TrainingStation } from "../../models/TrainingStation"; import _TrainingStationAdminValidator from "./_TrainingStationAdmin.validator"; import { Config } from "../../core/Config"; - -type HomepageStation = { - id: number; - name: string; - ident: string; - frequency: number; - gcap_status?: string; - gcap_training_airport?: boolean; - s1_twr?: boolean; -}; +import { updateTrainingStations } from "../../libraries/vatger/GithubLibrary"; async function getAll(request: Request, response: Response, next: NextFunction) { try { @@ -45,36 +36,9 @@ async function getByID(request: Request, response: Response, next: NextFunction) } } -type DataHubStations = { - logon: string; - frequency: string; - abbreviation: string; - description: string; -}; - async function syncStations(request: Request, response: Response, next: NextFunction) { try { - const res = await axios.get("https://raw.githubusercontent.com/VATGER-Nav/datahub/main/data.json"); - const stations = res.data as DataHubStations[]; - - for (const station of stations) { - if (station.logon.length === 0 || station.frequency.length === 0) continue; - - const dbStation = await TrainingStation.findOne({ where: { callsign: station.logon } }); - if (dbStation == null) { - await TrainingStation.create({ - callsign: station.logon, - frequency: Number(station.frequency), - }); - continue; - } - - await dbStation.update({ - callsign: station.logon, - frequency: Number(station.frequency), - }); - } - + await updateTrainingStations(); const newStations = await TrainingStation.findAll(); response.send(newStations); } catch (e) { diff --git a/src/core/Config.ts b/src/core/Config.ts index a5c5581..67091a3 100644 --- a/src/core/Config.ts +++ b/src/core/Config.ts @@ -78,7 +78,7 @@ export const Config = { }, VATEUD_CORE_CONFIG: { - API_KEY: process.env.VATEUD_CORE_API_KEY + API_KEY: process.env.VATEUD_CORE_API_KEY, }, CONNECT_CONFIG: { @@ -113,6 +113,7 @@ export const Config = { SYSLOG_CLEANUP: "30 2 * * 1-6", SEND_EMAIL: "*/5 * * * *", CHECK_ENDORSEMENT: "* 0 12 * *", + SYNC_TRAINING_STATION: "0 */4 * * *", }, }; diff --git a/src/core/TaskScheduler.ts b/src/core/TaskScheduler.ts index 99cdbdd..fef8637 100644 --- a/src/core/TaskScheduler.ts +++ b/src/core/TaskScheduler.ts @@ -7,6 +7,7 @@ import dayjs from "dayjs"; import SendEmailTask from "./tasks/SendEmailTask"; import CheckEndorsementTask from "./tasks/CheckEndorsementTask"; import { Config } from "./Config"; +import UpdateTrainingStationsTask from "./tasks/UpdateTrainingStationsTask"; export async function initScheduledJobs() { Logger.log(LogLevels.LOG_INFO, `Scheduling Tasks (CRON)`); @@ -36,5 +37,9 @@ export async function initScheduledJobs() { await CheckEndorsementTask.handle(); }).start(); + CronJob.schedule(Config.CRON_CONFIG.SYNC_TRAINING_STATION, async () => { + await UpdateTrainingStationsTask.handle(); + }).start(); + Logger.log(LogLevels.LOG_SUCCESS, "\0 \u2713 All Tasks scheduled.\n"); } diff --git a/src/core/tasks/UpdateTrainingStationsTask.ts b/src/core/tasks/UpdateTrainingStationsTask.ts index ae43dc2..e9ff701 100644 --- a/src/core/tasks/UpdateTrainingStationsTask.ts +++ b/src/core/tasks/UpdateTrainingStationsTask.ts @@ -1,5 +1,10 @@ +import { updateTrainingStations } from "../../libraries/vatger/GithubLibrary"; + async function handle() { console.log("Handle..."); + try { + await updateTrainingStations(); + } catch (e) {} } export default { diff --git a/src/libraries/vatger/GithubLibrary.ts b/src/libraries/vatger/GithubLibrary.ts new file mode 100644 index 0000000..0a13e98 --- /dev/null +++ b/src/libraries/vatger/GithubLibrary.ts @@ -0,0 +1,42 @@ +import axios from "axios"; +import { TrainingStation } from "../../models/TrainingStation"; + +type DataHubStations = { + logon: string; + frequency: string; + abbreviation: string; + description: string; + gcap_status?: string; + gcap_training_airport?: boolean; + s1_twr?: boolean; +}; + +export async function updateTrainingStations() { + const res = await axios.get("https://raw.githubusercontent.com/VATGER-Nav/datahub/main/data.json"); + + const stations = res.data as DataHubStations[]; + + for (const station of stations) { + if (station.logon.length === 0 || station.frequency.length === 0) continue; + + const gcap_class_str = station.gcap_status ?? "0"; + const gcap_class_nr = gcap_class_str === "0" || gcap_class_str === "1" ? parseInt(gcap_class_str, 10) : 2; + + const stationData = { + callsign: station.logon, + frequency: Number(station.frequency), + gcap_class: gcap_class_nr, + gcap_class_group: gcap_class_str, + gcap_training_airport: station.gcap_training_airport ?? false, + s1_twr: station.s1_twr ?? false, + }; + + const dbStation = await TrainingStation.findOne({ where: { callsign: station.logon } }); + if (dbStation == null) { + await TrainingStation.create(stationData); + continue; + } + + await dbStation.update(stationData); + } +}