UNPKG

@golemio/parkings

Version:
130 lines 9.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParkingsWorker = void 0; const ParkingsLocationRepository_1 = require("./repositories/ParkingsLocationRepository"); const ParkingLotsTransformation_1 = require("./transformations/ParkingLotsTransformation"); const UpdateAddressWorker_1 = require("./workers/UpdateAddressWorker"); const InputParkingLotsSchema_1 = require("../schema-definitions/datasources/InputParkingLotsSchema"); const ParkingMeasurementsDtoSchema_1 = require("../schema-definitions/datasources/ParkingMeasurementsDtoSchema"); const index_1 = require("../schema-definitions/index"); const integration_engine_1 = require("@golemio/core/dist/integration-engine"); const datasources_1 = require("@golemio/core/dist/integration-engine/datasources"); const HTTPFetchProtocolStrategy_1 = require("@golemio/core/dist/integration-engine/datasources/protocol-strategy/HTTPFetchProtocolStrategy"); const ContainerToken_1 = require("@golemio/core/dist/integration-engine/ioc/ContainerToken"); const models_1 = require("@golemio/core/dist/integration-engine/models"); const workers_1 = require("@golemio/core/dist/integration-engine/workers"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator"); const StaticParkingLotsGeoDataSourceFactory_1 = require("./datasources/StaticParkingLotsGeoDataSourceFactory"); const Di_1 = require("./ioc/Di"); const ModuleContainerToken_1 = require("./ioc/ModuleContainerToken"); const TskAverageOccupancyRepository_1 = require("./repositories/TskAverageOccupancyRepository"); const KoridParkingConfigTransformation_1 = require("./transformations/KoridParkingConfigTransformation"); const KoridParkingDataTransformation_1 = require("./transformations/KoridParkingDataTransformation"); const SourceEnum_1 = require("../helpers/constants/SourceEnum"); class ParkingsWorker extends workers_1.BaseWorker { constructor() { super(); /** * Parking lots Prague queue worker method * - store all data for parking lots in Prague. */ this.saveParkingLotsPrague = async () => { const data = await this.dataSource.getAll(); if (data.length === 0) { this.logger.debug("No recent data. Nothing to do."); return; } let staticGeoData; try { // "optional" datasource enhancing geolocations // objectively the datasource is static data maintained by IPT staticGeoData = (await StaticParkingLotsGeoDataSourceFactory_1.StaticParkingLotsGeoDataSourceFactory.getDataSource(this.config.datasources.StaticParkingLotsGeoSourceUrl).getAll()).features; } catch (err) { this.logger.error(`Error while getting static parking lots geo: ${err.message}`); } const { geo: geoData } = await this.parkingLotsTransformation.transform(data, staticGeoData); await this.parkingsModel.saveActiveParkingsWithoutAddress(geoData, SourceEnum_1.SourceEnum.TSK); await integration_engine_1.QueueManager.sendMessageToExchange(this.config.RABBIT_EXCHANGE_NAME + "." + UpdateAddressWorker_1.UpdateAddressWorker.workerName.toLowerCase(), "updateMissingParkingsAddresses", {}); }; /** * Parking lots measurements Prague queue worker method * - store all data for parking lots in Prague. */ this.saveParkingLotsMeasurementsPrague = async () => { const data = await this.dataSource.getAll(); if (data.length === 0) { this.logger.debug("No recent data. Nothing to do."); return; } const { measurements: measurementsData } = await this.parkingLotsTransformation.transform(data); await this.parkingsMeasurementsModel.bulkSave(measurementsData); await this.parkingsMeasurementsActualModel.bulkSave(measurementsData); }; /** * Stores locations and tariffs of parking lots in Liberec. */ this.saveKoridConfToDB = async (msg) => { const inputData = JSON.parse(msg.content.toString()); const transformedData = await this.koridParkingConfigTransformation.transform(inputData); await this.parkingsModel.saveActiveParkingsWithoutAddress(transformedData.geo, SourceEnum_1.SourceEnum.Korid); await this.parkingsTariffsModel.bulkSave(transformedData.tariff); await this.parkingTariffRelationsRepository.saveTariffsRelations(transformedData.parkingTariffsRelations, SourceEnum_1.SourceEnum.Korid); await this.parkingsLocationRepository.saveWithoutAddress(transformedData.location); await integration_engine_1.QueueManager.sendMessageToExchange(this.config.RABBIT_EXCHANGE_NAME + "." + UpdateAddressWorker_1.UpdateAddressWorker.workerName.toLowerCase(), "updateMissingParkingsAddresses", {}); }; /** * Stores measurements (available spots/occupancies) of parking lots in Liberec. */ this.saveKoridDataToDB = async (msg) => { const inputData = JSON.parse(msg.content.toString()); const transformedData = await this.koridParkingDataTransformation.transform(inputData); await this.parkingsMeasurementsModel.bulkSave(transformedData); await this.parkingsMeasurementsActualModel.bulkSave(transformedData); }; /** * Refresh TSK average occupancy view (serving data to legacy /parkings endpoints) */ this.refreshTskOccupancyView = async () => { try { await this.tskAverageOccupancyRepository.refreshData(); } catch (err) { throw new golemio_errors_1.GeneralError("refreshTskOccupancyView: error while refreshing TSK average occupancy view", this.constructor.name, err); } }; this.config = Di_1.ParkingsContainer.resolve(ContainerToken_1.ContainerToken.Config); this.logger = Di_1.ParkingsContainer.resolve(ContainerToken_1.ContainerToken.Logger); const dataTypeStrategy = new datasources_1.JSONDataTypeStrategy({ resultsPath: "results" }); // filter items with lastUpdated lower than two days dataTypeStrategy.setFilter((item) => item.lastUpdated > new Date().getTime() - 2 * 24 * 60 * 60 * 1000); this.dataSource = new datasources_1.DataSource(index_1.Parkings.name + "DataSource", new HTTPFetchProtocolStrategy_1.HTTPFetchProtocolStrategy({ headers: {}, method: "GET", url: this.config.datasources.TSKParkings, }), dataTypeStrategy, new golemio_validator_1.JSONSchemaValidator(index_1.Parkings.name + "DataSource", InputParkingLotsSchema_1.InputParkingLotsSchema)); this.parkingLotsTransformation = new ParkingLotsTransformation_1.ParkingLotsTransformation(); this.koridParkingConfigTransformation = new KoridParkingConfigTransformation_1.KoridParkingConfigTransformation(); this.koridParkingDataTransformation = new KoridParkingDataTransformation_1.KoridParkingDataTransformation(); this.parkingsMeasurementsModel = new models_1.PostgresModel(index_1.Parkings.measurements.name + "Model", { outputSequelizeAttributes: index_1.Parkings.measurements.outputSequelizeAttributes, pgSchema: index_1.Parkings.pgSchema, pgTableName: index_1.Parkings.measurements.pgTableName, savingType: "insertOrUpdate", }, new golemio_validator_1.JSONSchemaValidator(index_1.Parkings.measurements.name + "PgModelValidator", ParkingMeasurementsDtoSchema_1.ParkingMeasurementsDtoSchema)); this.parkingsMeasurementsActualModel = new models_1.PostgresModel(index_1.Parkings.measurementsActual.name + "Model", { outputSequelizeAttributes: index_1.Parkings.measurementsActual.outputSequelizeAttributes, pgSchema: index_1.Parkings.pgSchema, pgTableName: index_1.Parkings.measurementsActual.pgTableName, savingType: "insertOrUpdate", }, new golemio_validator_1.JSONSchemaValidator(index_1.Parkings.measurementsActual.name + "PgModelValidator", ParkingMeasurementsDtoSchema_1.ParkingMeasurementsDtoSchema)); this.tskAverageOccupancyRepository = new TskAverageOccupancyRepository_1.TskAverageOccupancyRepository(); this.parkingTariffRelationsRepository = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.ParkingTariffRelationsRepository); this.parkingsModel = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.ParkingsRepository); this.parkingsTariffsModel = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.ParkingTariffsRepository); this.parkingsLocationRepository = new ParkingsLocationRepository_1.ParkingsLocationRepository(); } } exports.ParkingsWorker = ParkingsWorker; //# sourceMappingURL=ParkingsWorker.js.map