@golemio/pid
Version:
Golemio PID Module
86 lines • 4.59 kB
JavaScript
;
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 GtfsStopsRedisRepository_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GtfsStopsRedisRepository = void 0;
const GtfsStopParser_1 = require("../../../../../../../helpers/GtfsStopParser");
const const_1 = require("../../../../../../../schema-definitions/ropid-gtfs/redis/const");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const node_crypto_1 = require("node:crypto");
const TTL_SECONDS_BEFORE_DELETE = 60;
let GtfsStopsRedisRepository = GtfsStopsRedisRepository_1 = class GtfsStopsRedisRepository extends integration_engine_1.RedisModel {
constructor() {
super("GtfsStopsRedisRepository", {
isKeyConstructedFromData: false,
prefix: GtfsStopsRedisRepository_1.NAMESPACE_PREFIX,
});
}
async refreshData(cisStops, gtfsStops) {
try {
const oldSetName = (await this.connection.get(`${this.prefix}:activeSetName`)) ?? "";
await this.createNewCache(cisStops, gtfsStops);
await this.deleteOldCache(oldSetName);
}
catch (err) {
throw new golemio_errors_1.GeneralError("refreshData failed", this.constructor.name, err);
}
}
async createNewCache(cisStops, gtfsStops) {
try {
const cisToAswNode = new Map();
for (const stop of cisStops) {
cisToAswNode.set(stop.cis, GtfsStopParser_1.GtfsStopParser.getAswNodeFromId(stop.id));
}
const aswNodeToGtfsStops = new Map();
const staticData = new Map();
for (const stop of gtfsStops) {
staticData.set(stop.computed_cis_stop_id, JSON.stringify(stop));
const nodeStopIds = aswNodeToGtfsStops.get(stop.asw_node_id) ?? [];
if (nodeStopIds.length === 0) {
aswNodeToGtfsStops.set(stop.asw_node_id, nodeStopIds);
}
nodeStopIds.push(stop.stop_id);
}
const setName = (0, node_crypto_1.randomBytes)(4).toString("hex");
const pipeline = this.connection.pipeline();
pipeline.hset(`${this.prefix}:${setName}:staticData`, staticData);
pipeline.hset(`${this.prefix}:${setName}:aswNodeToGtfsStops`, aswNodeToGtfsStops);
pipeline.hset(`${this.prefix}:${setName}:cisToAswNode`, cisToAswNode);
pipeline.publish(`${this.prefix}`, `${this.prefix}:${setName}`);
pipeline.set(`${this.prefix}:activeSetName`, `${this.prefix}:${setName}`);
await pipeline.exec();
}
catch (err) {
throw new golemio_errors_1.GeneralError("createNewCache failed", this.constructor.name, err);
}
}
async deleteOldCache(setName) {
try {
const pipeline = this.connection.pipeline();
pipeline.expire(`${setName}:aswNodeToGtfsStops`, TTL_SECONDS_BEFORE_DELETE);
pipeline.expire(`${setName}:cisToAswNode`, TTL_SECONDS_BEFORE_DELETE);
pipeline.expire(`${setName}:staticData`, TTL_SECONDS_BEFORE_DELETE);
await pipeline.exec();
}
catch (err) {
throw new golemio_errors_1.GeneralError("deleteOldCache failed", this.constructor.name, err);
}
}
};
exports.GtfsStopsRedisRepository = GtfsStopsRedisRepository;
GtfsStopsRedisRepository.NAMESPACE_PREFIX = const_1.GTFS_STOPS_NAMESPACE_PREFIX;
exports.GtfsStopsRedisRepository = GtfsStopsRedisRepository = GtfsStopsRedisRepository_1 = __decorate([
(0, tsyringe_1.injectable)(),
__metadata("design:paramtypes", [])
], GtfsStopsRedisRepository);
//# sourceMappingURL=GtfsStopsRedisRepository.js.map