@golemio/pid
Version:
Golemio PID Module
115 lines • 5.67 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 __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JISInfotextsRedisRepository = void 0;
const JISInfotextSeverityLevelPriority_1 = require("../../../../helpers/jis/JISInfotextSeverityLevelPriority");
const const_1 = require("../../../../schema-definitions/jis/redis/const");
const IoRedisConnector_1 = require("@golemio/core/dist/helpers/data-access/redis/IoRedisConnector");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const ioc_1 = require("@golemio/core/dist/output-gateway/ioc");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let JISInfotextsRedisRepository = class JISInfotextsRedisRepository {
constructor(redisConnector, log) {
this.redisConnector = redisConnector;
this.log = log;
this.setName = undefined; // loaded via pub sub or loaded from redis
}
async checkSetNameAndConnection() {
if (!this.redisConnector.isConnected()) {
await this.redisConnector.connect();
}
const connection = this.redisConnector.getConnection();
if (!this.setName) {
const loadedSetName = await connection.get(`${const_1.JIS_NAMESPACE_PREFIX}:activeSetName`);
if (!loadedSetName) {
this.log.debug(`Empty setName for ${const_1.JIS_NAMESPACE_PREFIX}`);
return null;
}
else {
this.setName = loadedSetName;
}
}
return connection;
}
setCurrentSetName(name) {
this.setName = name;
}
async getActiveStopsInfotexts(stopIds, timeFrom = new Date()) {
const infotexts = await this.getStopsInfotexts(stopIds);
// filter infotexts by repeat_enabled + repeat_time_start + repeat_time_end
return this.sortAndFilter(infotexts, timeFrom);
}
async getStopsInfotexts(stopIds) {
try {
const connection = await this.checkSetNameAndConnection();
if (!connection) {
return [];
}
const infotextIds = await this.getStopsInfotextIds(stopIds, connection);
if (infotextIds.size === 0)
return [];
return this.getInfotextsByIds(infotextIds, connection);
}
catch (error) {
throw new golemio_errors_1.GeneralError("Cannot get stop infotexts from cache", this.constructor.name, error);
}
}
async getStopsInfotextIds(stopIds, connection) {
const stopsInfotexts = await connection.hmget(`${this.setName}:stops-infotexts`, ...stopIds);
const infotextIds = new Set();
for (const infotextIdsRaw of stopsInfotexts) {
for (const infotextId of infotextIdsRaw?.split(",") ?? []) {
infotextIds.add(infotextId);
}
}
return infotextIds;
}
async getInfotextsByIds(infotextIds, connection) {
const infotextsRaw = await connection.hmget(`${this.setName}:infotexts`, ...infotextIds);
const infotexts = [];
for (const infotext of infotextsRaw) {
if (infotext === null)
continue;
infotexts.push(JSON.parse(infotext));
}
return infotexts;
}
sortAndFilter(infotexts, timeFrom) {
const activeInfotexts = infotexts.filter((infotext) => new Date(infotext.active_period_start) <= timeFrom &&
(!infotext.active_period_end || new Date(infotext.active_period_end) >= timeFrom));
return activeInfotexts.sort((a, b) => {
// First sort by severity_level DESC
if (a.severity_level !== b.severity_level) {
return JISInfotextSeverityLevelPriority_1.JISInfotextSeverityLevelPriority[b.severity_level] - JISInfotextSeverityLevelPriority_1.JISInfotextSeverityLevelPriority[a.severity_level];
}
// Then sort by created_timestamp DESC
const createdA = a.created_timestamp;
const createdB = b.created_timestamp;
if (createdA < createdB)
return -1;
if (createdA > createdB)
return 1;
return 0;
});
}
};
exports.JISInfotextsRedisRepository = JISInfotextsRedisRepository;
exports.JISInfotextsRedisRepository = JISInfotextsRedisRepository = __decorate([
(0, tsyringe_1.injectable)(),
__param(0, (0, tsyringe_1.inject)(ioc_1.ContainerToken.RedisConnector)),
__param(1, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)),
__metadata("design:paramtypes", [IoRedisConnector_1.IoRedisConnector, Object])
], JISInfotextsRedisRepository);
//# sourceMappingURL=JISInfotextsRedisRepository.js.map