UNPKG

@golemio/pid

Version:
79 lines 4.37 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var GtfsTripStopsCacheRepository_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.GtfsTripStopsCacheRepository = void 0; const const_1 = require("../../../../schema-definitions/ropid-gtfs/redis/const"); const GtfsTripStopsCacheDtoSchema_1 = require("../../../../schema-definitions/ropid-gtfs/redis/schemas/GtfsTripStopsCacheDtoSchema"); const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken"); const RedisModel_1 = require("@golemio/core/dist/integration-engine/models/RedisModel"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator"); const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe"); // TTL must exceed max trip duration (~3.5h) + task interval (1h) to keep running trips in cache. // Task refreshes every ~1h with a -1h...+2h query window, so a trip gets refreshed up to 1h after start. // 6h TTL ensures data survives until well after the longest trip ends. const CACHE_TTL_SECONDS = 6 * 3600; let GtfsTripStopsCacheRepository = GtfsTripStopsCacheRepository_1 = class GtfsTripStopsCacheRepository extends RedisModel_1.RedisModel { constructor(log) { super("GtfsTripStopsCacheRepository", { isKeyConstructedFromData: false, prefix: GtfsTripStopsCacheRepository_1.NAMESPACE_PREFIX, }, new golemio_validator_1.JSONSchemaValidator("GtfsTripStopsCacheRepositoryValidator", GtfsTripStopsCacheDtoSchema_1.GtfsTripStopsCacheDtoSchema)); this.log = log; } async saveMultipleTripStops(tripStops) { const BATCH_SIZE = 200; await this.validateAndRemoveInvalid(tripStops); try { let pipeline = this.connection.pipeline(); for (const [tripId, dto] of tripStops.entries()) { const key = `${GtfsTripStopsCacheRepository_1.NAMESPACE_PREFIX}:${tripId}`; pipeline.set(key, JSON.stringify(dto), "EX", CACHE_TTL_SECONDS); if (pipeline.length >= BATCH_SIZE) { await pipeline.exec(); pipeline = this.connection.pipeline(); } } if (pipeline.length > 0) { await pipeline.exec(); } } catch (err) { throw new golemio_errors_1.GeneralError("Error while saving trip stops to cache", this.constructor.name, err); } } async validateAndRemoveInvalid(tripStops) { for (const [tripId, dto] of tripStops.entries()) { try { await this.validator.Validate(dto); } catch (exc) { this.log.error(`${this.constructor.name}: Invalid trip stops for trip ${tripId}`, { ...exc, stops: dto.stops, }); tripStops.delete(tripId); } } } }; exports.GtfsTripStopsCacheRepository = GtfsTripStopsCacheRepository; GtfsTripStopsCacheRepository.NAMESPACE_PREFIX = const_1.GTFS_TRIP_STOPS_NAMESPACE_PREFIX; exports.GtfsTripStopsCacheRepository = GtfsTripStopsCacheRepository = GtfsTripStopsCacheRepository_1 = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)), __metadata("design:paramtypes", [Object]) ], GtfsTripStopsCacheRepository); //# sourceMappingURL=GtfsTripStopsCacheRepository.js.map